Skip to content

Commit 750090e

Browse files
author
Martin Brecht-Precht
committed
WIP version 3
1 parent 9eaa0c9 commit 750090e

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

src/SimpleStringBuilder.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,20 @@ public function replace($position, $length, $string)
111111
$type = is_object($position) ? get_class($position) : gettype($position);
112112
throw new \InvalidArgumentException('Position invalid. Expected integer. Got ' . $type . '.');
113113
}
114+
if ($position > $this->length()) {
115+
throw new \InvalidArgumentException('Position invalid.');
116+
}
114117
if (!is_int($length)) {
115118
$type = is_object($length) ? get_class($length) : gettype($length);
116119
throw new \InvalidArgumentException('Length invalid. Expected integer. Got ' . $type . '.');
117120
}
121+
if ($position + $length > $this->length()) {
122+
throw new \InvalidArgumentException('Length invalid.');
123+
}
118124
if (!is_scalar($string)) {
119125
$type = is_object($string) ? get_class($string) : gettype($string);
120126
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
121127
}
122-
if ($position > $this->length()) {
123-
$position = $this->length();
124-
}
125128
$this->string = mb_substr($this->string, 0, $position) . (string)$string . mb_substr($this->string, $position + $length);
126129
return $this;
127130
}
@@ -269,7 +272,7 @@ public function length()
269272
*/
270273
public function buildSubstring($startPosition, $length = null)
271274
{
272-
if ($this->length() > $startPosition) {
275+
if ($startPosition > $this->length()) {
273276
throw new \InvalidArgumentException('Start position ' . (string)$startPosition . ' invalid.');
274277
}
275278
if (!is_int($length) && !is_null($length)) {

test/SimpleStringBuilderTest.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,14 @@ public function testBuilder()
2020
->append(12)
2121
->append(false)
2222
->prepend('b')
23-
->remove(1)
24-
->replace(0, 'ab')
2523
->append(true);
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());
24+
$this->assertEquals('a12b1', $builder->build());
25+
$this->assertEquals('12b1', $builder->buildSubstring(1));
26+
$this->assertEquals('a1', $builder->buildSubstring(0, 2));
27+
$this->assertEquals(5, $builder->size());
3028
$this->assertEquals(5, $builder->length());
31-
$this->assertTrue($builder->contains('ab'));
29+
$this->assertTrue($builder->contains('12b'));
3230
$this->assertFalse($builder->contains('abc'));
33-
$this->assertTrue($builder->stringContains('ab1'));
34-
$this->assertFalse($builder->stringContains('abc'));
3531
}
3632

3733
public function testBuilderAppendFail()
@@ -45,14 +41,14 @@ public function testBuilderPrependFail()
4541
{
4642
$this->setExpectedException(get_class(new \InvalidArgumentException()));
4743
$builder = new SimpleStringBuilder();
48-
$builder->prepend(new \DateTime());
44+
$builder->prepend(new \DateTimeZone('Europe/Berlin'));
4945
}
5046

5147
public function testBuilderReplaceFail1()
5248
{
5349
$this->setExpectedException(get_class(new \InvalidArgumentException()));
5450
$builder = new SimpleStringBuilder();
55-
$builder->replace(0, 'a');
51+
$builder->replace(0, 1, 'a');
5652
}
5753

5854
public function testBuilderReplaceFail2()
@@ -61,14 +57,16 @@ public function testBuilderReplaceFail2()
6157
$builder = new SimpleStringBuilder();
6258
$builder
6359
->append('a')
64-
->replace(0, new \DateTime());
60+
->replace(0, 2, 'a');
6561
}
6662

67-
public function testBuilderRemoveFail()
63+
public function testBuilderReplaceFail3()
6864
{
6965
$this->setExpectedException(get_class(new \InvalidArgumentException()));
7066
$builder = new SimpleStringBuilder();
71-
$builder->remove(0);
67+
$builder
68+
->append('a')
69+
->replace(0, 1, new \DateTimeZone('Europe/Berlin'));
7270
}
7371

7472
public function testBuilderContainsFail()
@@ -78,13 +76,6 @@ public function testBuilderContainsFail()
7876
$builder->contains(array());
7977
}
8078

81-
public function testBuilderStringContainsFail()
82-
{
83-
$this->setExpectedException(get_class(new \InvalidArgumentException()));
84-
$builder = new SimpleStringBuilder();
85-
$builder->stringContains(array());
86-
}
87-
8879
public function testBuilderBuildSubstringFail1()
8980
{
9081
$this->setExpectedException(get_class(new \InvalidArgumentException()));

0 commit comments

Comments
 (0)