Skip to content

Commit

Permalink
refactor enum building:
Browse files Browse the repository at this point in the history
- schema: decide on backed type / const names automatically by provided cases
- legacy enums: readonly properties for "name", "value" instead of getters (as in php8 enums)
  • Loading branch information
klkvsk committed Apr 7, 2023
1 parent 5cf588a commit e05173f
Show file tree
Hide file tree
Showing 19 changed files with 244 additions and 181 deletions.
6 changes: 3 additions & 3 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
all: gen-php7.4 gen-php8.0 gen-php8.1

gen-php7.4: dto.schema.php
OUTPUT_DIR=php7.4 ../bin/dto-gen --target 7.4
OUTPUT_DIR=php7.4 php8.1 ../bin/dto-gen --target 7.4

gen-php8.0: dto.schema.php
OUTPUT_DIR=php8.0 ../bin/dto-gen --target 8.0
OUTPUT_DIR=php8.0 php8.1 ../bin/dto-gen --target 8.0

gen-php8.1: dto.schema.php
OUTPUT_DIR=php8.1 ../bin/dto-gen --target 8.1
OUTPUT_DIR=php8.1 php8.1 ../bin/dto-gen --target 8.1

clean:
rm -r php*
10 changes: 6 additions & 4 deletions example/php7.4/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*
* @link https://github.com/klkvsk/dto-generator
* @link https://packagist.org/klkvsk/dto-generator
*
* ---
*/
class Author implements \JsonSerializable
{
Expand All @@ -23,7 +25,7 @@ public function __construct(int $id, string $firstName, ?string $lastName = null
$this->id = $id;
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->validate([['lastName' => ['tooShort' => fn ($x) => \strlen($x) > 5, 'tooLong' => fn ($x) => \strlen($x) < 40]]]);
$this->validate(['lastName' => ['tooShort' => fn ($x) => \strlen($x) > 5, 'tooLong' => fn ($x) => \strlen($x) < 40]]);
}

public function getId(): int
Expand Down Expand Up @@ -98,6 +100,7 @@ public static function create(array $data): self
}

// create
/** @psalm-suppress PossiblyNullArgument */
return new static(
$constructorParams["id"],
$constructorParams["firstName"],
Expand All @@ -121,12 +124,11 @@ public function toArray(): array
return $array;
}

#[\ReturnTypeWillChange]
public function jsonSerialize()
public function jsonSerialize(): array
{
$array = [];
foreach (get_mangled_object_vars($this) as $var => $value) {
$var = preg_replace("/.+\0/", "", $var);
$var = substr($var, strrpos($var, "\0") ?: 0);
if ($value instanceof \DateTimeInterface) {
$value = $value->format('Y-m-d\TH:i:sP');
}
Expand Down
22 changes: 12 additions & 10 deletions example/php7.4/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*
* @link https://github.com/klkvsk/dto-generator
* @link https://packagist.org/klkvsk/dto-generator
*
* ---
*/
class Book implements \JsonSerializable
{
Expand All @@ -20,23 +22,23 @@ class Book implements \JsonSerializable
protected Author $author;
protected ?int $rating;

/** @var array<Genre> $genres */
protected array $genres;
/** @var ?array<Genre> $genres */
protected ?array $genres;

public function __construct(
int $id,
string $title,
Author $author,
?\DateTimeInterface $released = null,
?int $rating = 5,
array $genres = []
?array $genres = []
) {
$this->id = $id;
$this->title = $title;
$this->released = $released;
$this->author = $author;
$this->rating = $rating;
(function(Genre ...$_) {})( ...$genres);
$genres && (function(Genre ...$_) {})( ...$genres);
$this->genres = $genres;
}

Expand Down Expand Up @@ -66,9 +68,9 @@ public function getRating(): ?int
}

/**
* @return array<Genre>
* @return ?array<Genre>
*/
public function getGenres(): array
public function getGenres(): ?array
{
return $this->genres;
}
Expand Down Expand Up @@ -140,13 +142,14 @@ public static function create(array $data): self
}

// create
/** @psalm-suppress PossiblyNullArgument */
return new static(
$constructorParams["id"],
$constructorParams["title"],
$constructorParams["author"],
$constructorParams["released"] ?? null,
$constructorParams["rating"] ?? true,
$constructorParams["genres"]
$constructorParams["genres"] ?? null
);
}

Expand All @@ -166,12 +169,11 @@ public function toArray(): array
return $array;
}

