Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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
    /**
621 lars 98
     * Get an array representing the properties of a process result.
99
     *
100
     * @param  \Illuminate\Process\ProcessResult  $result
101
     * @return array
102
     */
103
    public static function castProcessResult($result)
104
    {
105
        return [
106
            Caster::PREFIX_VIRTUAL.'output' => $result->output(),
107
            Caster::PREFIX_VIRTUAL.'errorOutput' => $result->errorOutput(),
108
            Caster::PREFIX_VIRTUAL.'exitCode' => $result->exitCode(),
109
            Caster::PREFIX_VIRTUAL.'successful' => $result->successful(),
110
        ];
111
    }
112
 
113
    /**
148 lars 114
     * Get an array representing the properties of a model.
115
     *
116
     * @param  \Illuminate\Database\Eloquent\Model  $model
117
     * @return array
118
     */
119
    public static function castModel($model)
120
    {
121
        $attributes = array_merge(
122
            $model->getAttributes(), $model->getRelations()
123
        );
124
 
125
        $visible = array_flip(
126
            $model->getVisible() ?: array_diff(array_keys($attributes), $model->getHidden())
127
        );
128
 
129
        $hidden = array_flip($model->getHidden());
130
 
131
        $appends = (function () {
132
            return array_combine($this->appends, $this->appends);
133
        })->bindTo($model, $model)();
134
 
135
        foreach ($appends as $appended) {
136
            $attributes[$appended] = $model->{$appended};
137
        }
138
 
139
        $results = [];
140
 
141
        foreach ($attributes as $key => $value) {
142
            $prefix = '';
143
 
144
            if (isset($visible[$key])) {
145
                $prefix = Caster::PREFIX_VIRTUAL;
146
            }
147
 
148
            if (isset($hidden[$key])) {
149
                $prefix = Caster::PREFIX_PROTECTED;
150
            }
151
 
152
            $results[$prefix.$key] = $value;
153
        }
154
 
155
        return $results;
156
    }
157
}