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

ParameterBag::hasParams()   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
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
class ParameterBag
15
{
16
    /**
17
     * The params.
18
     *
19
     * @var array
20
     */
21
    protected $params;
22
23 79
    public function __construct($params = [])
24
    {
25 79
        $this->params = $params;
26 79
    }
27
28
    /**
29
     * @param string $name
30
     * @param mixed  $value
31
     */
32 19
    public function setParam($name, $value)
33
    {
34 19
        $this->params[$name] = $value;
35 19
    }
36
37
    /**
38
     * @param $name
39
     *
40
     * @return array|mixed
41
     */
42 2
    public function getParam($name)
43
    {
44 2
        if (isset($this->params[$name])) {
45 1
            return $this->params[$name];
46
        }
47
48 1
        return null;
49
    }
50
51
    /**
52
     * Checks if there are any params.
53
     *
54
     * @return bool
55
     */
56 32
    public function hasParams(): bool
57
    {
58 32
        return count($this->params) > 0;
59
    }
60
61
    /**
62
     * @return string
63
     */
64 21
    public function toString(): string
65
    {
66 21
        $line = '';
67 21
        foreach ($this->params as $param => $paramValues) {
68 21
            if (!is_array($paramValues)) {
69 21
                $paramValues = [$paramValues];
70
            }
71 21
            foreach ($paramValues as $k => $v) {
72 21
                $paramValues[$k] = $this->escapeParamValue($v);
73
            }
74
75 21
            if ('' != $line) {
76 11
                $line .= ';';
77
            }
78
79 21
            $line .= $param . '=' . implode(',', $paramValues);
80
        }
81
82 21
        return $line;
83
    }
84
85
    /**
86
     * Returns an escaped string for a param value.
87
     *
88
     * @param string $value
89
     *
90
     * @return string
91
     */
92 21
    private function escapeParamValue($value)
93
    {
94 21
        $count = 0;
95 21
        $value = str_replace('\\', '\\\\', $value);
96 21
        $value = str_replace('"', '\"', $value, $count);
97 21
        $value = str_replace("\n", '\\n', $value);
98 21
        if (false !== strpos($value, ';') || false !== strpos($value, ',') || false !== strpos($value, ':') || $count) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $count of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
99 6
            $value = '"' . $value . '"';
100
        }
101
102 21
        return $value;
103
    }
104
105
    /**
106
     * @return string
107
     */
108 1
    public function __toString()
109
    {
110 1
        return $this->toString();
111
    }
112
}
113