Completed
Push — master ( a05270...10fb5b )
by Ben
02:15
created

DictionaryObject::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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\OutOfBoundsException;
13
use IteratorAggregate;
14
use SplFileObject;
15
16
/**
17
 * Dictionary object as defined by section 3.2.6
18
 */
19
class DictionaryObject extends AbstractObject implements IteratorAggregate
20
{
21
    /**
22
     * @var ObjectInterface[]
23
     */
24
    private $objects;
25
26
    /**
27
     * @param ObjectInterface[] $objects
28
     */
29
    public function __construct(array $objects)
30
    {
31
        foreach ($objects as $key => $object) {
32
            $this->set($key, $object);
33
        }
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function writeToStream(SplFileObject $fileObject, $encryptionKey)
40
    {
41
        $fileObject->fwrite("<<\n");
42
43
        foreach ($this->items as $key => $value) {
0 ignored issues
show
Bug introduced by
The property items does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
            (new NameObject($key))->writeToStream($fileObject, $encryptionKey);
45
            $fileObject->fwrite(' ');
46
            $value->writeToStream($fileObject, $encryptionKey);
47
        }
48
49
        $fileObject->fwrite('>>');
50
    }
51
52
    /**
53
     * Checks whether an object with a given key eixsts.
54
     *
55
     * @param  string $key
56
     * @return bool
57
     */
58
    public function has($key)
59
    {
60
        return array_key_exists($key, $this->objects);
61
    }
62
63
    /**
64
     * Returns an object with the given key.
65
     *
66
     * @param  string $key
67
     * @return ObjectInterface
68
     * @throws OutOfBoundsException
69
     */
70
    public function get($key)
71
    {
72
        if (!array_key_exists($key, $this->objects)) {
73
            throw new OutOfBoundsException(sprintf(
74
                'Could not find an object with key %s',
75
                $key
76
            ));
77
        }
78
79
        return $this->objects[$key];
80
    }
81
82
    /**
83
     * Sets an object with the given key.
84
     *
85
     * @param string          $key
86
     * @param ObjectInterface $object
87
     */
88
    public function set($key, ObjectInterface $object)
89
    {
90
        $this->objects[$key] = $object;
91
    }
92
93
    /**
94
     * Removes an object with the given key.
95
     *
96
     * @param string $key
97
     */
98
    public function remove($key)
99
    {
100
        unset($this->objects[$key]);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function getIterator()
107
    {
108
        foreach ($this->items as $index => $item) {
109
            yield $index => $item;
110
        }
111
    }
112
}
113