TypedCollection::setItems()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
  namespace Fiv\Collection;
4
5
  /**
6
   * @author Ivan Shcherbak <[email protected]> 11/7/14
7
   */
8
  abstract class TypedCollection extends BaseCollection {
9
10
11
    /**
12
     * You can add or append only one type of items to this collection
13
     *
14
     * @param $item
15
     * @throws \Exception
16
     */
17
    public abstract function validateType($item);
18
19
20
    /**
21
     * @inheritdoc
22
     */
23
    public function prepend($item) {
24
      $this->validateType($item);
25
      return parent::prepend($item);
26
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function append($item) {
32
      $this->validateType($item);
33
      return parent::append($item);
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function addAfter($index, $items) {
40
41
      if (!is_array($items)) {
42
        throw new \InvalidArgumentException('You can add after only array of items');
43
      }
44
45
      foreach ($items as $item) {
46
        $this->validateType($item);
47
      }
48
49
      return parent::addAfter($index, $items);
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function setItems($items) {
56
      if (!is_array($items)) {
57
        throw new \InvalidArgumentException("You can set only array of items");
58
      }
59
60
      foreach ($items as $key => $item) {
61
        $this->validateType($item);
62
      }
63
64
      return parent::setItems($items);
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function offsetSet($offset, $item) {
71
      $this->validateType($item);
72
      return parent::offsetSet($offset, $item);
73
    }
74
75
  }