Completed
Push — master ( ecee68...d1b502 )
by Ben
04:00
created

ArrayObject::insert()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 9
nc 6
nop 2
1
<?php
2
/**
3
 * BaconPdf
4
 *
5
 * @link      http://github.com/Bacon/BaconPdf For the canonical source repository
6
 * @copyright 2015 Ben 'DASPRiD' Scholzen
7
 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
8
 */
9
10
namespace Bacon\Pdf\Object;
11
12
use Bacon\Pdf\Exception\OutOfRangeException;
13
use Countable;
14
use IteratorAggregate;
15
use SplFileObject;
16
17
/**
18
 * Array object as defined by section 3.2.5
19
 */
20
class ArrayObject extends AbstractObject implements IteratorAggregate, Countable
21
{
22
    /**
23
     * @var ObjectInterface[]
24
     */
25
    private $items = [];
26
27
    /**
28
     * @param ObjectInterface[] $items
29
     */
30
    public function __construct(array $items = [])
31
    {
32
        foreach ($items as $item) {
33
            $this->append($item);
34
        }
35
    }
36
37
    /**
38
     * @param ObjectInterface $object
39
     */
40
    public function append(ObjectInterface $object)
41
    {
42
        $this->items[] = $object;
43
    }
44
45
    /**
46
     * @param int             $index
47
     * @param ObjectInterface $object
48
     */
49
    public function insert($index, ObjectInterface $object)
50
    {
51
        $size = count($this->items);
52
53
        if ($index < 0) {
54
            $index = max(0, $index + $size);
55
        } elseif ($index > $size) {
56
            $index = $size;
57
        }
58
59
        for ($i = $size; --$i >= $index; ) {
60
            $this->items[$i + 1] = $this->items[$i];
61
        }
62
63
        $this->items[$index] = $object;
64
    }
65
66
    /**
67
     * @param int $index
68
     */
69 View Code Duplication
    public function remove($index)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        if (!array_key_exists($index, $this->items)) {
72
            throw new OutOfRangeException(sprintf(
73
                'Could not find an item with index %d',
74
                $index
75
            ));
76
        }
77
78
        array_splice($this->items, $index, 1);
79
    }
80
81
    /**
82
     * @param  int $index
83
     * @return ObjectInterface
84
     */
85 View Code Duplication
    public function get($index)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87
        if (!array_key_exists($index, $this->items)) {
88
            throw new OutOfRangeException(sprintf(
89
                'Could not find an item with index %d',
90
                $index
91
            ));
92
        }
93
94
        return $this->items[$index];
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function writeToStream(SplFileObject $fileObject, $encryptionKey)
101
    {
102
        $fileObject->fwrite('[');
103
        $size = count($this->items);
104
105
        for ($index = 0; $index < $size; ++$index) {
106
            $this->items[$index]->writeToStream($fileObject, $encryptionKey);
107
            $fileObject->fwrite(' ');
108
        }
109
110
        $fileObject->fseek(-1, SEEK_CUR);
111
        $fileObject->fwrite(']');
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function getIterator()
118
    {
119
        $size = count($this->items);
120
121
        for ($index = 0; $index < $size; ++$index) {
122
            yield $index => $this->items[$index];
123
        }
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function count($mode = 'COUNT_NORMAL')
130
    {
131
        return count($this->items);
132
    }
133
}
134