Skip to content

Commit 5578426

Browse files
committed
feat: add more string handle methods, class
1 parent e80dff7 commit 5578426

File tree

11 files changed

+792
-392
lines changed

11 files changed

+792
-392
lines changed

src/Obj/ConfigObject.php renamed to src/Obj/DataObject.php

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
namespace Toolkit\Stdlib\Obj;
1111

1212
use ArrayObject;
13+
use JsonSerializable;
14+
use Toolkit\Stdlib\Helper\JsonHelper;
15+
use UnexpectedValueException;
1316

1417
/**
1518
* Class ConfigObject
1619
*
1720
* @package Toolkit\Stdlib\Obj
1821
*/
19-
class ConfigObject extends ArrayObject
22+
class DataObject extends ArrayObject implements JsonSerializable
2023
{
2124
/**
2225
* @param array $data
@@ -28,6 +31,17 @@ public static function new(array $data = []): self
2831
return new static($data);
2932
}
3033

34+
/**
35+
* @param string $key
36+
* @param mixed $default
37+
*
38+
* @return mixed|null
39+
*/
40+
public function get(string $key, $default = null)
41+
{
42+
return $this[$key] ?? $default;
43+
}
44+
3145
/**
3246
* @param string $key
3347
* @param mixed $default
@@ -95,6 +109,21 @@ public function getString(string $key, string $default = ''): string
95109
return $default;
96110
}
97111

112+
/**
113+
* @param string $key
114+
*
115+
* @return string
116+
*/
117+
public function getNotBlock(string $key): string
118+
{
119+
$val = $this->get($key, '');
120+
if ($val === '') {
121+
throw new UnexpectedValueException("the $key value cannot be empty");
122+
}
123+
124+
return $val;
125+
}
126+
98127
/**
99128
* @param string $key
100129
* @param array $default
@@ -110,11 +139,53 @@ public function getArray(string $key, array $default = []): array
110139
return $default;
111140
}
112141

142+
/**
143+
* @param string $key
144+
*
145+
* @return self
146+
*/
147+
public function getSubObject(string $key): self
148+
{
149+
return new self($this->getArray($key));
150+
}
151+
113152
/**
114153
* @return array
115154
*/
116155
public function toArray(): array
117156
{
118157
return $this->getArrayCopy();
119158
}
159+
160+
/**
161+
* @return string
162+
*/
163+
public function toString(): string
164+
{
165+
return JsonHelper::enc($this->getArrayCopy(), JSON_THROW_ON_ERROR);
166+
}
167+
168+
/**
169+
* @return string
170+
*/
171+
public function __toString(): string
172+
{
173+
return $this->toString();
174+
}
175+
176+
/**
177+
* @return mixed
178+
*/
179+
public function jsonSerialize(): array
180+
{
181+
return $this->getArrayCopy();
182+
}
183+
184+
/**
185+
* @return bool
186+
*/
187+
public function isEmpty(): bool
188+
{
189+
return $this->count() === 0;
190+
}
120191
}

src/Util/BaseStream.php renamed to src/Std/BaseStream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22

3-
namespace Toolkit\Stdlib\Util;
3+
namespace Toolkit\Stdlib\Std;
44

55
/**
66
* class BaseStream

src/Std/StrStream.php

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\Stdlib\Std;
4+
5+
use Throwable;
6+
7+
/**
8+
* class StrStream - String value wrapper
9+
*/
10+
class StrStream
11+
{
12+
/**
13+
* @var string
14+
*/
15+
protected $str = '';
16+
17+
/**
18+
* @param string $str
19+
*
20+
* @return static
21+
*/
22+
public static function new(string $str): self
23+
{
24+
return new self($str);
25+
}
26+
27+
/**
28+
* Class constructor.
29+
*
30+
* @param string $str
31+
*/
32+
public function __construct(string $str)
33+
{
34+
$this->str = $str;
35+
}
36+
37+
/**
38+
* @param string $str
39+
*
40+
* @return $this
41+
*/
42+
public function set(string $str): self
43+
{
44+
$this->str = $str;
45+
return $this;
46+
}
47+
48+
/**
49+
* @param callable(string): string $fn
50+
*
51+
* @return $this
52+
*/
53+
public function apply(callable $fn): self
54+
{
55+
return self::new($fn($this->str));
56+
}
57+
58+
/**
59+
* @param callable(string): bool $fn
60+
*
61+
* @return bool
62+
*/
63+
public function is(callable $fn): bool
64+
{
65+
return (bool)$fn($this->str);
66+
}
67+
68+
/**
69+
* @param callable(string): string $fn
70+
*
71+
* @return $this
72+
*/
73+
public function applyIf(callable $fn, $ifExpr): self
74+
{
75+
if ($ifExpr) {
76+
return self::new($fn($this->str));
77+
}
78+
79+
return $this;
80+
}
81+
82+
/**
83+
* @param string $str
84+
*
85+
* @return string
86+
*/
87+
public function getOrNew(string $str): string
88+
{
89+
return $this->isEmpty() ? $str : $this->str;
90+
}
91+
92+
/**
93+
* @param Throwable $e
94+
*
95+
* @return string
96+
* @throws Throwable
97+
*/
98+
public function getOrThrow(Throwable $e): string
99+
{
100+
if ($this->isEmpty()) {
101+
throw $e;
102+
}
103+
104+
return $this->str;
105+
}
106+
107+
/**
108+
* @return bool
109+
*/
110+
public function isEmpty(): bool
111+
{
112+
return $this->str === '';
113+
}
114+
115+
/**
116+
* @return StrValue
117+
*/
118+
public function getValue(): StrValue
119+
{
120+
return StrValue::new($this->str);
121+
}
122+
123+
/**
124+
* @return StrValue
125+
*/
126+
public function toValue(): StrValue
127+
{
128+
return StrValue::new($this->str);
129+
}
130+
131+
/**
132+
* @return string
133+
*/
134+
public function toString(): string
135+
{
136+
return $this->str;
137+
}
138+
139+
/**
140+
* @return string
141+
*/
142+
public function __toString(): string
143+
{
144+
return $this->str;
145+
}
146+
}

0 commit comments

Comments
 (0)