src/Common/Collection/MainCollection.php line 19
<?php
namespace App\Common\Collection;
class MainCollection extends \ArrayObject
{
private $collectionType;
public function __construct($type, $data = [])
{
$this->collectionType = $type;
parent::__construct($data);
}
private function checkType($value): bool
{
return $value instanceof $this->collectionType;
}
public function append($value)
{
if (!$this->checkType($value)) {
switch ($this->collectionType) {
case 'float':
# code...
parent::append((float) $value);
break;
case 'integer':
# code...
parent::append((integer) $value);
break;
case 'string':
# code...
parent::append((string) $value);
break;
default:
# code...
throw new \InvalidArgumentException('Type error');
break;
}
return;
}
parent::append($value);
}
public function offsetSet($key, $value)
{
if (!$this->checkType($value)) {
throw new \InvalidArgumentException('Type error');
}
parent::offsetSet($key, $value);
}
}