Skip to content

Commit b9ae229

Browse files
authored
Loosen storage contracts, add hydratable interface, and allow hydration instances (#10)
* Loosen the storage related contracts to allow things, other than strings, to be stored, and retrieved. * Adding a hydratable interface, and being more specific, at least in the documentation, about what our storage related interfaces support. * Allowing an instance to be passed during hydration.
1 parent 86f667c commit b9ae229

File tree

7 files changed

+20
-14
lines changed

7 files changed

+20
-14
lines changed

src/HydratableInterface.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Contracts;
4+
5+
interface HydratableInterface extends \JsonSerializable
6+
{
7+
public static function hydrate(string $json, $instance = null);
8+
}

src/Mock/IdGenerator/Sequential.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Contracts\Mock\IdGenerator;
44

5-
class Sequential implements \Contracts\IdGeneratorInterface
5+
use Contracts\IdGeneratorInterface;
6+
7+
class Sequential implements IdGeneratorInterface
68
{
79
private $id = 0;
810
public function generate()

src/Mock/Storage/JsonObjectMemory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function retrieveAll(): array
3131
return $results;
3232
}
3333

34-
public function store(string $data, string $id = null): string
34+
public function store($data, string $id = null): string
3535
{
3636
$this->validate($data);
3737
return parent::store($data, $id);

src/Mock/Storage/Memory.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
namespace Contracts\Mock\Storage;
44

5+
use Contracts\RetrieverInterface;
56
use Contracts\StorerInterface;
67
use Contracts\BulkRetrieverInterface;
78

8-
class Memory implements StorerInterface, BulkRetrieverInterface
9+
class Memory implements RetrieverInterface, StorerInterface, BulkRetrieverInterface
910
{
1011

1112
protected $storage = [];
1213

13-
public function retrieve(string $id): ?string
14+
public function retrieve(string $id)
1415
{
1516
if (isset($this->storage[$id])) {
1617
return $this->storage[$id];
@@ -23,7 +24,7 @@ public function retrieveAll(): array
2324
return $this->storage;
2425
}
2526

26-
public function store(string $data, string $id = null): string
27+
public function store($data, string $id = null): string
2728
{
2829
if (!isset($id)) {
2930
throw new \Exception("An id is required to store the data.");

src/RetrieverInterface.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ interface RetrieverInterface
1010
* @param string $id
1111
* The identifier for the data.
1212
*
13-
* @return string
13+
* @return string | HydratableInterface
1414
* The data or null if no data could be retrieved.
15-
*
16-
* @throws \Exception
17-
* No data matched the identifier.
1815
*/
19-
public function retrieve(string $id): ?string;
16+
public function retrieve(string $id);
2017
}

src/StorerInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface StorerInterface
77
/**
88
* Store.
99
*
10-
* @param string $data
10+
* @param string|HydratableInterface $data
1111
* The data to be stored.
1212
* @param string $id
1313
* The identifier for the data. If the act of storing generates the
@@ -19,5 +19,5 @@ interface StorerInterface
1919
* @throws \Exception
2020
* Issues storing the data.
2121
*/
22-
public function store(string $data, string $id = null): string;
22+
public function store($data, string $id = null): string;
2323
}

test/Mock/IdGenerator/SequentialTest.php

-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace ContractsTest\Mock\IdGenerator;
54

6-
75
use Contracts\Mock\IdGenerator\Sequential;
86
use PHPUnit\Framework\TestCase;
97

0 commit comments

Comments
 (0)