for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Diacdg\TypedArray;
abstract class AbstractTypedArray extends \ArrayObject
{
protected $valueType;
public function __construct(string $valueType, array $array = [], int $flags = 0, string $iteratorClass = "ArrayIterator") {
$this->valueType = $valueType;
$this->checkAllElements($array);
parent::__construct($array, $flags, $iteratorClass);
}
public function offsetSet($offset, $value)
$this->checkType($value);
$this->checkOffset($offset);
parent::offsetSet($offset, $value);
public function exchangeArray($array)
parent::exchangeArray($array);
protected function checkType($value): void
if (gettype($value) !== strtolower($this->valueType) && !$value instanceof $this->valueType && !($this->valueType === 'callable' && is_callable($value))) {
throw new \InvalidArgumentException('Value must be of type ' . $this->valueType . " but value of type " . gettype($value) . " given.");
abstract protected function checkOffset($offset): void;
private function checkAllElements(array $array): void
array_walk($array, function ($value, $offset) {
});