Passed
Push — 0.8.x ( fca16e...a09e23 )
by Alexander
07:28 queued 04:00
created

Parameters::all()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Lenevor Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file license.md.
10
 * It is also available through the world-wide-web at this URL:
11
 * https://lenevor.com/license
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @package     Lenevor
17
 * @subpackage  Base
18
 * @link        https://lenevor.com
19
 * @copyright   Copyright (c) 2019 - 2023 Alexander Campo <[email protected]>
20
 * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md
21
 */
22
23
namespace Syscodes\Components\Http\Loaders;
24
25
use Countable;
26
use Traversable;
27
use ArrayIterator;
28
use IteratorAggregate;
29
use Syscodes\Components\Support\Arr;
30
use Syscodes\Components\Http\Exceptions\BadRequestException;
31
32
/**
33
 * Parameters is a container for key/value pairs.
34
 */
35
class Parameters implements IteratorAggregate, Countable
36
{
37
	/**
38
	 * Array parameters from the Server global.
39
	 *
40
	 * @var array $parameters
41
	 */
42
	protected $parameters;
43
44
	/**
45
	 * Parameter Object Constructor.
46
	 *
47
	 * @param  array  $parameters
48
	 *
49
	 * @return array
50
	 */
51
	public function __construct(array $parameters = [])
52
	{
53
		$this->parameters = $parameters;
54
	}
55
56
	/**
57
	 * Returns the parameters.
58
	 * 
59
	 * @param  string|null  $key
60
	 * 
61
	 * @return array
62
	 */
63
	public function all(string $key = null): array
64
	{
65
		if (null === $key) {
66
			return $this->parameters;
67
		}
68
69
		if ( ! is_array($value = $this->parameters[$key] ?? [])) {
70
			throw new BadRequestException(
71
				sprintf('Unexpected value for parameter "%s", got "%s"', $key, get_debug_type($value))
72
			);
73
		}
74
75
		return $value;
76
	}
77
78
	/**
79
	 * Returns the parameter keys.
80
	 * 
81
	 * @return array
82
	 */
83
	public function keys(): array
84
	{
85
		return array_keys($this->parameters);
86
	}
87
88
	/**
89
	 * Replaces the current parameters.
90
	 * 
91
	 * @param  array  $parameters
92
	 * 
93
	 * @return void
94
	 */
95
	public function replace(array $parameters = []): void
96
	{
97
		$this->parameters = $parameters;
98
	}
99
100
	/**
101
	 * Adds parameters.
102
	 * 
103
	 * @param  array  $parameters
104
	 * 
105
	 * @return void
106
	 */
107
	public function add(array $parameters = []): void
108
	{
109
		$this->parameters = array_replace($this->parameters, $parameters);
110
	}
111
112
	/**
113
	 * Get a parameter array item.
114
	 *
115
	 * @param  string  $key
116
	 * @param  mixed  $default  
117
	 *
118
	 * @return mixed
119
	 */
120
	public function get(string $key, mixed $default = null): mixed
121
	{
122
		return $this->has($key) ? $this->parameters[$key] : $default;
123
	}
124
125
	/**
126
	 * Check if a parameter array item exists.
127
	 *
128
	 * @param  string  $key
129
	 *
130
	 * @return bool
131
	 */
132
	public function has(string $key): bool
133
	{
134
		return Arr::exists($this->parameters, $key);
135
	}
136
137
	/**
138
	 * Set a parameter array item.
139
	 *
140
	 * @param  string  $key
141
	 * @param  string  $value 
142
	 *
143
	 * @return void
144
	 */
145
	public function set(string $key, $value): void
146
	{
147
		$this->parameters[$key] = $value;
148
	}
149
150
	/**
151
	 * Remove a parameter array item.
152
	 *
153
	 * @param  string  $key 
154
	 *
155
	 * @return void
156
	 */
157
	public function remove(string $key): void
158
	{
159
		unset($this->parameters[$key]);
160
	}
161
162
	/*
163
	|-----------------------------------------------------------------
164
	| IteratorAggregate Method
165
	|-----------------------------------------------------------------
166
	*/
167
	
168
	/**
169
	 * Retrieve an external iterator.
170
	 * 
171
	 * @return \ArrayIterator
172
	 */
173
	public function getIterator(): Traversable
174
	{
175
		return new ArrayIterator($this->parameters);
176
	}
177
	
178
	/*
179
	|-----------------------------------------------------------------
180
	| Countable Method
181
	|-----------------------------------------------------------------
182
	*/
183
	
184
	/**
185
	 * Returns the number of parameters.
186
	 * 
187
	 * @return int The number of parameters
188
	 */
189
	public function count(): int
190
	{
191
		return count($this->parameters);
192
	}
193
}