From 1b305d8f0b416010cfe86606b1b69ae8911697ea Mon Sep 17 00:00:00 2001 From: rv1971 Date: Mon, 14 Dec 2020 17:51:28 +0100 Subject: [PATCH] Access extension properties like `x-foo-bar` as `x_foo_bar` The OpenAPI specification allows for extension properties whose names must start with `x-`, citing `x-internal-id` as an example. Since such a property cannot be accessed as `$obj->x-internal-id`, the present commit translates the name so that it can be accessed as `$obj->x_internal_id`. --- src/SpecBaseObject.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/SpecBaseObject.php b/src/SpecBaseObject.php index 4ae4539..f0db62d 100644 --- a/src/SpecBaseObject.php +++ b/src/SpecBaseObject.php @@ -327,6 +327,11 @@ protected function validateUrl(string $property) public function __get($name) { + /* Access extension properties like `x-foo-bar` as `x_foo_bar` */ + if ( substr( $name, 0, 2) == 'x_' ) { + $name = str_replace( '_', '-', $name ); + } + if (isset($this->_properties[$name])) { return $this->_properties[$name]; } @@ -347,11 +352,21 @@ public function __get($name) public function __set($name, $value) { + /* see __get() */ + if ( substr( $name, 0, 2) == 'x_' ) { + $name = str_replace( '_', '-', $name ); + } + $this->_properties[$name] = $value; } public function __isset($name) { + /* see __get() */ + if ( substr( $name, 0, 2) == 'x_' ) { + $name = str_replace( '_', '-', $name ); + } + if (isset($this->_properties[$name]) || isset($this->attributeDefaults()[$name]) || isset($this->attributes()[$name])) { return $this->__get($name) !== null; } @@ -361,6 +376,11 @@ public function __isset($name) public function __unset($name) { + /* see __get() */ + if ( substr( $name, 0, 2) == 'x_' ) { + $name = str_replace( '_', '-', $name ); + } + unset($this->_properties[$name]); }