UrlBuilder::getAllParameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
4
  namespace ReRoute;
5
6
  use ReRoute\Route\AbstractRoute;
7
8
9
  /**
10
   * @package ReRoute
11
   */
12
  class UrlBuilder {
13
14
15
    /**
16
     * @var AbstractRoute
17
     */
18
    private $route;
19
20
21
    /**
22
     * @var array
23
     */
24
    protected $params = [];
25
26
27
    /**
28
     * @var array
29
     */
30
    protected $usedParams = [];
31
32
33
    /**
34
     * @param AbstractRoute $route
35
     */
36 36
    public function __construct(AbstractRoute $route) {
37 36
      $this->route = $route;
38 36
      $this->storeRouteParameters($route);
39 36
    }
40
41
42
    /**
43
     * @param AbstractRoute $route
44
     */
45 36
    protected function storeRouteParameters(AbstractRoute $route) {
46 36
      foreach ($route->getDefaultParameters() as $key => $value) {
47 3
        $this->setParameter($key, $value);
48 36
      }
49 36
      foreach ($route->getModifiers() as $modifier) {
50 18
        foreach ($modifier->getDefaultParameters() as $key => $value) {
51 6
          $this->setParameter($key, $value);
52 18
        }
53 36
      }
54 36
      if ($urlTemplate = $route->getUrlTemplate()) {
55 24
        foreach ($urlTemplate->getDefaultParameters() as $key => $value) {
56
          $this->setParameter($key, $value);
57 24
        }
58 24
      }
59 36
      if ($parentRoute = $route->getParentRoute()) {
60 21
        $this->storeRouteParameters($parentRoute);
61 21
      }
62 36
    }
63
64
65
    /**
66
     * @return AbstractRoute
67
     */
68 30
    public function getRoute() {
69 30
      return $this->route;
70
    }
71
72
73
    /**
74
     * @param string $param
75
     *
76
     * @return bool
77
     */
78 3
    public function hasParameter($param) {
79 3
      return isset($this->params[$param]);
80
    }
81
82
83
    /**
84
     * @param string $param
85
     *
86
     * @return string
87
     */
88 21
    public function getParameter($param) {
89 21
      if (!empty($this->params[$param])) {
90 21
        return $this->params[$param];
91
      }
92 15
      return null;
93
    }
94
95
96
    /**
97
     * @return array
98
     */
99 27
    public function getAllParameters() {
100 27
      return $this->params;
101
    }
102
103
104
    /**
105
     * @param string $param
106
     *
107
     * @return string
108
     */
109 21
    public function useParameter($param) {
110 21
      $this->usedParams[$param] = true;
111 21
      return $this->getParameter($param);
112
    }
113
114
115
    /**
116
     * @return string[]
117
     */
118 24
    private function getUnusedParameters() {
119 24
      return array_diff_key($this->params, $this->usedParams);
120
    }
121
122
123
    /**
124
     * @param string $param
125
     * @param string $value
126
     *
127
     * @return UrlBuilder
128
     */
129 24
    public function setParameter($param, $value) {
130 24
      $this->params[$param] = $value;
131 24
      return $this;
132
    }
133
134
135
    /**
136
     * @param array $parameters
137
     * @return $this
138
     */
139
    public function setParameterList(array $parameters) {
140
      foreach ($parameters as $param => $value) {
141
        $this->setParameter($param, $value);
142
      }
143
      return $this;
144
    }
145
146
147
    /**
148
     * @param string $param
149
     */
150
    public function removeParameter($param) {
151
      unset($this->params[$param]);
152
      unset($this->usedParams[$param]);
153
    }
154
155
156
    /**
157
     * @return string
158
     */
159 27
    public function assemble() {
160 27
      $url = new Url();
161 27
      $this->getRoute()->build($url, $this);
162 24
      foreach ($this->getUnusedParameters() as $param => $value) {
163 6
        $url->setParameter($param, $value);
164 24
      }
165 24
      return $url->getUrl();
166
    }
167
168
169
    /**
170
     * @return string
171
     */
172 3
    public function __toString() {
173 3
      return $this->assemble();
174
    }
175
176
  }