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 diff --git a/src/HalResource.php b/src/HalResource.php index 59f6f22f..dc7fae42 100644 --- a/src/HalResource.php +++ b/src/HalResource.php @@ -34,6 +34,18 @@ 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) + { + return in_array(strtolower($name), ['resource', 'id'], true); + } + /** * Retrieve properties * @@ -42,19 +54,14 @@ public function __construct($resource, $id) */ 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}; } /**