Skip to content

Commit 30ee5de

Browse files
author
Wazabii
committed
Improvements
1 parent 2b5d838 commit 30ee5de

File tree

5 files changed

+355
-3
lines changed

5 files changed

+355
-3
lines changed

Dom/Middleware/Meta.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use MaplePHP\Http\Interfaces\RequestInterface;
77
use MaplePHP\Handler\Interfaces\MiddlewareInterface;
88
use MaplePHP\Output\Dom\Document;
9-
use MaplePHP\Output\Json;
9+
use MaplePHP\Foundation\Http\Json;
1010
use MaplePHP\Foundation\Http\Provider;
1111

1212
class Meta implements MiddlewareInterface

Form/Validate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use MaplePHP\Container\Interfaces\ContainerInterface;
66
use MaplePHP\Http\Interfaces\ServerRequestInterface;
77
use MaplePHP\Foundation\Security\Csrf;
8-
use MaplePHP\Output\Json;
8+
use MaplePHP\Foundation\Http\Json;
99
use MaplePHP\Form\Validate as valid;
1010
use MaplePHP\Foundation\Form\Builder;
1111
use MaplePHP\Foundation\Form\Forms\AbstractForm;

Http/Interfaces/JsonInterface.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace MaplePHP\Foundation\Http\Interfaces;
4+
5+
interface JsonInterface
6+
{
7+
/**
8+
* Merge string to json array
9+
* @param string $key Set array key
10+
* @param mixed $value Set array value
11+
* @return self
12+
*/
13+
public function add(string $key, $value): self;
14+
15+
/**
16+
* Overwrite whole json array
17+
* @param array $array
18+
* @return self
19+
*/
20+
public function set($array): self;
21+
22+
/**
23+
* Merge array to json array
24+
* @param array $array
25+
* @return self
26+
*/
27+
public function merge(array $array): self;
28+
29+
/**
30+
* Merge array to json array
31+
* @param array $array
32+
* @return self
33+
*/
34+
public function mergeTo(string $key, array $array): self;
35+
36+
/**
37+
* same as @data method
38+
* @return mixed
39+
*/
40+
public function get(?string $key = null);
41+
42+
/**
43+
* Convert json array to json string
44+
* @param int $options Bitmask
45+
* @param int $depth Set the maximum depth. Must be greater than zero
46+
* @return string|null (bool if could not load json data)
47+
*/
48+
public function encode(int $options = JSON_UNESCAPED_UNICODE, int $depth = 512): string|null;
49+
50+
/**
51+
* Decode json data
52+
* @param string $json Json data
53+
* @param boolean $assoc When TRUE, returned objects will be converted into associative arrays.
54+
* @return object|array|false Resturns as array or false if error occoured.
55+
*/
56+
public function decode($json, $assoc = true): object|array|false;
57+
58+
/**
59+
* Validate output
60+
* @return void
61+
*/
62+
public function validate(): void;
63+
}

