Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/Action/ActionAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ abstract class ActionAbstract implements ActionInterface
public function __construct(ContainerInterface $c)
{
$this->container = $c;
if ($c->has('request')) {
$this->request = $c['request'];
}

if ($c->has('response')) {
$this->response = $c['response'];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function export()
*/
public function isEnvironment($environment)
{
return in_array($this->get('app.environment'), (array)$environment);
return strtolower($this->get('app.environment')) === strtolower($environment);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public function __construct(array $values = [])
# Default Request Query Modifier (Do nothing),
# Use your own implementation of RequestQueryModifierInterface
if (!isset($values[self::ENTITY_FACTORY])) {
$this[self::ENTITY_FACTORY] = function () {
return new EntityFactory();
$this[self::ENTITY_FACTORY] = function (ContainerInterface $c) {
return new EntityFactory($c);
};
}

Expand Down
22 changes: 17 additions & 5 deletions src/Entity/EntityFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
namespace Eukles\Entity;

use Eukles\Action\ActionInterface;
use Eukles\Container\ContainerInterface;
use Eukles\Container\ContainerTrait;
use Eukles\Util\PksFinder;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -17,6 +19,18 @@
class EntityFactory implements EntityFactoryInterface
{

use ContainerTrait;

/**
* EntityFactoryInterface constructor.
*
* @param ContainerInterface $c
*/
public function __construct(ContainerInterface $c)
{
$this->container = $c;
}

/**
* Create a new instance of activeRecord and add it to Request attributes
*
Expand All @@ -33,7 +47,7 @@ public function create(
ResponseInterface $response,
callable $next
): ResponseInterface {
$entityRequest = $config->getEntityRequest();
$entityRequest = $config->createEntityRequest($this->container);

# make a new empty record
$obj = $entityRequest->instantiateActiveRecord();
Expand Down Expand Up @@ -77,13 +91,11 @@ public function fetch(
ResponseInterface $response,
callable $next
): ResponseInterface {
$entityRequest = $config->getEntityRequest();
$entityRequest = $config->createEntityRequest($this->container);

# First, we try to determine PK in request path (most common case)
if (isset($request->getAttribute('routeInfo')[2][$config->getRequestParameterName()])) {
$config->getEntityRequest()->setPrimaryKey(
$request->getAttribute('routeInfo')[2][$config->getRequestParameterName()]
);
$entityRequest->setPrimaryKey($request->getAttribute('routeInfo')[2][$config->getRequestParameterName()]);
}

# Next, we create the query (ModelCriteria), based on Action class (which can alter the query)
Expand Down
22 changes: 15 additions & 7 deletions src/Entity/EntityFactoryConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Eukles\Entity;

use Eukles\Container\ContainerInterface;

class EntityFactoryConfig
{
Expand Down Expand Up @@ -44,13 +45,11 @@ class EntityFactoryConfig
* @var string
*/
protected $requestParameterName = "id";

/**
* @var string
*/
protected $type;


/**
* Constructor wrapper
*
Expand All @@ -62,19 +61,29 @@ public static function create()
}

/**
* @return EntityRequestInterface
* @return string
*/
public function getEntityRequest(): EntityRequestInterface
public function getEntityRequest(): string
{
return $this->entityRequest;
}

/**
* @param EntityRequestInterface $entityRequestClass
* @param ContainerInterface $container
*
* @return EntityRequestInterface
*/
public function createEntityRequest(ContainerInterface $container): EntityRequestInterface
{
return new $this->entityRequest($container);
}

/**
* @param string $entityRequestClass Name
*
* @return EntityFactoryConfig
*/
public function setEntityRequest(EntityRequestInterface $entityRequestClass): EntityFactoryConfig
public function setEntityRequest(string $entityRequestClass): EntityFactoryConfig
{
$this->entityRequest = $entityRequestClass;

Expand Down Expand Up @@ -121,7 +130,6 @@ public function setRequestParameterName(string $requestParameterName): EntityFac
return $this;
}


/**
* @return string
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Entity/EntityFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Eukles\Entity;

use Eukles\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

Expand All @@ -13,6 +14,13 @@
interface EntityFactoryInterface
{

/**
* EntityFactoryInterface constructor.
*
* @param ContainerInterface $c
*/
public function __construct(ContainerInterface $c);

/**
* Create a new instance of activeRecord and add it to Request attributes
*
Expand Down
123 changes: 123 additions & 0 deletions src/Service/QueryModifier/Util/EasyFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* Created by PhpStorm.
* User: steve
* Date: 05/01/18
* Time: 12:47
*/

namespace Eukles\Service\QueryModifier\Util;

use Propel\Runtime\ActiveQuery\Criteria;

class EasyFilter extends EasyUtil
{

/**
* @var mixed
*/
protected $value;

/**
* @param $value
*
* @return array
*/
public static function build($value)
{
# Use default operator
$operator = null;

# Handle negate operator
$firstChar = mb_substr($value, 0, 1);
$negate = $firstChar === '!';
if ($negate) {
$value = substr($value, 1);
$operator = Criteria::NOT_EQUAL;
$firstChar = mb_substr($value, 0, 1);
}

# Handle LIKE operator when % is present in value
if ($firstChar === '%' || strpos($value, '%') === strlen($value) - 1) {
$operator = $negate ? Criteria::NOT_LIKE : Criteria::LIKE;
}# Handle min/max operator when [ is present
elseif ($firstChar === '[') {
$operator = null;
$value = substr($value, 1);
$lastChar = substr($value, -1);
if ($lastChar === ']') {
$value = substr($value, 0, -1);
}
$value = explode(',', $value);
if (empty($value[0])) {
$value = null;
} else {
$valueTmp = ['min' => $value[0]];
if (!empty($value[1])) {
$valueTmp['max'] = $value[1];
}
$value = $valueTmp;
}
} # Handle > operators
elseif ($firstChar === '>') {
$value = substr($value, 1);
$operator = Criteria::GREATER_THAN;
if (mb_substr($value, 0, 1) === '=') {
$value = substr($value, 1);
$operator = Criteria::GREATER_EQUAL;
}
} # Handle < operators
elseif ($firstChar === '<') {
$value = substr($value, 1);
$operator = Criteria::LESS_THAN;
if (strpos($value, '=') === 0) {
$value = substr($value, 1);
$operator = Criteria::LESS_EQUAL;
}
} elseif ($firstChar === '"' || $firstChar === "'") {
$value = trim($value, "\"'");
} # Handle IN operator when comma is present
elseif (strpos($value, ',') !== false) {
# IN operator is handled by propel
$operator = $negate ? Criteria::NOT_IN : null;
$value = explode(',', $value);
}

return [$operator, $value];
}

/**
* @param $property
* @param $value
*
* @return bool
*/
public function apply($property, $value): bool
{
$this->value = $value;
$this->property = $property;
if ($this->isAutoUseRelationQuery()) {
return $this->useRelationQuery();
} else {
return $this->filter();
}
}

/**
* @return bool
*/
protected function filter()
{
# Determine if method is callable in Query class
$method = 'filterBy' . ucfirst($this->property);
if (!method_exists($this->query, $method)) {
return false;
}

list($value, $operator) = $this->build($this->value);

$this->query = call_user_func([$this->query, $method], $value, $operator);

return true;
}
}
67 changes: 67 additions & 0 deletions src/Service/QueryModifier/Util/EasySort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Created by PhpStorm.
* User: steve
* Date: 05/01/18
* Time: 12:47
*/

namespace Eukles\Service\QueryModifier\Util;

use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\ActiveQuery\Exception\UnknownColumnException;
use Propel\Runtime\ActiveQuery\Exception\UnknownModelException;

class EasySort extends EasyUtil
{

/**
* @var string
*/
protected $direction;

/**
* @param $property
*
* @return array
*/
public static function build($property)
{
$direction = Criteria::ASC;
if (strpos($property, '+') === 0) {
$property = substr($property, 1);
} elseif (strpos($property, '-') === 0) {
$property = substr($property, 1);
$direction = Criteria::DESC;
}

return [$property, $direction];
}

public function apply($sort): bool
{
list($this->property, $this->direction) = self::build($sort);
if ($this->isAutoUseRelationQuery()) {
return $this->useRelationQuery();
} else {
return $this->filter();
}
}

/**
* @return bool
*/
protected function filter()
{
# Try to call method in Query class
try {
$this->query = $this->query->orderBy(ucfirst($this->property), $this->direction);
} catch (UnknownColumnException $e) {
return false;
} catch (UnknownModelException $e) {
return false;
}

return true;
}
}
Loading