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

ParameterBag::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
nc 1
cc 1
eloc 2
nop 0
crap 2
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\Util\PropertyParameterValueUtil;
15
16
class ParameterBag
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $params;
22
23 10
    public function __construct(array $params = [])
24
    {
25 10
        $this->params = $params;
26 10
    }
27
28
    /**
29
     * @param string $name
30
     * @param mixed  $value
31
     */
32 1
    public function setParam($name, $value)
33
    {
34 1
        assert(is_string($name), '$name parameter should be a string');
35
36 1
        $this->params[$name] = $value;
37 1
    }
38
39
    /**
40
     * Checks if there are any params.
41
     *
42
     * @return bool
43
     */
44 8
    public function hasParams()
45
    {
46 8
        return count($this->params) > 0;
47
    }
48
49
    /**
50
     * @return string
51
     */
52 4
    public function toString()
53
    {
54 4
        $line = '';
55
56 4
        foreach ($this->params as $param => $paramValues) {
57 4
            if (!is_array($paramValues)) {
58 4
                $paramValues = [$paramValues];
59 4
            }
60
61 4
            foreach ($paramValues as $k => $v) {
62 4
                $paramValues[$k] = PropertyParameterValueUtil::escapeParamValue($v);
63 4
            }
64
65 4
            if ('' != $line) {
66 1
                $line .= ';';
67 1
            }
68
69 4
            $line .= $param . '=' . implode(',', $paramValues);
70 4
        }
71
72 4
        return $line;
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function __toString()
79
    {
80
        return $this->toString();
81
    }
82
}
83