Skip to content

Commit

Permalink
fix creative urls
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvsk committed Feb 25, 2023
1 parent 7915c2c commit be6b816
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/Data/Creative/CreativeUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
declare(strict_types=1);

namespace BeelineOrd\Data\Creative;

/**
* This class is auto-generated with klkvsk/dto-generator
* Do not modify it, any changes might be overwritten!
*
* @see project://src/Data/dto.schema.php
*
* @link https://github.com/klkvsk/dto-generator
* @link https://packagist.org/klkvsk/dto-generator
*/
class CreativeUrl implements \JsonSerializable
{
protected string $url;

public function __construct(string $url)
{
$this->url = $url;
}

public function getUrl(): string
{
return $this->url;
}

protected static function required(): array
{
return ['url'];
}

/**
* @return iterable<int,\Closure>
*/
protected static function importers(string $key): iterable
{
switch ($key) {
case "url":
yield \Closure::fromCallable('strval');
break;
};
}

/**
* @return static
*/
public static function create(array $data): self
{
// check required
if ($diff = array_diff(static::required(), array_keys($data))) {
throw new \InvalidArgumentException("missing keys: " . implode(", ", $diff));
}

// import
$constructorParams = [];
foreach ($data as $key => $value) {
foreach (static::importers($key) as $importer) if ($value !== null) {
$value = call_user_func($importer, $value);
}
if (property_exists(static::class, $key)) {
$constructorParams[$key] = $value;
}
}

// create
/** @psalm-suppress PossiblyNullArgument */
return new static(
$constructorParams["url"]
);
}

public function toArray(): array
{
$array = [];
foreach (get_mangled_object_vars($this) as $var => $value) {
$var = preg_replace("/.+\0/", "", $var);
if ($value instanceof \DateTimeInterface) {
$value = $value->format('Y-m-d\TH:i:sP');
}
if (is_object($value) && method_exists($value, 'toArray')) {
$value = $value->toArray();
}
$array[$var] = $value;
}
return $array;
}

public function jsonSerialize(): array
{
$array = [];
foreach (get_mangled_object_vars($this) as $var => $value) {
$var = substr($var, strrpos($var, "\0") ?: 0);
if ($value instanceof \DateTimeInterface) {
$value = $value->format('Y-m-d\TH:i:sP');
}
if ($value instanceof \JsonSerializable) {
$value = $value->jsonSerialize();
}
$array[$var] = $value;
}
return $array;
}
}

0 comments on commit be6b816

Please sign in to comment.