|
8 | 8 |
|
9 | 9 | class Json implements JsonInterface { |
10 | 10 |
|
11 | | - protected $data = array("status" => 0, "error" => 0); |
| 11 | + public $data = array("status" => 0, "error" => 0); |
| 12 | + public $fields = array(); |
12 | 13 |
|
13 | 14 | function __construct() { |
14 | 15 | } |
15 | 16 |
|
| 17 | + |
| 18 | + function __toString() { |
| 19 | + return $this->encode(); |
| 20 | + } |
| 21 | + |
16 | 22 | /** |
17 | 23 | * Merge array to json array |
18 | 24 | * @param array $array |
@@ -63,13 +69,65 @@ public function add(string $key, $value): self |
63 | 69 | return $this; |
64 | 70 | } |
65 | 71 |
|
| 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 | + |
66 | 122 | /** |
67 | 123 | * Reset |
68 | 124 | * @return void |
69 | 125 | */ |
70 | | - public function reset(): void |
| 126 | + public function reset(?array $new = NULL): void |
71 | 127 | { |
72 | | - $this->data = array("status" => 0, "error" => 0); |
| 128 | + if(is_null($new)) $new = array("status" => 0, "error" => 0); |
| 129 | + |
| 130 | + $this->data = $new; |
73 | 131 | } |
74 | 132 |
|
75 | 133 | /** |
|
0 commit comments