Skip to content

Commit

Permalink
Add handlePOST viewCallback parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
nohponex committed Feb 7, 2016
1 parent 14e6724 commit f528578
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
29 changes: 20 additions & 9 deletions src/Controller/POST.php
Expand Up @@ -55,7 +55,8 @@ protected static function handlePOST(
$modelClass,
$primaryDataParameters = [],
$relationshipParameters = [],
$validationCallbacks = []
$validationCallbacks = [],
$viewCallback = null
) {
$queue = new \SplQueue();

Expand All @@ -72,7 +73,7 @@ protected static function handlePOST(
}

$requestAttributes = (
isset($resource->attributes) && $resource->attributes
isset($resource->attributes) && $resource->attributes
? $resource->attributes
: new \stdClass()
);
Expand Down Expand Up @@ -135,7 +136,17 @@ protected static function handlePOST(
$ids[] = $id;
}

return $ids; //TODO REMOVE

if ($viewCallback !== null) {
if (!is_callable($viewCallback)) {
throw new \Exception('View callback is not callable!');
}

return call_user_func(
$viewCallback,
$ids
);
}

if (count($ids) === 1) {
//Prepare response with 201 Created status code
Expand Down Expand Up @@ -184,7 +195,7 @@ private static function handlePOSTResource(
$validator = $modelClass::getValidationModel();

$attributesValidator = (
isset($validator->attributes) && $validator->attributes
isset($validator->attributes) && $validator->attributes
? $validator->attributes
: new ObjectValidator()
);
Expand Down Expand Up @@ -318,13 +329,13 @@ private static function handlePOSTResource(

//Parse attributes using relationship's validation model
$parsedRelationshipAttributes =
(
(
isset($validator->relationships)
? $validator->relationships->parse(
? $validator->relationships->parse(
$relationshipAttributes
)
: new \stdClass()
);
: new \stdClass()
);

/**
* Foreach request relationship
Expand All @@ -341,7 +352,7 @@ private static function handlePOSTResource(
$parsedRelationshipValue = $parsedRelationshipAttributes->{$relationshipKey};

$tempIds = (
is_array($parsedRelationshipValue)
is_array($parsedRelationshipValue)
? $parsedRelationshipValue
: [$parsedRelationshipValue]
);
Expand Down
1 change: 0 additions & 1 deletion src/Resource.php
Expand Up @@ -279,7 +279,6 @@ public static function parseFromRecord(
return call_user_func(
$relationshipObject->dataCallback,
$resource->id,
$relationshipKey,
$fields,
$flags // use $flagRelationshipsAttributes to enable/disable parsing of relationship attributes
);
Expand Down
8 changes: 7 additions & 1 deletion tests/APP/Controllers/ArticleController.php
Expand Up @@ -55,7 +55,13 @@ public static function POST($params, $method, $headers)
$params,
$method,
$headers,
Article::class
Article::class,
[],
[],
[],
function ($ids) {
self::viewData(new \stdClass());
}
);
}

Expand Down
1 change: 0 additions & 1 deletion tests/APP/Models/Tag.php
Expand Up @@ -133,7 +133,6 @@ public static function postRelationshipByArticle(
*/
public static function getRelationshipByArticle(
$articleId,
//$relationshipKey,
Fields $fields = null,
$flags = Resource::PARSE_DEFAULT
) {
Expand Down
1 change: 0 additions & 1 deletion tests/src/Model/RelationshipTest.php
Expand Up @@ -38,7 +38,6 @@ public function testGetRelationshipData()

$relationship = Tag::getRelationshipByArticle(
'1',
'tag',
null
);

Expand Down

0 comments on commit f528578

Please sign in to comment.