Skip to content

Commit

Permalink
Work on internal model and page
Browse files Browse the repository at this point in the history
- prepare post method for InternalModel
- prepare settings for internal model
- premature design of an abstract data source interface
- complete page methods and tests
  • Loading branch information
nohponex committed May 29, 2016
1 parent 3bb4147 commit 2ac8cc5
Show file tree
Hide file tree
Showing 32 changed files with 480 additions and 3,895 deletions.
22 changes: 11 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
"license": "Apache-2.0",
"homepage": "https://phramework.github.io/",
"type": "library",
"authors": [{
"name": "Xenofon Spafaridis",
"email": "nohponex@gmail.com",
"homepage": "https://nohponex.gr"
}],
"authors": [
{
"name": "Xenofon Spafaridis",
"email": "nohponex@gmail.com",
"homepage": "https://nohponex.gr"
}
],
"require": {
"php": ">=7",
"phramework/phramework": "1.*",
"ext-json": "*",
"phramework/util": "^0.0.0"
"phramework/phramework": "dev-dev-2.x",
"ext-json": "*"
},
"require-dev": {
"squizlabs/php_codesniffer": "*",
"phpunit/phpunit": "5.*",
"apigen/apigen": "^4.1",
"phpdocumentor/phpdocumentor": "2.9",
"satooshi/php-coveralls": "dev-master",
"codacy/coverage": "^1.0"
},
Expand Down Expand Up @@ -49,8 +50,7 @@
],
"lint": "./vendor/bin/phpcs -p -s --standard=PSR2 ./src ./tests",
"doc": [
"apigen generate -s ./src -d ./doc --template-theme bootstrap --todo --tree --deprecated --no-source-code --title \"phramework/jsonapi doc\"",
"start=\"file://\";end=\"doc/index.html\"; echo \"\nOpen $start$(pwd)/$end\" in browser..."
"./vendor/bin/phpdoc run -d ./src,./vendor/phramework -t ./doc"
]
}
}
29 changes: 12 additions & 17 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public function validate(InternalModel $modelClass)

