src/Common/Collection/MainCollection.php line 19

  1. <?php
  2. namespace App\Common\Collection;
  3. class MainCollection extends \ArrayObject
  4. {
  5.     private $collectionType;
  6.     public function __construct($type$data = [])
  7.     {
  8.         $this->collectionType $type;
  9.         parent::__construct($data);
  10.     }
  11.     private function checkType($value): bool
  12.     {
  13.         return $value instanceof $this->collectionType;
  14.     }
  15.     public function append($value)
  16.     {
  17.         if (!$this->checkType($value)) {
  18.           switch ($this->collectionType) {
  19.             case 'float':
  20.               # code...
  21.               parent::append((float) $value);
  22.               break;
  23.             case 'integer':
  24.               # code...
  25.               parent::append((integer) $value);
  26.               break;
  27.             case 'string':
  28.               # code...
  29.               parent::append((string) $value);
  30.               break;
  31.             default:
  32.               # code...
  33.               throw new \InvalidArgumentException('Type error');
  34.               break;
  35.           }
  36.           return;
  37.         }
  38.         parent::append($value);
  39.     }
  40.     public function offsetSet($key$value)
  41.     {
  42.         if (!$this->checkType($value)) {
  43.             throw new \InvalidArgumentException('Type error');
  44.         }
  45.         parent::offsetSet($key$value);
  46.     }
  47. }