-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
developer
committed
Aug 15, 2018
1 parent
5229bb5
commit 69deae9
Showing
8 changed files
with
287 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php namespace Neomerx\Tests\JsonApi\Data; | ||
|
||
/** | ||
* Copyright 2015-2018 info@neomerx.com | ||
* | ||
* 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. | ||
*/ | ||
|
||
use ArrayAccess; | ||
use ArrayIterator; | ||
use IteratorAggregate; | ||
|
||
/** | ||
* This model emulates a resource which looks like an array (Traversable) with its properties. | ||
* The library should successfully distinguish arrays from resources that may act as arrays. | ||
* | ||
* A real word example of such a resource is Yii CModel https://www.yiiframework.com/doc/api/1.1/CModel | ||
* | ||
* @package Neomerx\Tests\JsonApi | ||
*/ | ||
class AuthorCModel implements ArrayAccess, IteratorAggregate | ||
{ | ||
const ATTRIBUTE_ID = 'author_id'; | ||
const ATTRIBUTE_FIRST_NAME = 'first_name'; | ||
const ATTRIBUTE_LAST_NAME = 'last_name'; | ||
const LINK_COMMENTS = 'comments'; | ||
|
||
/** | ||
* Resource properties. | ||
* | ||
* @var array | ||
*/ | ||
private $properties = []; | ||
|
||
/** | ||
* @param string $identity | ||
* @param string $firstName | ||
* @param string $lastName | ||
* @param array|null $comments | ||
*/ | ||
public function __construct(string $identity, string $firstName, string $lastName, array $comments = null) | ||
{ | ||
$this[self::ATTRIBUTE_ID] = $identity; | ||
$this[self::ATTRIBUTE_FIRST_NAME] = $firstName; | ||
$this[self::ATTRIBUTE_LAST_NAME] = $lastName; | ||
$this[self::LINK_COMMENTS] = $comments; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getIterator() | ||
{ | ||
return new ArrayIterator($this->properties); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function offsetExists($offset) | ||
{ | ||
return array_key_exists($offset, $this->properties); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function offsetGet($offset) | ||
{ | ||
return $this->properties[$offset]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function offsetSet($offset, $value) | ||
{ | ||
$this->properties[$offset] = $value; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function offsetUnset($offset) | ||
{ | ||
unset($this->properties[$offset]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php namespace Neomerx\Tests\JsonApi\Data; | ||
|
||
/** | ||
* Copyright 2015-2018 info@neomerx.com | ||
* | ||
* 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. | ||
*/ | ||
|
||
use Neomerx\JsonApi\Contracts\Document\LinkInterface; | ||
|
||
/** | ||
* @package Neomerx\Tests\JsonApi | ||
*/ | ||
class AuthorCModelSchema extends DevSchema | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
protected $resourceType = 'people'; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getId($author): ?string | ||
{ | ||
return $author[AuthorCModel::ATTRIBUTE_ID]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getAttributes($author, array $fieldKeysFilter = null): ?array | ||
{ | ||
return [ | ||
AuthorCModel::ATTRIBUTE_FIRST_NAME => $author[AuthorCModel::ATTRIBUTE_FIRST_NAME], | ||
AuthorCModel::ATTRIBUTE_LAST_NAME => $author[AuthorCModel::ATTRIBUTE_LAST_NAME], | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getRelationships($author, bool $isPrimary, array $includeRelationships): ?array | ||
{ | ||
assert($author instanceof AuthorCModel); | ||
|
||
if (($isPrimary && $this->isIsLinksInPrimary()) || (!$isPrimary && $this->isIsLinksInIncluded())) { | ||
$selfLink = $this->getRelationshipSelfLink($author, AuthorCModel::LINK_COMMENTS); | ||
$links = [ | ||
AuthorCModel::LINK_COMMENTS => [ | ||
self::LINKS => [LinkInterface::SELF => $selfLink], | ||
self::SHOW_DATA => false, | ||
], | ||
]; | ||
} else { | ||
$links = [ | ||
AuthorCModel::LINK_COMMENTS => [ | ||
// closures for data are supported as well | ||
self::DATA => function () use ($author) { | ||
return isset($author[AuthorCModel::LINK_COMMENTS]) ? | ||
$author[AuthorCModel::LINK_COMMENTS] : null; | ||
}, | ||
], | ||
]; | ||
} | ||
|
||
// NOTE: The line(s) below for testing purposes only. Not for production. | ||
$this->fixLinks($author, $links); | ||
|
||
return $links; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters