Skip to content

Commit c94028b

Browse files
authored
Merge pull request #55 from wollanup/develop
Develop
2 parents 64ba636 + 6176c2c commit c94028b

File tree

20 files changed

+787
-131
lines changed

20 files changed

+787
-131
lines changed

src/Action/ActionAbstract.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ abstract class ActionAbstract implements ActionInterface
3333
public function __construct(ContainerInterface $c)
3434
{
3535
$this->container = $c;
36-
if ($c->has('request')) {
37-
$this->request = $c['request'];
38-
}
36+
3937
if ($c->has('response')) {
4038
$this->response = $c['response'];
4139
}

src/Config/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function export()
3333
*/
3434
public function isEnvironment($environment)
3535
{
36-
return in_array($this->get('app.environment'), (array)$environment);
36+
return strtolower($this->get('app.environment')) === strtolower($environment);
3737
}
3838

3939
/**

src/Container/Container.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public function __construct(array $values = [])
7474
# Default Request Query Modifier (Do nothing),
7575
# Use your own implementation of RequestQueryModifierInterface
7676
if (!isset($values[self::ENTITY_FACTORY])) {
77-
$this[self::ENTITY_FACTORY] = function () {
78-
return new EntityFactory();
77+
$this[self::ENTITY_FACTORY] = function (ContainerInterface $c) {
78+
return new EntityFactory($c);
7979
};
8080
}
8181

src/Entity/EntityFactory.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
namespace Eukles\Entity;
1010

1111
use Eukles\Action\ActionInterface;
12+
use Eukles\Container\ContainerInterface;
13+
use Eukles\Container\ContainerTrait;
1214
use Eukles\Util\PksFinder;
1315
use Psr\Http\Message\ResponseInterface;
1416
use Psr\Http\Message\ServerRequestInterface;
@@ -17,6 +19,18 @@
1719
class EntityFactory implements EntityFactoryInterface
1820
{
1921

22+
use ContainerTrait;
23+
24+
/**
25+
* EntityFactoryInterface constructor.
26+
*
27+
* @param ContainerInterface $c
28+
*/
29+
public function __construct(ContainerInterface $c)
30+
{
31+
$this->container = $c;
32+
}
33+
2034
/**
2135
* Create a new instance of activeRecord and add it to Request attributes
2236
*
@@ -33,7 +47,7 @@ public function create(
3347
ResponseInterface $response,
3448
callable $next
3549
): ResponseInterface {
36-
$entityRequest = $config->getEntityRequest();
50+
$entityRequest = $config->createEntityRequest($this->container);
3751

3852
# make a new empty record
3953
$obj = $entityRequest->instantiateActiveRecord();
@@ -77,13 +91,11 @@ public function fetch(
7791
ResponseInterface $response,
7892
callable $next
7993
): ResponseInterface {
80-
$entityRequest = $config->getEntityRequest();
94+
$entityRequest = $config->createEntityRequest($this->container);
8195

8296
# First, we try to determine PK in request path (most common case)
8397
if (isset($request->getAttribute('routeInfo')[2][$config->getRequestParameterName()])) {
84-
$config->getEntityRequest()->setPrimaryKey(
85-
$request->getAttribute('routeInfo')[2][$config->getRequestParameterName()]
86-
);
98+
$entityRequest->setPrimaryKey($request->getAttribute('routeInfo')[2][$config->getRequestParameterName()]);
8799
}
88100

89101
# Next, we create the query (ModelCriteria), based on Action class (which can alter the query)

src/Entity/EntityFactoryConfig.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Eukles\Entity;
1010

11+
use Eukles\Container\ContainerInterface;
1112

1213
class EntityFactoryConfig
1314
{
@@ -44,13 +45,11 @@ class EntityFactoryConfig
4445
* @var string
4546
*/
4647
protected $requestParameterName = "id";
47-
4848
/**
4949
* @var string
5050
*/
5151
protected $type;
5252

53-
5453
/**
5554
* Constructor wrapper
5655
*
@@ -62,19 +61,29 @@ public static function create()
6261
}
6362

6463
/**
65-
* @return EntityRequestInterface
64+
* @return string
6665
*/
67-
public function getEntityRequest(): EntityRequestInterface
66+
public function getEntityRequest(): string
6867
{
6968
return $this->entityRequest;
7069
}
7170

7271
/**
73-
* @param EntityRequestInterface $entityRequestClass
72+
* @param ContainerInterface $container
73+
*
74+
* @return EntityRequestInterface
75+
*/
76+
public function createEntityRequest(ContainerInterface $container): EntityRequestInterface
77+
{
78+
return new $this->entityRequest($container);
79+
}
80+
81+
/**
82+
* @param string $entityRequestClass Name
7483
*
7584
* @return EntityFactoryConfig
7685
*/
77-
public function setEntityRequest(EntityRequestInterface $entityRequestClass): EntityFactoryConfig
86+
public function setEntityRequest(string $entityRequestClass): EntityFactoryConfig
7887
{
7988
$this->entityRequest = $entityRequestClass;
8089

@@ -121,7 +130,6 @@ public function setRequestParameterName(string $requestParameterName): EntityFac
121130
return $this;
122131
}
123132

124-
125133
/**
126134
* @return string
127135
*/

src/Entity/EntityFactoryInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Eukles\Entity;
44

5+
use Eukles\Container\ContainerInterface;
56
use Psr\Http\Message\ResponseInterface;
67
use Psr\Http\Message\ServerRequestInterface;
78

@@ -13,6 +14,13 @@
1314
interface EntityFactoryInterface
1415
{
1516

17+
/**
18+
* EntityFactoryInterface constructor.
19+
*
20+
* @param ContainerInterface $c
21+
*/
22+
public function __construct(ContainerInterface $c);
23+
1624
/**
1725
* Create a new instance of activeRecord and add it to Request attributes
1826
*
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: steve
5+
* Date: 05/01/18
6+
* Time: 12:47
7+
*/
8+
9+
namespace Eukles\Service\QueryModifier\Util;
10+
11+
use Propel\Runtime\ActiveQuery\Criteria;
12+
13+
class EasyFilter extends EasyUtil
14+
{
15+
16+
/**
17+
* @var mixed
18+
*/
19+
protected $value;
20+
21+
/**
22+
* @param $value
23+
*
24+
* @return array
25+
*/
26+
public static function build($value)
27+
{
28+
# Use default operator
29+
$operator = null;
30+
31+
# Handle negate operator
32+
$firstChar = mb_substr($value, 0, 1);
33+
$negate = $firstChar === '!';
34+
if ($negate) {
35+
$value = substr($value, 1);
36+
$operator = Criteria::NOT_EQUAL;
37+
$firstChar = mb_substr($value, 0, 1);
38+
}
39+
40+
# Handle LIKE operator when % is present in value
41+
if ($firstChar === '%' || strpos($value, '%') === strlen($value) - 1) {
42+
$operator = $negate ? Criteria::NOT_LIKE : Criteria::LIKE;
43+
}# Handle min/max operator when [ is present
44+
elseif ($firstChar === '[') {
45+
$operator = null;
46+
$value = substr($value, 1);
47+
$lastChar = substr($value, -1);
48+
if ($lastChar === ']') {
49+
$value = substr($value, 0, -1);
50+
}
51+
$value = explode(',', $value);
52+
if (empty($value[0])) {
53+
$value = null;
54+
} else {
55+
$valueTmp = ['min' => $value[0]];
56+
if (!empty($value[1])) {
57+
$valueTmp['max'] = $value[1];
58+
}
59+
$value = $valueTmp;
60+
}
61+
} # Handle > operators
62+
elseif ($firstChar === '>') {
63+
$value = substr($value, 1);
64+
$operator = Criteria::GREATER_THAN;
65+
if (mb_substr($value, 0, 1) === '=') {
66+
$value = substr($value, 1);
67+
$operator = Criteria::GREATER_EQUAL;
68+
}
69+
} # Handle < operators
70+
elseif ($firstChar === '<') {
71+
$value = substr($value, 1);
72+
$operator = Criteria::LESS_THAN;
73+
if (strpos($value, '=') === 0) {
74+
$value = substr($value, 1);
75+
$operator = Criteria::LESS_EQUAL;
76+
}
77+
} elseif ($firstChar === '"' || $firstChar === "'") {
78+
$value = trim($value, "\"'");
79+
} # Handle IN operator when comma is present
80+
elseif (strpos($value, ',') !== false) {
81+
# IN operator is handled by propel
82+
$operator = $negate ? Criteria::NOT_IN : null;
83+
$value = explode(',', $value);
84+
}
85+
86+
return [$operator, $value];
87+
}
88+
89+
/**
90+
* @param $property
91+
* @param $value
92+
*
93+
* @return bool
94+
*/
95+
public function apply($property, $value): bool
96+
{
97+
$this->value = $value;
98+
$this->property = $property;
99+
if ($this->isAutoUseRelationQuery()) {
100+
return $this->useRelationQuery();
101+
} else {
102+
return $this->filter();
103+
}
104+
}
105+
106+
/**
107+
* @return bool
108+
*/
109+
protected function filter()
110+
{
111+
# Determine if method is callable in Query class
112+
$method = 'filterBy' . ucfirst($this->property);
113+
if (!method_exists($this->query, $method)) {
114+
return false;
115+
}
116+
117+
list($value, $operator) = $this->build($this->value);
118+
119+
$this->query = call_user_func([$this->query, $method], $value, $operator);
120+
121+
return true;
122+
}
123+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: steve
5+
* Date: 05/01/18
6+
* Time: 12:47
7+
*/
8+
9+
namespace Eukles\Service\QueryModifier\Util;
10+
11+
use Propel\Runtime\ActiveQuery\Criteria;
12+
use Propel\Runtime\ActiveQuery\Exception\UnknownColumnException;
13+
use Propel\Runtime\ActiveQuery\Exception\UnknownModelException;
14+
15+
class EasySort extends EasyUtil
16+
{
17+
18+
/**
19+
* @var string
20+
*/
21+
protected $direction;
22+
23+
/**
24+
* @param $property
25+
*
26+
* @return array
27+
*/
28+
public static function build($property)
29+
{
30+
$direction = Criteria::ASC;
31+
if (strpos($property, '+') === 0) {
32+
$property = substr($property, 1);
33+
} elseif (strpos($property, '-') === 0) {
34+
$property = substr($property, 1);
35+
$direction = Criteria::DESC;
36+
}
37+
38+
return [$property, $direction];
39+
}
40+
41+
public function apply($sort): bool
42+
{
43+
list($this->property, $this->direction) = self::build($sort);
44+
if ($this->isAutoUseRelationQuery()) {
45+
return $this->useRelationQuery();
46+
} else {
47+
return $this->filter();
48+
}
49+
}
50+
51+
/**
52+
* @return bool
53+
*/
54+
protected function filter()
55+
{
56+
# Try to call method in Query class
57+
try {
58+
$this->query = $this->query->orderBy(ucfirst($this->property), $this->direction);
59+
} catch (UnknownColumnException $e) {
60+
return false;
61+
} catch (UnknownModelException $e) {
62+
return false;
63+
}
64+
65+
return true;
66+
}
67+
}

0 commit comments

Comments
 (0)