"Fossies" - the Fresh Open Source Software Archive

Member "framework-8.64.0/src/Illuminate/Database/Eloquent/Casts/AsArrayObject.php" (12 Oct 2021, 1032 Bytes) of package /linux/www/laravel-framework-8.64.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AsArrayObject.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Illuminate\Database\Eloquent\Casts;
    4 
    5 use Illuminate\Contracts\Database\Eloquent\Castable;
    6 use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
    7 
    8 class AsArrayObject implements Castable
    9 {
   10     /**
   11      * Get the caster class to use when casting from / to this cast target.
   12      *
   13      * @param  array  $arguments
   14      * @return object|string
   15      */
   16     public static function castUsing(array $arguments)
   17     {
   18         return new class implements CastsAttributes
   19         {
   20             public function get($model, $key, $value, $attributes)
   21             {
   22                 return isset($attributes[$key]) ? new ArrayObject(json_decode($attributes[$key], true)) : null;
   23             }
   24 
   25             public function set($model, $key, $value, $attributes)
   26             {
   27                 return [$key => json_encode($value)];
   28             }
   29 
   30             public function serialize($model, string $key, $value, array $attributes)
   31             {
   32                 return $value->getArrayCopy();
   33             }
   34         };
   35     }
   36 }