Skip to content

Commit

Permalink
Close #82
Browse files Browse the repository at this point in the history
  • Loading branch information
neomerx committed Jan 30, 2019
1 parent a20a16e commit e17d3ba
Show file tree
Hide file tree
Showing 19 changed files with 342 additions and 128 deletions.
6 changes: 3 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/phpunit.xml export-ignore
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/tests export-ignore
/perf export-ignore
/sample export-ignore

./sample/composer.json merge=ours
/spec export-ignore
/tests export-ignore
8 changes: 6 additions & 2 deletions src/Contracts/Schema/SchemaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,18 @@ public function getResourceMeta($resource);
/**
* If `self` links should be added in relationships by default.
*
* @param string $relationshipName
*
* @return bool
*/
public function isAddSelfLinkInRelationshipByDefault(): bool;
public function isAddSelfLinkInRelationshipByDefault(string $relationshipName): bool;

/**
* If `related` links should be added in relationships by default.
*
* @param string $relationshipName
*
* @return bool
*/
public function isAddRelatedLinkInRelationshipByDefault(): bool;
public function isAddRelatedLinkInRelationshipByDefault(string $relationshipName): bool;
}
3 changes: 3 additions & 0 deletions src/Encoder/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

/**
* @package Neomerx\JsonApi
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Encoder implements EncoderInterface
{
Expand Down Expand Up @@ -179,6 +181,7 @@ protected static function createFactory(): FactoryInterface
* @return array
*
* @SuppressWarnings(PHPMD.ElseExpression)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function encodeDataToArray($data): array
{
Expand Down
6 changes: 6 additions & 0 deletions src/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@

/**
* @package Neomerx\JsonApi
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Factory implements FactoryInterface
{
Expand Down Expand Up @@ -278,6 +281,9 @@ public function createLink(bool $isSubUrl, string $value, bool $hasMeta, $meta =

/**
* @inheritdoc
*
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function createRelationship(
PositionInterface $position,
Expand Down
52 changes: 51 additions & 1 deletion src/Http/Headers/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function matchesTo(MediaTypeInterface $mediaType): bool
return
$this->isTypeMatches($mediaType) &&
$this->isSubTypeMatches($mediaType) &&
$this->isMediaParametersEqual($mediaType);
$this->isMediaParametersMatch($mediaType);
}

/**
Expand Down Expand Up @@ -179,6 +179,42 @@ private function isSubTypeEquals(MediaTypeInterface $mediaType): bool
return strcasecmp($this->getSubType(), $mediaType->getSubType()) === 0;
}

/**
* @param MediaTypeInterface $mediaType
*
* @return bool
*/
private function isMediaParametersMatch(MediaTypeInterface $mediaType): bool
{
if ($this->bothMediaTypeParamsEmpty($mediaType) === true) {
return true;
} elseif ($this->bothMediaTypeParamsNotEmptyAndEqualInSize($mediaType)) {
// Type, subtype and param name should be compared case-insensitive
// https://tools.ietf.org/html/rfc7231#section-3.1.1.1
$ourParameters = array_change_key_case($this->getParameters());
$parametersToCompare = array_change_key_case($mediaType->getParameters());

// if at least one name are different they are not equal
if (empty(array_diff_key($ourParameters, $parametersToCompare)) === false) {
return false;
}

// If we are here we have to compare values. Also some of the values should be compared case-insensitive
// according to https://tools.ietf.org/html/rfc7231#section-3.1.1.1
// > 'Parameter values might or might not be case-sensitive, depending on
// the semantics of the parameter name.'
foreach ($ourParameters as $name => $value) {
if ($this->paramValuesMatch($name, $value, $parametersToCompare[$name]) === false) {
return false;
}
}

return true;
}

return false;
}

/**
* @param MediaTypeInterface $mediaType
*
Expand Down Expand Up @@ -262,4 +298,18 @@ private function paramValuesEqual(string $name, string $value, string $valueToCo

return $valuesEqual;
}

/**
* @param string $name
* @param string $value
* @param string $valueToCompare
*
* @return bool
*/
private function paramValuesMatch(string $name, string $value, string $valueToCompare): bool
{
$valuesEqual = $valueToCompare === '*' || $this->paramValuesEqual($name, $value, $valueToCompare);

return $valuesEqual;
}
}
2 changes: 2 additions & 0 deletions src/I18n/format.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
* @see Messages::compose
*
* @return string
*
* @SuppressWarnings(PHPMD.StaticAccess)
*/
function format(string $message, ...$parameters): string
{
Expand Down
4 changes: 4 additions & 0 deletions src/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

/**
* @package Neomerx\JsonApi
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Parser implements ParserInterface
{
Expand Down Expand Up @@ -232,6 +234,8 @@ private function parseAsResource(
* @param ResourceInterface $resource
*
* @return iterable
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function parseResource(ResourceInterface $resource): iterable
{
Expand Down
6 changes: 4 additions & 2 deletions src/Parser/RelationshipData/ParseRelationshipLinksTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ trait ParseRelationshipLinksTrait
* @param array $description
*
* @return array
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function parseRelationshipLinks(
SchemaInterface $parentSchema,
Expand All @@ -41,9 +43,9 @@ private function parseRelationshipLinks(
array $description
): array {
$addSelfLink = $description[SchemaInterface::RELATIONSHIP_LINKS_SELF] ??
$parentSchema->isAddSelfLinkInRelationshipByDefault();
$parentSchema->isAddSelfLinkInRelationshipByDefault($name);
$addRelatedLink = $description[SchemaInterface::RELATIONSHIP_LINKS_RELATED] ??
$parentSchema->isAddRelatedLinkInRelationshipByDefault();
$parentSchema->isAddRelatedLinkInRelationshipByDefault($name);
assert(is_bool($addSelfLink) === true || $addSelfLink instanceof LinkInterface);
assert(is_bool($addRelatedLink) === true || $addRelatedLink instanceof LinkInterface);

Expand Down
4 changes: 2 additions & 2 deletions src/Schema/BaseSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ public function getResourceMeta($resource)
/**
* @inheritdoc
*/
public function isAddSelfLinkInRelationshipByDefault(): bool
public function isAddSelfLinkInRelationshipByDefault(string $relationshipName): bool
{
return true;
}

/**
* @inheritdoc
*/
public function isAddRelatedLinkInRelationshipByDefault(): bool
public function isAddRelatedLinkInRelationshipByDefault(string $relationshipName): bool
{
return true;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Schema/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ class Error implements ErrorInterface
* @param array|null $source
* @param bool $hasMeta
* @param mixed $meta
*
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
$idx = null,
Expand Down
Loading

0 comments on commit e17d3ba

Please sign in to comment.