Skip to content

Commit

Permalink
Test promise solution for #45
Browse files Browse the repository at this point in the history
Use callable to return the ResourceModel instead of using resource model
as argument at Relationship constructor
Fixes #45
  • Loading branch information
nohponex committed Oct 30, 2016
1 parent f9474cb commit 5d27482
Show file tree
Hide file tree
Showing 11 changed files with 320 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/Directive/FilterAttribute.php
Expand Up @@ -118,7 +118,7 @@ public static function parse($filterKey, $filterValue)
}

//@todo is this required?
$singleFilterValue = urldecode($singleFilterValue);
$singleFilterValue = urldecode((string) $singleFilterValue);

list($operator, $operand) = Operator::parse($singleFilterValue);

Expand Down
20 changes: 12 additions & 8 deletions src/Relationship.php
Expand Up @@ -48,11 +48,6 @@ class Relationship
*/
const TYPE_TO_MANY = 2;

/**
* @var ResourceModel
*/
protected $resourceModel;

/**
* The type of relationship from the resource to relationship resource
* @var int
Expand All @@ -77,6 +72,8 @@ class Relationship
*/
protected $flags;

protected $modelPromise;

/**
* @param ResourceModel $model Class path of relationship resource resourceModel
* @param int $type *[Optional] Relationship type
Expand All @@ -103,7 +100,7 @@ class Relationship
* ```
*/
public function __construct(
ResourceModel $model,
callable $modelPromise,
int $type = Relationship::TYPE_TO_ONE,
string $recordDataAttribute = null,
\stdClass $callbacks = null,
Expand Down Expand Up @@ -131,7 +128,7 @@ public function __construct(
$this->callbacks = $callbacks;
}

$this->resourceModel = $model;
$this->modelPromise = $modelPromise;
$this->type = $type;
$this->recordDataAttribute = $recordDataAttribute;
$this->flags = $flags;
Expand All @@ -142,7 +139,14 @@ public function __construct(
*/
public function getResourceModel()
{
return $this->resourceModel;
$promise = $this->getModelPromise();

return $promise();
}

public function getModelPromise() : callable
{
return $this->modelPromise;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Resource.php
Expand Up @@ -221,6 +221,7 @@ public static function parseFromRecords(
* );
* ```
* @todo what about getRelationshipData method ?
* @todo restore links
*/
public static function parseFromRecord(
\stdClass $record,
Expand All @@ -238,7 +239,6 @@ public static function parseFromRecord(
$flagRelationshipLinks = ($flags & Resource::PARSE_RELATIONSHIP_LINKS) != 0;
$flagRelationshipData = ($flags & Resource::PARSE_RELATIONSHIP_DATA) != 0;


$idAttribute = $model->getIdAttribute();

if (!isset($record->{$idAttribute})) {
Expand Down Expand Up @@ -328,6 +328,7 @@ public static function parseFromRecord(
* @var ResourceModel
*/
$relationshipModel = $relationshipObject->getResourceModel();

$relationshipType = $relationshipObject->getType();
$recordDataAttribute = $relationshipObject->getRecordDataAttribute();

Expand Down
56 changes: 56 additions & 0 deletions tests/APP/Models/A.php
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
/*
* 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\APP\Models;

use Phramework\JSONAPI\APP\DataSource\MemoryDataSource;
use Phramework\JSONAPI\Directive\Directive;
use Phramework\JSONAPI\Relationship;
use Phramework\JSONAPI\ResourceModel;
use Phramework\JSONAPI\Model;
use Phramework\JSONAPI\ModelTrait;

/**
* @since 3.0.0
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
*/
class A extends Model
{
use ModelTrait;

/**
* @inheritDoc
*/
protected static function defineModel() : ResourceModel
{
$model = (new ResourceModel('A', new MemoryDataSource()))
->addVariable('table', 'user')
->setRelationships((object) [
'A' => new Relationship(
function ()
{
return A::getResourceModel();
},
Relationship::TYPE_TO_ONE,
'referrer-user_id'
)
]);

return $model;
}
}
71 changes: 71 additions & 0 deletions tests/APP/Models/Article.php
@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
/*
* 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\APP\Models;

use Phramework\JSONAPI\APP\DataSource\MemoryDataSource;
use Phramework\JSONAPI\Model;
use Phramework\JSONAPI\ModelTrait;
use Phramework\JSONAPI\Relationship;
use Phramework\JSONAPI\ResourceModel;

/**
* @since 3.0.0
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
*/
class Article extends Model
{
use ModelTrait;

protected static function defineModel() : ResourceModel
{
return (new ResourceModel('article', new MemoryDataSource()))
->addVariable('table', 'article')
->setRelationships(
(object) [
'author' => new Relationship(
function () {
return User::getResourceModel();
},
Relationship::TYPE_TO_ONE,
'creator-user_id'
),
'tag' => new Relationship(
function () {
return Tag::getResourceModel();
},
Relationship::TYPE_TO_MANY,
null,//'tag_id'
(object) [
/**
* @param string $articleId
* @return string[]
*/
'GET' => function (string $articleId) {
$ids = [];
return $ids;
}
]
)
]
);
}
}
{

}
22 changes: 21 additions & 1 deletion tests/APP/Models/Tag.php
Expand Up @@ -19,6 +19,7 @@

use Phramework\JSONAPI\APP\DataSource\MemoryDataSource;
use Phramework\JSONAPI\Directive\Directive;
use Phramework\JSONAPI\Relationship;
use Phramework\JSONAPI\ResourceModel;
use Phramework\JSONAPI\Model;
use Phramework\JSONAPI\ModelTrait;
Expand All @@ -38,7 +39,26 @@ class Tag extends Model
protected static function defineModel() : ResourceModel
{
$model = (new ResourceModel('tag', new MemoryDataSource()))
->addVariable('table', 'tag');
->addVariable('table', 'tag')
->setRelationships((object) [
'article' => new Relationship(
function () {
return Article::getResourceModel();
},
Relationship::TYPE_TO_MANY,
null,
(object) [
/**
* @param string $tagId
* @return string[]
*/
'GET' => function (string $tagId) {
$ids = [];
return $ids;
}
]
)
]);

return $model;
}
Expand Down
12 changes: 9 additions & 3 deletions tests/APP/Models/User.php
Expand Up @@ -45,17 +45,23 @@ protected static function defineModel() : ResourceModel
)
->setRelationships((object) [
'group' => new Relationship(
Group::getResourceModel(),
function () {
return Group::getResourceModel();
},
Relationship::TYPE_TO_ONE,
'group_id'
),
'tag' => new Relationship(
Tag::getResourceModel(),
function () {
return Tag::getResourceModel();
},
Relationship::TYPE_TO_MANY,
'tag_id'
),
'company' => new Relationship(
Company::getResourceModel(),
function () {
return Company::getResourceModel();
},
Relationship::TYPE_TO_MANY,
null,
(object) [
Expand Down
24 changes: 22 additions & 2 deletions tests/bootstrap.php
Expand Up @@ -87,6 +87,24 @@
]
);

/**
* company
*/

MemoryDataSource::addTable('article');

MemoryDataSource::insert(
'article',
(object) [
'id' => '1',
'title' => 'Hello world',
'body' => 'Lorem ipsum',
'creator-user_id' => '1',
'tag_id' => ['1', '2']
]
);


/**
* user
*/
Expand All @@ -100,7 +118,8 @@
'username' => 'nohponex',
'email' => 'nohponex@gmail.com',
'group_id' => '1',
'tag_id' => ['1', '2']
'tag_id' => ['1', '2'],
'referrer-user_id' => '1'
]
);

Expand All @@ -111,7 +130,8 @@
'username' => 'nohponex2',
'email' => 'nohponex+2@gmail.com',
'group_id' => '2',
'tag_id' => []
'tag_id' => [],
'referrer-user_id' => '2'
]
);

Expand Down

0 comments on commit 5d27482

Please sign in to comment.