Http/Json.php

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MaplePHP\Foundation\Http;
6+
7+
use MaplePHP\Foundation\Http\Interfaces\JsonInterface;
8+
9+
class Json implements JsonInterface
10+
{
11+
public const ERROR_MESSAGES = [
12+
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
13+
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
14+
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
15+
JSON_ERROR_SYNTAX => 'Syntax error',
16+
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
17+
JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded',
18+
JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded',
19+
JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given',
20+
JSON_ERROR_INVALID_PROPERTY_NAME => 'A property name that cannot be encoded was given',
21+
JSON_ERROR_UTF16 => 'Malformed UTF-16 characters, possibly incorrectly encoded'
22+
];
23+
24+
25+
public $data = array("status" => 0, "error" => 0);
26+
public $fields = array();
27+
28+
public function __construct()
29+
{
30+
}
31+
32+
33+
public function __toString(): string
34+
{
35+
return (string)$this->encode();
36+
}
37+
38+
/**
39+
* New json instance
40+
* @param array|string $data
41+
* @return self
42+
*/
43+
public function withData(array|string $data): self
44+
{
45+
$inst = new self();
46+
if(is_array($data)) {
47+
$inst->data = array_merge($this->data, $data);
48+
} else {
49+
$inst->data = $this->decode($data);
50+
}
51+
return $inst;
52+
}
53+
54+
/**
55+
* Merge array to json array
56+
* @param array $array
57+
* @return self
58+
*/
59+
public function merge(array $array): self
60+
{
61+
$this->data = array_merge($this->data, $array);
62+
return $this;
63+
}
64+
65+
/**
66+
* Overwrite whole json array
67+
* @param array $array
68+
* @return self
69+
*/
70+
public function set($array): self
71+
{
72+
$this->data = $array;
73+
return $this;
74+
}
75+
76+
/**
77+
* Merge array to json array
78+
* @param array $array
79+
* @return self
80+
*/
81+
public function mergeTo(string $key, array $array): self
82+
{
83+
if (empty($this->data[$key])) {
84+
$this->data = array_merge($this->data, [$key => $array]);
85+
} else {
86+
$this->data[$key] = array_merge($this->data[$key], $array);
87+
}
88+
return $this;
89+
}
90+
91+
/**
92+
* Merge string to json array
93+
* @param string $key Set array key
94+
* @param mixed $value Set array value
95+
* @return self
96+
*/
97+
public function add(string $key, $value): self
98+
{
99+
$this->data = array_merge($this->data, [$key => $value]);
100+
return $this;
101+
}
102+
103+
104+
/**
105+
* Merge string to json array
106+
* @param string $key Set array key
107+
* @param mixed $value Set array value
108+
* @return array
109+
*/
110+
public function item(...$args): array
111+
{
112+
$key = null;
113+
if (isset($args[0]) && !is_array($args[0])) {
114+
$key = array_shift($args);
115+
if (count($args) === 1) {
116+
$args = $args[0];
117+
}
118+
}
119+
$argumnets = (!is_null($key)) ? [[$key => $args]] : $args;
120+
$this->data = array_merge($this->data, $argumnets);
121+
return reset($argumnets);
122+
}
123+
124+
/**
125+
* Merge string to json array
126+
* @param string|array $key Set array key
127+
* @param array $args Set array value
128+
* @return self
129+
*/
130+
public function field(string|array $key, array $args): self
131+
{
132+
if (is_array($key) && count($key) > 0) {
133+
$key = key($key);
134+
135+
if (!is_string($key)) {
136+
throw new \Exception("The key need to be string value", 1);
137+
}
138+
139+
$this->fields = array_merge($this->fields, [$key => [
140+
"type" => $key,
141+
...$args
142+
]]);
143+
} else {
144+
if (!is_string($key)) {
145+
throw new \Exception("The key need to be string value", 1);
146+
}
147+
$this->fields = array_merge($this->fields, [$key => $args]);
148+
}
149+
return $this;
150+
}
151+
152+
public function form($fields): self
153+
{
154+
$this->fields = array_merge($this->fields, $fields);
155+
return $this;
156+
}
157+
158+
/**
159+
* Reset
160+
* @return void
161+
*/
162+
public function reset(?array $new = null): void
163+
{
164+
if (is_null($new)) {
165+
$new = array("status" => 0, "error" => 0);
166+
}
167+
168+
$this->data = $new;
169+
}
170+
171+
/**
172+
* same as @data method
173+
* @return mixed
174+
*/
175+
public function get(?string $key = null)
176+
{
177+
return $this->data($key);
178+
}
179+
180+
/**
181+
* Get current added json array data
182+
* @return mixed
183+
*/
184+
public function data(?string $key = null)
185+
{
186+
if (!is_null($key)) {
187+
return $this->select($key);
188+
}
189+
return $this->data;
190+
}
191+
192+
/**
193+
* Get data has HTML friendly string
194+
* @param string $key select key (you can use comma sep. to traverse array)
195+
* @return string
196+
*/
197+
public function output(string $key): ?string
198+
{
199+
$arr = $this->select($key);
200+
if ($get = self::encodeData($arr)) {
201+
return htmlentities($get);
202+
}
203+
return null;
204+
}
205+
206+
/**
207+
* Convert json array to json string
208+
* @param int $options Bitmask
209+
* @param int $depth Set the maximum depth. Must be greater than zero
210+
* @return string|null (bool if could not load json data)
211+
*/
212+
public function encode(int $options = JSON_UNESCAPED_UNICODE, int $depth = 512): ?string
213+
{
214+
return self::encodeData($this->data, $options, $depth);
215+
}
216+
217+
/**
218+
* Decode json data
219+
* @param string $json Json data
220+
* @param boolean $assoc When TRUE, returned objects will be converted into associative arrays.
221+
* @return object|array|false Resturns as array or false if error occoured.
222+
*/
223+
public function decode($json, $assoc = true): object|array|false
224+
{
225+
if ($array = json_decode($json, $assoc)) {
226+
return $array;
227+
}
228+
return false;
229+
}
230+
231+
/**
232+
* Validate output
233+
* @return void
234+
*/
235+
public function validate(): void
236+
{
237+
$error = (static::ERROR_MESSAGES[self::error()] ?? null);
238+
if (!is_null($error)) {
239+
throw new \Exception($error, self::error());
240+
}
241+
//throw new \Exception('An unexpected Json error has occurred', self::error());
242+
}
243+
244+
/**
245+
* Travers slect data
246+
* @param string $key
247+
* @return mixed
248+
*/
249+
private function select(string $key)
250+
{
251+
$set = $this->data;
252+
$exp = explode(",", $key);
253+
foreach ($exp as $key) {
254+
if (isset($set[$key])) {
255+
$set = $set[$key];
256+
} else {
257+
return null;
258+
}
259+
}
260+
return $set;
261+
}
262+
263+
/**
264+
* Json encode data
265+
* @param array $json array to json
266+
* @param int $flag read php.net (or use the default)
267+
* @param int $depth read php.net
268+
* @return string|null
269+
*/
270+
final protected static function encodeData(array $json, int $flag = JSON_UNESCAPED_UNICODE, int $depth = 512): ?string
271+
{
272+
if (!($depth > 0 && $depth <= 2147483647)) {
273+
throw new \Exception("The json encode depth need to be min 1 and max 2147483647!", 1);
274+
}
275+
if (count($json) > 0 && ($encode = json_encode($json, $flag, $depth))) {
276+
return $encode;
277+
}
278+
return null;
279+
}
280+
281+
/**
282+
* Get last json error
283+
* @return int
284+
*/
285+
final protected static function error(): int
286+
{
287+
return json_last_error();
288+
}
289+
}

Http/Responder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use MaplePHP\Container\Interfaces\ContainerInterface;
66
use MaplePHP\Http\Interfaces\ResponseInterface;
7-
use MaplePHP\Output\Json;
7+
use MaplePHP\Foundation\Http\Json;
88
use MaplePHP\DTO\Traverse;
99

1010
class Responder

0 commit comments

Comments
 (0)