Skip to content

Commit

Permalink
Issue #6: Allow Link creation with null attribute.
Browse files Browse the repository at this point in the history
* Issue #6: Allow Link creation with null attribute.

Signed-off-by: alexmerlin <[email protected]>

---------

Signed-off-by: alexmerlin <[email protected]>
  • Loading branch information
alexmerlin committed Apr 20, 2023
1 parent 6df1f87 commit 999952a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,14 @@ private function validateAttributeName($name, string $context): void

/**
* @param mixed $value
* @throws InvalidArgumentException If $value is neither a scalar nor an array.
* @throws InvalidArgumentException If $value is an array, but one or more values
* is not a string.
* @throws InvalidArgumentException If $value is neither a scalar, an array nor null.
* @throws InvalidArgumentException If $value is an array, but one or more values is not a string.
*/
private function validateAttributeValue($value, string $context): void
{
if (! is_scalar($value) && ! is_array($value)) {
if (! is_scalar($value) && ! is_array($value) && $value !== null) {
throw new InvalidArgumentException(sprintf(
'%s expects the $value to be a PHP primitive or array of strings; received %s',
'%s expects the $value to be a PHP primitive, an array of strings or null; received %s',
$context,
is_object($value) ? $value::class : gettype($value)
));
Expand Down
5 changes: 3 additions & 2 deletions test/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ public function testCanConstructLinkWithRelationAndTemplatedFlag(): void

public function testCanConstructLinkWithRelationAndAttributes(): void
{
$link = new Link('self', '', false, ['foo' => 'bar']);
$attributes = ['foo' => 'bar', 'baz' => null];
$link = new Link('self', '', false, $attributes);
$this->assertEquals(['self'], $link->getRels());
$this->assertEquals(['foo' => 'bar'], $link->getAttributes());
$this->assertEquals($attributes, $link->getAttributes());
}

public function testCanConstructFullyPopulatedLink(): void
Expand Down

0 comments on commit 999952a

Please sign in to comment.