-
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
Showing
10 changed files
with
719 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Neomerx\Tests\JsonApi\Extensions\Issue236; | ||
|
||
/** | ||
* Copyright 2015-2019 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\Factories\FactoryInterface; | ||
use Neomerx\JsonApi\Schema\BaseSchema; | ||
|
||
/** | ||
* @package Neomerx\Tests\JsonApi | ||
*/ | ||
abstract class BaseCustomSchema extends BaseSchema | ||
{ | ||
use SchemaFieldsTrait; | ||
|
||
/** @var int If data should be really used */ | ||
public const RELATIONSHIP_HAS_DATA = self::RELATIONSHIP_LINKS_RELATED + 1; | ||
|
||
/** | ||
* @param mixed $resource | ||
* @param string $currentPath | ||
* | ||
* @return iterable | ||
*/ | ||
abstract public function getNonHorrificRelationships($resource, string $currentPath): iterable; | ||
|
||
/** | ||
* @param FactoryInterface $factory | ||
* @param SchemaFields $fields | ||
*/ | ||
public function __construct(FactoryInterface $factory, SchemaFields $fields) | ||
{ | ||
parent::__construct($factory); | ||
|
||
$this->setSchemaFields($fields); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getRelationships($resource): iterable | ||
{ | ||
throw new \LogicException('Use `getNonHorrificRelationships` instead.'); | ||
} | ||
} |
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,84 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Neomerx\Tests\JsonApi\Extensions\Issue236; | ||
|
||
/** | ||
* Copyright 2015-2019 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\Tests\JsonApi\Data\Models\Author; | ||
|
||
/** | ||
* @package Neomerx\Tests\JsonApi | ||
*/ | ||
final class CustomAuthorSchema extends BaseCustomSchema | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getType(): string | ||
{ | ||
return 'people'; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getId($resource): ?string | ||
{ | ||
\assert($resource instanceof Author); | ||
|
||
return (string)$resource->{Author::ATTRIBUTE_ID}; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getAttributes($resource): iterable | ||
{ | ||
\assert($resource instanceof Author); | ||
|
||
return [ | ||
Author::ATTRIBUTE_FIRST_NAME => $resource->{Author::ATTRIBUTE_FIRST_NAME}, | ||
Author::ATTRIBUTE_LAST_NAME => $resource->{Author::ATTRIBUTE_LAST_NAME}, | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getNonHorrificRelationships($resource, string $currentPath): iterable | ||
{ | ||
\assert($resource instanceof Author); | ||
|
||
// The whole point of the custom schema is to demonstrate we have access to current path | ||
// of the resource being parsed and associated field filters and include paths. | ||
|
||
return [ | ||
Author::LINK_COMMENTS => [ | ||
self::RELATIONSHIP_LINKS_RELATED => false, | ||
self::RELATIONSHIP_HAS_DATA => false, | ||
self::RELATIONSHIP_DATA => function (): void { | ||
throw new \LogicException('I told you, I don\'t have any data.'); | ||
}, | ||
self::RELATIONSHIP_META => [ | ||
'current_path' => $currentPath, | ||
'fields_filter' => $this->getSchemaFields()->getRequestedFields($this->getType()), | ||
'relationships_to_include' => $this->getSchemaFields()->getRequestedRelationships($currentPath), | ||
], | ||
] | ||
]; | ||
} | ||
} |
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,80 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Neomerx\Tests\JsonApi\Extensions\Issue236; | ||
|
||
/** | ||
* Copyright 2015-2019 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\Tests\JsonApi\Data\Models\Comment; | ||
|
||
/** | ||
* @package Neomerx\Tests\JsonApi | ||
*/ | ||
final class CustomCommentSchema extends BaseCustomSchema | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getType(): string | ||
{ | ||
return 'comments'; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getId($resource): ?string | ||
{ | ||
\assert($resource instanceof Comment); | ||
|
||
return (string)$resource->{Comment::ATTRIBUTE_ID}; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getAttributes($resource): iterable | ||
{ | ||
\assert($resource instanceof Comment); | ||
|
||
return [ | ||
Comment::ATTRIBUTE_BODY => $resource->{Comment::ATTRIBUTE_BODY}, | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getNonHorrificRelationships($resource, string $currentPath): iterable | ||
{ | ||
\assert($resource instanceof Comment); | ||
|
||
// The whole point of the custom schema is to demonstrate we have access to current path | ||
// of the resource being parsed and associated field filters and include paths. | ||
|
||
return [ | ||
Comment::LINK_AUTHOR => [ | ||
self::RELATIONSHIP_DATA => $resource->{Comment::LINK_AUTHOR}, | ||
self::RELATIONSHIP_LINKS_RELATED => false, | ||
self::RELATIONSHIP_META => [ | ||
'current_path' => $currentPath, | ||
'fields_filter' => $this->getSchemaFields()->getRequestedFields($this->getType()), | ||
'relationships_to_include' => $this->getSchemaFields()->getRequestedRelationships($currentPath), | ||
], | ||
] | ||
]; | ||
} | ||
} |
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,36 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Neomerx\Tests\JsonApi\Extensions\Issue236; | ||
|
||
/** | ||
* Copyright 2015-2019 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\Factories\FactoryInterface; | ||
use Neomerx\JsonApi\Encoder\Encoder; | ||
|
||
/** | ||
* @package Neomerx\Tests\JsonApi | ||
*/ | ||
final class CustomEncoder extends Encoder | ||
{ | ||
/** | ||
* @return FactoryInterface | ||
*/ | ||
protected static function createFactory(): FactoryInterface | ||
{ | ||
return new CustomFactory(); | ||
} | ||
} |
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,41 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Neomerx\Tests\JsonApi\Extensions\Issue236; | ||
|
||
/** | ||
* Copyright 2015-2019 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\Parser\PositionInterface; | ||
use Neomerx\JsonApi\Contracts\Parser\ResourceInterface; | ||
use Neomerx\JsonApi\Contracts\Schema\SchemaContainerInterface; | ||
use Neomerx\JsonApi\Factories\Factory; | ||
|
||
/** | ||
* @package Neomerx\Tests\JsonApi | ||
*/ | ||
final class CustomFactory extends Factory | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function createParsedResource( | ||
PositionInterface $position, | ||
SchemaContainerInterface $container, | ||
$data | ||
): ResourceInterface { | ||
return new CustomIdentifierAndResource($position, $this, $container, $data); | ||
} | ||
} |
Oops, something went wrong.