Skip to content

Commit 6a4245c

Browse files
author
Martin Brecht-Precht
committed
Extended the Builder class by different useful methods.
1 parent 9a18b1a commit 6a4245c

File tree

3 files changed

+222
-10
lines changed

3 files changed

+222
-10
lines changed

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ require_once('path/to/vendor/autoload.php');
3131

3232
### Building a string
3333

34-
In this first implementation the only provided method is appending strings to the builder.
35-
3634
```{php}
3735
use Markenwerk\SimpleStringBuilder\SimpleStringBuilder;
3836
@@ -41,16 +39,35 @@ $builder
4139
->append('a')
4240
->append(12)
4341
->append(false)
42+
->prepend('b')
43+
->remove(1)
44+
->replace(0, 'ab')
4445
->append(true);
4546
46-
$string = $builder->toString();
47+
$string = $builder->build();
4748
fwrite(STDOUT, 'Result "' . $string . '"' . PHP_EOL);
49+
50+
$substring = $builder->buildSubstring(0, 2);
51+
fwrite(STDOUT, 'Substring result from position 0 and size 2 "' . $substring . '"' . PHP_EOL);
52+
53+
$substring = $builder->buildSubstring(1);
54+
fwrite(STDOUT, 'Substring result from position 1 till the end "' . $substring . '"' . PHP_EOL);
55+
56+
$size = $builder->size();
57+
fwrite(STDOUT, 'Builder holds "' . $size . '" partial strings' . PHP_EOL);
58+
59+
$length = $builder->length();
60+
fwrite(STDOUT, 'Resulting string length is "' . $length . '" characters' . PHP_EOL);
4861
```
4962

5063
will output the following
5164

5265
```{http}
53-
Result "a121"
66+
Result "ab121"
67+
Substring result from position 0 and size 2 "ab12"
68+
Substring result from position 1 till the end "121"
69+
Builder holds "4" partial strings
70+
Resulting string length is "5" characters
5471
```
5572

5673
---

src/SimpleStringBuilder.php

Lines changed: 120 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,140 @@
1212
class SimpleStringBuilder
1313
{
1414

15-
private $string = '';
15+
/**
16+
* @var array
17+
*/
18+
private $strings = array();
1619

1720
/**
1821
* @param string $string
1922
* @return $this
2023
*/
2124
public function append($string)
2225
{
23-
$this->string .= (string)$string;
26+
if (!is_scalar($string)) {
27+
$type = is_object($string) ? get_class($string) : gettype($string);
28+
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
29+
}
30+
$this->strings[] = (string)$string;
31+
return $this;
32+
}
33+
34+
/**
35+
* @param string $string
36+
* @return $this
37+
*/
38+
public function prepend($string)
39+
{
40+
if (!is_scalar($string)) {
41+
$type = is_object($string) ? get_class($string) : gettype($string);
42+
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
43+
}
44+
array_unshift($this->strings, (string)$string);
2445
return $this;
2546
}
2647

48+
/**
49+
* @param int $position
50+
* @param string $string
51+
* @return $this
52+
*/
53+
public function replace($position, $string)
54+
{
55+
if (!is_scalar($string)) {
56+
$type = is_object($string) ? get_class($string) : gettype($string);
57+
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
58+
}
59+
if (!isset($this->strings[$position])) {
60+
throw new \InvalidArgumentException('Position ' . (string)$position . ' invalid.');
61+
}
62+
array_splice($this->strings, $position, 1, (string)$string);
63+
return $this;
64+
}
65+
66+
/**
67+
* @param int $position
68+
* @return $this
69+
*/
70+
public function remove($position)
71+
{
72+
if (!isset($this->strings[$position])) {
73+
throw new \InvalidArgumentException('Position ' . (string)$position . ' invalid.');
74+
}
75+
array_splice($this->strings, $position, 1);
76+
return $this;
77+
}
78+
79+
/**
80+
* @param string $string
81+
* @return bool
82+
*/
83+
public function contains($string)
84+
{
85+
if (!is_scalar($string)) {
86+
$type = is_object($string) ? get_class($string) : gettype($string);
87+
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
88+
}
89+
for ($i = 0, $n = $this->size(); $i < $n; $i++) {
90+
if ($this->strings[$i] === (string)$string) {
91+
return true;
92+
}
93+
}
94+
return false;
95+
}
96+
97+
/**
98+
* @param string $string
99+
* @return bool
100+
*/
101+
public function stringContains($string)
102+
{
103+
if (!is_scalar($string)) {
104+
$type = is_object($string) ? get_class($string) : gettype($string);
105+
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
106+
}
107+
return strpos($this->build(), (string)$string) !== false;
108+
}
109+
110+
/**
111+
* @return int
112+
*/
113+
public function size()
114+
{
115+
return count($this->strings);
116+
}
117+
118+
/**
119+
* @return int
120+
*/
121+
public function length()
122+
{
123+
return mb_strlen($this->build());
124+
}
125+
126+
/**
127+
* @param int $startPosition
128+
* @param int $size
129+
* @return string
130+
*/
131+
public function buildSubstring($startPosition, $size = null)
132+
{
133+
if (!isset($this->strings[$startPosition])) {
134+
throw new \InvalidArgumentException('Start position ' . (string)$startPosition . ' invalid.');
135+
}
136+
if (!is_int($size) && !is_null($size)) {
137+
$type = is_object($size) ? get_class($size) : gettype($size);
138+
throw new \InvalidArgumentException('Length invalid. Expected integer. Got ' . $type . '.');
139+
}
140+
return implode('', array_slice($this->strings, $startPosition, $size));
141+
}
142+
27143
/**
28144
* @return string
29145
*/
30-
public function toString()
146+
public function build()
31147
{
32-
return $this->string;
148+
return implode('', $this->strings);
33149
}
34150

35151
}

