Skip to content

Commit

Permalink
Loosen storage contracts, add hydratable interface, and allow hydrati…
Browse files Browse the repository at this point in the history
…on 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.
  • Loading branch information
fmizzell authored Oct 21, 2019
1 parent 86f667c commit b9ae229
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 14 deletions.
8 changes: 8 additions & 0 deletions src/HydratableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Contracts;

interface HydratableInterface extends \JsonSerializable
{
public static function hydrate(string $json, $instance = null);
}
4 changes: 3 additions & 1 deletion src/Mock/IdGenerator/Sequential.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Contracts\Mock\IdGenerator;

class Sequential implements \Contracts\IdGeneratorInterface
use Contracts\IdGeneratorInterface;

class Sequential implements IdGeneratorInterface
{
private $id = 0;
public function generate()
Expand Down
2 changes: 1 addition & 1 deletion src/Mock/Storage/JsonObjectMemory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function retrieveAll(): array
return $results;
}

public function store(string $data, string $id = null): string
public function store($data, string $id = null): string
{
$this->validate($data);
return parent::store($data, $id);
Expand Down
7 changes: 4 additions & 3 deletions src/Mock/Storage/Memory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace Contracts\Mock\Storage;

use Contracts\RetrieverInterface;
use Contracts\StorerInterface;
use Contracts\BulkRetrieverInterface;

class Memory implements StorerInterface, BulkRetrieverInterface
class Memory implements RetrieverInterface, StorerInterface, BulkRetrieverInterface
{

protected $storage = [];

public function retrieve(string $id): ?string
public function retrieve(string $id)
{
if (isset($this->storage[$id])) {
return $this->storage[$id];
Expand All @@ -23,7 +24,7 @@ public function retrieveAll(): array
return $this->storage;
}

public function store(string $data, string $id = null): string
public function store($data, string $id = null): string
{
if (!isset($id)) {
throw new \Exception("An id is required to store the data.");
Expand Down
7 changes: 2 additions & 5 deletions src/RetrieverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ interface RetrieverInterface
* @param string $id
* The identifier for the data.
*
* @return string
* @return string | HydratableInterface
* The data or null if no data could be retrieved.
*
* @throws \Exception
* No data matched the identifier.
*/
public function retrieve(string $id): ?string;
public function retrieve(string $id);
}
4 changes: 2 additions & 2 deletions src/StorerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface StorerInterface
/**
* Store.
*
* @param string $data
* @param string|HydratableInterface $data
* The data to be stored.
* @param string $id
* The identifier for the data. If the act of storing generates the
Expand All @@ -19,5 +19,5 @@ interface StorerInterface
* @throws \Exception
* Issues storing the data.
*/
public function store(string $data, string $id = null): string;
public function store($data, string $id = null): string;
}
2 changes: 0 additions & 2 deletions test/Mock/IdGenerator/SequentialTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace ContractsTest\Mock\IdGenerator;


use Contracts\Mock\IdGenerator\Sequential;
use PHPUnit\Framework\TestCase;

Expand Down

0 comments on commit b9ae229

Please sign in to comment.