Skip to content

Commit

Permalink
Prepare new controller handlers as traits
Browse files Browse the repository at this point in the history
Introdure new AdditionalParameter directive
Move directives to \Directive namespace
Rename Setting to Variable
  • Loading branch information
nohponex committed Jun 4, 2016
1 parent 993c466 commit 2d9759d
Show file tree
Hide file tree
Showing 58 changed files with 1,432 additions and 471 deletions.
2 changes: 1 addition & 1 deletion src/Controller/Base.php → src/Controller-legact/Base.php
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2015 - 2016 Xenofon Spafaridis
* 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.
Expand Down
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2015 - 2016 Xenofon Spafaridis
* 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.
Expand Down
10 changes: 5 additions & 5 deletions src/Controller/GET.php → src/Controller-legact/GET.php
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2015 - 2016 Xenofon Spafaridis
* 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.
Expand All @@ -16,11 +16,11 @@
*/
namespace Phramework\JSONAPI\Controller;

use Phramework\JSONAPI\Filter;
use Phramework\JSONAPI\Fields;
use Phramework\JSONAPI\Page;
use Phramework\JSONAPI\Directive\Filter;
use Phramework\JSONAPI\Directive\Fields;
use Phramework\JSONAPI\Directive\Page;
use Phramework\JSONAPI\Relationship;
use Phramework\JSONAPI\Sort;
use Phramework\JSONAPI\Directive\Sort;
use Phramework\JSONAPI\Model;
use \Phramework\Models\Request;
use \Phramework\Models\Operator;
Expand Down
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2015 - 2016 Xenofon Spafaridis
* 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.
Expand All @@ -17,7 +17,7 @@
namespace Phramework\JSONAPI\Controller;

use Phramework\Models\Request;
use Phramework\JSONAPI\Fields;
use Phramework\JSONAPI\Directive\Fields;
use Phramework\Exceptions\RequestException;

