-
-
Notifications
You must be signed in to change notification settings - Fork 57
POC: Data objects #37
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6301c02
Added data object for components
joelbutcher fd7d29c
Rename test files and folders to Data
joelbutcher 081fd40
Allow building from JSON strings
joelbutcher 9db658b
Assert data objects can be cast to a JSON string
joelbutcher 526e427
Reword test
joelbutcher 5078d82
Comment formatting
joelbutcher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Cachet\Data; | ||
|
||
use Cachet\Enums\ComponentStatusEnum; | ||
|
||
final class ComponentData extends Data | ||
{ | ||
public function __construct( | ||
public readonly string $name, | ||
public readonly ?string $description = null, | ||
public readonly ?ComponentStatusEnum $status = null, | ||
public readonly ?string $link = null, | ||
public readonly ?int $order = null, | ||
public readonly ?int $componentGroupId = null, | ||
public readonly bool $enabled = true, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace Cachet\Data; | ||
|
||
use Illuminate\Contracts\Support\Arrayable; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Support\Str; | ||
|
||
abstract class Data implements Arrayable, \ArrayAccess, \JsonSerializable, \Stringable | ||
{ | ||
/** | ||
* Create a new data object from a form request. | ||
*/ | ||
final public static function fromRequest(FormRequest $request): static | ||
{ | ||
return static::fromArray(array_filter($request->validated())); | ||
} | ||
|
||
/** | ||
* Create a new data object from an array. | ||
*/ | ||
public static function fromArray(array $data): static | ||
{ | ||
return new static( | ||
...collect($data)->mapWithKeys( | ||
fn ($value, $key) => [ | ||
Str::of($key)->camel()->toString() => $value, | ||
] | ||
) | ||
->all() | ||
); | ||
} | ||
|
||
/** | ||
* Create a new data object from a JSON string. | ||
*/ | ||
public static function fromJson(string $json): static | ||
{ | ||
return static::fromArray( | ||
json_decode($json, associative: true, flags: JSON_THROW_ON_ERROR), | ||
); | ||
} | ||
|
||
/** | ||
* Determine if a given index exists on the data object. | ||
*/ | ||
public function offsetExists(mixed $offset): bool | ||
{ | ||
return isset($this->toArray()[$offset]); | ||
} | ||
|
||
/** | ||
* Retrieve the value of the given index. | ||
*/ | ||
public function offsetGet(mixed $offset): mixed | ||
{ | ||
return $this->toArray()[$offset]; | ||
} | ||
|
||
/** | ||
* Set the value of the given index. | ||
*/ | ||
public function offsetSet(mixed $offset, mixed $value): void | ||
{ | ||
$klass = static::class; | ||
|
||
throw new \Error("Cannot modify readonly property $klass::\${$offset}", code: 2); | ||
} | ||
|
||
/** | ||
* Remove the given index from the data object. | ||
*/ | ||
public function offsetUnset(mixed $offset): void | ||
{ | ||
$klass = static::class; | ||
|
||
throw new \Error("Cannot modify readonly property $klass::\${$offset}", code: 2); | ||
} | ||
|
||
/** | ||
* Get the array representation of the data object. | ||
*/ | ||
final public function toArray(): array | ||
{ | ||
$properties = (new \ReflectionClass($this))->getProperties( | ||
\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_READONLY | ||
); | ||
|
||
return collect($properties)->mapWithKeys( | ||
fn (\ReflectionProperty $property) => [ | ||
Str::of($property->getName())->snake()->toString() => $property->getValue($this), | ||
] | ||
)->all(); | ||
} | ||
|
||
/** | ||
* Get the data to be serialized into JSON. | ||
*/ | ||
public function jsonSerialize(): string | ||
{ | ||
return json_encode($this->toArray()); | ||
} | ||
|
||
/** | ||
* JSON serialize the object into a string. | ||
*/ | ||
public function __toString(): string | ||
{ | ||
return $this->jsonSerialize(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Cachet\Tests\Architecture; | ||
|
||
use Cachet\Data\Data; | ||
|
||
test('data objects test') | ||
->expect('Cachet\Data') | ||
->toBeClasses() | ||
->toBeFinal() | ||
->ignoring(Data::class) | ||
->toExtend(Data::class); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not too happy about the use of
array_filter
here. This assumes that you shouldn't be allowed to update a columns value tonull
. Might have to think about this a bit more…Removing
array_filter
means that columns that aren't passed in the request (e.g.status
) are returned as'status' => null
when calling$data->toArray()
. This doesn't necessarily indicate that the value we want to set in the DB is null, it just indicates that the data was not sent.This raises the discussion around the appropriateness of
PUT
versusPATCH
requests for the API... @jbrooksuk correct me if I'm wrong, but aPUT
request should send all attributes, even if they've not changed andPATCH
only sends the ones we want to update?