Skip to content

Commit 9bcea6b

Browse files
committed
add Components: Params
1 parent eb31ad9 commit 9bcea6b

File tree

2 files changed

+163
-1
lines changed

2 files changed

+163
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
}
1515
],
1616
"require": {
17-
"php": ">=7.1"
17+
"php": ">=7.1",
18+
"ext-json": "*"
1819
},
1920
"require-dev": {
2021
"phpunit/phpunit": "~7.0",

src/Components/Params.php

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
namespace Php\Support\Components;
4+
5+
use Php\Support\Helpers\Json;
6+
use Php\Support\Interfaces\Arrayable;
7+
use Php\Support\Interfaces\Jsonable;
8+
9+
class Params implements
10+
\ArrayAccess,
11+
\JsonSerializable,
12+
\Countable,
13+
Arrayable,
14+
Jsonable
15+
{
16+
/** @var array */
17+
private $_data = [];
18+
19+
/**
20+
* Params constructor.
21+
*
22+
* @param array|null $array
23+
*/
24+
public function __construct(array $array)
25+
{
26+
$this->fromArray($array);
27+
}
28+
29+
/**
30+
* @return array
31+
*/
32+
public function jsonSerialize(): array
33+
{
34+
return array_map(function ($value) {
35+
if ($value instanceof \JsonSerializable) {
36+
return $value->jsonSerialize();
37+
} elseif ($value instanceof Jsonable) {
38+
return json_decode($value->toJson(), true);
39+
} elseif ($value instanceof Arrayable) {
40+
return $value->toArray();
41+
}
42+
43+
return $value;
44+
}, $this->_data);
45+
}
46+
47+
/**
48+
* Get items as JSON
49+
*
50+
* @param int $options
51+
*
52+
* @return string
53+
*/
54+
public function toJson($options = 320): string
55+
{
56+
return Json::encode($this->jsonSerialize(), $options);
57+
}
58+
59+
/**
60+
* @param string $str
61+
*
62+
* @return array
63+
*/
64+
public function fromJson(string $str): array
65+
{
66+
return Json::decode($str);
67+
}
68+
69+
/**
70+
* Convert items to its string representation.
71+
*
72+
* @return string
73+
*/
74+
public function __toString()
75+
{
76+
return $this->toJson();
77+
}
78+
79+
80+
/**
81+
* @param array $keys
82+
*
83+
* @return array
84+
*/
85+
public function toArray(array $keys = []): array
86+
{
87+
$result = [];
88+
foreach ($keys as $key) {
89+
if (isset($this->_data[ $key ])) {
90+
$result[ $key ] = $this->_data[ $key ];
91+
}
92+
}
93+
94+
return Json::dataToArray($result);
95+
}
96+
97+
/**
98+
* @param array $array
99+
*/
100+
public function fromArray(array $array): void
101+
{
102+
$this->_data = $array;
103+
}
104+
105+
/**
106+
* @return int
107+
*/
108+
public function count(): int
109+
{
110+
return count($this->_data);
111+
}
112+
113+
/**
114+
* Checks if the given key or index exists in the array
115+
*
116+
* @param mixed $offset
117+
*
118+
* @return bool
119+
*/
120+
public function offsetExists($offset): bool
121+
{
122+
return array_key_exists($offset, $this->_data);
123+
}
124+
125+
/**
126+
* Offset to retrieve
127+
*
128+
* @param mixed $offset
129+
*
130+
* @return mixed
131+
*/
132+
public function offsetGet($offset)
133+
{
134+
return $this->_data[ $offset ];
135+
}
136+
137+
/**
138+
* Offset to set
139+
*
140+
* @param mixed $offset The offset to assign the value to.
141+
* @param mixed $value The value to set.
142+
*/
143+
public function offsetSet($offset, $value)
144+
{
145+
if (is_null($offset)) {
146+
$this->_data[] = $value;
147+
} else {
148+
$this->_data[ $offset ] = $value;
149+
}
150+
}
151+
152+
/**
153+
* Offset to unset
154+
*
155+
* @param mixed $offset The offset to unset.
156+
*/
157+
public function offsetUnset($offset)
158+
{
159+
unset($this->_data[ $offset ]);
160+
}
161+
}

0 commit comments

Comments
 (0)