Completed
Pull Request — master (#165)
by Paul
03:21
created

RoutingHelper::setParam()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
/**
3
 * This file is part of the PPI Framework.
4
 *
5
 * @copyright  Copyright (c) 2011-2016 Paul Dragoonis <[email protected]>
6
 * @license    http://opensource.org/licenses/mit-license.php MIT
7
 *
8
 * @link       http://www.ppi.io
9
 */
10
11
namespace PPI\Framework\Router;
12
13
/**
14
 * A routing helper for the controller.
15
 *
16
 * @author Paul Dragoonis <[email protected]>
17
 */
18
class RoutingHelper
19
{
20
    /**
21
     * The routing params.
22
     *
23
     * @var array
24
     */
25
    protected $params = array();
26
27
    /**
28
     * @var string
29
     */
30
    protected $activeRouteName;
31
32
    /**
33
     * Constructor.
34
     *
35
     * @param array $params
36
     */
37
    public function __construct(array $params = array())
38
    {
39
        if (!empty($params)) {
40
            $this->setParams($params);
41
        }
42
    }
43
44
    /**
45
     * Obtain a param's value.
46
     *
47
     * @param string $param The param name
48
     *
49
     * @throws \InvalidArgumentException When the param does not exist
50
     *
51
     * @return type
52
     */
53
    public function getParam($param)
54
    {
55
        if (!isset($this->params[$param])) {
56
            throw new \InvalidArgumentException('Unable to find routing param: ' . $param);
57
        }
58
59
        return $this->params[$param];
60
    }
61
62
    /**
63
     * Set a routing param's value.
64
     *
65
     * @param string $param
66
     * @param string $value
67
     *
68
     * @return $this
69
     */
70
    public function setParam($param, $value)
71
    {
72
        $this->params[$param] = $value;
73
74
        return $this;
75
    }
76
77
    /**
78
     * Check if a routing param exists.
79
     *
80
     * @param string $param
81
     *
82
     * @return bool
83
     */
84
    public function hasParam($param)
85
    {
86
        return array_key_exists($param, $this->params);
87
    }
88
89
    /**
90
     * Get all routing params.
91
     *
92
     * @return array
93
     */
94
    public function getParams()
95
    {
96
        return $this->params;
97
    }
98
99
    /**
100
     * Set the routing params.
101
     *
102
     * @param array $params
103
     *
104
     * @return $this
105
     */
106
    public function setParams(array $params)
107
    {
108
        $this->params = $params;
109
110
        return $this;
111
    }
112
113
    /**
114
     * Set the active route's name key.
115
     *
116
     * @param  $name
117
     *
118
     * @return $this
119
     */
120
    public function setActiveRouteName($name)
121
    {
122
        $this->activeRouteName = $name;
123
124
        return $this;
125
    }
126
127
    /**
128
     * Get the active route's name key.
129
     *
130
     * @return mixed
131
     */
132
    public function getActiveRouteName()
133
    {
134
        return $this->activeRouteName;
135
    }
136
}
137