Skip to content

Commit 7bab2e5

Browse files
committed
up: add more test, add some new methods
1 parent e28dbae commit 7bab2e5

File tree

3 files changed

+117
-25
lines changed

3 files changed

+117
-25
lines changed

src/Str/StrBuffer.php

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,89 @@
99

1010
namespace Toolkit\Stdlib\Str;
1111

12+
use function array_unshift;
13+
use function implode;
14+
use function sprintf;
15+
1216
/**
1317
* Class StrBuffer
18+
*
1419
* @package Toolkit\Stdlib\Str
1520
*/
16-
final class StrBuffer
21+
class StrBuffer
1722
{
1823
/**
19-
* @var string
24+
* @var self
2025
*/
21-
private $body;
26+
private static $global;
2227

2328
/**
24-
* @param string $content
29+
* @var string[]
30+
*/
31+
private $parts = [];
32+
33+
/**
34+
* @param string $str
35+
*
36+
* @return self
37+
*/
38+
public static function global(string $str = ''): self
39+
{
40+
if (!self::$global) {
41+
self::$global = new self($str);
42+
}
43+
44+
return self::$global;
45+
}
46+
47+
/**
48+
* @param string $str
2549
*
2650
* @return static
2751
*/
28-
public static function new(string $content): self
52+
public static function new(string $str = ''): self
53+
{
54+
return new self($str);
55+
}
56+
57+
/**
58+
* Class constructor.
59+
*
60+
* @param string $str
61+
*/
62+
public function __construct(string $str = '')
2963
{
30-
return new self($content);
64+
$this->write($str);
3165
}
3266

33-
public function __construct(string $content = '')
67+
public function reset(): void
3468
{
35-
$this->body = $content;
69+
$this->parts = [];
3670
}
3771

3872
/**
3973
* @param string $content
4074
*/
4175
public function write(string $content): void
4276
{
43-
$this->body .= $content;
77+
$this->parts[] = $content;
78+
}
79+
80+
/**
81+
* @param string $fmt
82+
* @param mixed ...$args
83+
*/
84+
public function writef(string $fmt, ...$args): void
85+
{
86+
$this->parts[] = sprintf($fmt, ...$args);
87+
}
88+
89+
/**
90+
* @param string $content
91+
*/
92+
public function writeln(string $content): void
93+
{
94+
$this->parts[] = $content . "\n";
4495
}
4596

4697
/**
@@ -56,43 +107,35 @@ public function append(string $content): void
56107
*/
57108
public function prepend(string $content): void
58109
{
59-
$this->body = $content . $this->body;
110+
array_unshift($this->parts, $content);
60111
}
61112

62113
/**
63114
* clear data
64115
*/
65116
public function clear(): string
66117
{
67-
$string = $this->body;
118+
$strings = $this->parts;
68119
// clear
69-
$this->body = '';
70-
71-
return $string;
72-
}
120+
$this->parts = [];
73121

74-
/**
75-
* @return string
76-
*/
77-
public function getBody(): string
78-
{
79-
return $this->body;
122+
return implode($strings);
80123
}
81124

82125
/**
83-
* @param string $body
126+
* @return string[]
84127
*/
85-
public function setBody(string $body): void
128+
public function getParts(): array
86129
{
87-
$this->body = $body;
130+
return $this->parts;
88131
}
89132

90133
/**
91134
* @return string
92135
*/
93136
public function toString(): string
94137
{
95-
return $this->body;
138+
return implode($this->parts);
96139
}
97140

98141
/**

src/Str/StrObject.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Toolkit\Stdlib\Str;
44

55
use Toolkit\Stdlib\Str;
6+
use function in_array;
67
use function trim;
78

89
/**
@@ -155,6 +156,26 @@ public function hasSuffix(string $suffix): bool
155156
return Str::hasSuffix($this->string, $suffix);
156157
}
157158

159+
/**
160+
* @param string $str
161+
*
162+
* @return bool
163+
*/
164+
public function isSubstrOf(string $str): bool
165+
{
166+
return Str::strpos($str, $this->string) !== false;
167+
}
168+
169+
/**
170+
* @param string[] $arr
171+
*
172+
* @return bool
173+
*/
174+
public function isOneOf(array $arr): bool
175+
{
176+
return in_array($this->string, $arr, true);
177+
}
178+
158179
// ------------------ convert ------------------
159180

160181
/**

test/Str/StrBufferTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\StdlibTest\Str;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Toolkit\Stdlib\Str\StrBuffer;
7+
8+
/**
9+
* Class StrBufferTest
10+
*
11+
* @package Toolkit\StdlibTest\Str
12+
*/
13+
class StrBufferTest extends TestCase
14+
{
15+
public function testBasic(): void
16+
{
17+
$buf = StrBuffer::new();
18+
19+
$buf->write('a');
20+
$buf->writef(' %s an ', 'is');
21+
$buf->writeln('alpha');
22+
23+
self::assertEquals("a is an alpha\n", $buf->toString());
24+
25+
$buf->reset();
26+
self::assertEmpty($buf->toString());
27+
}
28+
}

0 commit comments

Comments
 (0)