Skip to content

Commit 4c8bc02

Browse files
committed
feat: add some new string utl func and update base object
1 parent f25aa92 commit 4c8bc02

File tree

8 files changed

+131
-21
lines changed

8 files changed

+131
-21
lines changed

src/Obj/AbstractDTO.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\Stdlib\Obj;
4+
5+
/**
6+
* Class AbstractDTO
7+
*/
8+
abstract class AbstractDTO extends BaseObject
9+
{
10+
11+
}

src/Obj/BaseObject.php

+17-16
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ abstract class BaseObject implements JsonSerializable
2424
use QuickInitTrait;
2525

2626
/**
27-
* Field alias mapping. [alias => field]
27+
* JSON field mapping. Format: [json-field => field]
2828
*
2929
* @var array
3030
*/
31-
protected array $_aliasMap = [];
31+
protected array $_jsonMap = [];
3232

3333
/**
3434
* Class constructor.
@@ -58,12 +58,12 @@ protected function init(): void
5858
* - Automatically convert fields to camelCase format
5959
*
6060
* @param array $data
61-
* @param array $aliasMap
61+
* @param array $jsonMap json field mapping
6262
*/
63-
public function load(array $data, array $aliasMap = []): void
63+
public function load(array $data, array $jsonMap = []): void
6464
{
6565
if ($data) {
66-
Obj::init($this, $data, true, $aliasMap ?: $this->_aliasMap);
66+
Obj::init($this, $data, true, $jsonMap ?: $this->_jsonMap);
6767
}
6868
}
6969

@@ -76,7 +76,7 @@ public function load(array $data, array $aliasMap = []): void
7676
*/
7777
public function setValue(string $field, mixed $value): static
7878
{
79-
Obj::init($this, [$field => $value], true, $this->_aliasMap);
79+
Obj::init($this, [$field => $value], true, $this->_jsonMap);
8080
return $this;
8181
}
8282

@@ -85,19 +85,20 @@ public function setValue(string $field, mixed $value): static
8585
*/
8686
public function toArray(): array
8787
{
88-
return Obj::toArray($this, false, false);
88+
return $this->convToMap(true, true);
8989
}
9090

9191
/**
9292
* Convert to array map.
9393
*
94-
* @param bool $filter
94+
* @param bool $filter exclude empty value
95+
* @param bool $useJsonMap use json field map
9596
*
9697
* @return array
9798
*/
98-
public function toMap(bool $filter = false): array
99+
public function toMap(bool $filter = false, bool $useJsonMap = false): array
99100
{
100-
return $this->convToMap($filter);
101+
return $this->convToMap($filter, $useJsonMap);
101102
}
102103

103104
/**
@@ -107,7 +108,7 @@ public function toMap(bool $filter = false): array
107108
*/
108109
public function toCleaned(): array
109110
{
110-
return $this->convToMap(true);
111+
return $this->convToMap(true, true);
111112
}
112113

113114
/**
@@ -135,8 +136,8 @@ protected function convToMap(bool $filter = false, bool $aliased = false): array
135136
{
136137
$data = [];
137138
$full = get_object_vars($this);
138-
if ($aliased && $this->_aliasMap) {
139-
$this->_name2alias = array_flip($this->_aliasMap);
139+
if ($aliased && $this->_jsonMap) {
140+
$this->_name2alias = array_flip($this->_jsonMap);
140141
}
141142

142143
// filter empty value
@@ -211,11 +212,11 @@ protected function convToMap(bool $filter = false, bool $aliased = false): array
211212
* 转成 JSON 字符串
212213
*
213214
* @param bool $filter filter empty value
214-
* @param bool $aliased
215+
* @param bool $aliased use json field map
215216
*
216217
* @return string
217218
*/
218-
public function toJson(bool $filter = true, bool $aliased = false): string
219+
public function toJson(bool $filter = true, bool $aliased = true): string
219220
{
220221
return Json::unescaped($this->convToMap($filter, $aliased));
221222
}
@@ -233,7 +234,7 @@ public function __toString(): string
233234
*/
234235
public function jsonSerialize(): array
235236
{
236-
return $this->toMap(true);
237+
return $this->convToMap(true, true);
237238
}
238239

239240
}

src/Str.php

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
*/
1919
final class Str extends StringHelper
2020
{
21+
2122
}

src/Str/Traits/StringTruncateHelperTrait.php

+69-1
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,41 @@ trait StringTruncateHelperTrait
4141
/// Truncate
4242
////////////////////////////////////////////////////////////////////////
4343

