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

Property::toLine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 6
Bugs 0 Features 3
Metric Value
c 6
b 0
f 3
dl 0
loc 15
ccs 7
cts 8
cp 0.875
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 2.0078
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 = [])
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 8
        if ($this->parameterBag->hasParams()) {
68 3
            $line .= ';' . $this->parameterBag->toString();
69 3
        }
70
71
        // Property value
72 8
        $line .= ':' . $this->value->getEscapedValue();
73
74 8
        return $line;
75
    }
76
77
    /**
78
     * Get all unfolded lines.
79
     *
80
     * @return array
81
     */
82 4
    public function toLines()
83
    {
84 4
        return [$this->toLine()];
85
    }
86
87
    /**
88
     * @param $value
89
     *
90
     * @return $this
91
     *
92
     * @throws \InvalidArgumentException
93
     */
94 9
    protected function setValue($value)
95
    {
96 9
        if (is_scalar($value)) {
97 9
            $this->value = new StringValue($value);
98 9
        } elseif (is_array($value)) {
99
            $this->value = new ArrayValue($value);
100
        } else {
101 2
            if (!$value instanceof ValueInterface) {
102
                throw new \InvalidArgumentException('The value must implement the ValueInterface.');
103
            }
104 2
            $this->value = $value;
105
        }
106 9
    }
107
108
    /**
109
     * @return mixed
110
     */
111
    public function getValue()
112
    {
113
        return $this->value;
114
    }
115
116
    /**
117
     * @return string
118
     */
119 9
    public function getName()
120
    {
121 9
        return $this->name;
122
    }
123
}
124