test/SimpleStringBuilderTest.php

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
*
1010
* @package Test
1111
*/
12-
class SimpleStringBuilderTest extends \PHPUnit_Framework_TestCase{
12+
class SimpleStringBuilderTest extends \PHPUnit_Framework_TestCase
13+
{
1314

1415
public function testBuilder()
1516
{
@@ -18,8 +19,86 @@ public function testBuilder()
1819
->append('a')
1920
->append(12)
2021
->append(false)
22+
->prepend('b')
23+
->remove(1)
24+
->replace(0, 'ab')
2125
->append(true);
22-
$this->assertEquals('a121', $builder->toString());
26+
$this->assertEquals('ab121', $builder->build());
27+
$this->assertEquals('121', $builder->buildSubstring(1));
28+
$this->assertEquals('ab12', $builder->buildSubstring(0, 2));
29+
$this->assertEquals(4, $builder->size());
30+
$this->assertEquals(5, $builder->length());
31+
$this->assertTrue($builder->contains('ab'));
32+
$this->assertFalse($builder->contains('abc'));
33+
$this->assertTrue($builder->stringContains('ab1'));
34+
$this->assertFalse($builder->stringContains('abc'));
35+
}
36+
37+
public function testBuilderAppendFail()
38+
{
39+
$this->setExpectedException(get_class(new \InvalidArgumentException()));
40+
$builder = new SimpleStringBuilder();
41+
$builder->append(array());
42+
}
43+
44+
public function testBuilderPrependFail()
45+
{
46+
$this->setExpectedException(get_class(new \InvalidArgumentException()));
47+
$builder = new SimpleStringBuilder();
48+
$builder->prepend(new \DateTime());
49+
}
50+
51+
public function testBuilderReplaceFail1()
52+
{
53+
$this->setExpectedException(get_class(new \InvalidArgumentException()));
54+
$builder = new SimpleStringBuilder();
55+
$builder->replace(0, 'a');
56+
}
57+
58+
public function testBuilderReplaceFail2()
59+
{
60+
$this->setExpectedException(get_class(new \InvalidArgumentException()));
61+
$builder = new SimpleStringBuilder();
62+
$builder
63+
->append('a')
64+
->replace(0, new \DateTime());
65+
}
66+
67+
public function testBuilderRemoveFail()
68+
{
69+
$this->setExpectedException(get_class(new \InvalidArgumentException()));
70+
$builder = new SimpleStringBuilder();
71+
$builder->remove(0);
72+
}
73+
74+
public function testBuilderContainsFail()
75+
{
76+
$this->setExpectedException(get_class(new \InvalidArgumentException()));
77+
$builder = new SimpleStringBuilder();
78+
$builder->contains(array());
79+
}
80+
81+
public function testBuilderStringContainsFail()
82+
{
83+
$this->setExpectedException(get_class(new \InvalidArgumentException()));
84+
$builder = new SimpleStringBuilder();
85+
$builder->contains(array());
86+
}
87+
88+
public function testBuilderBuildSubstringFail1()
89+
{
90+
$this->setExpectedException(get_class(new \InvalidArgumentException()));
91+
$builder = new SimpleStringBuilder();
92+
$builder->buildSubstring(1);
93+
}
94+
95+
public function testBuilderBuildSubstringFail2()
96+
{
97+
$this->setExpectedException(get_class(new \InvalidArgumentException()));
98+
$builder = new SimpleStringBuilder();
99+
$builder
100+
->append('ab')
101+
->buildSubstring(0, array());
23102
}
24103

25104
}

0 commit comments

Comments
 (0)