Skip to content

Commit e7a26d8

Browse files
committed
Add WriteOnceArray
1 parent ca78b7d commit e7a26d8

6 files changed

+94
-3
lines changed

README.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Table of Contents
1717
* [XPath Key Array](#xpath-key-array)
1818
* [Dotted Key Array](#dotted-key-array)
1919
* [One-off Array](#one-off-array)
20+
* [Write-once Array](#write-once-array)
2021

2122
<a name="composite-key-array"></a>
2223

@@ -191,4 +192,27 @@ Compared to `CompositeKeyArray`, `DottedKeyArray` has some limitations:
191192
Sometimes you want to get value from an array by key and `unset` this key after that. The `OneOffArray` class helps you with this.
192193

193194
Again this class can be used in combination with `CompositeKeyArray` or its descendents: `XPathKeyArray` or `DottedKeyArray`.
194-
Actually, it can be used in combination with any object that implemets `ArrayAccess`.
195+
Actually, it can be used in combination with any object that implemets `ArrayAccess`.
196+
197+
<a name="write-once-array"></a>
198+
199+
## Write-once Array
200+
201+
If you want to be sure that each offset in your array would be written only once you can use `WriteOnceArray`. If you try to set one particular offset more than one time `IllegalOffsetException` will be thrown:
202+
203+
```php
204+
$array = new WriteOnceArray();
205+
206+
$array['foo'] = 'bar'; // => OK
207+
$array['foo'] = 'baz'; // => throws `IllegalOffsetException`
208+
```
209+
210+
Because `offsetExists` method is used in order to ensure write-once behaviour, `offsetUnset` method call is illegal:
211+
212+
```php
213+
$array = new WriteOnceArray([
214+
'foo' => 'bar',
215+
]);
216+
217+
unset($array['foo']); // => throws `IllegalOffsetUnsetMethodCallException`
218+
```

phpunit.xml.dist

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
convertNoticesToExceptions="true"
88
convertWarningsToExceptions="true"
99
processIsolation="false"
10-
stopOnFailure="false"
11-
syntaxCheck="false">
10+
stopOnFailure="false">
1211
<filter>
1312
<whitelist processUncoveredFilesFromWhitelist="true">
1413
<directory suffix=".php">./src</directory>

src/IllegalOffsetException.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ThoroughPHP\Arrays;
4+
5+
final class IllegalOffsetException extends \InvalidArgumentException
6+
{
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ThoroughPHP\Arrays;
4+
5+
final class IllegalOffsetUnsetMethodCallException extends \BadMethodCallException
6+
{
7+
8+
}

src/WriteOnceArray.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace ThoroughPHP\Arrays;
4+
5+
final class WriteOnceArray extends BaseArray
6+
{
7+
public function offsetSet($offset, $value): void
8+
{
9+
if ($this->offsetExists($offset)) {
10+
throw new IllegalOffsetException("Offset {$offset} was already written");
11+
}
12+
13+
parent::offsetSet($offset, $value);
14+
}
15+
16+
public function offsetUnset($offset): void
17+
{
18+
throw new IllegalOffsetUnsetMethodCallException("Cannot unset offset {$offset}. Array is write-once");
19+
}
20+
}

tests/unit/WriteOnceArrayTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace ThoroughPHP\Arrays\Tests\Unit;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use ThoroughPHP\Arrays\IllegalOffsetException;
7+
use ThoroughPHP\Arrays\IllegalOffsetUnsetMethodCallException;
8+
use ThoroughPHP\Arrays\WriteOnceArray;
9+
10+
final class WriteOnceArrayTest extends TestCase
11+
{
12+
public function testOffsetSet(): void
13+
{
14+
$this->expectException(IllegalOffsetException::class);
15+
16+
$array = new WriteOnceArray();
17+
18+
$array['foo'] = 'bar';
19+
$array['foo'] = 'baz';
20+
}
21+
22+
public function testOffsetUnset(): void
23+
{
24+
$this->expectException(IllegalOffsetUnsetMethodCallException::class);
25+
26+
$array = new WriteOnceArray([
27+
'foo' => 'bar',
28+
]);
29+
30+
unset($array['foo']);
31+
}
32+
}

0 commit comments

Comments
 (0)