Replies: 4 comments 2 replies
-
Hi @Tachii, Thanks for the suggestion! I'm leaning towards keeping things lightweight, so I'll be avoiding additional dependencies like spatie/laravel-data. Instead, I'll opt for a custom, generic DTO implementation to maintain simplicity and flexibility without introducing extra dependencies. |
Beta Was this translation helpful? Give feedback.
-
Would it be sufficient if the context type was widened out to include the Arrayable interface? |
Beta Was this translation helpful? Give feedback.
-
I think it might be easiest to put together a small PR. But the general idea was to accept a context value that is array|Arrayable typed and handle turning it into the array representation as necessary in the Trait(s) that have function using the current array only context param. |
Beta Was this translation helpful? Give feedback.
-
Hey everyone, I'm working on a PR to add a global DTOInterface for general use and a BaseDTO class. When BaseDTO is extended, it will automatically generate getters, setters, and implement a toArray method. This allows for minimalistic DTOs. Even after this, you'll still have the freedom to either use the BaseDTO or implement your own approach with the DTOInterface. <?php
interface DTOInterface extends Arrayable
{
}
// Example : ProductDTO
namespace App\DTO;
class ProductDTO extends BaseDTO
{
public bool $inStock;
public function __construct(
public string $name,
public float $price,
public int $quantity,
) {
// Set the default stock status based on the quantity
$this->inStock = $this->quantity > 0;
}
}
// Usage Example
$productDTO = new ProductDTO(name: 'Laptop', price: 999.99, quantity: 5);
// Use the auto generated getters && setters methods
$productDTO->getQuantity(); // Output: 5
$productDTO->setQuantity(3);
$productDTO->setInStock(true);
$productDTO->getInStock() // Output: bool(true)
// Convert the DTO to an array
$productDTO->toArray(); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Basically what the title says.
I think that it would be more usefull if
$context
would be a DTO so you could call$context->param
instead of needing to check array keys like this:issset($data['param') ?:
Pseudo code:
Beta Was this translation helpful? Give feedback.
All reactions