bytic /
data-objects
| 1 | <?php |
||||||||
| 2 | |||||||||
| 3 | namespace ByTIC\DataObjects\Casts\Metadata; |
||||||||
| 4 | |||||||||
| 5 | use ByTIC\DataObjects\Casts\Castable; |
||||||||
| 6 | use ByTIC\DataObjects\Casts\CastsAttributes; |
||||||||
| 7 | use Nip\Utility\Json; |
||||||||
| 8 | |||||||||
| 9 | /** |
||||||||
| 10 | * Class AsMetadataObject |
||||||||
| 11 | * @package ByTIC\DataObjects\Casts\Metadata |
||||||||
| 12 | */ |
||||||||
| 13 | class AsMetadataObject implements Castable |
||||||||
| 14 | { |
||||||||
| 15 | /** |
||||||||
| 16 | * Get the caster class to use when casting from / to this cast target. |
||||||||
| 17 | * |
||||||||
| 18 | * @param array $arguments |
||||||||
| 19 | * @return object|string |
||||||||
| 20 | */ |
||||||||
| 21 | public static function castUsing(array $arguments) |
||||||||
| 22 | { |
||||||||
| 23 | $encoder = $arguments[0] ?? null; |
||||||||
| 24 | $metadataClass = $arguments[1] ?? Metadata::class; |
||||||||
| 25 | |||||||||
| 26 | return new class($encoder, $metadataClass) implements CastsAttributes { |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 27 | |||||||||
| 28 | protected $encoder = 'json'; |
||||||||
| 29 | protected $metadataClass = Metadata::class; |
||||||||
| 30 | |||||||||
| 31 | /** |
||||||||
| 32 | * constructor. |
||||||||
| 33 | * @param $encoder |
||||||||
| 34 | */ |
||||||||
| 35 | public function __construct($encoder, $metadataClass) |
||||||||
| 36 | { |
||||||||
| 37 | $this->encoder = $encoder; |
||||||||
| 38 | $this->metadataClass = $metadataClass; |
||||||||
| 39 | } |
||||||||
| 40 | |||||||||
| 41 | /** |
||||||||
| 42 | * @inheritDoc |
||||||||
| 43 | */ |
||||||||
| 44 | public function get($model, $key, $value, $attributes): Metadata |
||||||||
| 45 | { |
||||||||
| 46 | if (empty($value)) { |
||||||||
| 47 | $value = []; |
||||||||
| 48 | } else { |
||||||||
| 49 | $value = $this->decode($value); |
||||||||
| 50 | } |
||||||||
| 51 | if (!is_array($value)) { |
||||||||
| 52 | $value = []; |
||||||||
| 53 | } |
||||||||
| 54 | return (new $this->metadataClass($value)) |
||||||||
| 55 | ->setObserver( |
||||||||
| 56 | function (Metadata $metadata) use ($model, $key) { |
||||||||
| 57 | $model->set($key, $metadata); |
||||||||
| 58 | } |
||||||||
| 59 | ); |
||||||||
| 60 | } |
||||||||
| 61 | |||||||||
| 62 | /** |
||||||||
| 63 | * @inheritDoc |
||||||||
| 64 | */ |
||||||||
| 65 | public function set($model, $key, $value, $attributes) |
||||||||
| 66 | { |
||||||||
| 67 | if (is_string($value)) { |
||||||||
| 68 | return [$key => $value]; |
||||||||
| 69 | } |
||||||||
| 70 | if ($value instanceof Metadata) { |
||||||||
| 71 | $value = $this->encode($value); |
||||||||
| 72 | } |
||||||||
| 73 | return [$key => $value]; |
||||||||
| 74 | } |
||||||||
| 75 | |||||||||
| 76 | /** |
||||||||
| 77 | * @inheritDoc |
||||||||
| 78 | */ |
||||||||
| 79 | public function serialize($model, string $key, $value, array $attributes) |
||||||||
|
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
$model 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...
|
|||||||||
| 80 | { |
||||||||
| 81 | return $value->getArrayCopy(); |
||||||||
| 82 | } |
||||||||
| 83 | |||||||||
| 84 | /** |
||||||||
| 85 | * @param $value |
||||||||
| 86 | * @return false|string |
||||||||
| 87 | */ |
||||||||
| 88 | protected function encode($value) |
||||||||
| 89 | { |
||||||||
| 90 | if ($this->encoder == 'serialize') { |
||||||||
| 91 | return $value instanceof Metadata ? $value->serialize() : serialize($value); |
||||||||
| 92 | } |
||||||||
| 93 | return json_encode($value); |
||||||||
| 94 | } |
||||||||
| 95 | |||||||||
| 96 | /** |
||||||||
| 97 | * @param $value |
||||||||
| 98 | * @return mixed |
||||||||
| 99 | */ |
||||||||
| 100 | protected function decode($value) |
||||||||
| 101 | { |
||||||||
| 102 | if ($this->encoder == 'serialize') { |
||||||||
| 103 | return unserialize($value); |
||||||||
| 104 | } |
||||||||
| 105 | return Json::decode($value, true); |
||||||||
| 106 | } |
||||||||
| 107 | }; |
||||||||
| 108 | } |
||||||||
| 109 | } |
||||||||
| 110 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: