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 Laravel\Tinker;
4
 
5
use Exception;
6
use Symfony\Component\VarDumper\Caster\Caster;
7
 
8
class TinkerCaster
9
{
10
    /**
11
     * Application methods to include in the presenter.
12
     *
13
     * @var array
14
     */
15
    private static $appProperties = [
16
        'configurationIsCached',
17
        'environment',
18
        'environmentFile',
19
        'isLocal',
20
        'routesAreCached',
21
        'runningUnitTests',
22
        'version',
23
        'path',
24
        'basePath',
25
        'configPath',
26
        'databasePath',
27
        'langPath',
28
        'publicPath',
29
        'storagePath',
30
        'bootstrapPath',
31
    ];
32
 
33
    /**
34
     * Get an array representing the properties of an application.
35
     *
36
     * @param  \Illuminate\Foundation\Application  $app
37
     * @return array
38
     */
39
    public static function castApplication($app)
40
    {
41
        $results = [];
42
 
43
        foreach (self::$appProperties as $property) {
44
            try {
45
                $val = $app->$property();
46
 
47
                if (! is_null($val)) {
48
                    $results[Caster::PREFIX_VIRTUAL.$property] = $val;
49
                }
50
            } catch (Exception $e) {
51
                //
52
            }
53
        }
54
 
55
        return $results;
56
    }
57
 
58
    /**
59
     * Get an array representing the properties of a collection.
60
     *
61
     * @param  \Illuminate\Support\Collection  $collection
62
     * @return array
63
     */
64
    public static function castCollection($collection)
65
    {
66
        return [
67
            Caster::PREFIX_VIRTUAL.'all' => $collection->all(),
68
        ];
69
    }
70
 
71
    /**
72
     * Get an array representing the properties of an html string.
73
     *
74
     * @param  \Illuminate\Support\HtmlString  $htmlString
75
     * @return array
76
     */
77
    public static function castHtmlString($htmlString)
78
    {
79
        return [
80
            Caster::PREFIX_VIRTUAL.'html' => $htmlString->toHtml(),
81
        ];
82
    }
83
 
84
    /**
85
     * Get an array representing the properties of a fluent string.
86
     *
87
     * @param  \Illuminate\Support\Stringable  $stringable
88
     * @return array
89
     */
90
    public static function castStringable($stringable)
91
    {
92
        return [
93
            Caster::PREFIX_VIRTUAL.'value' => (string) $stringable,
94
        ];
95
    }
96
 
97
    /**
98
     * Get an array representing the properties of a model.
99
     *
100
     * @param  \Illuminate\Database\Eloquent\Model  $model
101
     * @return array
102
     */
103
    public static function castModel($model)
104
    {
105
        $attributes = array_merge(
106
            $model->getAttributes(), $model->getRelations()
107
        );
108
 
109
        $visible = array_flip(
110
            $model->getVisible() ?: array_diff(array_keys($attributes), $model->getHidden())
111
        );
112
 
113
        $hidden = array_flip($model->getHidden());
114
 
115
        $appends = (function () {
116
            return array_combine($this->appends, $this->appends);
117
        })->bindTo($model, $model)();
118
 
119
        foreach ($appends as $appended) {
120
            $attributes[$appended] = $model->{$appended};
121
        }
122
 
123
        $results = [];
124
 
125
        foreach ($attributes as $key => $value) {
126
            $prefix = '';
127
 
128
            if (isset($visible[$key])) {
129
                $prefix = Caster::PREFIX_VIRTUAL;
130
            }
131
 
132
            if (isset($hidden[$key])) {
133
                $prefix = Caster::PREFIX_PROTECTED;
134
            }
135
 
136
            $results[$prefix.$key] = $value;
137
        }
138
 
139
        return $results;
140
    }
141
}