dev-think-one /
laravel-json-field-cast
| 1 | <?php |
||||||||
| 2 | |||||||||
| 3 | |||||||||
| 4 | namespace JsonFieldCast\Casts; |
||||||||
| 5 | |||||||||
| 6 | use Illuminate\Contracts\Database\Eloquent\CastsAttributes; |
||||||||
| 7 | |||||||||
| 8 | /** |
||||||||
| 9 | * @psalm-suppress MissingTemplateParam |
||||||||
| 10 | */ |
||||||||
| 11 | abstract class AbstractMeta implements CastsAttributes |
||||||||
| 12 | { |
||||||||
| 13 | 70 | public function get($model, string $key, $value, array $attributes): \JsonFieldCast\Json\AbstractMeta|\JsonFieldCast\Json\ArrayOfJsonObjectsField |
|||||||
| 14 | { |
||||||||
| 15 | 70 | $data = is_string($value) ? json_decode($value, true): $value; |
|||||||
| 16 | 70 | $data = is_array($data) ? $data : []; |
|||||||
| 17 | |||||||||
| 18 | 70 | return $this->newInstance( |
|||||||
| 19 | 70 | $this->getClass($model, $data, $key, $value, $attributes), |
|||||||
| 20 | 70 | $model, |
|||||||
| 21 | 70 | $data, |
|||||||
| 22 | 70 | $key, |
|||||||
| 23 | 70 | $value, |
|||||||
| 24 | 70 | $attributes |
|||||||
| 25 | 70 | ); |
|||||||
| 26 | } |
||||||||
| 27 | |||||||||
| 28 | 69 | public function set($model, string $key, $value, array $attributes): string |
|||||||
| 29 | { |
||||||||
| 30 | 69 | return json_encode($value); |
|||||||
| 31 | } |
||||||||
| 32 | |||||||||
| 33 | 70 | protected function getClass($model, array $data = [], string $key = '', mixed $value = null, array $attributes = []): string |
|||||||
|
0 ignored issues
–
show
The parameter
$key is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$attributes is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||||
| 34 | { |
||||||||
| 35 | 70 | $class = $this->metaClass(); |
|||||||
| 36 | 70 | if (method_exists($class, 'getCastableClassByModel')) { |
|||||||
| 37 | 1 | $class = $class::getCastableClassByModel($model, $data); |
|||||||
| 38 | } |
||||||||
| 39 | |||||||||
| 40 | 70 | return $class; |
|||||||
| 41 | } |
||||||||
| 42 | |||||||||
| 43 | 70 | protected function newInstance(string $class, $model, array $data = [], string $key = '', mixed $value = null, array $attributes = []) |
|||||||
|
0 ignored issues
–
show
The parameter
$value is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$attributes is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$key is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||||
| 44 | { |
||||||||
| 45 | 70 | return new $class($model, $data); |
|||||||
| 46 | } |
||||||||
| 47 | |||||||||
| 48 | abstract protected function metaClass(): string; |
||||||||
| 49 | } |
||||||||
| 50 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.