| 1 | <?php |
||
| 10 | trait FreezableCollectionTrait |
||
| 11 | { |
||
| 12 | use FreezableTrait; |
||
| 13 | |||
| 14 | 6 | private static function ensureItemsAreTraversable($items) |
|
| 15 | { |
||
| 16 | 6 | if (! is_array($items) and ! $items instanceof \Traversable) { |
|
| 17 | 2 | throw new \UnexpectedValueException( |
|
| 18 | 2 | 'Collection returned from getItems() must be either an array or a Traversable object.' |
|
| 19 | ); |
||
| 20 | } |
||
| 21 | 4 | } |
|
| 22 | |||
| 23 | /** |
||
| 24 | * {@inheritdoc} |
||
| 25 | */ |
||
| 26 | 4 | public function performFreeze() |
|
| 27 | { |
||
| 28 | 4 | $items = $this->getItems(); |
|
| 29 | |||
| 30 | 4 | self::ensureItemsAreTraversable($items); |
|
| 31 | |||
| 32 | 3 | foreach ($items as $item) { |
|
| 33 | 3 | if (! $item instanceof FreezableInterface) { |
|
| 34 | 1 | throw new \UnexpectedValueException( |
|
| 35 | 1 | 'Item must be instance of Clippings\Freezable\FreezableInterface to be freezed' |
|
| 36 | ); |
||
| 37 | } |
||
| 38 | |||
| 39 | 2 | $item->freeze(); |
|
| 40 | } |
||
| 41 | 2 | } |
|
| 42 | |||
| 43 | /** |
||
| 44 | * {@inheritdoc} |
||
| 45 | */ |
||
| 46 | 4 | public function performUnfreeze() |
|
| 47 | { |
||
| 48 | 4 | $items = $this->getItems(); |
|
| 49 | |||
| 50 | 4 | self::ensureItemsAreTraversable($items); |
|
| 51 | |||
| 52 | 3 | foreach ($items as $item) { |
|
| 53 | 3 | if (! $item instanceof FreezableInterface) { |
|
| 54 | 1 | throw new \UnexpectedValueException( |
|
| 55 | 1 | 'Item must be instance of Clippings\Freezable\FreezableInterface to be unfreezed' |
|
| 56 | ); |
||
| 57 | } |
||
| 58 | |||
| 59 | 2 | $item->unfreeze(); |
|
| 60 | } |
||
| 61 | 2 | } |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @return FreezableInterface[]|Traversable array or traversable of FreezableInterface objects |
||
| 65 | */ |
||
| 66 | abstract public function getItems(); |
||
| 67 | } |
||
| 68 |