From a0bb0eea8ccdfd4218ee0b21cd80f2f75912365d Mon Sep 17 00:00:00 2001 From: alcidesrh Date: Mon, 5 Apr 2021 00:21:56 -0400 Subject: [PATCH] Add alias for properties closed. --- src/GenericResource.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/GenericResource.php b/src/GenericResource.php index 9252105..49f3e9c 100755 --- a/src/GenericResource.php +++ b/src/GenericResource.php @@ -50,13 +50,13 @@ public function toArray($request) foreach ($value as $key2 => $value2) { try { - if ($this->{$key2} instanceof Collection) { - $data[$key2] = new GenericResourceCollection($this->{$key2}, $value2); - } else if (\gettype($this->{$key2}) === 'object') { - $data[$key2] = new GenericResource($this->{$key2}, $value2); + if ($this->$key2 instanceof Collection) { + $data[$key2] = new GenericResourceCollection($this->$key2, $value2); + } else if (\gettype($this->$key2) === 'object') { + $data[$key2] = new GenericResource($this->$key2, $value2); } //Property name change - else if(\gettype($this->{$value2}) === 'string' && ($this->$value2 || method_exists($this->resource, $value2) || property_exists($this->resource, $value2))) + else if((\gettype($value2) === 'string' && \gettype($this->$value2) === 'string') && ($this->$value2 || method_exists($this->resource, $value2) || property_exists($this->resource, $value2))) $data[$key2] = $this->$value2; else if (method_exists($this->resource, $key2) || property_exists($this->resource, $key2)) { $data[$key2] = $this->$key2; @@ -72,12 +72,12 @@ public function toArray($request) $newKey = !\is_numeric($key) ? $key : $value; - if ($this->{$value} instanceof Collection) { - $data[$newKey] = new GenericResourceCollection($this->{$value}); - } else if (\gettype($this->{$value}) === 'object') { - $data[$newKey] = new GenericResource($this->{$value}); - } else if ($this->{$value} || method_exists($this->resource, $value) || property_exists($this->resource, $value)) { - $data[$newKey] = $this->{$value}; + if ($this->$value instanceof Collection) { + $data[$newKey] = new GenericResourceCollection($this->$value); + } else if (\gettype($this->$value) === 'object') { + $data[$newKey] = new GenericResource($this->$value); + } else if ($this->$value || method_exists($this->resource, $value) || property_exists($this->resource, $value)) { + $data[$newKey] = $this->$value; } } catch (\Throwable $th) {