From a3a291c51c378cc4a7f3aec6cd2478bdbd110575 Mon Sep 17 00:00:00 2001 From: Sebastiaan de Rooij Date: Thu, 23 Feb 2023 17:50:46 +0100 Subject: [PATCH] Fix #315 PHP 8.2 deprecation for dynamic properties (#316) Co-authored-by: Jonathan Goode --- src/ICal/Event.php | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/ICal/Event.php b/src/ICal/Event.php index cc6b995..935abb6 100644 --- a/src/ICal/Event.php +++ b/src/ICal/Event.php @@ -127,6 +127,13 @@ class Event */ public $attendee; + /** + * Manage additional properties + * + * @var array + */ + private $additionalProperties = []; + /** * Creates the Event object * @@ -137,10 +144,29 @@ public function __construct(array $data = array()) { foreach ($data as $key => $value) { $variable = self::snakeCase($key); - $this->{$variable} = self::prepareData($value); + if (property_exists($this, $variable)) { + $this->{$variable} = $this->prepareData($value); + } else { + $this->additionalProperties[$variable] = $this->prepareData($value); + } } } + /** + * Magic getter method + * + * @param string $additionalPropertyName + * @return mixed + */ + public function __get($additionalPropertyName) + { + if (array_key_exists($additionalPropertyName, $this->additionalProperties)) { + return $this->additionalProperties[$additionalPropertyName]; + } + + return null; + } + /** * Prepares the data for output * @@ -151,8 +177,12 @@ protected function prepareData($value) { if (is_string($value)) { return stripslashes(trim(str_replace('\n', "\n", $value))); - } elseif (is_array($value)) { - return array_map('self::prepareData', $value); + } + + if (is_array($value)) { + return array_map(function ($value) { + return $this->prepareData($value); + }, $value); } return $value;