Skip to content

Commit c512da4

Browse files
author
Wazabii
committed
Added fields value
1 parent dfaaca5 commit c512da4

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

Json.php

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@
88

99
class Json implements JsonInterface {
1010

11-
protected $data = array("status" => 0, "error" => 0);
11+
public $data = array("status" => 0, "error" => 0);
12+
public $fields = array();
1213

1314
function __construct() {
1415
}
1516

17+
18+
function __toString() {
19+
return $this->encode();
20+
}
21+
1622
/**
1723
* Merge array to json array
1824
* @param array $array
@@ -63,13 +69,65 @@ public function add(string $key, $value): self
6369
return $this;
6470
}
6571

72+
73+
/**
74+
* Merge string to json array
75+
* @param string $key Set array key
76+
* @param mixed $value Set array value
77+
* @return self
78+
*/
79+
public function item(...$args): array
80+
{
81+
$key = NULL;
82+
if(isset($args[0]) && !is_array($args[0])) {
83+
$key = array_shift($args);
84+
if(count($args) === 1) $args = $args[0];
85+
}
86+
$argumnets = (!is_null($key)) ? [[$key => $args]] : [...$args];
87+
$this->data = array_merge($this->data, $argumnets);
88+
return reset($argumnets);
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 field($key, $args): self
98+
{
99+
100+
101+
if(is_array($key)) {
102+
$key = key($key);
103+
$this->fields = array_merge($this->fields, [$key => [
104+
"type" => $key,
105+
...$args
106+
]]);
107+
} else {
108+
109+
$this->fields = array_merge($this->fields, [$key => $args]);
110+
}
111+
112+
113+
return $this;
114+
}
115+
116+
public function form($fields): self
117+
{
118+
$this->fields = array_merge($this->fields, $fields);
119+
return $this;
120+
}
121+
66122
/**
67123
* Reset
68124
* @return void
69125
*/
70-
public function reset(): void
126+
public function reset(?array $new = NULL): void
71127
{
72-
$this->data = array("status" => 0, "error" => 0);
128+
if(is_null($new)) $new = array("status" => 0, "error" => 0);
129+
130+
$this->data = $new;
73131
}
74132

75133
/**

0 commit comments

Comments
 (0)