Skip to content

Commit 9119cf3

Browse files
committed
Update README.md
1 parent 31e2a38 commit 9119cf3

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

README.md

+34-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Laravel Workflow combines feature details into one class, allowing for action de
1616
- [Define Workflow Logic](#define-workflow-logic)
1717
- [Execute Workflow](#execute-workflow)
1818
- [Conditional Action Execution](#conditional-action-execution)
19+
- [Using DTO for Context](#using-dto-for-context)
1920

2021
## Installation
2122

@@ -47,18 +48,19 @@ php artisan make:workflow-action ValidateCartItems
4748
namespace App\Actions;
4849

4950
use Safemood\Workflow\Action;
51+
use App\DTO\CartDTO;
5052

5153
class ValidateCartItems extends Action
5254
{
53-
public function handle(array &$context)
55+
public function handle(CartDTO &$context)
5456
{
5557
// Simulate extra validation logic
56-
if (empty($context['cart'])) {
58+
if (empty($context->cart)) {
5759
throw new \Exception('Cart is empty');
5860
}
5961

6062
// you can pass data to the next action if you want
61-
$context['validated'] = true;
63+
$context->validated = true;
6264

6365
}
6466
}
@@ -132,19 +134,20 @@ namespace App\Http\Controllers;
132134

133135
use App\Workflows\PaymentWorkflow;
134136
use Illuminate\Http\Request;
137+
use App\DTO\CartDTO;
135138

136139
class PaymentController extends Controller
137140
{
138141
public function payment(Request $request)
139142
{
140-
// Example context data representing a user's cart and user information
141-
$context = [
142-
'cart' => [
143+
// Example context data representing a user's cart and user information
144+
$context = new CartDTO(
145+
[
143146
['id' => 1, 'name' => 'Product A', 'price' => 100, 'quantity' => 2],
144147
['id' => 2, 'name' => 'Product B', 'price' => 50, 'quantity' => 1]
145148
],
146-
'user_id' => 123
147-
];
149+
123
150+
);
148151

149152
// Execute the PaymentWorkflow with the provided context
150153
$paymentWorkflow = (new PaymentWorkflow)->run($context);
@@ -214,3 +217,26 @@ class PaymentWorkflow extends WorkflowManager
214217
}
215218
}
216219
```
220+
221+
## Using DTO for Context
222+
223+
DTOInterface and BaseDTO allow you to handle context type-safely by using DTOs instead of raw arrays. Here's an example of defining a CartDTO class:
224+
225+
```php
226+
<?php
227+
228+
namespace App\DTO;
229+
230+
use Safemood\Workflow\DTO\BaseDTO;
231+
232+
class CartDTO extends BaseDTO
233+
{
234+
public function __construct(
235+
public array $cart,
236+
public int $user_id,
237+
public bool $validated = false
238+
) {}
239+
// BaseDTO already implements the DTOInterface
240+
// Automatically generates getter, setter, and toArray() methods
241+
}
242+
```

0 commit comments

Comments
 (0)