Skip to content

Commit 77d0b4e

Browse files
committed
Refactor Result class to implement Arrayable and Jsonable interfaces
1 parent 49ca15f commit 77d0b4e

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/Algos/Result.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
namespace Foxws\Algos\Algos;
44

55
use Foxws\Algos\Enums\Status;
6+
use Illuminate\Contracts\Support\Arrayable;
7+
use Illuminate\Contracts\Support\Jsonable;
8+
use Illuminate\Support\Traits\Macroable;
69

7-
class Result
10+
class Result implements Arrayable, Jsonable
811
{
12+
use Macroable;
13+
914
public Algo $algo;
1015

1116
public ?Status $status = null;
@@ -56,4 +61,25 @@ public function with(string $key, mixed $value = null): static
5661

5762
return $this;
5863
}
64+
65+
public function merge(array $meta): static
66+
{
67+
$this->meta = array_merge_recursive($this->meta, $meta);
68+
69+
return $this;
70+
}
71+
72+
public function toJson($options = 0): string
73+
{
74+
return json_encode($this->toArray(), $options);
75+
}
76+
77+
public function toArray(): array
78+
{
79+
return [
80+
'status' => $this->status,
81+
'message' => $this->message,
82+
'meta' => $this->meta,
83+
];
84+
}
5985
}

0 commit comments

Comments
 (0)