Completed
Pull Request — master (#74)
by Stefan
02:52
created

Property::toLines()   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

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
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
use Eluceo\iCal\Property\ArrayValue;
15
use Eluceo\iCal\Property\StringValue;
16
use Eluceo\iCal\Property\ValueInterface;
17
18
/**
19
 * The Property Class represents a property as defined in RFC 2445.
20
 *
21
 * The content of a line (unfolded) will be rendered in this class
22
 */
23
class Property
24
{
25
    /**
26
     * The value of the Property.
27
     *
28
     * @var ValueInterface
29
     */
30
    protected $value;
31
32
    /**
33
     * The params of the Property.
34
     *
35
     * @var ParameterBag
36
     */
37
    protected $parameterBag;
38
39
    /**
40
     * @var string
41
     */
42
    protected $name;
43
44
    /**
45
     * @param       $name
46
     * @param       $value
47
     * @param array $params
48
     */
49 9
    public function __construct($name, $value, $params = array())
50
    {
51 9
        $this->name = $name;
52 9
        $this->setValue($value);
53 9
        $this->parameterBag = new ParameterBag($params);
54 9
    }
55
56
    /**
57
     * Renders an unfolded line.
58
     *
59
     * @return string
60
     */
61 8
    public function toLine()
62
    {
63
        // Property-name
64 8
        $line = $this->getName();
65
66
        // Adding params
67
        //@todo added check for $this->parameterBag because doctrine/orm proxies won't execute constructor - ok?
68 8
        if ($this->parameterBag && $this->parameterBag->hasParams()) {
69 3
            $line .= ';' . $this->parameterBag->toString();
70 3
        }
71
72
        // Property value
73 8
        $line .= ':' . $this->value->getEscapedValue();
74
75 8
        return $line;
76
    }
77
78
    /**
79
     * Get all unfolded lines.
80
     *
81
     * @return array
82
     */
83 4
    public function toLines()
84
    {
85 4
        return array($this->toLine());
86
    }
87
88
    /**
89
     * @param string $name
90
     * @param mixed  $value
91
     *
92
     * @return $this
93
     */
94
    public function setParam($name, $value)
95
    {
96
        $this->parameterBag->setParam($name, $value);
97
98
        return $this;
99
    }
100
101
    /**
102
     * @param $name
103
     */
104
    public function getParam($name)
105
    {
106
        return $this->parameterBag->getParam($name);
107
    }
108
109
    /**
110
     * @param mixed $value
111
     *
112
     * @return $this
113
     *
114
     * @throws \Exception
115
     */
116 9
    public function setValue($value)
117
    {
118 9
        if (is_scalar($value)) {
119 9
            $this->value = new StringValue($value);
120 9
        } elseif (is_array($value)) {
121
            $this->value = new ArrayValue($value);
122
        } else {
123 2
            if (!$value instanceof ValueInterface) {
124
                throw new \Exception('The value must implement the ValueInterface.');
125
            } else {
126 2
                $this->value = $value;
127
            }
128
        }
129
130 9
        return $this;
131
    }
132
133
    /**
134
     * @return mixed
135
     */
136
    public function getValue()
137
    {
138
        return $this->value;
139
    }
140
141
    /**
142
     * @return string
143
     */
144 9
    public function getName()
145
    {
146 9
        return $this->name;
147
    }
148
}
149