PropertyBag.php 1.66 KB
Newer Older
Markus Poerschke's avatar
Markus Poerschke committed
1 2
<?php

Markus Poerschke's avatar
Markus Poerschke committed
3 4 5 6 7
/*
 * This file is part of the eluceo/iCal package.
 *
 * (c) Markus Poerschke <markus@eluceo.de>
 *
8 9
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
Markus Poerschke's avatar
Markus Poerschke committed
10 11
 */

Markus Poerschke's avatar
Markus Poerschke committed
12 13 14 15
namespace Eluceo\iCal;

class PropertyBag implements \IteratorAggregate
{
Markus Poerschke's avatar
Markus Poerschke committed
16 17 18
    /**
     * @var array
     */
19
    protected $elements = [];
efalcy's avatar
efalcy committed
20
    protected $xprops = [];
Markus Poerschke's avatar
Markus Poerschke committed
21

Markus Poerschke's avatar
Markus Poerschke committed
22
    /**
Markus Poerschke's avatar
Markus Poerschke committed
23
     * Creates a new Property with $name, $value and $params.
Markus Poerschke's avatar
Markus Poerschke committed
24
     *
25 26
     * @param       $name
     * @param       $value
Markus Poerschke's avatar
Markus Poerschke committed
27
     * @param array $params
28
     *
Markus Poerschke's avatar
Markus Poerschke committed
29
     * @return $this
Markus Poerschke's avatar
Markus Poerschke committed
30
     */
31
    public function set($name, $value, $params = [])
Markus Poerschke's avatar
Markus Poerschke committed
32
    {
Markus Poerschke's avatar
Markus Poerschke committed
33
        $this->add(new Property($name, $value, $params));
Markus Poerschke's avatar
Markus Poerschke committed
34

Rvanlaak's avatar
Rvanlaak committed
35
        return $this;
Markus Poerschke's avatar
Markus Poerschke committed
36 37
    }

Markus Poerschke's avatar
Markus Poerschke committed
38
    /**
Markus Poerschke's avatar
Markus Poerschke committed
39
     * @param string $name
40
     *
Markus Poerschke's avatar
Markus Poerschke committed
41 42
     * @return null|Property
     */
Patrick Barroca's avatar
Patrick Barroca committed
43
    public function get($name)
Markus Poerschke's avatar
Markus Poerschke committed
44
    {
Markus Poerschke's avatar
Markus Poerschke committed
45 46
        if (isset($this->elements[$name])) {
            return $this->elements[$name];
Markus Poerschke's avatar
Markus Poerschke committed
47
        }
Markus Poerschke's avatar
Markus Poerschke committed
48 49

        return null;
Markus Poerschke's avatar
Markus Poerschke committed
50 51
    }

Markus Poerschke's avatar
Markus Poerschke committed
52 53 54
    /**
     * Adds a Property. If Property already exists an Exception will be thrown.
     *
Markus Poerschke's avatar
Markus Poerschke committed
55
     * @param Property $property
56
     *
Markus Poerschke's avatar
Markus Poerschke committed
57
     * @return $this
Markus Poerschke's avatar
Markus Poerschke committed
58
     *
Markus Poerschke's avatar
Markus Poerschke committed
59 60 61
     * @throws \Exception
     */
    public function add(Property $property)
Markus Poerschke's avatar
Markus Poerschke committed
62
    {
Markus Poerschke's avatar
Markus Poerschke committed
63
        $name = $property->getName();
efalcy's avatar
efalcy committed
64 65 66 67
        if ('X-' == substr($name, 0, 2)) {
          $this->xprops[] = $property;
          return $this;
        }
Markus Poerschke's avatar
Markus Poerschke committed
68 69 70

        if (isset($this->elements[$name])) {
            throw new \Exception("Property with name '{$name}' already exists");
Markus Poerschke's avatar
Markus Poerschke committed
71
        }
Markus Poerschke's avatar
Markus Poerschke committed
72

Markus Poerschke's avatar
Markus Poerschke committed
73
        $this->elements[$name] = $property;
Markus Poerschke's avatar
Markus Poerschke committed
74

Rvanlaak's avatar
Rvanlaak committed
75
        return $this;
Markus Poerschke's avatar
Markus Poerschke committed
76 77 78 79
    }

    public function getIterator()
    {
efalcy's avatar
efalcy committed
80
        return new \ArrayObject($this->elements + $this->xprops);
Markus Poerschke's avatar
Markus Poerschke committed
81
    }
Markus Poerschke's avatar
Markus Poerschke committed
82
}