Skip to content

Commit

Permalink
Fix call to prepareRecord on DatabaseDataSource
Browse files Browse the repository at this point in the history
Prevent warning on empty respose body data at handle post
  • Loading branch information
nohponex committed Sep 27, 2016
1 parent a58efbe commit 73181c1
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Controller/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static function handlePost(
array $directives = []
) : ResponseInterface {
//Request primary data
$data = $request->getParsedBody()->data;
$data = $request->getParsedBody()->data ?? new \stdClass();

/**
* @var bool
Expand Down
2 changes: 1 addition & 1 deletion src/DataSource/DatabaseDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function get(
$query
);

array_walk($records, $this->prepareRecords);
array_walk($records, $this->resourceModel->prepareRecord);

return $this->resourceModel->collection($records, $directives);
}
Expand Down
30 changes: 20 additions & 10 deletions src/Directive/Filter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* Copyright 2015-2016 Xenofon Spafaridis
*
Expand Down Expand Up @@ -58,7 +59,7 @@ class Filter extends Directive
protected $relationships;

/**
* @var (FilterAttributeFilterJSONAttribute)[]
* @var (FilterAttribute|FilterJSONAttribute)[]
*/
protected $attributes = [];

Expand Down Expand Up @@ -159,15 +160,11 @@ public function validate(ResourceModel $model) : bool
*/

//Use filterValidator for idAttribute if set else use unsigned integer validator to parse filtered values
$idAttributeValidator = (
!empty($filterValidator) && isset($filterValidator->properties->{$idAttribute})
? [$filterValidator->properties->{$idAttribute}, 'parse']
: [UnsignedIntegerValidator::class, 'parseStatic']
);
$idAttributeValidator = $model->getIdAttributeValidator();

//Run validator, if any value is incorrect IncorrectParametersException will be thrown
foreach ($this->primary as $id) {
call_user_func($idAttributeValidator, $id);
//Run parse, if any value is incorrect IncorrectParametersException will be thrown
foreach ($this->primary as &$id) {
$id = $idAttributeValidator->parse($id);
}

/**
Expand Down Expand Up @@ -221,13 +218,21 @@ public function validate(ResourceModel $model) : bool
* Validate attributes
*/

/**
* Get dictionary of filterable attributes key => value
* Where key is the attribute and
* value the allowed operator classes (represented as flags)
*/
$filterable = $model->getFilterableAttributes();

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


/**
* @var bool
*/
$isJSONFilter = ($filterAttribute instanceof FilterJSONAttribute);

if (!property_exists($filterable, $attribute)) {
Expand All @@ -240,6 +245,7 @@ public function validate(ResourceModel $model) : bool
$attributeValidator = null;

//Attempt to use filter validation resourceModel first
//todo rewrite with better indentation
if ($filterValidator
//&& isset($filterValidator)
&& isset($filterValidator->properties->{$attribute})
Expand All @@ -254,6 +260,7 @@ public function validate(ResourceModel $model) : bool
$attributeValidator =
$validationModel->attributes->properties->{$attribute};
} else {
//todo add source
throw new \Exception(sprintf(
'Filter attribute "%s" has not a filter validator',
$attribute
Expand All @@ -263,6 +270,7 @@ public function validate(ResourceModel $model) : bool
$operatorClass = $filterable->{$attribute};

if ($isJSONFilter && ($operatorClass & Operator::CLASS_JSONOBJECT) === 0) {
//todo add source
throw new RequestException(sprintf(
'Filter attribute "%s" is not accepting JSON object filtering',
$attribute
Expand All @@ -274,6 +282,7 @@ public function validate(ResourceModel $model) : bool
$operator,
Operator::getByClassFlags($operatorClass)
)) {
//todo add source
throw new RequestException(sprintf(
'Filter operator "%s" is not allowed for attribute "%s"',
$operator,
Expand All @@ -295,6 +304,7 @@ public function validate(ResourceModel $model) : bool

$attributePropertyValidator->parse($operand);
}
//todo
//} else {
// //**NOTE** Remain unparsed!
//}
Expand Down
25 changes: 22 additions & 3 deletions src/Model/DirectivesTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* Copyright 2015-2016 Xenofon Spafaridis
*
Expand All @@ -16,7 +17,6 @@
*/
namespace Phramework\JSONAPI\Model;


use Phramework\JSONAPI\Directive\Directive;

/**
Expand All @@ -33,6 +33,8 @@ trait DirectivesTrait
protected $defaultDirectives;

/**
* @todo check restriction should be applied based on this property
* List of supported directives
* @var string[]
*/
protected $supportedDirectives = [];
Expand All @@ -56,6 +58,10 @@ trait DirectivesTrait
protected $fieldableAtributes = [];

/**
* Attribute that are not returned via the API response,
* they are useful for internal data processing
* Resource parse will store the specified attributes under it's
* private-attributes object member
* @var string[]
*/
protected $privateAttributes = [];
Expand All @@ -67,7 +73,9 @@ trait DirectivesTrait
protected $maxPageLimit = 25000;

/**
* Attributes that are allow filter directive, and specifying allowed operator classes to be applied
* Dictionary of filterable attributes key => value
* Where key is the attribute and
* value the allowed operator classes (represented as flags)
* @var \stdClass
* @example
* <code>
Expand Down Expand Up @@ -138,17 +146,23 @@ class_implements($directiveClassName)
* Returns an array with class names of supported directives
* @return string[]
*/
public function getSupportedDirectives() : array
public function getSupportedDirectives() : array
{
return $this->supportedDirectives;
}

/**
* @param int $maxPageLimit
* @throws \InvalidArgumentException
* @return $this
*/
public function setMaxPageLimit(int $maxPageLimit)
{
if ($maxPageLimit < 1) {
throw new \InvalidArgumentException(
'maxPageLimit must be a positive interger'
);
}
$this->maxPageLimit = $maxPageLimit;

return $this;
Expand All @@ -163,6 +177,9 @@ public function getMaxPageLimit() : int
}

/**
* Get dictionary of filterable attributes key => value
* Where key is the attribute and
* value the allowed operator classes (represented as flags)
* @param \stdClass $filterableAttributes
* @return $this
* @example
Expand Down Expand Up @@ -228,6 +245,7 @@ public function getSortableAttributes() : array
}

/**
* Set the resource's that are allowed to be changed by patch method
* @param string[] $mutableAttributes
* @return $this
*/
Expand All @@ -239,6 +257,7 @@ public function setMutableAttributes(string ...$mutableAttributes)
}

/**
* Get the resource's that are allowed to be changed by patch method
* @return string[]
*/
public function getMutableAttributes() : array
Expand Down
2 changes: 1 addition & 1 deletion src/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function __isset($name)
case 'attributes':
case 'relationships':
case 'meta':
case 'private':
case 'private-attributes':
return isset($this->{$name});
}

Expand Down
1 change: 1 addition & 0 deletions tests/src/Model/VariableTraitTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* Copyright 2015-2016 Xenofon Spafaridis
*
Expand Down

0 comments on commit 73181c1

Please sign in to comment.