-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |