Skip to content

Commit

Permalink
Add getPatchValidationModel for PATCH requests
Browse files Browse the repository at this point in the history
  • Loading branch information
nohponex committed Apr 2, 2016
1 parent d176084 commit 9549423
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 33 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"php": ">=7",
"phramework/phramework": "1.*",
"ext-json": "*",
"phramework/util": "dev-master"
"phramework/util": "^0.0.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "*",
Expand All @@ -22,6 +22,7 @@
"satooshi/php-coveralls": "dev-master",
"codacy/coverage": "^1.0"
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"psr-4": {
Expand Down
66 changes: 34 additions & 32 deletions src/Controller/PATCH.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,38 +88,6 @@ protected static function handlePATCH(
$validationCallbacks = [],
$viewCallback = null
) {
if ($viewCallback !== null && !is_callable($viewCallback)) {
throw new ServerException('View callback is not callable!');
}

//Construct a validator
$validator = new ObjectValidator(
(object) [],
[],
false
);

$attributeValidator = $modelClass::getValidationModel()->attributes;

if ($attributeValidator === null) {
//TODO ???
}

foreach ($modelClass::getMutable() as $mutable) {
if (!isset($attributeValidator->properties->{$mutable})) {
throw new \Exception(sprintf(
'Validation model for attribute "%s" is not set!',
$mutable
));
}

//Push property to validator
$validator->addProperty(
$mutable,
$attributeValidator->properties->{$mutable}
);
}

Request::requireParameters($parameters, ['data']);
Request::requireParameters($parameters->data, ['id', 'type']);

Expand All @@ -139,6 +107,40 @@ protected static function handlePATCH(
));
}

if (($patchValidationModel = $modelClass::getPatchValidationModel()) !== null) {
$validator = $patchValidationModel->attributes;
} else {

//Construct a validator
$validator = new ObjectValidator(
(object)[],
[],
false
);

$attributeValidator = $modelClass::getValidationModel()->attributes;

if ($attributeValidator === null) {
//TODO ???
}

foreach ($modelClass::getMutable() as $mutable) {
if (!isset($attributeValidator->properties->{$mutable})) {
throw new \Exception(sprintf(
'Validation model for attribute "%s" is not set!',
$mutable
));
}

//Push property to validator
$validator->addProperty(
$mutable,
$attributeValidator->properties->{$mutable}
);
}
}


if (($requestData = self::getRequestData($parameters)) !== null
&& property_exists($requestData, 'attributes')) {
$requestAttributes = $requestData->attributes;
Expand Down
9 changes: 9 additions & 0 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public static function getValidationModel()
);
}

/**
* When not null, will override getValidationModel, getMutable on PATCH requests
* @return ValidationModel|null
*/
public static function getPatchValidationModel()
{
return null;
}

/**
* Get validation model used for filters, if set for a property, this
* will override the validation model defined in getValidationModel for this
Expand Down

0 comments on commit 9549423

Please sign in to comment.