Skip to content

Commit

Permalink
Make a trait that implements ModelManagerInterface
Browse files Browse the repository at this point in the history
This makes it easy to implement other ModelMangers in memory
  • Loading branch information
mcfedr committed Aug 8, 2017
1 parent d923b94 commit 8f4276f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 63 deletions.
64 changes: 1 addition & 63 deletions src/Model/AccessTokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,73 +18,11 @@
*/
class AccessTokenManager implements AccessTokenManagerInterface
{
protected $models;
use ModelManagerTrait;

public function getClassName()
{
return 'AuthBucket\\OAuth2\\Model\\AccessToken';
}

public function createModel(ModelInterface $model)
{
$this->models[$model->getId()] = $model;

return $model;
}

public function readModelAll()
{
return $this->models;
}

public function readModelBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
$models = [];

foreach ($this->models as $model) {
foreach ($criteria as $key => $value) {
$getter = 'get'.ucfirst($key);

if (method_exists($model, $getter) && $model->$getter() === $value) {
$models[$model->getId()] = $model;
}
}
}

// For simplified implementation, we only order by first key/value pair here.
if ($orderBy !== null && is_array($orderBy)) {
usort($models, function ($a, $b) {
$getter = 'get'.ucfirst(key($orderBy));

return strtolower(reset($orderBy)) !== 'asc'
? strcmp($b->$getter(), $a->$getter())
: strcmp($a->$getter(), $b->$getter());
});
}

$models = array_slice($models, $offset, $limit);

return $models ?: null;
}

public function readModelOneBy(array $criteria, array $orderBy = null)
{
$models = $this->readModelBy($criteria, $orderBy, 1, 0);

return is_array($models) ? reset($models) : $models;
}

public function updateModel(ModelInterface $model)
{
$this->models[$model->getId()] = $model;

return $model;
}

public function deleteModel(ModelInterface $model)
{
$this->models[$model->getId()] = null;

return $model;
}
}
77 changes: 77 additions & 0 deletions src/Model/ModelManagerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php


namespace AuthBucket\OAuth2\Model;


trait ModelManagerTrait
{

protected $models;

public function createModel(ModelInterface $model)
{
$this->models[$model->getId()] = $model;

return $model;
}

public function readModelAll()
{
return $this->models;
}

public function readModelBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
$models = [];

foreach ($this->models as $model) {
foreach ($criteria as $key => $value) {
$getter = 'get'.ucfirst($key);

if (method_exists($model, $getter) && $model->$getter() === $value) {
$models[$model->getId()] = $model;
}
}
}

// For simplified implementation, we only order by first key/value pair here.
if ($orderBy !== null && is_array($orderBy)) {
usort(
$models,
function ($a, $b) {
$getter = 'get'.ucfirst(key($orderBy));

return strtolower(reset($orderBy)) !== 'asc'
? strcmp($b->$getter(), $a->$getter())
: strcmp($a->$getter(), $b->$getter());
}
);
}

$models = array_slice($models, $offset, $limit);

return $models ?: null;
}

public function readModelOneBy(array $criteria, array $orderBy = null)
{
$models = $this->readModelBy($criteria, $orderBy, 1, 0);

return is_array($models) ? reset($models) : $models;
}

public function updateModel(ModelInterface $model)
{
$this->models[$model->getId()] = $model;

return $model;
}

public function deleteModel(ModelInterface $model)
{
$this->models[$model->getId()] = null;

return $model;
}
}

0 comments on commit 8f4276f

Please sign in to comment.