Skip to content

Commit c3d9bd9

Browse files
committed
[Feature] Add put methods to the resource object class
1 parent 8c7c81b commit c3d9bd9

File tree

3 files changed

+110
-30
lines changed

3 files changed

+110
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file. This projec
1010
Eloquent adapters now have a `filterWithScopes()` method, that maps JSON API filters to
1111
model scopes and the Eloquent `where*` method names. This is opt-in: i.e. to use, the
1212
developer must call `filterWithScopes()` within their adapter's `filter()` method.
13+
- Added `put`, `putAttr` and `putRelation` methods to the `ResourceObject` class.
1314

1415
## [1.3.1] - 2019-08-19
1516

src/Document/ResourceObject.php

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,58 @@ public function replace(string $field, $value): self
521521
throw new \OutOfBoundsException("Field {$field} is not an attribute or relationship.");
522522
}
523523

524+
/**
525+
* Set a field.
526+
*
527+
* Sets the provided value as a relation if it is already defined as a relation.
528+
* Otherwise, sets it as an attribute.
529+
*
530+
* @param string $field
531+
* @param mixed|null $value
532+
* @return ResourceObject
533+
*/
534+
public function put(string $field, $value): self
535+
{
536+
if ($this->isRelationship($field)) {
537+
return $this->putRelation($field, $value);
538+
}
539+
540+
return $this->putAttr($field, $value);
541+
}
542+
543+
/**
544+
* Set an attribute.
545+
*
546+
* @param string $field
547+
* @param mixed|null $value
548+
* @return ResourceObject
549+
*/
550+
public function putAttr(string $field, $value): self
551+
{
552+
$copy = clone $this;
553+
$copy->attributes[$field] = $value;
554+
$copy->normalize();
555+
556+
return $copy;
557+
}
558+
559+
/**
560+
* Set a relation.
561+
*
562+
* @param string $field
563+
* @param array|null $value
564+
* @return ResourceObject
565+
*/
566+
public function putRelation(string $field, ?array $value): self
567+
{
568+
$copy = clone $this;
569+
$copy->relationships[$field] = $copy->relationships[$field] ?? [];
570+
$copy->relationships[$field]['data'] = $value;
571+
$copy->normalize();
572+
573+
return $copy;
574+
}
575+
524576
/**
525577
* Convert a validation key to a JSON pointer.
526578
*
@@ -688,33 +740,4 @@ private function putIdentifier(string $type, ?string $id): self
688740
return $copy;
689741
}
690742

691-
/**
692-
* @param string $field
693-
* @param $value
694-
* @return ResourceObject
695-
*/
696-
private function putAttr(string $field, $value): self
697-
{
698-
$copy = clone $this;
699-
$copy->attributes[$field] = $value;
700-
$copy->normalize();
701-
702-
return $copy;
703-
}
704-
705-
/**
706-
* @param string $field
707-
* @param array|null $value
708-
* @return ResourceObject
709-
*/
710-
private function putRelation(string $field, ?array $value): self
711-
{
712-
$copy = clone $this;
713-
$copy->relationships[$field] = $copy->relationships[$field] ?? [];
714-
$copy->relationships[$field]['data'] = $value;
715-
$copy->normalize();
716-
717-
return $copy;
718-
}
719-
720743
}

tests/lib/Unit/Document/ResourceObjectTest.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected function setUp(): void
7474
],
7575
],
7676
];
77-
77+
7878
$this->resource = ResourceObject::create($this->values);
7979
}
8080

@@ -392,6 +392,62 @@ public function testReplaceToMany(): void
392392
$this->assertSame($expected, $actual->toArray());
393393
}
394394

395+
public function testPutAttribute(): void
396+
{
397+
$expected = $this->values;
398+
$expected['attributes']['foobar'] = 'My first post.';
399+
400+
$this->assertNotSame($this->resource, $actual = $this->resource->put('foobar', 'My first post.'));
401+
$this->assertSame($this->values, $this->resource->toArray(), 'original resource is not modified');
402+
$this->assertSame($expected, $actual->toArray());
403+
}
404+
405+
public function testPutArrayAttribute(): void
406+
{
407+
$expected = $this->values;
408+
$expected['attributes']['foobar'] = ['baz', 'bat'];
409+
410+
$this->assertNotSame($this->resource, $actual = $this->resource->put('foobar', ['baz', 'bat']));
411+
$this->assertSame($this->values, $this->resource->toArray(), 'original resource is not modified');
412+
$this->assertSame($expected, $actual->toArray());
413+
}
414+
415+
public function testPutToOne(): void
416+
{
417+
$author = ['type' => 'users', 'id' => '999'];
418+
419+
$expected = $this->values;
420+
$expected['relationships']['foobar']['data'] = $author;
421+
422+
$this->assertNotSame($this->resource, $actual = $this->resource->putRelation('foobar', $author));
423+
$this->assertSame($this->values, $this->resource->toArray(), 'original resource is not modified');
424+
$this->assertSame($expected, $actual->toArray());
425+
}
426+
427+
public function testPutToOneNull(): void
428+
{
429+
$expected = $this->values;
430+
$expected['relationships']['foobar']['data'] = null;
431+
432+
$this->assertNotSame($this->resource, $actual = $this->resource->putRelation('foobar', null));
433+
$this->assertSame($this->values, $this->resource->toArray(), 'original resource is not modified');
434+
$this->assertSame($expected, $actual->toArray());
435+
}
436+
437+
public function testPutToMany(): void
438+
{
439+
$comments = [
440+
['type' => 'comments', 'id' => '123456'],
441+
];
442+
443+
$expected = $this->values;
444+
$expected['relationships']['foobar']['data'] = $comments;
445+
446+
$this->assertNotSame($this->resource, $actual = $this->resource->putRelation('foobar', $comments));
447+
$this->assertSame($this->values, $this->resource->toArray(), 'original resource is not modified');
448+
$this->assertSame($expected, $actual->toArray());
449+
}
450+
395451
public function testWithType(): void
396452
{
397453
$expected = $this->values;

0 commit comments

Comments
 (0)