From ffdfa27a828e0077614dda3039249f4c48bcb243 Mon Sep 17 00:00:00 2001 From: Wilt Date: Wed, 21 May 2014 14:40:47 +0200 Subject: [PATCH 1/4] Add magic method __isset method to HalResource To be able to use the isset function in the getIdFromResource method (line 154 in HalLinks) we need to add the magic __isset method to the HalResource. Right now the isset always returns false even if the 'id' is set in $resource. This can be considered a bug. --- src/PhlyRestfully/HalResource.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/PhlyRestfully/HalResource.php b/src/PhlyRestfully/HalResource.php index efa110a4..36a50b97 100644 --- a/src/PhlyRestfully/HalResource.php +++ b/src/PhlyRestfully/HalResource.php @@ -34,6 +34,30 @@ public function __construct($resource, $id) $this->id = $id; } + /** + * Check if properties are set + * + * @param string $name + * @throws Exception\InvalidArgumentException + * @return mixed + */ + public function __isset($name) + { + $names = array( + 'resource' => 'resource', + 'id' => 'id', + ); + $name = strtolower($name); + if (!in_array($name, array_keys($names))) { + throw new Exception\InvalidArgumentException(sprintf( + 'Invalid property name "%s"', + $name + )); + } + $prop = $names[$name]; + return $this->{$prop}; + } + /** * Retrieve properties * From bbe5d4c3533120faad4690efcba44568376cce67 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Thu, 26 May 2016 14:57:20 -0500 Subject: [PATCH 2/4] Use short array notation --- src/HalResource.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/HalResource.php b/src/HalResource.php index b5ef6c04..e57c3a1c 100644 --- a/src/HalResource.php +++ b/src/HalResource.php @@ -43,17 +43,19 @@ public function __construct($resource, $id) */ public function __isset($name) { - $names = array( - 'resource' => 'resource', - 'id' => 'id', - ); + $names = [ + 'resource' => 'resource', + 'id' => 'id', + ]; + $name = strtolower($name); - if (!in_array($name, array_keys($names))) { + if (! in_array($name, array_keys($names))) { throw new Exception\InvalidArgumentException(sprintf( 'Invalid property name "%s"', $name )); } + $prop = $names[$name]; return $this->{$prop}; } From 227f0d59354ec959c48ab7d1a90e338359338ce9 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Thu, 26 May 2016 15:02:22 -0500 Subject: [PATCH 3/4] Fixed __isset() and __get() logic - `__isset()` needs to return a boolean. - Updated `__get()` to call on `__isset()`, raising an exception on failure, and returning the named property on success. --- src/HalResource.php | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/src/HalResource.php b/src/HalResource.php index e57c3a1c..dc7fae42 100644 --- a/src/HalResource.php +++ b/src/HalResource.php @@ -43,21 +43,7 @@ public function __construct($resource, $id) */ public function __isset($name) { - $names = [ - 'resource' => 'resource', - 'id' => 'id', - ]; - - $name = strtolower($name); - if (! in_array($name, array_keys($names))) { - throw new Exception\InvalidArgumentException(sprintf( - 'Invalid property name "%s"', - $name - )); - } - - $prop = $names[$name]; - return $this->{$prop}; + return in_array(strtolower($name), ['resource', 'id'], true); } /** @@ -68,19 +54,14 @@ public function __isset($name) */ public function __get($name) { - $names = [ - 'resource' => 'resource', - 'id' => 'id', - ]; $name = strtolower($name); - if (!in_array($name, array_keys($names))) { + if (! $this->__isset($name)) { throw new Exception\InvalidArgumentException(sprintf( 'Invalid property name "%s"', $name )); } - $prop = $names[$name]; - return $this->{$prop}; + return $this->{$name}; } /** From df5ede044caa655e076c2d29c996dbb3cc2c48c8 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Thu, 26 May 2016 15:03:08 -0500 Subject: [PATCH 4/4] Added CHANGELOG for #126 --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc2b77cd..53e65f86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ All notable changes to this project will be documented in this file, in reverse and HHVM. - [#107](https://github.com/phly/PhlyRestfully/pull/107) suggests using zfr/zfr-cors to provide CORS support for your API. +- [#126](https://github.com/phly/PhlyRestfully/pull/126) adds an `__isset()` + method to `HalResource`, ensuring you can test for the identifier and/or + resource (e.g., via `isset($halResource->id)`). ### Deprecated