44+
/**
45+
* @param string $s
46+
* @param array $prefixes
47+
*
48+
* @return string
49+
*/
50+
public static function removePrefixes(string $s, array $prefixes): string
51+
{
52+
foreach ($prefixes as $prefix) {
53+
if (str_starts_with($s, $prefix)) {
54+
return substr($s, strlen($prefix));
55+
}
56+
}
57+
return $s;
58+
}
59+
60+
/**
61+
* @param string $s
62+
* @param array $suffixes
63+
*
64+
* @return string
65+
*/
66+
public static function removeSuffixes(string $s, array $suffixes): string
67+
{
68+
foreach ($suffixes as $suffix) {
69+
if (str_ends_with($s, $suffix)) {
70+
return substr($s, 0, -strlen($suffix));
71+
}
72+
}
73+
return $s;
74+
}
75+
4476
/**
4577
* @param string $s
46-
* @param string $start
78+
* @param string $start prefix string
4779
* @param int|null $length
4880
*
4981
* @return string
@@ -53,6 +85,42 @@ public static function afterStart(string $s, string $start, ?int $length = null)
5385
return substr($s, strlen($start), $length);
5486
}
5587

88+
/**
89+
* get substring before $sub.
90+
* eg: s='hello/world', sub='/' => 'hello'
91+
*
92+
* @param string $s
93+
* @param string $sub
94+
* @param string $fallback
95+
*
96+
* @return string
97+
*/
98+
public static function before(string $s, string $sub, string $fallback = ''): string
99+
{
100+
if ($i = strpos($s, $sub)) {
101+
return substr($s, 0, $i);
102+
}
103+
return $fallback;
104+
}
105+
106+
/**
107+
* get substring after $sub.
108+
* eg: s='hello/world', sub='/' => 'world'
109+
*
110+
* @param string $s
111+
* @param string $sub
112+
* @param string $fallback
113+
*
114+
* @return string
115+
*/
116+
public static function after(string $s, string $sub, string $fallback = ''): string
117+
{
118+
if (($i = strpos($s, $sub)) !== false) {
119+
return substr($s, $i + strlen($sub));
120+
}
121+
return $fallback;
122+
}
123+
56124
/**
57125
* @param string $str
58126
* @param int $start

src/Type.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ public static function getDefault(string $type): float|bool|int|array|string|nul
9696
return match ($type) {
9797
self::INT, self::INTEGER => 0,
9898
self::BOOL, self::BOOLEAN => false,
99-
self::FLOAT => (float)0,
100-
self::DOUBLE => (double)0,
99+
self::FLOAT, self::DOUBLE => 0.0,
101100
self::STRING => '',
102101
self::ARRAY => [],
103102
default => null,
@@ -188,4 +187,24 @@ public static function complexes(): array
188187
self::RESOURCE,
189188
];
190189
}
190+
191+
/**
192+
* @param string $type
193+
*
194+
* @return bool
195+
*/
196+
public static function isInt(string $type): bool
197+
{
198+
return in_array($type, [self::INT, self::INTEGER], true);
199+
}
200+
201+
/**
202+
* @param string $type
203+
*
204+
* @return bool
205+
*/
206+
public static function isComplex(string $type): bool
207+
{
208+
return in_array($type, self::complexes(), true);
209+
}
191210
}

test/Obj/ADemoPo2.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
class ADemoPo2 extends AbstractObj
1111
{
12-
protected array $_aliasMap = [
12+
protected array $_jsonMap = [
1313
'ot2' => 'oneTwo2',
1414
];
1515

test/Obj/BaseObjectTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function testSubArray(): void
111111
$this->assertArrayNotHasKey('str1', $subItem);
112112
}
113113

114-
public function testUseAlias(): void
114+
public function testUseJsonMap(): void
115115
{
116116
// use snake
117117
$po = new ADemoPo2(['one_two' => 'abc']);

test/Str/StringHelperTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,14 @@ public function testRenderVars(): void
292292
$text = Str::renderVars('tags: ${ tags }', $vars, '${%s}');
293293
$this->assertEquals('tags: [php, java]', $text);
294294
}
295+
296+
public function testBeforeAfter(): void
297+
{
298+
$s = 'hello/world';
299+
$this->assertEquals('hello', Str::before($s, '/'));
300+
$this->assertEquals('some', Str::before($s, '+', 'some'));
301+
302+
$this->assertEquals('world', Str::after($s, '/'));
303+
$this->assertEquals('some', Str::after($s, '+', 'some'));
304+
}
295305
}

0 commit comments

Comments
 (0)