Skip to content

Commit

Permalink
Close #236
Browse files Browse the repository at this point in the history
  • Loading branch information
neomerx committed May 4, 2019
1 parent 1807c3b commit 95cc4aa
Show file tree
Hide file tree
Showing 10 changed files with 719 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Parser/RelationshipData/ParseRelationshipDataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ trait ParseRelationshipDataTrait
* @param int $nextLevel
* @param string $nextPathPrefix
*
* @return array [has data, parsed data]
* @return array [has data, parsed data, next position]
*/
private function parseRelationshipData(
FactoryInterface $factory,
Expand Down
60 changes: 60 additions & 0 deletions tests/Extensions/Issue236/BaseCustomSchema.php
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.');
}
}
84 changes: 84 additions & 0 deletions tests/Extensions/Issue236/CustomAuthorSchema.php
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),
],
]
];
}
}
80 changes: 80 additions & 0 deletions tests/Extensions/Issue236/CustomCommentSchema.php
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),
],
]
];
}
}
36 changes: 36 additions & 0 deletions tests/Extensions/Issue236/CustomEncoder.php
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();
}
}
41 changes: 41 additions & 0 deletions tests/Extensions/Issue236/CustomFactory.php
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);
}
}
Loading

0 comments on commit 95cc4aa

Please sign in to comment.