From b39a48213606e616eb2cfefe4434cb603ae0c804 Mon Sep 17 00:00:00 2001 From: Sebastian Meyer Date: Wed, 15 Nov 2023 12:01:40 +0100 Subject: [PATCH] Add generic interface implementations --- README.md | 2 +- composer.json | 21 ++++--- src/Traits/ArrayAccess.php | 96 ++++++++++++++++++++++++++++++++ src/Traits/Countable.php | 50 +++++++++++++++++ src/Traits/Iterator.php | 94 +++++++++++++++++++++++++++++++ src/Traits/IteratorAggregate.php | 52 +++++++++++++++++ 6 files changed, 305 insertions(+), 10 deletions(-) create mode 100644 src/Traits/ArrayAccess.php create mode 100644 src/Traits/Countable.php create mode 100644 src/Traits/Iterator.php create mode 100644 src/Traits/IteratorAggregate.php diff --git a/README.md b/README.md index cfe77cc..b4e671e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # Useful PHP Basics -This is a collection of generic [Classes](https://www.php.net/manual/en/language.oop5.php) and useful [Traits](https://www.php.net/manual/en/language.oop5.traits.php) for your PHP projects. +This is a collection of generic [Classes](https://www.php.net/manual/en/language.oop5.php), [Functions](https://www.php.net/manual/en/language.functions.php) and useful [Traits](https://www.php.net/manual/en/language.oop5.traits.php) for your PHP projects. diff --git a/composer.json b/composer.json index dbcf188..8216d2d 100644 --- a/composer.json +++ b/composer.json @@ -1,16 +1,19 @@ { "name": "opencultureconsulting/basics", - "description": "This is a collection of generic Classes and useful Traits for PHP projects.", + "description": "This is a collection of generic Classes, Functions and useful Traits for PHP projects.", "type": "library", "keywords": [ - "trait", - "getter", - "setter", - "singleton", - "list", - "queue", - "stack", - "errorhandler" + "ArrayAccess", + "Countable", + "Getter", + "Iterator", + "IteratorAggregate", + "Setter", + "Singleton", + "StrictList", + "StrictQueue", + "StrictStack", + "throwErrorException" ], "homepage": "https://github.com/opencultureconsulting/php-basics", "readme": "README.md", diff --git a/src/Traits/ArrayAccess.php b/src/Traits/ArrayAccess.php new file mode 100644 index 0000000..7f0c376 --- /dev/null +++ b/src/Traits/ArrayAccess.php @@ -0,0 +1,96 @@ + + * + * 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; + +/** + * A generic implementation of the ArrayAccess interface. + * + * @author Sebastian Meyer + * @package opencultureconsulting/basics + * + * @implements \ArrayAccess + */ +trait ArrayAccess /* implements \ArrayAccess */ +{ + /** + * Holds the array-accessible data. + */ + private array $data = []; + + /** + * Whether the specified offset exists. + * @see \ArrayAccess::offsetExists() + * + * @param mixed $offset The offset to check for + * + * @return bool Whether the offset exists + */ + public function offsetExists(mixed $offset): bool + { + return isset($this->data[$offset]); + } + + /** + * Retrieve data at the specified offset. + * @see \ArrayAccess::offsetGet() + * + * @param mixed $offset The offset to retrieve at + * + * @return mixed The value at the offset or NULL + */ + public function offsetGet(mixed $offset): mixed + { + return $this->data[$offset] ?? null; + } + + /** + * Assign a value to the specified offset. + * @see \ArrayAccess::offsetSet() + * + * @param mixed $offset The offset to assign to or NULL to append + * @param mixed $value The value to set + * + * @return void + */ + public function offsetSet(mixed $offset, mixed $value): void + { + if (is_null($offset)) { + $this->data[] = $value; + } else { + $this->data[$offset] = $value; + } + } + + /** + * Unset the specified offset. + * @see \ArrayAccess::offsetUnset() + * + * @param mixed $offset The offset to unset + * + * @return void + */ + public function offsetUnset(mixed $offset): void + { + unset($this->data[$offset]); + } +} diff --git a/src/Traits/Countable.php b/src/Traits/Countable.php new file mode 100644 index 0000000..4c8ed99 --- /dev/null +++ b/src/Traits/Countable.php @@ -0,0 +1,50 @@ + + * + * 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; + +/** + * A generic implementation of the Countable interface. + * + * @author Sebastian Meyer + * @package opencultureconsulting/basics + * + * @implements \Countable + */ +trait Countable /* implements \Countable */ +{ + /** + * Holds the countable data. + */ + private array $data = []; + + /** + * Count the data items. + * @see \Countable::count() + * + * @return int The number of data items + */ + public function count(): int + { + return count($this->data); + } +} diff --git a/src/Traits/Iterator.php b/src/Traits/Iterator.php new file mode 100644 index 0000000..443f696 --- /dev/null +++ b/src/Traits/Iterator.php @@ -0,0 +1,94 @@ + + * + * 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; + +/** + * A generic implementation of the Iterator interface. + * + * @author Sebastian Meyer + * @package opencultureconsulting/basics + * + * @implements \Iterator + */ +trait Iterator /* implements \Iterator */ +{ + /** + * Holds the iterable data. + */ + private array $data = []; + + /** + * Return the current item. + * @see \Iterator::current() + * + * @return mixed The current item or FALSE if invalid + */ + public function current(): mixed + { + return current($this->data); + } + + /** + * Return the current key. + * @see \Iterator::key() + * + * @return mixed The current key or NULL if invalid + */ + public function key(): mixed + { + return key($this->data); + } + + /** + * Move forward to next item. + * @see \Iterator::next() + * + * @return void + */ + public function next(): void + { + next($this->data); + } + + /** + * Rewind the iterator to the first item. + * @see \Iterator::rewind() + * + * @return void + */ + public function rewind(): void + { + reset($this->data); + } + + /** + * Checks if current position is valid. + * @see \Iterator::valid() + * + * @return bool Whether the current position is valid + */ + public function valid(): bool + { + return !is_null($this->key()); + } +} diff --git a/src/Traits/IteratorAggregate.php b/src/Traits/IteratorAggregate.php new file mode 100644 index 0000000..e8a96bc --- /dev/null +++ b/src/Traits/IteratorAggregate.php @@ -0,0 +1,52 @@ + + * + * 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; + +use ArrayIterator; + +/** + * A generic implementation of the IteratorAggregate interface. + * + * @author Sebastian Meyer + * @package opencultureconsulting/basics + * + * @implements \IteratorAggregate + */ +trait IteratorAggregate /* implements \IteratorAggregate */ +{ + /** + * Holds the iterable data. + */ + private array $data = []; + + /** + * Retrieve an external iterator. + * @see \IteratorAggregate::getIterator() + * + * @return \ArrayIterator + */ + public function getIterator(): ArrayIterator + { + return new ArrayIterator($this->data); + } +}