Add __get/__set/etc methods to the Metadata class.

Update metadata can include additional, arbitrary fields such as request statistics, licence information, and so on. This data was stored as dynamic properties on Metadata subclasses, which triggered a deprecation notice in PHP 8.2. Fixed by explicitly storing dynamic fields in a new protected property and adding magic methods to access/modify those fields.

Fixes #536
This commit is contained in:
Yahnis Elsts 2023-06-16 14:11:14 +03:00
parent 48b03e93c9
commit ad59ffe9a3

View file

@ -15,6 +15,12 @@ if ( !class_exists(Metadata::class, false) ):
* @access public
*/
abstract class Metadata {
/**
* Additional dynamic properties, usually copied from the API response.
*
* @var array<string,mixed>
*/
protected $extraProperties = array();

/**
* Create an instance of this class from a JSON document.
@ -135,6 +141,22 @@ if ( !class_exists(Metadata::class, false) ):
protected function getPrefixedFilter($tag) {
return 'puc_' . $tag;
}

public function __set($name, $value) {
$this->extraProperties[$name] = $value;
}

public function __get($name) {
return isset($this->extraProperties[$name]) ? $this->extraProperties[$name] : null;
}

public function __isset($name) {
return isset($this->extraProperties[$name]);
}

public function __unset($name) {
unset($this->extraProperties[$name]);
}
}

endif;