From 4335b7275bcbd066a81fd2de998531719144429f Mon Sep 17 00:00:00 2001 From: Craig McMahon Date: Mon, 25 Sep 2023 15:54:51 +0100 Subject: [PATCH] Improve php8 compatability - jsonSerialize return type - dynamic properties - guzzle requirement --- .gitignore | 3 +- composer.json | 4 +-- phpunit.xml.dist | 43 +++++++++++------------- src/Groups/Area.php | 2 +- src/Groups/Areas.php | 2 +- src/Groups/Content.php | 2 +- src/Groups/Coordinate.php | 2 +- src/Groups/Description.php | 2 +- src/Groups/Dimensions.php | 2 +- src/Groups/EpcRating.php | 2 +- src/Groups/External.php | 2 +- src/Groups/GoogleStreetView.php | 2 +- src/Groups/LeaseExpiry.php | 2 +- src/Groups/Location.php | 2 +- src/Groups/MinMaxArea.php | 2 +- src/Groups/MinimumContractLength.php | 2 +- src/Groups/PafAddress.php | 2 +- src/Groups/PricePerUnitArea.php | 2 +- src/Groups/Pricing.php | 2 +- src/Groups/ServiceCharge.php | 2 +- src/Groups/TenantEligibility.php | 2 +- src/Request/RequestBase.php | 12 ------- src/SynthesizeTrait.php | 50 +++++++++++++++++----------- tests/SendPropertyTest.php | 1 - 24 files changed, 72 insertions(+), 77 deletions(-) diff --git a/.gitignore b/.gitignore index b43d531..dfb17e1 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ composer.phar /composer.lock /phpunit.xml /phpunit.phar -/zoopla \ No newline at end of file +/zoopla +.phpunit.cache \ No newline at end of file diff --git a/composer.json b/composer.json index 7da2d91..a1cbe1b 100644 --- a/composer.json +++ b/composer.json @@ -13,11 +13,11 @@ "php": ">=7.2", "ext-json": "*", "myclabs/php-enum": "1.*", - "guzzlehttp/guzzle": "^6.0.0", + "guzzlehttp/guzzle": "^6.0 || ^7.0", "justinrainbow/json-schema": "^5.2.10" }, "require-dev": { - "phpunit/phpunit": "7.*" + "phpunit/phpunit": "9.*" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 05337b0..023731a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,28 +1,25 @@ - - + - - ./tests + + tests - - - ./src - - - - - - - - - - \ No newline at end of file + + + src + + + diff --git a/src/Groups/Area.php b/src/Groups/Area.php index 2cbdcf9..8826018 100644 --- a/src/Groups/Area.php +++ b/src/Groups/Area.php @@ -5,7 +5,7 @@ use mehmetbulut\Zoopla\SynthesizeTrait; use mehmetbulut\Zoopla\Values\AreaUnit; -class Area +class Area implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Groups/Areas.php b/src/Groups/Areas.php index 177fda6..599c97c 100644 --- a/src/Groups/Areas.php +++ b/src/Groups/Areas.php @@ -4,7 +4,7 @@ use mehmetbulut\Zoopla\SynthesizeTrait; -class Areas +class Areas implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Groups/Content.php b/src/Groups/Content.php index 0ef77df..9239c9a 100644 --- a/src/Groups/Content.php +++ b/src/Groups/Content.php @@ -5,7 +5,7 @@ use mehmetbulut\Zoopla\SynthesizeTrait; use mehmetbulut\Zoopla\Values\ContentType; -class Content +class Content implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Groups/Coordinate.php b/src/Groups/Coordinate.php index 84a58c7..3ddecb0 100644 --- a/src/Groups/Coordinate.php +++ b/src/Groups/Coordinate.php @@ -6,7 +6,7 @@ use mehmetbulut\Zoopla\SynthesizeTrait; -class Coordinate +class Coordinate implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Groups/Description.php b/src/Groups/Description.php index afab614..f23d24b 100644 --- a/src/Groups/Description.php +++ b/src/Groups/Description.php @@ -4,7 +4,7 @@ use mehmetbulut\Zoopla\SynthesizeTrait; -class Description +class Description implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Groups/Dimensions.php b/src/Groups/Dimensions.php index 5a8633a..165bdda 100644 --- a/src/Groups/Dimensions.php +++ b/src/Groups/Dimensions.php @@ -5,7 +5,7 @@ use mehmetbulut\Zoopla\SynthesizeTrait; use mehmetbulut\Zoopla\Values\DimensionUnit; -class Dimensions +class Dimensions implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Groups/EpcRating.php b/src/Groups/EpcRating.php index 6b971aa..e613615 100644 --- a/src/Groups/EpcRating.php +++ b/src/Groups/EpcRating.php @@ -4,7 +4,7 @@ use mehmetbulut\Zoopla\SynthesizeTrait; -class EpcRating +class EpcRating implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Groups/External.php b/src/Groups/External.php index fafeeaa..462280b 100644 --- a/src/Groups/External.php +++ b/src/Groups/External.php @@ -4,7 +4,7 @@ use mehmetbulut\Zoopla\SynthesizeTrait; -class External +class External implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Groups/GoogleStreetView.php b/src/Groups/GoogleStreetView.php index 34de626..e5a5727 100644 --- a/src/Groups/GoogleStreetView.php +++ b/src/Groups/GoogleStreetView.php @@ -4,7 +4,7 @@ use mehmetbulut\Zoopla\SynthesizeTrait; -class GoogleStreetView +class GoogleStreetView implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Groups/LeaseExpiry.php b/src/Groups/LeaseExpiry.php index 927dcd9..29e6e5e 100644 --- a/src/Groups/LeaseExpiry.php +++ b/src/Groups/LeaseExpiry.php @@ -4,7 +4,7 @@ use mehmetbulut\Zoopla\SynthesizeTrait; -class LeaseExpiry +class LeaseExpiry implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Groups/Location.php b/src/Groups/Location.php index 6082c6e..0bd8345 100644 --- a/src/Groups/Location.php +++ b/src/Groups/Location.php @@ -4,7 +4,7 @@ use mehmetbulut\Zoopla\SynthesizeTrait; -class Location +class Location implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Groups/MinMaxArea.php b/src/Groups/MinMaxArea.php index 0dfd161..bafb4b6 100644 --- a/src/Groups/MinMaxArea.php +++ b/src/Groups/MinMaxArea.php @@ -4,7 +4,7 @@ use mehmetbulut\Zoopla\SynthesizeTrait; -class MinMaxArea +class MinMaxArea implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Groups/MinimumContractLength.php b/src/Groups/MinimumContractLength.php index cb625f9..01362f2 100644 --- a/src/Groups/MinimumContractLength.php +++ b/src/Groups/MinimumContractLength.php @@ -5,7 +5,7 @@ use mehmetbulut\Zoopla\SynthesizeTrait; use mehmetbulut\Zoopla\Values\RentalTermUnit; -class MinimumContractLength +class MinimumContractLength implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Groups/PafAddress.php b/src/Groups/PafAddress.php index 340d99f..be0b4b0 100644 --- a/src/Groups/PafAddress.php +++ b/src/Groups/PafAddress.php @@ -7,7 +7,7 @@ use mehmetbulut\Zoopla\SynthesizeTrait; use mehmetbulut\Zoopla\Values\PostCodeType; -class PafAddress +class PafAddress implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Groups/PricePerUnitArea.php b/src/Groups/PricePerUnitArea.php index 218be3f..a38999d 100644 --- a/src/Groups/PricePerUnitArea.php +++ b/src/Groups/PricePerUnitArea.php @@ -7,7 +7,7 @@ use mehmetbulut\Zoopla\SynthesizeTrait; use mehmetbulut\Zoopla\Values\AreaUnit; -class PricePerUnitArea +class PricePerUnitArea implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Groups/Pricing.php b/src/Groups/Pricing.php index a70c893..38e8b6c 100644 --- a/src/Groups/Pricing.php +++ b/src/Groups/Pricing.php @@ -8,7 +8,7 @@ use mehmetbulut\Zoopla\Values\PricingRentFrequency; use mehmetbulut\Zoopla\Values\TransactionType; -class Pricing +class Pricing implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Groups/ServiceCharge.php b/src/Groups/ServiceCharge.php index ebd022b..0947d23 100644 --- a/src/Groups/ServiceCharge.php +++ b/src/Groups/ServiceCharge.php @@ -6,7 +6,7 @@ use mehmetbulut\Zoopla\Values\AreaUnit; use mehmetbulut\Zoopla\Values\Frequency; -class ServiceCharge +class ServiceCharge implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Groups/TenantEligibility.php b/src/Groups/TenantEligibility.php index a64de0e..3e85b9a 100644 --- a/src/Groups/TenantEligibility.php +++ b/src/Groups/TenantEligibility.php @@ -6,7 +6,7 @@ use mehmetbulut\Zoopla\Values\TenantEligibilityDss; use mehmetbulut\Zoopla\Values\TenantEligibilityStudent; -class TenantEligibility +class TenantEligibility implements \JsonSerializable { use SynthesizeTrait; diff --git a/src/Request/RequestBase.php b/src/Request/RequestBase.php index ad9a36e..3b3d1ed 100755 --- a/src/Request/RequestBase.php +++ b/src/Request/RequestBase.php @@ -44,18 +44,6 @@ public function getObject() return json_decode($this->getJson(), false); } - public function jsonSerialize() - { - $data = []; - foreach ($this->arrSynthesize as $key => $value) { - if (isset($this->{$key}) && !empty($this->{$key})) { - $data[$key] = $this->{$key}; - } - } - - return $data; - } - public function getUrl($environment) { if ($environment == ZooplaRealTime::TEST) { diff --git a/src/SynthesizeTrait.php b/src/SynthesizeTrait.php index cb9b823..fd6b277 100644 --- a/src/SynthesizeTrait.php +++ b/src/SynthesizeTrait.php @@ -6,6 +6,8 @@ trait SynthesizeTrait { + protected $_data = []; + public function __set($property, $value) { if ($this->hasProperty($property)) { @@ -15,29 +17,28 @@ public function __set($property, $value) } } + public function __isset($property) + { + return isset($this->_data[$property]); + } + public function __get($property) { - if (property_exists($this, $property)) { - return $this->{$property}; - } else if ($this->hasProperty($property)) { - if (is_array($property)) { - return $this->{$property}; - } else { - $class = $this->arrSynthesize[$property]['class']; - return $this->{$property} = new $class(); - } + if (isset($this->_data[$property])) { + return $this->_data[$property]; } - throw new \Exception('Invalid param', Response::HTTP_BAD_REQUEST); + if (!$this->hasProperty($property)) { + throw new \Exception('Invalid param', Response::HTTP_BAD_REQUEST); + } + + $class = $this->arrSynthesize[$property]['class']; + return $this->_data[$property] = new $class(); } private function hasProperty($property): bool { - if (isset($this->arrSynthesize[$property])) { - return true; - } else { - return false; - } + return isset($this->arrSynthesize[$property]); } private function setProperty($property, $value) @@ -82,7 +83,7 @@ private function setProperty($property, $value) if ($value) { if (is_string($value)) { $check = true; - } else if (is_object($value) && $value instanceof $param['class']) { + } else if ($value instanceof $param['class']) { $check = true; } } @@ -107,20 +108,29 @@ private function setProperty($property, $value) case 'date'://Y-m-d $format = 'Y-m-d'; $d = DateTime::createFromFormat($format, $value); - $check = $d && $d->format($format) == $value; + $check = $d && $d->format($format) === $value; break; case 'datetime'://Y-m-d H:i:s $format = 'Y-m-d H:i:s'; $d = DateTime::createFromFormat($format, $value); - $check = $d && $d->format($format) == $value; + $check = $d && $d->format($format) === $value; break; } if (!$check) { throw new \Exception('Invalid type '.$property.' '.$param['type'], Response::HTTP_BAD_REQUEST); - } else if ($value) { - $this->{$property} = $value; } + + if (!$value) { + return; + } + + $this->_data[$property] = $value; + } + + public function jsonSerialize():array + { + return $this->_data; } } \ No newline at end of file diff --git a/tests/SendPropertyTest.php b/tests/SendPropertyTest.php index 4767082..ccd945a 100644 --- a/tests/SendPropertyTest.php +++ b/tests/SendPropertyTest.php @@ -123,7 +123,6 @@ public function testSendPropertyParams() //location $request->location->property_number_or_name = 'ds'; $request->location->street_name = 'ds'; - $request->location->street_name = 'ds'; $request->location->locality = 'ds'; $request->location->town_or_city = 'ds'; $request->location->county = 'London';