Skip to content

Commit

Permalink
Close #231
Browse files Browse the repository at this point in the history
  • Loading branch information
neomerx committed Mar 22, 2019
1 parent b97c5ac commit 3356153
Show file tree
Hide file tree
Showing 5 changed files with 485 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/Parser/Parser.php
Expand Up @@ -52,6 +52,10 @@ class Parser implements ParserInterface
'For resource of type `%s` with ID `%s` relationship `%s` cannot be parsed because it either ' .
'has `null` or identifier as data. Skipping.';

/** @var string */
public const MSG_PATHS_HAVE_NOT_BEEN_NORMALIZED_YET =
'Paths have not been normalized yet. Have you called `parse` method already?';

/**
* @var SchemaContainerInterface
*/
Expand Down Expand Up @@ -146,6 +150,26 @@ private function parseAsResourcesOrIdentifiers(
}
}

/**
* @return array
*/
protected function getNormalizedPaths(): array
{
\assert($this->paths !== null, _(static::MSG_PATHS_HAVE_NOT_BEEN_NORMALIZED_YET));

return $this->paths;
}

/**
* @param string $path
*
* @return bool
*/
protected function isPathRequested(string $path): bool
{
return isset($this->paths[$path]);
}

/**
* @param PositionInterface $position
* @param mixed $data
Expand Down Expand Up @@ -408,16 +432,6 @@ public function isNull(): bool
};
}

/**
* @param string $path
*
* @return bool
*/
private function isPathRequested(string $path): bool
{
return isset($this->paths[$path]);
}

/**
* @param iterable $paths
*
Expand Down
39 changes: 39 additions & 0 deletions tests/Extensions/Issue231/CustomEncoder.php
@@ -0,0 +1,39 @@
<?php declare(strict_types=1);

namespace Neomerx\Tests\JsonApi\Extensions\Issue231;

/**
* 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
{
/** @var string Special value to be used in include paths */
public const PATH_WILDCARD_ALL = CustomParser::PATH_WILDCARD_ALL;

/**
* @return FactoryInterface
*/
protected static function createFactory(): FactoryInterface
{
return new CustomFactory();
}
}
37 changes: 37 additions & 0 deletions tests/Extensions/Issue231/CustomFactory.php
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

namespace Neomerx\Tests\JsonApi\Extensions\Issue231;

/**
* 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\ParserInterface;
use Neomerx\JsonApi\Contracts\Schema\SchemaContainerInterface;
use Neomerx\JsonApi\Factories\Factory;

/**
* @package Neomerx\Tests\JsonApi
*/
final class CustomFactory extends Factory
{
/**
* @inheritdoc
*/
public function createParser(SchemaContainerInterface $container): ParserInterface
{
return new CustomParser($this, $container);
}
}
79 changes: 79 additions & 0 deletions tests/Extensions/Issue231/CustomParser.php
@@ -0,0 +1,79 @@
<?php declare(strict_types=1);

namespace Neomerx\Tests\JsonApi\Extensions\Issue231;

/**
* 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\Schema\DocumentInterface;
use Neomerx\JsonApi\Parser\Parser;

/**
* @package Neomerx\Tests\JsonApi
*/
final class CustomParser extends Parser
{
/** @var string Special value to be used in include paths */
public const PATH_WILDCARD_ALL = '*';

/**
* @var array
*/
private $cachedPathsResults = [];

/**
* @inheritdoc
*/
public function isPathRequested(string $path): bool
{
if (\array_key_exists($path, $this->cachedPathsResults) === true) {
return $this->cachedPathsResults[$path];
}

$normalizedPaths = $this->getNormalizedPaths();
$result =
isset($normalizedPaths[$path]) ||
isset($normalizedPaths[static::PATH_WILDCARD_ALL]) ||
$this->doesMatchSubPath($path);

$this->cachedPathsResults[$path] = $result;

return $result;
}

/**
* @param string $path
*
* @return bool
*/
private function doesMatchSubPath(string $path): bool
{
$normalizedPaths = $this->getNormalizedPaths();
$separator = DocumentInterface::PATH_SEPARATOR;

// check if any wildcard like a.*, a.b.* is requested
$curPath = '';
foreach (\explode($separator, $path) as $part) {
$curPath .= $part . $separator;
$wildcardPath = $curPath . static::PATH_WILDCARD_ALL;
if (isset($normalizedPaths[$wildcardPath])) {
return true;
}
}

return false;
}
}

0 comments on commit 3356153

Please sign in to comment.