Skip to content

Commit b3ecaef

Browse files
author
Admin
committed
Implement Jsonable to BaseModelFrozenAttributes
1 parent 284297d commit b3ecaef

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ For model properties autocomplete:
170170
echo $client->name; // will print 'text'
171171
// $client changes can happen, but they will not be persisted in the $dto ($client is a stdClass clone)
172172
echo $dto->client->name; // will print 'name'
173+
echo $dto->client->name = 'text'; // will print 'text'
174+
echo $dto->client->name; // will print 'name'
173175
}
174176

175177
foreach (($dto->products ?? []) as $k => $product) {
@@ -179,7 +181,9 @@ For model properties autocomplete:
179181
$product->value = 2; // NO Exception
180182
echo $product->value; // will print 2
181183
// $product changes can happen, but they will not be persisted in the $dto ($product is a stdClass clone)
182-
echo $dto->products[$k]->name; // will print 1
184+
echo $dto->products[$k]->value; // will print 1
185+
echo $dto->products[$k]->value = 2; // will print 2
186+
echo $dto->products[$k]->value; // will print 1
183187
}
184188
}
185189
```

src/Models/Attributes/BaseModelFrozenAttributes.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ class BaseModelFrozenAttributes implements \Stringable, Jsonable
1717
protected bool $escapeWhenCastingToString;
1818
protected bool $returnNullOnInvalidColumnAttributeAccess;
1919

20-
public function __construct(BaseModel $ownerBaseModel) {
20+
public function __construct(BaseModel $ownerBaseModel)
21+
{
2122
$this->mirror = (object)\json_decode(\json_encode($ownerBaseModel->attributesToArray()));
2223
$this->columns = $ownerBaseModel->getColumns();
2324
$this->returnNullOnInvalidColumnAttributeAccess =
2425
$ownerBaseModel->shouldReturnNullOnInvalidColumnAttributeAccess();
25-
$this->escapeWhenCastingToString = $ownerBaseModel->escapeWhenCastingToString;
26+
$this->escapeWhenCastingToString = $ownerBaseModel->shouldEscapeWhenCastingToString();
2627
}
2728

2829
/**

src/Models/BaseModel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,11 @@ public function getAttributeValue($key): mixed
377377
return null;
378378
}
379379

380+
public function shouldEscapeWhenCastingToString(): bool
381+
{
382+
return $this->escapeWhenCastingToString;
383+
}
384+
380385
/**
381386
* This will mass update the whole table if the model does not exist!
382387
* @inheritDoc

0 commit comments

Comments
 (0)