Skip to content

Commit

Permalink
Modify error view for IncorrectParamtersException
Browse files Browse the repository at this point in the history
  • Loading branch information
nohponex committed Mar 27, 2016
1 parent f113152 commit c516f47
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 19 deletions.
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,25 @@
"Phramework\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Phramework\\Tests\\": "tests"
}
},
"repositories": [],
"require": {
"php": ">=7",
"ext-json": "*",
"phramework/validate": "dev-master#643f183ec41f5e87162d893be457bb98e62287bc",
"phramework/database": "dev-master#68299ecea5b1db0e256a34a6ec7892cff2b5f109",
"phramework/util": "dev-master"
"phramework/exceptions": "^1.0",
"phramework/validate": "^1.0",
"phramework/database": "^1.0",
"phramework/util": "^0.0"
},
"suggest": {
"ext-curl": "*"
},
"require-dev": {
"phpunit/phpunit": "5.*",
"phpunit/phpunit-skeleton-generator": "*",
"squizlabs/php_codesniffer": "*",
"apigen/apigen": "^4.1",
"ext-pdo_sqlite": "*",
Expand Down
53 changes: 39 additions & 14 deletions src/Phramework.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
use Phramework\Exceptions\Source\Pointer;
use Phramework\Exceptions\UnauthorizedException;
use Phramework\Extensions\Translation;
use \Phramework\Models\Request;
use \Phramework\Extensions\StepCallback;
use Phramework\Models\Request;
use Phramework\Extensions\StepCallback;
use Phramework\Route\IRoute;
use Phramework\Util\Util;

Expand Down Expand Up @@ -582,21 +582,46 @@ public function invoke()
$exception
);
} catch (IncorrectParametersException $exception) {

/**
* @var \Exception[]
*/
$incorrectParameters = [];
foreach ($exception->getParameters() as $incorrectParameter) {

$parameters = $exception->getExceptions();

foreach ($parameters as $incorrectParameter) {
//push
$incorrectParameters = (object) [
$o = (object) [
'id' => Phramework::getRequestUUID(),
'status' => $incorrectParameter->getCode(),
'detail' => $exception->getDetail() ?? $incorrectParameter->getMessage(),
//'detail' => $incorrectParameter->getMessage(),
'title' => $incorrectParameter->getMessage(),
'meta' => (object) [
'failure' => $exception->getFailure()
]
//'source' => $incorrectParameter->getSource(),
/*'meta' => (object) [
'failure' => $incorrectParameter->getFailure()
]*/
];

$class = get_class($incorrectParameter);

if ($class == IncorrectParameterException::class) {
$o->source = $incorrectParameter->getSource();
$o->detail = $incorrectParameter->getDetail();
$o->meta = (object) [
'failure' => $incorrectParameter->getFailure()
];
} elseif ($class == MissingParametersException::class) {
$o->source = $incorrectParameter->getSource();
$o->meta = (object) [
'missing' => $incorrectParameter->getParameters()
];
}

$incorrectParameters[] = $o;
}

//var_dump($exception->getParameters()[0]);

self::errorView(
$incorrectParameters,
$exception->getCode(),
Expand Down Expand Up @@ -826,12 +851,12 @@ private static function errorView(
* If requested method is HEAD then the response body will be empty
* Multiple arguments can be set, first argument will always be used as the parameters array.
* Custom IViewer implementation can use these additional parameters at they definition.
* @param object|array $parameters The output parameters.
* @param mixed $arguments The output parameters.
* @return mixed Returns the value returned by the invoked viewer
*/
public static function view($parameters = [])
public static function view(...$arguments)
{
$args = func_get_args();
//$args = func_get_args();

/**
* On HEAD method don't return response body, only the user's object
Expand All @@ -844,10 +869,10 @@ public static function view($parameters = [])
$viewer = new self::$viewer();

//rewrite $parameters to args
$args[0] = $parameters;
//$args[0] = $parameters;

//Call view method
return call_user_func_array([$viewer, 'view'], $args);
return $viewer->view(...$arguments);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/Server/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RewriteEngine On

#Required for URITemplate strategy
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
94 changes: 94 additions & 0 deletions tests/Server/Controllers/AuthorController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Copyright 2015-2016 Xenofon Spafaridis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Phramework\Tests\Server\Controllers;

use Phramework\Exceptions\Source\Pointer;
use Phramework\Models\Request;
use Phramework\Phramework;
use Phramework\Validate\AnyOf;
use Phramework\Validate\ArrayValidator;
use Phramework\Validate\IntegerValidator;
use Phramework\Validate\ObjectValidator;
use Phramework\Validate\StringValidator;

/**
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
*/
class AuthorController
{
public static function GET($params, $method, $headers) {
Phramework::view( (object) [
'data' => 'ok'
]);
}
public static function POST($params, $method, $headers) {
Request::requireParameters(
$params,
'data',
new Pointer('')
);

$validator = (new ObjectValidator(
(object) [
'data' => (new ObjectValidator(
(object) [
'id' => (new StringValidator(
0,
64,
'/^[1-9][0-9]*$/'
)),
'type' => (new StringValidator(
0,
64
))->setEnum(['author']),
'attributes' => new ObjectValidator(
(object) [
'value' => new AnyOf(
new IntegerValidator(),
new ArrayValidator(
1,
2,
new IntegerValidator()
)
)
],
['value']
)
],
['id', 'type', 'attributes']
))
],
['data']
))->setSource(new Pointer(''));

// var_dump($validator->validate($params));

$validator->parse($params);

/* Request::requireParameters(
$params->data,
['id', 'attributes', 'type'],
new Pointer('/data')
);*/


Phramework::view( (object) [
'data' => 'ok'
]);
}
}
59 changes: 59 additions & 0 deletions tests/Server/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright 2015-2016 Xenofon Spafaridis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Phramework\Tests\Server;

use Phramework\Phramework;
use Phramework\Route\URITemplate;
use Phramework\Tests\Server\Controllers\AuthorController;

//Show all errors
error_reporting(E_ALL);
ini_set('display_errors', '1');

include __DIR__ . '/../../vendor/autoload.php';

$APP = function () {
$route = new URITemplate([
[
'author/',
AuthorController::class,
'GET',
Phramework::METHOD_GET
],
[
'author/',
AuthorController::class,
'POST',
Phramework::METHOD_POST
],
]);

$phramework = new Phramework(
[

],
$route
);

Phramework::setViewer(
\Phramework\Viewers\JSON::class
);
//Execute API
$phramework->invoke();
};

$APP();
2 changes: 1 addition & 1 deletion tests/src/PhrameworkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PhrameworkTest extends \PHPUnit_Framework_TestCase
*/
public function setUp()
{
//Prepare phramework instance
//Prepare phramework-old instance
$this->phramework = new Phramework(
[],
new \Phramework\Route\URITemplate([
Expand Down

0 comments on commit c516f47

Please sign in to comment.