Skip to content

Commit

Permalink
fix serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvsk committed Apr 7, 2023
1 parent abea3db commit cd7ac81
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 41 deletions.
18 changes: 14 additions & 4 deletions example/php7.4/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
18 changes: 14 additions & 4 deletions example/php7.4/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
18 changes: 14 additions & 4 deletions example/php7.4/ScienceBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
18 changes: 14 additions & 4 deletions example/php8.0/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
18 changes: 14 additions & 4 deletions example/php8.0/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
18 changes: 14 additions & 4 deletions example/php8.0/ScienceBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
18 changes: 14 additions & 4 deletions example/php8.1/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
18 changes: 14 additions & 4 deletions example/php8.1/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
18 changes: 14 additions & 4 deletions example/php8.1/ScienceBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Loading

0 comments on commit cd7ac81

Please sign in to comment.