Add generic interface implementations
This commit is contained in:
parent
4b02d4f0e6
commit
b39a482136
|
@ -1,3 +1,3 @@
|
||||||
# Useful PHP Basics
|
# 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.
|
||||||
|
|
|
@ -1,16 +1,19 @@
|
||||||
{
|
{
|
||||||
"name": "opencultureconsulting/basics",
|
"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",
|
"type": "library",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"trait",
|
"ArrayAccess",
|
||||||
"getter",
|
"Countable",
|
||||||
"setter",
|
"Getter",
|
||||||
"singleton",
|
"Iterator",
|
||||||
"list",
|
"IteratorAggregate",
|
||||||
"queue",
|
"Setter",
|
||||||
"stack",
|
"Singleton",
|
||||||
"errorhandler"
|
"StrictList",
|
||||||
|
"StrictQueue",
|
||||||
|
"StrictStack",
|
||||||
|
"throwErrorException"
|
||||||
],
|
],
|
||||||
"homepage": "https://github.com/opencultureconsulting/php-basics",
|
"homepage": "https://github.com/opencultureconsulting/php-basics",
|
||||||
"readme": "README.md",
|
"readme": "README.md",
|
||||||
|
|
|
@ -0,0 +1,96 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Useful PHP Basics
|
||||||
|
* Copyright (C) 2023 Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace OCC\Basics\Traits;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A generic implementation of the ArrayAccess interface.
|
||||||
|
*
|
||||||
|
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
|
||||||
|
* @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]);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Useful PHP Basics
|
||||||
|
* Copyright (C) 2023 Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace OCC\Basics\Traits;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A generic implementation of the Countable interface.
|
||||||
|
*
|
||||||
|
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,94 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Useful PHP Basics
|
||||||
|
* Copyright (C) 2023 Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace OCC\Basics\Traits;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A generic implementation of the Iterator interface.
|
||||||
|
*
|
||||||
|
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
|
||||||
|
* @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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Useful PHP Basics
|
||||||
|
* Copyright (C) 2023 Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace OCC\Basics\Traits;
|
||||||
|
|
||||||
|
use ArrayIterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A generic implementation of the IteratorAggregate interface.
|
||||||
|
*
|
||||||
|
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue