Completed
Push — master ( 655053...db47c8 )
by Ben
02:17
created

ArrayObject::getIterator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4286
cc 2
eloc 3
nc 2
nop 0
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\OutOfBoundsException;
13
use IteratorAggregate;
14
use SplFileObject;
15
16
/**
17
 * Array object as defined by section 3.2.5
18
 */
19
class ArrayObject extends AbstractObject implements IteratorAggregate
20
{
21
    /**
22
     * @var ObjectInterface[]
23
     */
24
    private $items;
25
26
    /**
27
     * @param ObjectInterface[] $items
28
     */
29
    public function __construct(array $items = [])
30
    {
31
        foreach ($items as $item) {
32
            $this->append($item);
33
        }
34
    }
35
36
    /**
37
     * @param ObjectInterface $object
38
     */
39
    public function append(ObjectInterface $object)
40
    {
41
        $this->items[] = $object;
42
    }
43
44
    /**
45
     * @param  int $index
46
     * @return ObjectInterface
47
     */
48
    public function get($index)
49
    {
50
        if (!array_key_exists($index, $this->items)) {
51
            throw new OutOfBoundsException(sprintf(
52
                'Could not find an item with index %d',
53
                $index
54
            ));
55
        }
56
57
        return $this->items[$index];
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function writeToStream(SplFileObject $fileObject, $encryptionKey)
64
    {
65
        $fileObject->fwrite('[');
66
67
        foreach ($this->items as $value) {
68
            $fileObject->fwrite(' ');
69
            $value->writeToStream($fileObject);
0 ignored issues
show
Bug introduced by
The call to writeToStream() misses a required argument $encryptionKey.

This check looks for function calls that miss required arguments.

Loading history...
70
        }
71
72
        $fileObject->fwrite(']');
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getIterator()
79
    {
80
        foreach ($this->items as $index => $item) {
81
            yield $index => $item;
82
        }
83
    }
84
}
85