Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement enum json serialization #896

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Enums/CustomCreationMethodType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Spatie\LaravelData\Enums;

enum CustomCreationMethodType
enum CustomCreationMethodType: string
{
case None;
case Object;
case Collection;
case None = 'None';
case Object = 'Object';
case Collection = 'Collection';
}
12 changes: 6 additions & 6 deletions src/Enums/DataCollectableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Spatie\LaravelData\Enums;

enum DataCollectableType
enum DataCollectableType: string
{
case Default;
case Array;
case Collection;
case Paginated;
case CursorPaginated;
case Default = 'Default';
case Array = 'Array';
case Collection = 'Collection';
case Paginated = 'Paginated';
case CursorPaginated = 'CursorPaginated';
}
28 changes: 14 additions & 14 deletions src/Enums/DataTypeKind.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

use Exception;

enum DataTypeKind
enum DataTypeKind: string
{
case Default;
case Array;
case Enumerable;
case Paginator;
case CursorPaginator;
case DataObject;
case DataCollection;
case DataPaginatedCollection;
case DataCursorPaginatedCollection;
case DataArray;
case DataEnumerable;
case DataPaginator;
case DataCursorPaginator;
case Default = 'Default';
case Array = 'Array';
case Enumerable = 'Enumerable';
case Paginator = 'Paginator';
case CursorPaginator = 'CursorPaginator';
case DataObject = 'DataObject';
case DataCollection = 'DataCollection';
case DataPaginatedCollection = 'DataPaginatedCollection';
case DataCursorPaginatedCollection = 'DataCursorPaginatedCollection';
case DataArray = 'DataArray';
case DataEnumerable = 'DataEnumerable';
case DataPaginator = 'DataPaginator';
case DataCursorPaginator = 'DataCursorPaginator';

public function isDataObject(): bool
{
Expand Down
41 changes: 41 additions & 0 deletions tests/ExceptionTraceArgumentsJsonSerializationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Spatie\LaravelData\Data;
use Spatie\LaravelData\Exceptions\CannotCreateData;
use Spatie\LaravelData\Tests\Fakes\NotExtendedDataClass;
use Spatie\LaravelData\Tests\Fakes\SimpleData;

// enable args in exception trace
beforeEach(function () {
ini_set('zend.exception_ignore_args', 'Off');
});

it('can json_encode exception trace with args when not all required properties passed', function () {
try {
SimpleData::from([]);
} catch (CannotCreateData $e) {
$trace = $e->getTrace();
$encodedJson = json_encode($trace, JSON_THROW_ON_ERROR);

expect($encodedJson)->toBeJson();
}
});

it('can json_encode exception trace with args when nested property not extends Data or Dto class', function () {
$dataClass = new class ('', new NotExtendedDataClass('')) extends Data {
public function __construct(
public string $string,
public NotExtendedDataClass $nested
) {
}
};

try {
$dataClass::from(['string' => '', 'nested' => ['name' => '']]);
} catch (TypeError $e) {
$trace = $e->getTrace();
$encodedJson = json_encode($trace, JSON_THROW_ON_ERROR);

expect($encodedJson)->toBeJson();
}
});
11 changes: 11 additions & 0 deletions tests/Fakes/NotExtendedDataClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Spatie\LaravelData\Tests\Fakes;

class NotExtendedDataClass
{
public function __construct(
public string $name
) {
}
}
Loading