Skip to content

Commit

Permalink
PSR-2 Fixes (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmizzell authored Jul 31, 2019
1 parent 354ddfd commit 767be61
Show file tree
Hide file tree
Showing 21 changed files with 293 additions and 284 deletions.
7 changes: 3 additions & 4 deletions src/BulkRetriever.php → src/BulkRetrieverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Contracts;


interface BulkRetriever
interface BulkRetrieverInterface
{

/**
Expand All @@ -12,5 +11,5 @@ interface BulkRetriever
* @return array
* An array of strings keyed by id.
*/
public function retrieveAll(): array;
}
public function retrieveAll(): array;
}
9 changes: 0 additions & 9 deletions src/Conditioner.php

This file was deleted.

8 changes: 8 additions & 0 deletions src/ConditionerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Contracts;

interface ConditionerInterface
{
public function conditionByIsEqualTo(string $property, string $value);
}
6 changes: 3 additions & 3 deletions src/Countable.php → src/CountableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use phpDocumentor\Reflection\Types\Integer;

interface Countable
interface CountableInterface
{
public function count(): int;
}
public function count(): int;
}
8 changes: 0 additions & 8 deletions src/IdGenerator.php

This file was deleted.

8 changes: 8 additions & 0 deletions src/IdGeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Contracts;

interface IdGeneratorInterface
{
public function generate();
}
8 changes: 0 additions & 8 deletions src/Limiter.php

This file was deleted.

8 changes: 8 additions & 0 deletions src/LimiterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Contracts;

interface LimiterInterface
{
public function limitTo(int $number_of_items);
}
16 changes: 9 additions & 7 deletions src/Mock/IdGenerator/Sequential.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Contracts\Mock\IdGenerator;

class Sequential implements \Contracts\IdGenerator {
private $id = 0;
public function generate() {
$this->id++;
return $this->id;
}
}
class Sequential implements \Contracts\IdGeneratorInterface
{
private $id = 0;
public function generate()
{
$this->id++;
return $this->id;
}
}
216 changes: 112 additions & 104 deletions src/Mock/Storage/JsonObjectMemory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
}
}
Loading

0 comments on commit 767be61

Please sign in to comment.