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