From fec263c64b99964e6746fadcd193bf05d0b39cc6 Mon Sep 17 00:00:00 2001 From: Sebastian Meyer Date: Sun, 11 Feb 2024 19:05:56 +0100 Subject: [PATCH] Add Overloading traits --- composer.json | 3 ++ src/Traits/OverloadingGetter.php | 76 ++++++++++++++++++++++++++++++++ src/Traits/OverloadingSetter.php | 76 ++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 src/Traits/OverloadingGetter.php create mode 100644 src/Traits/OverloadingSetter.php diff --git a/composer.json b/composer.json index 135c916..fe9dd8b 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,9 @@ "Getter", "Iterator", "IteratorAggregate", + "Overloading", + "OverloadingGetter", + "OverloadingSetter", "Setter", "Singleton", "StrictCollection", diff --git a/src/Traits/OverloadingGetter.php b/src/Traits/OverloadingGetter.php new file mode 100644 index 0000000..01c200c --- /dev/null +++ b/src/Traits/OverloadingGetter.php @@ -0,0 +1,76 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +declare(strict_types=1); + +namespace OCC\Basics\Traits; + +/** + * Overloads a class with readable magic properties. + * + * Internally it reads the protected `$_data` array whose keys are interpreted + * as property names. + * + * > Example: Reading `Foo->bar` will return the value of `Foo->_data['bar']`. + * + * @author Sebastian Meyer + * @package Basics\Traits + */ +trait OverloadingGetter +{ + /** + * Holds the magically readable data. + * + * @var mixed[] + * + * @internal + */ + protected array $_data = []; + + /** + * Read data from an inaccessible property. + * + * @param string $property The class property to get + * + * @return mixed The property's current value or NULL if not set + * + * @internal + */ + public function __get(string $property): mixed + { + return $this->_data[$property] ?? null; + } + + /** + * Check if an inaccessible property is set and not empty. + * + * @param string $property The class property to check + * + * @return bool Whether the class property is set and not empty + * + * @internal + */ + public function __isset(string $property): bool + { + $value = $this->__get($property); + return boolval($value) !== false; + } +} diff --git a/src/Traits/OverloadingSetter.php b/src/Traits/OverloadingSetter.php new file mode 100644 index 0000000..8828de6 --- /dev/null +++ b/src/Traits/OverloadingSetter.php @@ -0,0 +1,76 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +declare(strict_types=1); + +namespace OCC\Basics\Traits; + +/** + * Overloads a class with writable magic properties. + * + * Internally it writes the protected `$_data` array whose keys are interpreted + * as property names. + * + * > Example: `Foo->bar = 42` will set `Foo->_data['bar']` to `42`. + * + * @author Sebastian Meyer + * @package Basics\Traits + */ +trait OverloadingSetter +{ + /** + * Holds the magically writable data. + * + * @var mixed[] + * + * @internal + */ + protected array $_data = []; + + /** + * Write data to an inaccessible property. + * + * @param string $property The class property to set + * @param mixed $value The new value of the property + * + * @return void + * + * @internal + */ + public function __set(string $property, mixed $value): void + { + $this->_data[$property] = $value; + } + + /** + * Unset an inaccessible property. + * + * @param string $property The class property to unset + * + * @return void + * + * @internal + */ + public function __unset(string $property): void + { + unset($this->_data[$property]); + } +}