Completed
Push — 1.x ( ab197c )
by Markus
02:55
created

ParameterBag::hasParam()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
class ParameterBag
15
{
16
    /**
17
     * The params.
18
     *
19
     * @var array
20
     */
21
    protected $params;
22
23 11
    public function __construct($params = [])
24
    {
25 11
        $this->params = $params;
26 11
    }
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
    public function hasParam($name)
40
    {
41
        assert('is_string($name)', '$name parameter should be a string');
42
43
        return isset($this->params[$name]);
44
    }
45
46
    /**
47
     * @param $name
48
     *
49
     * @return mixed
50
     */
51
    public function getParam($name)
52
    {
53
        assert('is_string($name)', '$name parameter should be a string');
54
55
        if (!isset($this->params[$name])) {
56
            throw new \InvalidArgumentException("There is no parameter named {$name}");
57
        }
58
59
        return $this->params[$name];
60
    }
61
62
    /**
63
     * Checks if there are any params.
64
     *
65
     * @return bool
66
     */
67 8
    public function hasParams()
68
    {
69 8
        return count($this->params) > 0;
70
    }
71
72
    /**
73
     * @return string
74
     */
75 4
    public function toString()
76
    {
77 4
        $line = '';
78
79 4
        foreach ($this->params as $param => $paramValues) {
80 4
            if (!is_array($paramValues)) {
81 4
                $paramValues = [$paramValues];
82 4
            }
83 4
            foreach ($paramValues as $k => $v) {
84 4
                $paramValues[$k] = $this->escapeParamValue($v);
85 4
            }
86
87 4
            if ('' != $line) {
88 1
                $line .= ';';
89 1
            }
90
91 4
            $line .= $param . '=' . implode(',', $paramValues);
92 4
        }
93
94 4
        return $line;
95
    }
96
97
    /**
98
     * Returns an escaped string for a param value.
99
     *
100
     * @param string $value
101
     *
102
     * @return string
103
     */
104 5
    public function escapeParamValue($value)
105
    {
106 5
        $count = 0;
107 5
        $value = str_replace('\\', '\\\\', $value);
108 5
        $value = str_replace('"', '\"', $value, $count);
109 5
        $value = str_replace("\n", '\\n', $value);
110 5
        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...
111 2
            $value = '"' . $value . '"';
112 2
        }
113
114 5
        return $value;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function __toString()
121
    {
122
        return $this->toString();
123
    }
124
}
125