Completed
Push — 1.x ( 8c7a9a...c7de71 )
by Markus
04:41 queued 02:47
created

PropertyBag::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
nc 1
cc 1
eloc 2
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the eluceo/iCal package.
5
 *
6
 * (c) Markus Poerschke <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Eluceo\iCal;
13
14
class PropertyBag implements \IteratorAggregate
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $elements = [];
20
21
    /**
22
     * Creates a new Property with $name, $value and $params.
23
     *
24
     * @deprecated The property bag should not be used as a property factory. Use `$this->add` instead.
25
     *
26
     * @param       $name
27
     * @param       $value
28
     * @param array $params
29
     *
30
     * @return $this
31
     */
32 4
    public function set($name, $value, $params = [])
33
    {
34 4
        $this->add(new Property($name, $value, $params));
35 4
    }
36
37
    /**
38
     * Adds a Property. If Property already exists an Exception will be thrown.
39
     *
40
     * @param Property $property
41
     *
42
     * @return $this
43
     *
44
     * @throws \Exception
45
     */
46 5
    public function add(Property $property)
47
    {
48 5
        $name = $property->getName();
49
50 5
        if (isset($this->elements[$name])) {
51 1
            throw new \Exception("Property with name '{$name}' already exists");
52
        }
53
54 5
        $this->elements[$name] = $property;
55 5
    }
56
57
    /**
58
     * @return \ArrayObject
59
     */
60 4
    public function getIterator()
61
    {
62 4
        return new \ArrayObject($this->elements);
63
    }
64
}
65