-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollection.php
More file actions
155 lines (137 loc) · 3.13 KB
/
Collection.php
File metadata and controls
155 lines (137 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/*
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2021 David Young
* @license https://github.com/opulencephp/Opulence/blob/1.2/LICENSE.md
*/
namespace Opulence\Http;
use ArrayAccess;
use Countable;
/**
* Defines the base class for HTTP connection parameter collections
*/
class Collection implements ArrayAccess, Countable
{
/** @var array The list of values */
protected $values = [];
/**
* @param array $values The list of values
*/
public function __construct(array $values)
{
foreach ($values as $key => $value) {
$this->add($key, $value);
}
}
/**
* Adds a value
*
* @param string $name The key to add
* @param mixed $value The value to add
*/
public function add(string $name, $value)
{
$this->values[$name] = $value;
}
/**
* Gets the number of values
*
* @return int The number of values
*/
public function count() : int
{
return count($this->values);
}
/**
* Exchanges the current values with the input
*
* @param mixed $array The values to exchange with
* @return array The old array
*/
public function exchangeArray($array) : array
{
$oldValues = $this->values;
$this->values = $array;
return $oldValues;
}
/**
* Gets the input value
*
* @param string $name The name of the value to get
* @param mixed $default The default value
* @return mixed The value of the value if it was found, otherwise the default value
*/
public function get(string $name, $default = null)
{
return $this->has($name) ? $this->values[$name] : $default;
}
/**
* Gets all of the values
*
* @return array All of the values
*/
public function getAll() : array
{
return $this->values;
}
/**
* Gets whether or not the key exists
*
* @param string $name The key to check for
* @return bool True if the key exists, otherwise false
*/
public function has(string $name) : bool
{
return isset($this->values[$name]);
}
/**
* @inheritdoc
*/
public function offsetExists($offset) : bool
{
return $this->has($offset);
}
/**
* @inheritdoc
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->get($offset, null);
}
/**
* @inheritdoc
*/
public function offsetSet($offset, $value) : void
{
$this->set($offset, $value);
}
/**
* @inheritdoc
*/
public function offsetUnset($offset) : void
{
$this->remove($offset);
}
/**
* Removes a key
*
* @param string $name The name of the key to remove
*/
public function remove(string $name)
{
unset($this->values[$name]);
}
/**
* Sets a value
*
* @param string $name The key to set
* @param mixed $value The value to set
*/
public function set(string $name, $value)
{
$this->values[$name] = $value;
}
}