-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding interfaces for more capable storage sources. (#1)
- Loading branch information
Showing
8 changed files
with
256 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
# SAE (Services API Engine) | ||
# Contracts | ||
![CircleCI (all branches)](https://img.shields.io/circleci/project/github/fmizzell/contracts.svg) | ||
[![Maintainability](https://api.codeclimate.com/v1/badges/3790fc0eeeb9dac4f0cb/maintainability)](https://codeclimate.com/github/fmizzell/contracts/maintainability) | ||
[![Test Coverage](https://api.codeclimate.com/v1/badges/3790fc0eeeb9dac4f0cb/test_coverage)](https://codeclimate.com/github/fmizzell/contracts/test_coverage) | ||
[![GPLv3 license](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0.en.html) | ||
|
||
|
||
A set of interfaces. | ||
A set of interfaces. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Contracts; | ||
|
||
|
||
interface Conditioner | ||
{ | ||
public function conditionByIsEqualTo(string $property, string $value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Contracts; | ||
|
||
interface Limiter | ||
{ | ||
public function limitTo(int $number_of_items); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
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 | ||
{ | ||
private $offset = 0; | ||
private $limit = 0; | ||
|
||
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; | ||
} | ||
|
||
|
||
foreach ($this->sorts as $type => $properties) { | ||
foreach ($properties as $property) { | ||
usort($results, function ($a, $b) use ($property) { | ||
return $this->compare($a, $b, $property); | ||
}); | ||
|
||
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 = []; | ||
} | ||
|
||
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}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Contracts; | ||
|
||
interface Offsetter | ||
{ | ||
public function offsetBy(int $offset); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Contracts; | ||
|
||
interface Sorter | ||
{ | ||
public function sortByAscending(string $property); | ||
public function sortByDescending(string $property); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters