Skip to content

Commit 0b7ac57

Browse files
committed
up: update some str class method
1 parent 7a3f5e7 commit 0b7ac57

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

Diff for: src/Str/StrBuffer.php

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Toolkit\Stdlib\Str;
1111

1212
use Stringable;
13+
use Toolkit\Stdlib\Helper\DataHelper;
1314
use function array_unshift;
1415
use function implode;
1516
use function sprintf;
@@ -99,6 +100,17 @@ public function writeln(string $content): self
99100
return $this;
100101
}
101102

103+
/**
104+
* @param mixed $content
105+
*
106+
* @return self
107+
*/
108+
public function writeAny(mixed $content): self
109+
{
110+
$this->parts[] = DataHelper::toString($content);
111+
return $this;
112+
}
113+
102114
/**
103115
* @param string $content
104116
*

Diff for: src/Str/StrValue.php

+36-4
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ public function trim(string $chars = " \t\n\r\0\x0B"): self
136136
}
137137

138138
/**
139-
* @return string
139+
* @return StrValue
140140
*/
141-
public function trimmed(): string
141+
public function trimmed(): self
142142
{
143-
return trim($this->value);
143+
return self::newTrim($this->value);
144144
}
145145

146146
// ------------------ check ------------------
@@ -207,6 +207,14 @@ public function isOneOf(array $arr): bool
207207

208208
// ------------------ convert ------------------
209209

210+
/**
211+
* @return int
212+
*/
213+
public function int(): int
214+
{
215+
return (int)$this->value;
216+
}
217+
210218
/**
211219
* @return int
212220
*/
@@ -239,10 +247,26 @@ public function getInts(string $sep = ','): array
239247
return $this->toInts($sep);
240248
}
241249

250+
/**
251+
* @return bool
252+
*/
253+
public function bool(): bool
254+
{
255+
return $this->getBool();
256+
}
257+
242258
/**
243259
* @return bool
244260
*/
245261
public function toBool(): bool
262+
{
263+
return $this->getBool();
264+
}
265+
266+
/**
267+
* @return bool
268+
*/
269+
public function getBool(): bool
246270
{
247271
return Str::toBool($this->value);
248272
}
@@ -274,11 +298,19 @@ public function getStrings(string $sep = ',', int $limit = 0): array
274298
/**
275299
* @return string
276300
*/
277-
public function toString(): string
301+
public function value(): string
278302
{
279303
return $this->value;
280304
}
281305

306+
/**
307+
* @return string
308+
*/
309+
public function toString(): string
310+
{
311+
return $this->getValue();
312+
}
313+
282314
/**
283315
* @return string
284316
*/

Diff for: test/Str/StrValueTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public function testStrObjectBasic(): void
1919
$s = StrValue::new('abc ');
2020

2121
self::assertEquals(4, $s->length());
22-
self::assertEquals('abc', $s->trimmed());
22+
self::assertEquals('abc', $s->trimmed()->value());
23+
self::assertEquals('abc', $s->trim()->toString());
2324
self::assertFalse($s->hasSuffix('c'));
2425

2526
$s->trim();

0 commit comments

Comments
 (0)