File tree Expand file tree Collapse file tree 4 files changed +30
-11
lines changed Expand file tree Collapse file tree 4 files changed +30
-11
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,9 @@ return new class extends Migration
1111 Schema::create('workflows', function (Blueprint $table) {
1212 $table->id();
1313
14+ // definition class
15+ $table->string('type')->index();
16+
1417 // serialized class
1518 $table->longText('definition');
1619
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Elegantly \Workflow \Concerns ;
4+
5+ use Illuminate \Database \Eloquent \Model ;
6+ use Illuminate \Database \Eloquent \Relations \MorphMany ;
7+
8+ trait HasWorkflows
9+ {
10+ /**
11+ * @return MorphMany<Workflow, Model>
12+ */
13+ public function workflows (): MorphMany
14+ {
15+ return $ this ->morphMany (Workflow::class, 'model ' );
16+ }
17+ }
Original file line number Diff line number Diff line change 1515
1616/**
1717 * @property int $id
18+ * @property string $type Definition class
1819 * @property ?string $step
1920 * @property WorkflowDefinition $definition Serialized Workflow definition
2021 * @property Collection<int, WorkflowItem> $items
@@ -69,12 +70,12 @@ public function model(): MorphTo
6970 }
7071
7172 /**
72- * @return Attribute<WorkflowDefinition, WorkflowDefinition|string >
73+ * @return Attribute<WorkflowDefinition, WorkflowDefinition>
7374 */
7475 protected function definition (): Attribute
7576 {
7677 return Attribute::make (
77- get: function ($ value ) {
78+ get: function (mixed $ value ) {
7879 if (is_string ($ value )) {
7980 return unserialize ($ value );
8081 }
@@ -84,15 +85,11 @@ protected function definition(): Attribute
8485
8586 return null ;
8687 },
87- set: function ($ value ) {
88- if (is_string ($ value )) {
89- return $ value ;
90- }
91- if ($ value instanceof WorkflowDefinition) {
92- return serialize ($ value );
93- }
94-
95- return null ;
88+ set: function (WorkflowDefinition $ value ) {
89+ return [
90+ 'definition ' => serialize ($ value ),
91+ 'type ' => $ value ::class,
92+ ];
9693 },
9794 );
9895 }
Original file line number Diff line number Diff line change 99
1010 $ workflow = $ definition ->start ();
1111
12+ expect ($ workflow ->type )->toBe (TestWorkflowDefinition::class);
13+ expect ($ workflow ->definition )->toBeInstanceOf (TestWorkflowDefinition::class);
1214 expect ($ workflow ->exists )->toBe (true );
1315 expect ($ workflow ->items )->toHaveLength (2 );
1416
You can’t perform that action at this time.
0 commit comments