#[\ReturnTypeWillChange]
public function jsonSerialize()
public function jsonSerialize(): array
{
$array = [];
foreach (get_mangled_object_vars($this) as $var => $value) {
$var = preg_replace("/.+\0/", "", $var);
$var = substr($var, strrpos($var, "\0") ?: 0);
if ($value instanceof \DateTimeInterface) {
$value = $value->format('Y-m-d\TH:i:sP');
}
Expand Down
52 changes: 32 additions & 20 deletions example/php7.4/Genre.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@
*
* @link https://github.com/klkvsk/dto-generator
* @link https://packagist.org/klkvsk/dto-generator
*
* ---
*
* @property-read string $name
* @property-read string $value
*/
final class Genre implements \JsonSerializable
{
public static ?array $map;
public string $name;
public $value;
private static ?array $map;
private string $name;
private string $value;

private function __construct(string $name, $value)
private function __construct(string $name, string $value)
{
$this->name = $name;
$this->value = $value;
Expand All @@ -30,31 +35,34 @@ private function __construct(string $name, $value)
public static function cases(): array
{
return self::$map = self::$map ?? [
'romance' => new self('ROMANCE', 'romance'),
'comedy' => new self('COMEDY', 'comedy'),
'drama' => new self('DRAMA', 'drama'),
'non-fiction' => new self('NON_FICTION', 'non-fiction'),
'scientific-work' => new self('SCIENTIFIC_WORK', 'scientific-work'),
new self('ROMANCE', 'romance'),
new self('COMEDY', 'comedy'),
new self('DRAMA', 'drama'),
new self('NON_FICTION', 'non-fiction'),
new self('SCIENTIFIC_WORK', 'scientific-work'),
];
}

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

public function value()
public function __get($propertyName)
{
return $this->value;
switch ($propertyName) {
case "name":
return $this->name;
case "value":
return $this->value;
default:
trigger_error("Undefined property: Genre::$propertyName");
return null;
}
}

public static function tryFrom($value): ?self
public static function tryFrom(string $value): ?self
{
$cases = self::cases();
return $cases[$value] ?? null;
}

public static function from($value): self
public static function from(string $value): self
{
$case = self::tryFrom($value);
if (!$case) {
Expand Down Expand Up @@ -91,8 +99,12 @@ public static function SCIENTIFIC_WORK(): self
return self::from('scientific-work');
}

#[\ReturnTypeWillChange]
public function jsonSerialize()
public function jsonSerialize(): string
{
return $this->value;
}

public function __toString()
{
return $this->value;
}
Expand Down
26 changes: 14 additions & 12 deletions example/php7.4/ScienceBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,32 @@
*
* @link https://github.com/klkvsk/dto-generator
* @link https://packagist.org/klkvsk/dto-generator
*
* ---
*/
class ScienceBook extends Book implements \JsonSerializable
{
/** @var array<ScienceBook> $references */
protected array $references;
/** @var ?array<ScienceBook> $references */
protected ?array $references;

public function __construct(
int $id,
string $title,
Author $author,
?\DateTimeInterface $released = null,
?int $rating = 5,
array $genres = [],
array $references = []
?array $genres = [],
?array $references = []
) {
parent::__construct($id, $title, $author, $released, $rating, $genres);
(function(ScienceBook ...$_) {})( ...$references);
$references && (function(ScienceBook ...$_) {})( ...$references);
$this->references = $references;
}

/**
* @return array<ScienceBook>
* @return ?array<ScienceBook>
*/
public function getReferences(): array
public function getReferences(): ?array
{
return $this->references;
}
Expand Down Expand Up @@ -100,14 +102,15 @@ public static function create(array $data): self
}

// create
/** @psalm-suppress PossiblyNullArgument */
return new static(
$constructorParams["id"],
$constructorParams["title"],
$constructorParams["author"],
$constructorParams["released"] ?? null,
$constructorParams["rating"] ?? true,
$constructorParams["genres"],
$constructorParams["references"]
$constructorParams["genres"] ?? null,
$constructorParams["references"] ?? null
);
}

Expand All @@ -127,12 +130,11 @@ public function toArray(): array
return $array;
}

#[\ReturnTypeWillChange]
public function jsonSerialize()
public function jsonSerialize(): array
{
$array = [];
foreach (get_mangled_object_vars($this) as $var => $value) {
$var = preg_replace("/.+\0/", "", $var);
$var = substr($var, strrpos($var, "\0") ?: 0);
if ($value instanceof \DateTimeInterface) {
$value = $value->format('Y-m-d\TH:i:sP');
}
Expand Down
10 changes: 6 additions & 4 deletions example/php8.0/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*
* @link https://github.com/klkvsk/dto-generator
* @link https://packagist.org/klkvsk/dto-generator
*
* ---
*/
class Author implements \JsonSerializable
{
Expand All @@ -19,7 +21,7 @@ public function __construct(
protected string $firstName,
protected ?string $lastName = null
) {
$this->validate([['lastName' => ['tooShort' => fn ($x) => \strlen($x) > 5, 'tooLong' => fn ($x) => \strlen($x) < 40]]]);
$this->validate(['lastName' => ['tooShort' => fn ($x) => \strlen($x) > 5, 'tooLong' => fn ($x) => \strlen($x) < 40]]);
}

public function getId(): int
Expand Down Expand Up @@ -84,6 +86,7 @@ public static function create(array $data): self
}

// create
/** @psalm-suppress PossiblyNullArgument */
return new static(...$constructorParams);
}

Expand All @@ -103,12 +106,11 @@ public function toArray(): array
return $array;
}

#[\ReturnTypeWillChange]
public function jsonSerialize()
public function jsonSerialize(): array
{
$array = [];
foreach (get_mangled_object_vars($this) as $var => $value) {
$var = preg_replace("/.+\0/", "", $var);
$var = substr($var, strrpos($var, "\0") ?: 0);
if ($value instanceof \DateTimeInterface) {
$value = $value->format('Y-m-d\TH:i:sP');
}
Expand Down
Loading

0 comments on commit e05173f

Please sign in to comment.