Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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
use Illuminate\Support\Collection;
8
 
9
class AsCollection 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
        return new class implements CastsAttributes
20
        {
21
            public function get($model, $key, $value, $attributes)
22
            {
23
                if (! isset($attributes[$key])) {
24
                    return;
25
                }
26
 
27
                $data = json_decode($attributes[$key], true);
28
 
29
                return is_array($data) ? new Collection($data) : null;
30
            }
31
 
32
            public function set($model, $key, $value, $attributes)
33
            {
34
                return [$key => json_encode($value)];
35
            }
36
        };
37
    }
38
}