/**
Expand Down
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2015 - 2016 Xenofon Spafaridis
* 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.
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/POST.php → src/Controller-legact/POST.php
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2015 - 2016 Xenofon Spafaridis
* 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.
Expand Down
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2015 - 2016 Xenofon Spafaridis
* 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.
Expand All @@ -18,7 +18,7 @@

use \Phramework\Models\Request;
use \Phramework\Exceptions\RequestException;
use \Phramework\JSONAPI\Relationship;
use Phramework\JSONAPI\Relationship;

/**
* POST queue item
Expand Down
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2015 - 2016 Xenofon Spafaridis
* 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.
Expand All @@ -17,7 +17,7 @@
namespace Phramework\JSONAPI\Controller;

use Phramework\Exceptions\IncorrectParametersException;
use Phramework\JSONAPI\Fields;
use Phramework\JSONAPI\Directive\Fields;
use Phramework\JSONAPI\Relationship;
use \Phramework\Models\Request;
use \Phramework\Exceptions\RequestException;
Expand Down
210 changes: 210 additions & 0 deletions src/Controller/Controller.php
@@ -0,0 +1,210 @@
<?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\JSONAPI\Controller;

use Phramework\JSONAPI\Directive\Directive;
use Phramework\JSONAPI\InternalModel;
use Phramework\JSONAPI\RelationshipResource;
use Phramework\JSONAPI\Resource;

/**
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
* @since 3.0.0
*/
trait Controller
{
/**
* @param string $id
* @param InternalModel $model
* @param Directive[] ...$directives
* @return Resource|null
*/
protected static function getById(
string $id,
InternalModel $model,
Directive ...$directives
) {
return $model->getById(
$id,
...$directives
);
}

protected static function includeRelationshipResources(
\stdClass $request,
InternalModel $model,
array $resources,
Directive ...$directives
) {
//todo

if ($includeResources === null) {
return null;
}
}

/**
* If !assert then a NotFoundException exceptions is thrown.
*
* @param mixed $assert
* @param string $exceptionMessage [Optional] Default is
* 'Resource not found'
* @throws \Phramework\Exceptions\NotFoundException
*/
public static function assertExists(
$assert,
$exceptionMessage = 'Resource not found'
) {
if (!$assert) {
throw new \Phramework\Exceptions\NotFoundException(
$exceptionMessage
);
}
}

/**
* If !assert then a Exception exception is thrown.
*
* @param mixed $assert
* @param string $exceptionMessage [Optional] Default is 'unknown_error'
*
* @throws \Exception
*/
public static function assertUnknownError(
mixed $assert,
string $exceptionMessage = 'Unknown error'
) {
if (!$assert) {
throw new \Exception($exceptionMessage);
}
}

/**
* @param Resource|Resource[] $data
* @param \stdClass $links
* @param \stdClass $meta
* @param (Resource|RelationshipResource)[] $included
* @return bool
*/
public static function viewData(
$data,
\stdClass $links = null,
\stdClass $meta = null,
array $included = null
) {
$viewParameters = new \stdClass();

if ($links) {
$viewParameters->links = $links;
}

$viewParameters->data = $data;

if ($included !== null) {
$viewParameters->included = $included;
}

if ($meta) {
$viewParameters->meta = $meta;
}

\Phramework\Phramework::view($viewParameters);

unset($viewParameters);

return true;
}

/**
* @param string[] $classes
* @param Directive[] $directives
* @return (Directive|null)[]
*/
public static function getByClasses(
array $classes,
array $directives
) : array {
$list = array_fill(0, count($classes), null);

$directives = array_values($directives); //clean any keys set

foreach ($directives as $directive) {
if (($key = array_search(get_class($directive), $classes, true)) !== false) {
$list[$key] = $directive;
}
}

return $list;
}

/**
* @param string[] $classes
* @param InternalModel $model
* @param \stdClass $request
* @param Directive[] $directives
* @param bool $ignoreIfExists
* @param bool $overwrite
* @return Directive[]
*/
public static function parseDirectives(
array $classes,
InternalModel $model,
\stdClass $request,
array $directives,
bool $ignoreIfExists = true,
bool $overwrite = false
) {
$existClasses = array_map(
function ($d) {
return get_class($d);
},
$directives
);

foreach($classes as $directiveClass) {
if ($ignoreIfExists && in_array($directiveClass, $existClasses)) {
continue;
}

$parsed = $directiveClass::parseFromRequest(
$request,
$model
);


if ($parsed !== null) {
//overwrite
if (
$overwrite
&& (
$key = array_search(
$directiveClass,
$existClasses,
true
) !== false)
) {
$directives[$key] = $parsed;
} else {
$directives[] = $parsed;
}
}
}

return $directives;
}
}
75 changes: 75 additions & 0 deletions src/Controller/Get.php
@@ -0,0 +1,75 @@
<?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\JSONAPI\Controller;

use Phramework\JSONAPI\Directive\Directive;
use Phramework\JSONAPI\Directive\Fields;
use Phramework\JSONAPI\Directive\Filter;
use Phramework\JSONAPI\Directive\IncludeResources;
use Phramework\JSONAPI\Directive\Page;
use Phramework\JSONAPI\Directive\Sort;
use Phramework\JSONAPI\InternalModel;

/**
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
* @since 3.0.0
*/
trait Get
{
use Controller;

public static function handleGet(
\stdClass $request,
InternalModel $model,
array $directives
) {
//Parse request related directives from request
$directives = static::parseDirectives(
[
Fields::class,
IncludeResources::class,
Page::class,
Sort::class,
Filter::class
],
$model,
$request,
$directives
);

$collection = $model->get(
...$directives
);

static::viewData(
$collection,
(object) [
//todo
// 'self' => $model->getSelfLink($id)
],
null,
//todo
static::includeRelationshipResources(
$request,
$model,
$collection,
...$directives
)
);
}
}

0 comments on commit 2d9759d

Please sign in to comment.