|
2 | 2 |
|
3 | 3 | namespace MacropaySolutions\LaravelCrudWizard\Models\Attributes;
|
4 | 4 |
|
| 5 | +use Illuminate\Contracts\Support\Jsonable; |
5 | 6 | use MacropaySolutions\LaravelCrudWizard\Models\BaseModel;
|
6 | 7 |
|
7 | 8 | /**
|
8 | 9 | * For properties autocompletion declare in the children classes (with @ property) all the model's parameters (columns)
|
9 | 10 | *
|
10 | 11 | * To avoid declaring them twice, put @ mixin ChildBaseModelFrozenAttributes in ChildBaseModelAttributes
|
11 | 12 | */
|
12 |
| -class BaseModelFrozenAttributes implements \Stringable |
| 13 | +class BaseModelFrozenAttributes implements \Stringable, Jsonable |
13 | 14 | {
|
14 | 15 | protected \stdClass $mirror;
|
15 | 16 | protected array $columns;
|
| 17 | + protected bool $escapeWhenCastingToString; |
16 | 18 | protected bool $returnNullOnInvalidColumnAttributeAccess;
|
17 | 19 |
|
18 | 20 | public function __construct(BaseModel $ownerBaseModel) {
|
19 | 21 | $this->mirror = (object)\json_decode(\json_encode($ownerBaseModel->attributesToArray()));
|
20 | 22 | $this->columns = $ownerBaseModel->getColumns();
|
21 | 23 | $this->returnNullOnInvalidColumnAttributeAccess =
|
22 | 24 | $ownerBaseModel->shouldReturnNullOnInvalidColumnAttributeAccess();
|
| 25 | + $this->escapeWhenCastingToString = $ownerBaseModel->escapeWhenCastingToString; |
23 | 26 | }
|
24 | 27 |
|
25 | 28 | /**
|
@@ -68,6 +71,11 @@ public function __isset(string $key): bool
|
68 | 71 |
|
69 | 72 | public function __toString(): string
|
70 | 73 | {
|
71 |
| - return (string)\json_encode($this->mirror); |
| 74 | + return $this->escapeWhenCastingToString ? \e($this->toJson()) : $this->toJson(); |
| 75 | + } |
| 76 | + |
| 77 | + public function toJson($options = 0): string |
| 78 | + { |
| 79 | + return (string)\json_encode($this->mirror, $options); |
72 | 80 | }
|
73 | 81 | }
|
0 commit comments