Skip to content

Commit b459f92

Browse files
committed
add some new func and class
1 parent 8c4b16a commit b459f92

File tree

3 files changed

+214
-9
lines changed

3 files changed

+214
-9
lines changed

src/Helper/JsonHelper.php

+80-8
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,16 @@
3636
*/
3737
class JsonHelper
3838
{
39+
// ----------- encode -----------
40+
3941
/**
40-
* @param mixed $data
41-
* @param int $flags
42+
* @param $data
4243
*
43-
* @return false|string
44+
* @return string
4445
*/
45-
public static function prettyJSON(
46-
$data,
47-
int $flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
48-
) {
49-
return json_encode($data, $flags);
46+
public static function enc($data): string
47+
{
48+
return json_encode($data);
5049
}
5150

5251
/**
@@ -80,6 +79,79 @@ public static function encodeCN(
8079
return json_encode($data, $options, $depth);
8180
}
8281

82+
/**
83+
* @param $data
84+
*
85+
* @return string
86+
*/
87+
public static function pretty($data): string
88+
{
89+
return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
90+
}
91+
92+
/**
93+
* @param mixed $data
94+
* @param int $flags
95+
*
96+
* @return false|string
97+
*/
98+
public static function prettyJSON(
99+
$data,
100+
int $flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
101+
) {
102+
return json_encode($data, $flags);
103+
}
104+
105+
/**
106+
* @param $data
107+
*
108+
* @return string
109+
*/
110+
public static function unescaped($data): string
111+
{
112+
return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
113+
}
114+
115+
/**
116+
* @param $data
117+
*
118+
* @return string
119+
*/
120+
public static function unescapedSlashes($data): string
121+
{
122+
return json_encode($data, JSON_UNESCAPED_SLASHES);
123+
}
124+
125+
/**
126+
* @param $data
127+
*
128+
* @return string
129+
*/
130+
public static function unescapedUnicode($data): string
131+
{
132+
return json_encode($data, JSON_UNESCAPED_UNICODE);
133+
}
134+
135+
// ----------- decode -----------
136+
137+
/**
138+
* @param string $json
139+
*
140+
* @param bool $assoc
141+
*
142+
* @return array|mixed
143+
*/
144+
public static function dec(string $json, bool $assoc = true)
145+
{
146+
$data = json_decode($json, $assoc);
147+
148+
if (JSON_ERROR_NONE !== json_last_error()) {
149+
throw new InvalidArgumentException('json_decode error: ' . json_last_error_msg());
150+
}
151+
152+
return $data;
153+
}
154+
83155
/**
84156
* Decode json
85157
*

src/Helper/PhpHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public static function callByArray(callable $cb, array $args)
124124
}
125125

126126
/**
127-
* Set property values ​​for object
127+
* Set property values for object
128128
* - 会先尝试用 setter 方法设置属性
129129
* - 再尝试直接设置属性
130130
*

src/Obj/AbstractMap.php

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/stdlib.
4+
*
5+
* @author https://github.com/inhere
6+
* @link https://github.com/php-toolkit/stdlib
7+
* @license MIT
8+
*/
9+
10+
namespace Toolkit\Stdlib\Obj;
11+
12+
use JsonSerializable;
13+
use Toolkit\Stdlib\Helper\JsonHelper;
14+
15+
/**
16+
* Class AbstractMap
17+
*
18+
* @package Toolkit\Stdlib\Obj
19+
*/
20+
abstract class AbstractMap implements JsonSerializable
21+
{
22+
/**
23+
* @param array $data
24+
*
25+
* @return static
26+
*/
27+
public static function new(array $data = [])
28+
{
29+
return new static($data);
30+
}
31+
32+
/**
33+
* Configurable constructor.
34+
*
35+
* @param array $config
36+
*/
37+
public function __construct(array $config = [])
38+
{
39+
if ($config) {
40+
ObjectHelper::init($this, $config);
41+
}
42+
}
43+
44+
/**
45+
* Batch set values
46+
*
47+
* @param array $data
48+
*/
49+
public function load(array $data): void
50+
{
51+
ObjectHelper::init($this, $data);
52+
}
53+
54+
/**
55+
* @param string $key
56+
* @param mixed $value
57+
*/
58+
public function setValue(string $key, $value): void
59+
{
60+
ObjectHelper::init($this, [$key => $value]);
61+
}
62+
63+
/**
64+
* @param array $fields Restrict to return only these fields, return all of them if empty
65+
*
66+
* @return array
67+
*/
68+
public function getValues(array $fields = []): array
69+
{
70+
$data = [];
71+
foreach ($this as $key => $value) {
72+
if ($fields && !in_array($key, $fields, true)) {
73+
continue;
74+
}
75+
76+
$data[$key] = $value;
77+
}
78+
79+
return $data;
80+
}
81+
82+
/**
83+
* Convert to array
84+
*
85+
* @param bool $filterEmpty
86+
* @param array $append
87+
*
88+
* @return array
89+
*/
90+
public function toArray(bool $filterEmpty = false, array $append = []): array
91+
{
92+
$data = $this->getValues();
93+
94+
if ($filterEmpty) {
95+
$data = array_filter($data);
96+
}
97+
98+
// 仅在data不为空时追加数据
99+
if ($append && $data) {
100+
$data = array_merge($data, $append);
101+
}
102+
103+
return $data;
104+
}
105+
106+
/**
107+
* @return string
108+
*/
109+
public function toString(): string
110+
{
111+
return $this->toJson();
112+
}
113+
114+
/**
115+
* Convert to JSON string
116+
*
117+
* @param bool $filterEmpty
118+
*
119+
* @return string
120+
*/
121+
public function toJson(bool $filterEmpty = false): string
122+
{
123+
return JsonHelper::enc($this->toArray($filterEmpty));
124+
}
125+
126+
/**
127+
* @return mixed
128+
*/
129+
public function jsonSerialize()
130+
{
131+
return $this->toArray();
132+
}
133+
}

0 commit comments

Comments
 (0)