Skip to content

Commit

Permalink
Fix #315 PHP 8.2 deprecation for dynamic properties (#316)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonathan Goode <u01jmg3@users.noreply.github.com>
  • Loading branch information
Hikariii and u01jmg3 committed Feb 23, 2023
1 parent b154621 commit a3a291c
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/ICal/Event.php
Expand Up @@ -127,6 +127,13 @@ class Event
*/
public $attendee;

/**
* Manage additional properties
*
* @var array<string, mixed>
*/
private $additionalProperties = [];

/**
* Creates the Event object
*
Expand All @@ -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
*
Expand All @@ -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;
Expand Down

0 comments on commit a3a291c

Please sign in to comment.