/**
* @param \stdClass $parameters Request parameters
* @param string $modelClass
* @param string $model
* @return Filter|null
* @todo allow strings and integers as id
* @todo Todo use filterValidation model for relationships
Expand Down Expand Up @@ -344,23 +344,23 @@ public function validate(InternalModel $modelClass)
*/
public static function parseFromRequest(
\stdClass $parameters,
InternalModel $modelClass
InternalModel $model
) {
if (!isset($parameters->filter)) {
return null;
}

$filterValidationModel = $modelClass::getFilterValidationModel();
$idAttribute = $modelClass::getIdAttribute();
$filterValidationModel = $model->getFilterValidationModel();
$idAttribute = $model->getIdAttribute();

$filterPrimary = [];
$filterRelationships = new \stdClass();
$filterAttributes = [];

foreach ($parameters->filter as $filterKey => $filterValue) {
if ($filterKey === $modelClass::getType()) { //Filter primary data
if ($filterKey === $model::getType()) { //Filter primary data
//Check filter value type
if (!is_string($filterValue) && !is_integer($filterValue)) {
if (!is_string($filterValue) && !is_int($filterValue)) {
throw new IncorrectParametersException(sprintf(
'String or integer value required for filter "%s"',
$filterKey
Expand All @@ -385,10 +385,10 @@ public static function parseFromRequest(
);

$filterPrimary = $values;
} elseif ($modelClass::relationshipExists($filterKey)) { //Filter relationship data
} elseif ($model::relationshipExists($filterKey)) { //Filter relationship data

//Check filter value type
if (!is_string($filterValue) && !is_integer($filterValue)) {
if (!is_string($filterValue) && !is_int($filterValue)) {
throw new IncorrectParametersException(sprintf(
'String or integer value required for filter "%s"',
$filterKey
Expand All @@ -399,15 +399,10 @@ public static function parseFromRequest(
$values = Operator::OPERATOR_EMPTY;
} else {
//Todo use filterValidation model
//$function = 'intval';

//Split multiples and trim additional spaces and force string
$values = array_map(
'strval',
//array_map(
// $function,
// array_map('trim', explode(',', trim($filterValue)))
//)
array_map('trim', explode(',', trim($filterValue)))
);
}
Expand All @@ -430,9 +425,9 @@ public static function parseFromRequest(

/**
* Merge two filters
* @param Filter|null $first
* @param Filter|null $second
* @return Filter
* @param Filter $first
* @param Filter $second
* @return Filter Merged filter
* @example
* ```php
* //Force additional filters to $filter object
Expand Down Expand Up @@ -468,7 +463,7 @@ public static function merge(Filter $first = null, Filter $second = null) {
$first->primary,
$second->primary
),
(object) array_merge(
(object) array_merge_recursive(
(array) $first->relationships,
(array) $second->relationships
),
Expand Down
2 changes: 1 addition & 1 deletion src/IDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface IDirective
public function validate(InternalModel $model);

/**
* @todo clarify/define request object in phramework
* @todo define request object in phramework
*/
public static function parseFromRequest(
\stdClass $request,
Expand Down
112 changes: 103 additions & 9 deletions src/InternalModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
*/
namespace Phramework\JSONAPI;

use Phramework\JSONAPI\Model\DatabaseDataSource;
use Phramework\JSONAPI\Model\Directives;
use Phramework\JSONAPI\Model\Settings;
use Phramework\Validate\ObjectValidator;

/**
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
Expand Down Expand Up @@ -56,17 +59,43 @@ class InternalModel
protected $get;

/**
* @var ValidationModel
* @todo add all validation models
* @var callable
*/
protected $post;

/**
* @var callable
*/
// protected $patch;

/**
* @var callable
*/
// protected $delete;

//todo add additional request methods ?
/**
* @var \stdClass
*/
protected $validationModels;

/**
* For settings like table, schema etc
* @var Settings
*/
protected $validationModel;
public $settings;

/**
* @var ObjectValidator
*/
public $filterValidator;

/**
* InternalModel constructor.
* Will create a new internal model initialized with:
* - defaultDirectives Page directive limit with value of getMaxPageLimit()
* @param string $resourceType
* @todo set default page limit common ?
*/
public function __construct($resourceType)
{
Expand All @@ -77,6 +106,27 @@ public function __construct($resourceType)
];
$this->relationships = new \stdClass();
$this->filterableAttributes = new \stdClass();

$model = $this; //alias

$this->post = function () use ($model) {
//todo use selected data source
//or add these default on setDataSource if $post is not set
return DatabaseDataSource::post(...array_merge(
[$model],
func_get_args()
));
};

$this->validationModels = new \stdClass();

$this->settings = new Settings();

$this->filterValidator = new ObjectValidator(
(object) [],
[],
false
);
}

/**
Expand Down Expand Up @@ -206,20 +256,46 @@ function ($directive) {
}

/**
* If a validation model for request method is not found, "DEFAULT" will be used
* @param string $requestMethod
* @return ValidationModel
* @throws \DomainException If none validation model is set
*/
public function getValidationModel()
public function getValidationModel(string $requestMethod = 'DEFAULT')
{
return $this->validationModel;
$key = 'DEFAULT';

if ($requestMethod !== null
&& property_exists($this->validationModels, $requestMethod)
) {
$key = $requestMethod;
}

if (!isset($this->validationModels->{$key})) {
throw new \DomainException(
'No validation model is set'
);
}

return $this->validationModels->{$key};
}

/**
* @param ValidationModel $validationModel
* @param string $requestMethod
* @return $this
*/
public function setValidationModel($validationModel)
{
$this->validationModel = $validationModel;
public function setValidationModel(
$validationModel,
string $requestMethod = 'DEFAULT'
) {
$key = 'DEFAULT';

if ($requestMethod !== null) {
$key = $requestMethod;
}

$this->validationModels->{$key} = $validationModel;

return $this;
}
Expand Down Expand Up @@ -297,4 +373,22 @@ public function resource(
);
}

/**
* @return ObjectValidator
*/
public function getFilterValidator() : ObjectValidator
{
return $this->filterValidator;
}

/**
* @param ObjectValidator $filterValidator
* @return $this
*/
public function setFilterValidator(ObjectValidator $filterValidator)
{
$this->filterValidator = $filterValidator;

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -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 @@ -14,33 +14,34 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Phramework\JSONAPI\APP\Models;
namespace Phramework\JSONAPI\Model;

use Phramework\JSONAPI\APP\Models\Article;
use Phramework\JSONAPI\APP\Models\Tag;
use Phramework\JSONAPI\Page;
use Phramework\JSONAPI\IDirective;
use Phramework\JSONAPI\InternalModel;

/**
* @coversDefaultClass Phramework\JSONAPI\Model\Relationship
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
* @since 3.0.0
*/
class RelationshipTest extends \PHPUnit_Framework_TestCase
class DatabaseDataSource implements IDataSource
{
/**
* @covers ::getRelationshipData
*/
public function testGetRelationshipData()
{
$articles = Article::get(new Page(1, 1))[0];

//print_r($articles);
public static function get(
array $directives
) {
// TODO: Implement get() method.
}

$relationship = Tag::getRelationshipArticle(
'1',
null
public static function post(
InternalModel $model,
\stdClass $attributes,
$return = \Phramework\Database\Operations\Create::RETURN_ID
) {
return \Phramework\Database\Operations\Create::create(
$attributes,
$model->settings->get('table' , null),
$model->settings->get('schema', null),
$return
);

//print_r($relationship);
}
}
2 changes: 1 addition & 1 deletion src/Model/Directives.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function setMaxPageLimit(int $maxPageLimit)
* 'status' => Operator::CLASS_COMPARABLE,
* 'title' => Operator::CLASS_COMPARABLE | Operator::CLASS_LIKE,
* ]);
* <code>
* </code>
*/
public function setFilterableAttributes($filterableAttributes)
{
Expand Down
Loading

0 comments on commit 2ac8cc5

Please sign in to comment.