Completed
Push — master ( 5db245...cdc2ed )
by Markus
17s queued 14s
created

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