Skip to content

Commit

Permalink
Update FilterAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
nohponex committed May 31, 2016
1 parent 3c582f3 commit 3267efb
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 144 deletions.
42 changes: 23 additions & 19 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,16 @@ public function validate(InternalModel $model)
$filterable = $model->getFilterableAttributes();

foreach ($this->attributes as $filterAttribute) {
$attribute = $filterAttribute->getAttribute();
$operator = $filterAttribute->getOperator();
$operand = $filterAttribute->getOperand();

$isJSONFilter = ($filterAttribute instanceof FilterJSONAttribute);

if (!property_exists($filterable, $filterAttribute->attribute)) {
if (!property_exists($filterable, $attribute)) {
throw new RequestException(sprintf(
'Filter attribute "%s" not allowed',
$filterAttribute->attribute
$attribute
));
}

Expand All @@ -240,64 +244,64 @@ public function validate(InternalModel $model)
//Attempt to use filter validation model first
if ($filterValidator
//&& isset($filterValidator)
&& isset($filterValidator->properties->{$filterAttribute->attribute})
&& isset($filterValidator->properties->{$attribute})
) {
$attributeValidator =
$filterValidator->properties->{$filterAttribute->attribute};
$filterValidator->properties->{$attribute};
} elseif ($validationModel
&& isset(
$validationModel->attributes,
$validationModel->attributes->properties->{$filterAttribute->attribute})
$validationModel->attributes->properties->{$attribute})
) { //Then attempt to use attribute validation model first
$attributeValidator =
$validationModel->attributes->properties->{$filterAttribute->attribute};
$validationModel->attributes->properties->{$attribute};
} else {
throw new \Exception(sprintf(
'Filter attribute "%s" has not a filter validator',
$filterAttribute->attribute
$attribute
));
}

$operatorClass = $filterable->{$filterAttribute->attribute};
$operatorClass = $filterable->{$attribute};

if ($isJSONFilter && ($operatorClass & Operator::CLASS_JSONOBJECT) === 0) {
throw new RequestException(sprintf(
'Filter attribute "%s" is not accepting JSON object filtering',
$filterAttribute->attribute
$attribute
));
}

//Check if operator is allowed
if (!$isJSONFilter && !in_array(
$filterAttribute->operator,
$operator,
Operator::getByClassFlags($operatorClass)
)) {
throw new RequestException(sprintf(
'Filter operator "%s" is not allowed for attribute "%s"',
$filterAttribute->operator,
$filterAttribute->attribute
$operator,
$attribute
));
}

//Validate filterAttribute operand against filter validator or validator if set
if (!in_array($filterAttribute->operator, Operator::getNullableOperators())) {
if (!in_array($operator, Operator::getNullableOperators())) {
if ($isJSONFilter) {
//If filter validator is set for dereference JSON object property
if ($filterValidator
&& isset($filterValidator->properties->{$filterAttribute->attribute})
&& isset($filterValidator->properties->{$filterAttribute->attribute}
->properties->{$filterAttribute->key})
&& isset($filterValidator->properties->{$attribute})
&& isset($filterValidator->properties->{$attribute}
->properties->{$filterAttribute->getKey()})
) {
$attributePropertyValidator = $filterValidator->properties
->{$filterAttribute->attribute}->properties->{$filterAttribute->key};
->{$attribute}->properties->{$filterAttribute->getKey()};

$attributePropertyValidator->parse($filterAttribute->operand);
$attributePropertyValidator->parse($operand);
}
//} else {
// //**NOTE** Remain unparsed!
//}
} else {
$attributeValidator->parse($filterAttribute->operand);
$attributeValidator->parse($operand);
}
}
}
Expand Down
25 changes: 0 additions & 25 deletions src/FilterAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
* @since 1.0.0
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
* @property-read string $attribute
* @property-read string $operator
* @property-read mixed|null $operand
*/
class FilterAttribute
{
Expand Down Expand Up @@ -145,28 +142,6 @@ public static function parse($filterKey, $filterValue)
return $filterAttributes;
}

/**
* @param string $name
* @return mixed
* @throws \Exception
*/
public function __get($name)
{
switch ($name) {
case 'operand':
return $this->operand;
case 'operator':
return $this->operator;
case 'attribute':
return $this->attribute;
}

throw new \Exception(sprintf(
'Undefined property via __get(): %s',
$name
));
}

/**
* @return string
*/
Expand Down
20 changes: 0 additions & 20 deletions src/FilterJSONAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
* @since 1.0.0
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
* @property-read string $attribute
* @property-read string $operator
* @property-read mixed|null $operand
* @property-read string $key
* todo remove
*/
class FilterJSONAttribute extends FilterAttribute
{
Expand Down Expand Up @@ -56,21 +51,6 @@ public function __construct(
$this->key = $key;
}

/**
* @param string $name
* @return mixed
* @throws \Exception
*/
public function __get($name)
{
switch ($name) {
case 'key':
return $this->key;
}

return parent::__get($name);
}

/**
* @return string
*/
Expand Down
66 changes: 29 additions & 37 deletions src/InternalModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace Phramework\JSONAPI;

use Phramework\JSONAPI\DataSource\DatabaseDataSource;
use Phramework\JSONAPI\DataSource\IDataSource;
use Phramework\JSONAPI\Model\DataSource;
use Phramework\JSONAPI\Model\Directives;
use Phramework\JSONAPI\Model\Relationships;
Expand Down Expand Up @@ -67,9 +68,11 @@ class InternalModel
* InternalModel constructor.
* Will create a new internal model initialized with:
* - defaultDirectives Page directive limit with value of getMaxPageLimit()
* - empty prepareRecords
* @param string $resourceType
* @param IDataSource $dataSource null will interpreted as a new DatabaseDataSource
*/
public function __construct($resourceType)
public function __construct(string $resourceType, IDataSource $dataSource = null)
{
$this->resourceType = $resourceType;

Expand All @@ -80,46 +83,31 @@ public function __construct($resourceType)
$this->filterableAttributes = new \stdClass();

$this->settings = new \stdClass();

$model = $this; //alias

$this->prepareRecords = function (array &$records) {

};

$dataSource = $this->dataSource = new DatabaseDataSource(
$model
);

$this->get = function () use ($dataSource) {
return $dataSource->get(
...func_get_args()
);
//Set default prepareRecords method as an empty method
$this->prepareRecords = function (array &$records) {
};

$this->post = function () use ($dataSource) {
return $dataSource->post(
...func_get_args()
);
};
$this->dataSource = $dataSource;

$this->patch = function () use ($dataSource) {
return $dataSource->patch(
...func_get_args()
if ($dataSource === null) {
//Set default data source
$this->dataSource = $dataSource = new DatabaseDataSource(
$this
);
};

$this->put = function () use ($dataSource) {
return $dataSource->put(
...func_get_args()
);
};
}

//experiment
//Setup default operations to use data source
$this->get = [$dataSource, 'get'];
$this->post = [$dataSource, 'post'];
$this->patch = [$dataSource, 'patch'];
$this->put = [$dataSource, 'put'];
$this->delete = [$dataSource, 'delete'];

$this->validationModels = new \stdClass();

//Set default empty filter validator
$this->filterValidator = new ObjectValidator(
(object) [],
[],
Expand All @@ -133,8 +121,9 @@ public function __construct($resourceType)
* @return ValidationModel
* @throws \DomainException If none validation model is set
*/
public function getValidationModel(string $requestMethod = 'DEFAULT')
{
public function getValidationModel(
string $requestMethod = 'DEFAULT'
) : ValidationModel {
$key = 'DEFAULT';

if ($requestMethod !== null
Expand All @@ -158,7 +147,7 @@ public function getValidationModel(string $requestMethod = 'DEFAULT')
* @return $this
*/
public function setValidationModel(
$validationModel,
ValidationModel $validationModel,
string $requestMethod = 'DEFAULT'
) {
$key = 'DEFAULT';
Expand Down Expand Up @@ -194,17 +183,20 @@ public function setResourceType($resourceType)
/**
* @return string
*/
public function getIdAttribute()
public function getIdAttribute() : string
{
return $this->idAttribute;
}

/**
* @return \stdClass
* @param string $idAttribute If non set, default is "id"
* @return $this
*/
public function getRelationships()
public function setIdAttribute(string $idAttribute)
{
return $this->relationships;
$this->idAttribute = $idAttribute;

return $this;
}

public function collection(
Expand Down

0 comments on commit 3267efb

Please sign in to comment.