Skip to content

Commit

Permalink
fix: adds toJsonResource to valueObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
phpsa committed Mar 4, 2024
1 parent 33bc7d6 commit 7e8af8d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"require-dev": {
"filament/filament": "^3.2",
"phpsa/laravel-api-controller": "*",
"nunomaduro/larastan": "^1.0|^2.0",
"orchestra/testbench": "^7.0|^8.0|^9.0",
"phpunit/phpunit": "^9.0|^10.0"
Expand Down
27 changes: 20 additions & 7 deletions src/ValueObjects/ValueObject.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php
namespace CustomD\LaravelHelpers\ValueObjects;

use CustomD\LaravelHelpers\ValueObjects\Attributes\ChildValueObject;
use CustomD\LaravelHelpers\ValueObjects\Attributes\CollectableValue;
use CustomD\LaravelHelpers\ValueObjects\Attributes\MakeableObject;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Support\Arrayable;
use ReflectionClass;
use ReflectionParameter;
use Illuminate\Support\Collection;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Arr;
use ReflectionParameter;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Contracts\Container\BindingResolutionException;
use CustomD\LaravelHelpers\ValueObjects\Attributes\MakeableObject;
use CustomD\LaravelHelpers\ValueObjects\Attributes\ChildValueObject;
use CustomD\LaravelHelpers\ValueObjects\Attributes\CollectableValue;

/**
* @implements Arrayable<string,mixed>
Expand Down Expand Up @@ -92,12 +92,25 @@ protected function validate(): void
*/
public function toArray(): array
{

return static::getConstructorArgs()
->map(fn(ReflectionParameter $parameter) => $parameter->getName())
->mapWithKeys(fn($property) => [$property => $this->{$property}])
->toArray();
}

public function __toString()
{
return json_encode($this->toArray(), JSON_THROW_ON_ERROR);
}


public function toJsonResource(?string $resource = null): JsonResource
{
$resource ??= JsonResource::class;
return $resource::make($this->toArray());
}


/**
* @param array<string, mixed|ValueObject|Collection> ...$args
Expand Down
38 changes: 38 additions & 0 deletions tests/ValueObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use CustomD\LaravelHelpers\Tests\ValueObjects\SimpleValue;
use CustomD\LaravelHelpers\Tests\ValueObjects\ComplexValue;
use CustomD\LaravelHelpers\Tests\ValueObjects\SimpleValueFormRequest;
use Illuminate\Http\Request;
use Phpsa\LaravelApiController\Http\Resources\ApiResource;

class ValueObjectTest extends TestCase
{
Expand Down Expand Up @@ -95,5 +97,41 @@ public function test_a_complex_value_construct()
$this->assertTrue($value->simpleValues instanceof \Illuminate\Support\Collection);
$this->assertSame(3, $value->simpleValues->count());
$this->assertSame('test', $value->simpleValues->first()->value);
$this->assertTrue($value->simpleValues->first() instanceof \CustomD\LaravelHelpers\Tests\ValueObjects\SimpleValue);
}

public function testing_standard_resource_object()
{
$value = SimpleValue::make('test', 10);
$this->assertInstanceOf(SimpleValue::class, $value);

$this->assertSame(
json_encode(['data' => $value->toArray()]),
$value->toJsonResource()->toResponse(new Request())->content()
);
}

public function test_a_complex_value_construct_with_api_resource()
{
$data = [
'value' => 'test',
'simpleValue' => [
'count' => '11',
'value' => 'test',
],
'address' => [
'street' => '123 Fake St'
],
'constructable' => ['a' => 'b', 'c' => 'd', 'this' => 'is_array'],
'simpleValues' => [
['value' => 'test', 'count' => 11],
['value' => 'test2', 'count' => 13],
['value' => 'test3', 'count' => 15],
]
];
$value = ComplexValue::make(...$data);

$res = json_decode($value->toJsonResource(ApiResource::class)->toResponse(new Request())->content(), true);
$this->assertIsArray($res['data']);
}
}
2 changes: 1 addition & 1 deletion tests/ValueObjects/ComplexValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(
#[MakeableObject(Constructable::class)]
readonly public ?Constructable $constructable = null,
#[CollectableValue(SimpleValue::class)]
readonly ?Collection $simpleValues = null,
readonly public ?Collection $simpleValues = null,
) {
}

Expand Down

0 comments on commit 7e8af8d

Please sign in to comment.