Skip to content

Commit 5b2ba69

Browse files
authored
Merge pull request #20 from colinodell/better-exceptions
Implement more-specific exceptions
2 parents 7564f35 + 1064bbd commit 5b2ba69

File tree

5 files changed

+66
-14
lines changed

5 files changed

+66
-14
lines changed

src/Data.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
namespace Dflydev\DotAccessData;
1313

14-
use RuntimeException;
1514
use ArrayAccess;
15+
use Dflydev\DotAccessData\Exception\DataException;
16+
use Dflydev\DotAccessData\Exception\InvalidPathException;
1617

1718
/**
1819
* @implements ArrayAccess<string, mixed>
@@ -42,7 +43,7 @@ public function __construct(array $data = [])
4243
public function append(string $key, $value = null): void
4344
{
4445
if (0 == strlen($key)) {
45-
throw new RuntimeException("Key cannot be an empty string");
46+
throw new InvalidPathException("Key cannot be an empty string");
4647
}
4748

4849
$currentValue =& $this->data;
@@ -88,7 +89,7 @@ public function append(string $key, $value = null): void
8889
public function set(string $key, $value = null): void
8990
{
9091
if (0 == strlen($key)) {
91-
throw new RuntimeException("Key cannot be an empty string");
92+
throw new InvalidPathException("Key cannot be an empty string");
9293
}
9394

9495
$currentValue =& $this->data;
@@ -107,7 +108,7 @@ public function set(string $key, $value = null): void
107108
$currentValue[$currentKey] = [];
108109
}
109110
if (!is_array($currentValue[$currentKey])) {
110-
throw new RuntimeException("Key path at $currentKey of $key cannot be indexed into (is not an array)");
111+
throw new DataException("Key path at $currentKey of $key cannot be indexed into (is not an array)");
111112
}
112113
$currentValue =& $currentValue[$currentKey];
113114
}
@@ -120,7 +121,7 @@ public function set(string $key, $value = null): void
120121
public function remove(string $key): void
121122
{
122123
if (0 == strlen($key)) {
123-
throw new RuntimeException("Key cannot be an empty string");
124+
throw new InvalidPathException("Key cannot be an empty string");
124125
}
125126

126127
$currentValue =& $this->data;
@@ -203,7 +204,7 @@ public function getData(string $key): DataInterface
203204
return new Data($value);
204205
}
205206

206-
throw new RuntimeException("Value at '$key' could not be represented as a DataInterface");
207+
throw new DataException("Value at '$key' could not be represented as a DataInterface");
207208
}
208209

209210
/**

src/DataInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111

1212
namespace Dflydev\DotAccessData;
1313

14+
use Dflydev\DotAccessData\Exception\DataException;
15+
use Dflydev\DotAccessData\Exception\InvalidPathException;
16+
1417
interface DataInterface
1518
{
1619
/**
1720
* Append a value to a key (assumes key refers to an array value)
1821
*
1922
* @param string $key
2023
* @param mixed $value
24+
*
25+
* @throws InvalidPathException if the given path is empty
2126
*/
2227
public function append(string $key, $value = null): void;
2328

@@ -26,13 +31,18 @@ public function append(string $key, $value = null): void;
2631
*
2732
* @param string $key
2833
* @param mixed $value
34+
*
35+
* @throws InvalidPathException if the given path is empty
36+
* @throws DataException if the given path does not target an array
2937
*/
3038
public function set(string $key, $value = null): void;
3139

3240
/**
3341
* Remove a key
3442
*
3543
* @param string $key
44+
*
45+
* @throws InvalidPathException if the given path is empty
3646
*/
3747
public function remove(string $key): void;
3848

@@ -66,6 +76,8 @@ public function has(string $key): bool;
6676
*
6777
* @return DataInterface
6878
*
79+
* @throws DataException if the given path does not reference an array
80+
*
6981
* @psalm-mutation-free
7082
*/
7183
public function getData(string $key): DataInterface;

src/Exception/DataException.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is a part of dflydev/dot-access-data.
5+
*
6+
* (c) Dragonfly Development Inc.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Dflydev\DotAccessData\Exception;
13+
14+
/**
15+
* Base runtime exception type thrown by this library
16+
*/
17+
class DataException extends \RuntimeException
18+
{
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is a part of dflydev/dot-access-data.
5+
*
6+
* (c) Dragonfly Development Inc.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Dflydev\DotAccessData\Exception;
13+
14+
/**
15+
* Thrown when trying to access an invalid path in the data array
16+
*/
17+
class InvalidPathException extends DataException
18+
{
19+
}

tests/DataTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
namespace Dflydev\DotAccessData;
1313

14-
use RuntimeException;
14+
use Dflydev\DotAccessData\Exception\DataException;
15+
use Dflydev\DotAccessData\Exception\InvalidPathException;
1516
use PHPUnit\Framework\TestCase;
1617

1718
class DataTest extends TestCase
@@ -81,7 +82,7 @@ public function testAppend()
8182
$this->assertEquals(['I', 'I2'], $data->get('h.i'));
8283
$this->assertEquals(['L'], $data->get('i.k.l'));
8384

84-
$this->expectException(RuntimeException::class);
85+
$this->expectException(InvalidPathException::class);
8586

8687
$data->append('', 'broken');
8788
}
@@ -104,7 +105,7 @@ public function testSet()
104105
$this->assertEquals('F', $data->get('d.e.f'));
105106
$this->assertEquals(['e' => ['f' => 'F', 'g' => 'G']], $data->get('d'));
106107

107-
$this->expectException(RuntimeException::class);
108+
$this->expectException(InvalidPathException::class);
108109

109110
$data->set('', 'broken');
110111
}
@@ -115,7 +116,7 @@ public function testSetClobberStringInPath()
115116

116117
$data->set('a.b.c', 'Should not be able to write to a.b.c.d.e');
117118

118-
$this->expectException(RuntimeException::class);
119+
$this->expectException(DataException::class);
119120

120121
$data->set('a.b.c.d.e', 'broken');
121122
}
@@ -137,7 +138,7 @@ public function testRemove()
137138
$this->assertNull(null);
138139
$this->assertEquals('D2', $data->get('b.d.d2'));
139140

140-
$this->expectException(RuntimeException::class);
141+
$this->expectException(InvalidPathException::class);
141142

142143
$data->remove('', 'broken');
143144
}
@@ -178,7 +179,7 @@ public function testGetData()
178179

179180
$this->runSampleDataTests($data);
180181

181-
$this->expectException(RuntimeException::class);
182+
$this->expectException(DataException::class);
182183

183184
$data = $wrappedData->getData('wrapped.sampleData.a');
184185
}
@@ -260,7 +261,7 @@ public function testOffsetSet()
260261
$this->assertEquals('F', $data['d.e.f']);
261262
$this->assertEquals(['e' => ['f' => 'F', 'g' => 'G']], $data['d']);
262263

263-
$this->expectException(RuntimeException::class);
264+
$this->expectException(InvalidPathException::class);
264265

265266
$data->set('', 'broken');
266267
}
@@ -282,7 +283,7 @@ public function testOffsetUnset()
282283
$this->assertNull(null);
283284
$this->assertEquals('D2', $data['b.d.d2']);
284285

285-
$this->expectException(RuntimeException::class);
286+
$this->expectException(InvalidPathException::class);
286287

287288
unset($data['']);
288289
}

0 commit comments

Comments
 (0)