Skip to content

Commit e17d3ba

Browse files
committed
Close #82
1 parent a20a16e commit e17d3ba

File tree

19 files changed

+342
-128
lines changed

19 files changed

+342
-128
lines changed

.gitattributes

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/phpunit.xml export-ignore
88
/.scrutinizer.yml export-ignore
99
/.travis.yml export-ignore
10-
/tests export-ignore
10+
/perf export-ignore
1111
/sample export-ignore
12-
13-
./sample/composer.json merge=ours
12+
/spec export-ignore
13+
/tests export-ignore

src/Contracts/Schema/SchemaInterface.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,18 @@ public function getResourceMeta($resource);
151151
/**
152152
* If `self` links should be added in relationships by default.
153153
*
154+
* @param string $relationshipName
155+
*
154156
* @return bool
155157
*/
156-
public function isAddSelfLinkInRelationshipByDefault(): bool;
158+
public function isAddSelfLinkInRelationshipByDefault(string $relationshipName): bool;
157159

158160
/**
159161
* If `related` links should be added in relationships by default.
160162
*
163+
* @param string $relationshipName
164+
*
161165
* @return bool
162166
*/
163-
public function isAddRelatedLinkInRelationshipByDefault(): bool;
167+
public function isAddRelatedLinkInRelationshipByDefault(string $relationshipName): bool;
164168
}

src/Encoder/Encoder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
/**
3636
* @package Neomerx\JsonApi
37+
*
38+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3739
*/
3840
class Encoder implements EncoderInterface
3941
{
@@ -179,6 +181,7 @@ protected static function createFactory(): FactoryInterface
179181
* @return array
180182
*
181183
* @SuppressWarnings(PHPMD.ElseExpression)
184+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
182185
*/
183186
protected function encodeDataToArray($data): array
184187
{

src/Factories/Factory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151

5252
/**
5353
* @package Neomerx\JsonApi
54+
*
55+
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
56+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
5457
*/
5558
class Factory implements FactoryInterface
5659
{
@@ -278,6 +281,9 @@ public function createLink(bool $isSubUrl, string $value, bool $hasMeta, $meta =
278281

279282
/**
280283
* @inheritdoc
284+
*
285+
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
286+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
281287
*/
282288
public function createRelationship(
283289
PositionInterface $position,

src/Http/Headers/MediaType.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function matchesTo(MediaTypeInterface $mediaType): bool
121121
return
122122
$this->isTypeMatches($mediaType) &&
123123
$this->isSubTypeMatches($mediaType) &&
124-
$this->isMediaParametersEqual($mediaType);
124+
$this->isMediaParametersMatch($mediaType);
125125
}
126126

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

182+
/**
183+
* @param MediaTypeInterface $mediaType
184+
*
185+
* @return bool
186+
*/
187+
private function isMediaParametersMatch(MediaTypeInterface $mediaType): bool
188+
{
189+
if ($this->bothMediaTypeParamsEmpty($mediaType) === true) {
190+
return true;
191+
} elseif ($this->bothMediaTypeParamsNotEmptyAndEqualInSize($mediaType)) {
192+
// Type, subtype and param name should be compared case-insensitive
193+
// https://tools.ietf.org/html/rfc7231#section-3.1.1.1
194+
$ourParameters = array_change_key_case($this->getParameters());
195+
$parametersToCompare = array_change_key_case($mediaType->getParameters());
196+
197+
// if at least one name are different they are not equal
198+
if (empty(array_diff_key($ourParameters, $parametersToCompare)) === false) {
199+
return false;
200+
}
201+
202+
// If we are here we have to compare values. Also some of the values should be compared case-insensitive
203+
// according to https://tools.ietf.org/html/rfc7231#section-3.1.1.1
204+
// > 'Parameter values might or might not be case-sensitive, depending on
205+
// the semantics of the parameter name.'
206+
foreach ($ourParameters as $name => $value) {
207+
if ($this->paramValuesMatch($name, $value, $parametersToCompare[$name]) === false) {
208+
return false;
209+
}
210+
}
211+
212+
return true;
213+
}
214+
215+
return false;
216+
}
217+
182218
/**
183219
* @param MediaTypeInterface $mediaType
184220
*
@@ -262,4 +298,18 @@ private function paramValuesEqual(string $name, string $value, string $valueToCo
262298

263299
return $valuesEqual;
264300
}
301+
302+
/**
303+
* @param string $name
304+
* @param string $value
305+
* @param string $valueToCompare
306+
*
307+
* @return bool
308+
*/
309+
private function paramValuesMatch(string $name, string $value, string $valueToCompare): bool
310+
{
311+
$valuesEqual = $valueToCompare === '*' || $this->paramValuesEqual($name, $value, $valueToCompare);
312+
313+
return $valuesEqual;
314+
}
265315
}

src/I18n/format.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
* @see Messages::compose
2626
*
2727
* @return string
28+
*
29+
* @SuppressWarnings(PHPMD.StaticAccess)
2830
*/
2931
function format(string $message, ...$parameters): string
3032
{

src/Parser/Parser.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
/**
3737
* @package Neomerx\JsonApi
38+
*
39+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3840
*/
3941
class Parser implements ParserInterface
4042
{
@@ -232,6 +234,8 @@ private function parseAsResource(
232234
* @param ResourceInterface $resource
233235
*
234236
* @return iterable
237+
*
238+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
235239
*/
236240
private function parseResource(ResourceInterface $resource): iterable
237241
{

src/Parser/RelationshipData/ParseRelationshipLinksTrait.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ trait ParseRelationshipLinksTrait
3333
* @param array $description
3434
*
3535
* @return array
36+
*
37+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
3638
*/
3739
private function parseRelationshipLinks(
3840
SchemaInterface $parentSchema,
@@ -41,9 +43,9 @@ private function parseRelationshipLinks(
4143
array $description
4244
): array {
4345
$addSelfLink = $description[SchemaInterface::RELATIONSHIP_LINKS_SELF] ??
44-
$parentSchema->isAddSelfLinkInRelationshipByDefault();
46+
$parentSchema->isAddSelfLinkInRelationshipByDefault($name);
4547
$addRelatedLink = $description[SchemaInterface::RELATIONSHIP_LINKS_RELATED] ??
46-
$parentSchema->isAddRelatedLinkInRelationshipByDefault();
48+
$parentSchema->isAddRelatedLinkInRelationshipByDefault($name);
4749
assert(is_bool($addSelfLink) === true || $addSelfLink instanceof LinkInterface);
4850
assert(is_bool($addRelatedLink) === true || $addRelatedLink instanceof LinkInterface);
4951

src/Schema/BaseSchema.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,15 @@ public function getResourceMeta($resource)
128128
/**
129129
* @inheritdoc
130130
*/
131-
public function isAddSelfLinkInRelationshipByDefault(): bool
131+
public function isAddSelfLinkInRelationshipByDefault(string $relationshipName): bool
132132
{
133133
return true;
134134
}
135135

136136
/**
137137
* @inheritdoc
138138
*/
139-
public function isAddRelatedLinkInRelationshipByDefault(): bool
139+
public function isAddRelatedLinkInRelationshipByDefault(string $relationshipName): bool
140140
{
141141
return true;
142142
}

src/Schema/Error.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ class Error implements ErrorInterface
9090
* @param array|null $source
9191
* @param bool $hasMeta
9292
* @param mixed $meta
93+
*
94+
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
95+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
9396
*/
9497
public function __construct(
9598
$idx = null,

0 commit comments

Comments
 (0)