diff --git a/example/php7.4/Author.php b/example/php7.4/Author.php index 3059c44..55eac61 100644 --- a/example/php7.4/Author.php +++ b/example/php7.4/Author.php @@ -109,14 +109,19 @@ public static function create(array $data): self public function toArray(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = preg_replace("/.+\0/", "", $var); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if (is_object($value) && method_exists($value, 'toArray')) { $value = $value->toArray(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; @@ -125,14 +130,19 @@ public function toArray(): array public function jsonSerialize(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = substr($var, strrpos($var, "\0") ?: 0); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if ($value instanceof \JsonSerializable) { $value = $value->jsonSerialize(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; diff --git a/example/php7.4/Book.php b/example/php7.4/Book.php index 95db22d..7739d97 100644 --- a/example/php7.4/Book.php +++ b/example/php7.4/Book.php @@ -154,14 +154,19 @@ public static function create(array $data): self public function toArray(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = preg_replace("/.+\0/", "", $var); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if (is_object($value) && method_exists($value, 'toArray')) { $value = $value->toArray(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; @@ -170,14 +175,19 @@ public function toArray(): array public function jsonSerialize(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = substr($var, strrpos($var, "\0") ?: 0); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if ($value instanceof \JsonSerializable) { $value = $value->jsonSerialize(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; diff --git a/example/php7.4/ScienceBook.php b/example/php7.4/ScienceBook.php index a8e08d4..e442c1f 100644 --- a/example/php7.4/ScienceBook.php +++ b/example/php7.4/ScienceBook.php @@ -115,14 +115,19 @@ public static function create(array $data): self public function toArray(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = preg_replace("/.+\0/", "", $var); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if (is_object($value) && method_exists($value, 'toArray')) { $value = $value->toArray(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; @@ -131,14 +136,19 @@ public function toArray(): array public function jsonSerialize(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = substr($var, strrpos($var, "\0") ?: 0); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if ($value instanceof \JsonSerializable) { $value = $value->jsonSerialize(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; diff --git a/example/php8.0/Author.php b/example/php8.0/Author.php index f6ca1f9..1baa58a 100644 --- a/example/php8.0/Author.php +++ b/example/php8.0/Author.php @@ -91,14 +91,19 @@ public static function create(array $data): self public function toArray(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = preg_replace("/.+\0/", "", $var); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if (is_object($value) && method_exists($value, 'toArray')) { $value = $value->toArray(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; @@ -107,14 +112,19 @@ public function toArray(): array public function jsonSerialize(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = substr($var, strrpos($var, "\0") ?: 0); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if ($value instanceof \JsonSerializable) { $value = $value->jsonSerialize(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; diff --git a/example/php8.0/Book.php b/example/php8.0/Book.php index 11e0542..f9c1a0d 100644 --- a/example/php8.0/Book.php +++ b/example/php8.0/Book.php @@ -125,14 +125,19 @@ public static function create(array $data): self public function toArray(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = preg_replace("/.+\0/", "", $var); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if (is_object($value) && method_exists($value, 'toArray')) { $value = $value->toArray(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; @@ -141,14 +146,19 @@ public function toArray(): array public function jsonSerialize(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = substr($var, strrpos($var, "\0") ?: 0); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if ($value instanceof \JsonSerializable) { $value = $value->jsonSerialize(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; diff --git a/example/php8.0/ScienceBook.php b/example/php8.0/ScienceBook.php index 808e098..1bfba18 100644 --- a/example/php8.0/ScienceBook.php +++ b/example/php8.0/ScienceBook.php @@ -103,14 +103,19 @@ public static function create(array $data): self public function toArray(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = preg_replace("/.+\0/", "", $var); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if (is_object($value) && method_exists($value, 'toArray')) { $value = $value->toArray(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; @@ -119,14 +124,19 @@ public function toArray(): array public function jsonSerialize(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = substr($var, strrpos($var, "\0") ?: 0); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if ($value instanceof \JsonSerializable) { $value = $value->jsonSerialize(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; diff --git a/example/php8.1/Author.php b/example/php8.1/Author.php index 11edd0c..3269415 100644 --- a/example/php8.1/Author.php +++ b/example/php8.1/Author.php @@ -76,14 +76,19 @@ public static function create(array $data): self public function toArray(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = preg_replace("/.+\0/", "", $var); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if (is_object($value) && method_exists($value, 'toArray')) { $value = $value->toArray(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; @@ -92,14 +97,19 @@ public function toArray(): array public function jsonSerialize(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = substr($var, strrpos($var, "\0") ?: 0); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if ($value instanceof \JsonSerializable) { $value = $value->jsonSerialize(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; diff --git a/example/php8.1/Book.php b/example/php8.1/Book.php index 218eb23..240eb5f 100644 --- a/example/php8.1/Book.php +++ b/example/php8.1/Book.php @@ -92,14 +92,19 @@ public static function create(array $data): self public function toArray(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = preg_replace("/.+\0/", "", $var); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if (is_object($value) && method_exists($value, 'toArray')) { $value = $value->toArray(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; @@ -108,14 +113,19 @@ public function toArray(): array public function jsonSerialize(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = substr($var, strrpos($var, "\0") ?: 0); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if ($value instanceof \JsonSerializable) { $value = $value->jsonSerialize(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; diff --git a/example/php8.1/ScienceBook.php b/example/php8.1/ScienceBook.php index 1ebed93..d865a6a 100644 --- a/example/php8.1/ScienceBook.php +++ b/example/php8.1/ScienceBook.php @@ -95,14 +95,19 @@ public static function create(array $data): self public function toArray(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = preg_replace("/.+\0/", "", $var); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if (is_object($value) && method_exists($value, 'toArray')) { $value = $value->toArray(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; @@ -111,14 +116,19 @@ public function toArray(): array public function jsonSerialize(): array { $array = []; - foreach (get_mangled_object_vars($this) as $var => $value) { - $var = substr($var, strrpos($var, "\0") ?: 0); + foreach (get_object_vars($this) as $var => $value) { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d\TH:i:sP'); } if ($value instanceof \JsonSerializable) { $value = $value->jsonSerialize(); } + if (class_exists(\UnitEnum::class) && $value instanceof \UnitEnum) { + $value = $value->value; + } + if (is_object($value) && method_exists($value, "__toString")) { + $value = (string)$value; + } $array[$var] = $value; } return $array; diff --git a/src/Generator/Builder/Class/ExportMethodsBuilder.php b/src/Generator/Builder/Class/ExportMethodsBuilder.php index 9d75729..d0f712c 100644 --- a/src/Generator/Builder/Class/ExportMethodsBuilder.php +++ b/src/Generator/Builder/Class/ExportMethodsBuilder.php @@ -24,8 +24,7 @@ public function build(Dto $object, PhpNamespace $ns, ClassType $class): void $toArrayMethod = $class->addMethod('toArray') ->setPublic() ->addBody('$array = [];') - ->addBody('foreach (get_mangled_object_vars($this) as $var => $value) {') - ->addBody(' $var = preg_replace("/.+\0/", "", $var);'); + ->addBody('foreach (get_object_vars($this) as $var => $value) {'); if ($this->dateFormat) { $toArrayMethod @@ -38,6 +37,12 @@ public function build(Dto $object, PhpNamespace $ns, ClassType $class): void ->addBody(' if (is_object($value) && method_exists($value, ?)) {', [self::TO_ARRAY_METHOD_NAME]) ->addBody(' $value = $value->?();', [self::TO_ARRAY_METHOD_NAME]) ->addBody(' }') + ->addBody(' if (class_exists(\\UnitEnum::class) && $value instanceof \\UnitEnum) {') + ->addBody(' $value = $value->value;') + ->addBody(' }') + ->addBody(' if (is_object($value) && method_exists($value, "__toString")) {') + ->addBody(' $value = (string)$value;') + ->addBody(' }') ->addBody(' $array[$var] = $value;') ->addBody('}') ->addBody('return $array;') @@ -50,8 +55,7 @@ public function build(Dto $object, PhpNamespace $ns, ClassType $class): void ->setPublic() ->setReturnType('array') ->addBody('$array = [];') - ->addBody('foreach (get_mangled_object_vars($this) as $var => $value) {') - ->addBody(' $var = substr($var, strrpos($var, "\\0") ?: 0);'); + ->addBody('foreach (get_object_vars($this) as $var => $value) {'); if ($this->dateFormat) { $jsonSerializeMethod @@ -64,10 +68,15 @@ public function build(Dto $object, PhpNamespace $ns, ClassType $class): void ->addBody(' if ($value instanceof \\JsonSerializable) {') ->addBody(' $value = $value->jsonSerialize();') ->addBody(' }') + ->addBody(' if (class_exists(\\UnitEnum::class) && $value instanceof \\UnitEnum) {') + ->addBody(' $value = $value->value;') + ->addBody(' }') + ->addBody(' if (is_object($value) && method_exists($value, "__toString")) {') + ->addBody(' $value = (string)$value;') + ->addBody(' }') ->addBody(' $array[$var] = $value;') ->addBody('}') ->addBody('return $array;'); } } } -