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