diff --git a/src/BulkRetriever.php b/src/BulkRetrieverInterface.php similarity index 62% rename from src/BulkRetriever.php rename to src/BulkRetrieverInterface.php index b07290c..3a60194 100644 --- a/src/BulkRetriever.php +++ b/src/BulkRetrieverInterface.php @@ -2,8 +2,7 @@ namespace Contracts; - -interface BulkRetriever +interface BulkRetrieverInterface { /** @@ -12,5 +11,5 @@ interface BulkRetriever * @return array * An array of strings keyed by id. */ - public function retrieveAll(): array; -} \ No newline at end of file + public function retrieveAll(): array; +} diff --git a/src/Conditioner.php b/src/Conditioner.php deleted file mode 100644 index 97084cc..0000000 --- a/src/Conditioner.php +++ /dev/null @@ -1,9 +0,0 @@ -id++; - return $this->id; - } -} \ No newline at end of file +class Sequential implements \Contracts\IdGeneratorInterface +{ + private $id = 0; + public function generate() + { + $this->id++; + return $this->id; + } +} diff --git a/src/Mock/Storage/JsonObjectMemory.php b/src/Mock/Storage/JsonObjectMemory.php index 1afa82a..ef6c14c 100644 --- a/src/Mock/Storage/JsonObjectMemory.php +++ b/src/Mock/Storage/JsonObjectMemory.php @@ -2,126 +2,134 @@ namespace Contracts\Mock\Storage; -use Contracts\Sorter; -use Contracts\Conditioner; -use Contracts\Offsetter; -use Contracts\Limiter; - -class JsonObjectMemory extends Memory implements Sorter, Conditioner, Offsetter, Limiter +use Contracts\SorterInterface; +use Contracts\ConditionerInterface; +use Contracts\OffsetterInterface; +use Contracts\LimiterInterface; + +class JsonObjectMemory extends Memory implements + SorterInterface, + ConditionerInterface, + OffsetterInterface, + LimiterInterface { - private $offset = 0; - private $limit = 0; + private $offset = 0; + private $limit = 0; - private $sorts = [ + private $sorts = [ 'ascend' => [], 'descend' => [], - ]; - - private $conditions = []; - - public function retrieveAll(): array - { - $results = parent::retrieveAll(); - $results = $this->applyFilters($results); - $this->resetFilters(); - return $results; - } - - public function store(string $data, string $id = NULL): string - { - $this->validate($data); - return parent::store($data, $id); - } - - public function conditionByIsEqualTo(string $property, string $value) - { - $this->conditions[$property][] = $value; - } - - public function limitTo(int $number_of_items) - { - $this->limit = $number_of_items; - } - - public function offsetBy(int $offset) - { - $this->offset = $offset; - } - - public function sortByAscending(string $property) - { - $this->sorts['ascend'][] = $property; - } - - public function sortByDescending(string $property) - { - $this->sorts['descend'][] = $property; - } - - private function applyFilters(array $results) { - - if (!empty($this->conditions)) { - $results2 = []; - - foreach ($this->conditions as $property => $values) { - foreach ($values as $value) { - foreach ($results as $key => $result) { - $obj = json_decode($result); - if ($obj->{$property} == $value) { - $results2[$key] = $result; - } - } - } - } + ]; - $results = $results2; + private $conditions = []; + + public function retrieveAll(): array + { + $results = parent::retrieveAll(); + $results = $this->applyFilters($results); + $this->resetFilters(); + return $results; } + public function store(string $data, string $id = null): string + { + $this->validate($data); + return parent::store($data, $id); + } - foreach ($this->sorts as $type => $properties) { - foreach ($properties as $property) { - usort($results, function ($a, $b) use ($property) { - return $this->compare($a, $b, $property); - }); + public function conditionByIsEqualTo(string $property, string $value) + { + $this->conditions[$property][] = $value; + } - if ($type == 'descend') { - $results = array_reverse($results); - } - } + public function limitTo(int $number_of_items) + { + $this->limit = $number_of_items; } - if ($this->limit > 0 || $this->offset > 0) { - $results = array_slice($results, $this->offset, $this->limit); + public function offsetBy(int $offset) + { + $this->offset = $offset; } - return $results; - } + public function sortByAscending(string $property) + { + $this->sorts['ascend'][] = $property; + } - private function resetFilters() { - $this->offset = 0; - $this->limit = 0; + public function sortByDescending(string $property) + { + $this->sorts['descend'][] = $property; + } + + private function applyFilters(array $results) + { + + if (!empty($this->conditions)) { + $results2 = []; + + foreach ($this->conditions as $property => $values) { + foreach ($values as $value) { + foreach ($results as $key => $result) { + $obj = json_decode($result); + if ($obj->{$property} == $value) { + $results2[$key] = $result; + } + } + } + } + + $results = $results2; + } - $this->sorts = [ - 'ascend' => [], - 'descend' => [], - ]; - $this->conditions = []; - } + foreach ($this->sorts as $type => $properties) { + foreach ($properties as $property) { + usort($results, function ($a, $b) use ($property) { + return $this->compare($a, $b, $property); + }); - private function validate(string $data) { - $decoded = json_decode($data); - if (is_null($decoded)) { - throw new \Exception("Only JSON strings can be stored"); + if ($type == 'descend') { + $results = array_reverse($results); + } + } + } + + if ($this->limit > 0 || $this->offset > 0) { + $results = array_slice($results, $this->offset, $this->limit); + } + + return $results; + } + + private function resetFilters() + { + $this->offset = 0; + $this->limit = 0; + + $this->sorts = [ + 'ascend' => [], + 'descend' => [], + ]; + + $this->conditions = []; } - if (!is_object($decoded)) { - throw new \Exception("Only strings with JSON objects can be stored"); + + private function validate(string $data) + { + $decoded = json_decode($data); + if (is_null($decoded)) { + throw new \Exception("Only JSON strings can be stored"); + } + if (!is_object($decoded)) { + throw new \Exception("Only strings with JSON objects can be stored"); + } + } + + private function compare($a, $b, $property) + { + $a = json_decode($a); + $b = json_decode($b); + return strnatcmp($a->{$property}, $b->{$property}); } - } - - private function compare($a, $b, $property) { - $a = json_decode($a); - $b = json_decode($b); - return strnatcmp($a->{$property}, $b->{$property}); - } -} \ No newline at end of file +} diff --git a/src/Mock/Storage/Memory.php b/src/Mock/Storage/Memory.php index 09a1526..17e7b33 100644 --- a/src/Mock/Storage/Memory.php +++ b/src/Mock/Storage/Memory.php @@ -3,45 +3,45 @@ namespace Contracts\Mock\Storage; use Contracts\StorerInterface; -use Contracts\BulkRetriever; +use Contracts\BulkRetrieverInterface; -class Memory implements StorerInterface, BulkRetriever { +class Memory implements StorerInterface, BulkRetrieverInterface +{ - protected $storage = []; + protected $storage = []; - public function retrieve(string $id): ?string - { - if (isset($this->storage[$id])) { - return $this->storage[$id]; + public function retrieve(string $id): ?string + { + if (isset($this->storage[$id])) { + return $this->storage[$id]; + } + return null; } - return NULL; - } - - public function retrieveAll(): array - { - return $this->storage; - } - - public function store(string $data, string $id = NULL): string - { - if (!isset($id)) { - throw new \Exception("An id is required to store the data."); - } - if (!isset($this->storage[$id])) { - $this->storage[$id] = $data; - return $id; + + public function retrieveAll(): array + { + return $this->storage; } - $this->storage[$id] = $data; - return TRUE; - } - - public function remove(string $id) - { - if (isset($this->storage[$id])) { - unset($this->storage[$id]); - return TRUE; + + public function store(string $data, string $id = null): string + { + if (!isset($id)) { + throw new \Exception("An id is required to store the data."); + } + if (!isset($this->storage[$id])) { + $this->storage[$id] = $data; + return $id; + } + $this->storage[$id] = $data; + return true; } - return FALSE; - } -} \ No newline at end of file + public function remove(string $id) + { + if (isset($this->storage[$id])) { + unset($this->storage[$id]); + return true; + } + return false; + } +} diff --git a/src/Offsetter.php b/src/Offsetter.php deleted file mode 100644 index fcc44c0..0000000 --- a/src/Offsetter.php +++ /dev/null @@ -1,8 +0,0 @@ -expectExceptionMessage("An id is required to store the data."); - $store->store("Data"); - } + $this->expectExceptionMessage("An id is required to store the data."); + $store->store("Data"); + } - public function testStorageMemory() { - $store = new \Contracts\Mock\Storage\Memory(); + public function testStorageMemory() + { + $store = new \Contracts\Mock\Storage\Memory(); - $store->store("Data", "1"); - $this->assertEquals("Data", $store->retrieve("1")); + $store->store("Data", "1"); + $this->assertEquals("Data", $store->retrieve("1")); - $store->store("Data 2", "2"); - $this->assertEquals("Data 2", $store->retrieve("2")); + $store->store("Data 2", "2"); + $this->assertEquals("Data 2", $store->retrieve("2")); - $all = $store->retrieveAll(); - $this->assertTrue(is_array($all)); + $all = $store->retrieveAll(); + $this->assertTrue(is_array($all)); - $this->assertEquals("Data", $all["1"]); + $this->assertEquals("Data", $all["1"]); - $store->remove("1"); + $store->remove("1"); - $this->assertNull($store->retrieve("1")); - } + $this->assertNull($store->retrieve("1")); + } - public function testStorageJsonObjectMemory() { - $objects = []; - $objects[] = << $object) { - $store->store($object, "{$index}"); - } + foreach ($objects as $index => $object) { + $store->store($object, "{$index}"); + } - $this->assertEquals(4, count($store->retrieveAll())); + $this->assertEquals(4, count($store->retrieveAll())); - $store->offsetBy(1); - $store->limitTo(1); + $store->offsetBy(1); + $store->limitTo(1); - foreach ($store->retrieveAll() as $string) { - $this->assertEquals($objects[1], $string); - } + foreach ($store->retrieveAll() as $string) { + $this->assertEquals($objects[1], $string); + } - $store->offsetBy(3); + $store->offsetBy(3); - foreach ($store->retrieveAll() as $string) { - $this->assertEquals($objects[3], $string); - } + foreach ($store->retrieveAll() as $string) { + $this->assertEquals($objects[3], $string); + } - $store->limitTo(1); + $store->limitTo(1); - foreach ($store->retrieveAll() as $string) { - $this->assertEquals($objects[0], $string); - } + foreach ($store->retrieveAll() as $string) { + $this->assertEquals($objects[0], $string); + } - $store->sortByAscending("first"); + $store->sortByAscending("first"); - $order = [0, 2, 3, 1]; - $order_index = 0; - foreach ($store->retrieveAll() as $string) { - $this->assertEquals($objects[$order[$order_index]], $string); - $order_index++; - } + $order = [0, 2, 3, 1]; + $order_index = 0; + foreach ($store->retrieveAll() as $string) { + $this->assertEquals($objects[$order[$order_index]], $string); + $order_index++; + } - $store->sortByDescending("first"); + $store->sortByDescending("first"); - $order = [1, 3, 2, 0]; - $order_index = 0; - foreach ($store->retrieveAll() as $string) { - $this->assertEquals($objects[$order[$order_index]], $string); - $order_index++; - } + $order = [1, 3, 2, 0]; + $order_index = 0; + foreach ($store->retrieveAll() as $string) { + $this->assertEquals($objects[$order[$order_index]], $string); + $order_index++; + } - $store->conditionByIsEqualTo("first", "Gerardo"); + $store->conditionByIsEqualTo("first", "Gerardo"); - $order = [0, 2]; - $order_index = 0; - foreach ($store->retrieveAll() as $string) { - $this->assertEquals($objects[$order[$order_index]], $string); - $order_index++; + $order = [0, 2]; + $order_index = 0; + foreach ($store->retrieveAll() as $string) { + $this->assertEquals($objects[$order[$order_index]], $string); + $order_index++; + } } - } - -} \ No newline at end of file +}