Completed
Push — master ( 61c49f...32fcdc )
by Vagner Luz do
09:05
created

TinkerCaster::castApplication()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 5
eloc 10
nc 8
nop 1
1
<?php
2
3
/*
4
 * This is a partial modification of Illuminate\Framework
5
 * and that is licenced by MIT License (MIT)
6
 * Copyright (c) <Taylor Otwell>
7
 */
8
9
namespace Vluzrmos\Tinker;
10
11
use Exception;
12
use Illuminate\Support\Collection;
13
use Laravel\Lumen\Application;
14
use Illuminate\Database\Eloquent\Model;
15
use Symfony\Component\VarDumper\Caster\Caster;
16
17
/**
18
 * Class TinkerCaster.
19
 */
20
class TinkerCaster
21
{
22
    /**
23
     * Illuminate application methods to include in the presenter.
24
     *
25
     * @var array
26
     */
27
    private static $appProperties = [
28
        'environment',
29
        'basePath',
30
        'databasePath',
31
        'resourcePath',
32
        'storagePath',
33
        'version',
34
        'getRoutes',
35
    ];
36
37
    /**
38
     * Get an array representing the properties of an application.
39
     *
40
     * @param  \Laravel\Lumen\Application  $app
41
     * @return array
42
     */
43
    public static function castApplication(Application $app)
44
    {
45
        $results = [];
46
        foreach (self::$appProperties as $property) {
47
            try {
48
				if (method_exists($app, $property)) {
49
					$val = $app->$property();
50
					
51
					if (! is_null($val)) {
52
						$results[Caster::PREFIX_VIRTUAL.$property] = $val;
53
					}
54
				}
55
            } catch (Exception $e) {
56
                //
57
            }
58
        }
59
60
        return $results;
61
    }
62
63
    /**
64
     * Get an array representing the properties of a collection.
65
     *
66
     * @param \Illuminate\Support\Collection $collection
67
     * @return array
68
     */
69
    public static function castCollection(Collection $collection)
70
    {
71
        return [
72
            Caster::PREFIX_VIRTUAL.'all' => $collection->all(),
73
        ];
74
    }
75
76
    /**
77
     * Get an array representing the properties of a model.
78
     *
79
     * @param  \Illuminate\Database\Eloquent\Model  $model
80
     * @return array
81
     */
82
    public static function castModel(Model $model)
83
    {
84
        $attributes = array_merge(
85
            $model->getAttributes(),
86
            $model->getRelations()
87
        );
88
89
        $visible = array_flip(
90
            $model->getVisible() ?: array_diff(array_keys($attributes), $model->getHidden())
91
        );
92
93
        $results = [];
94
95
        foreach (array_intersect_key($attributes, $visible) as $key => $value) {
96
            $results[(isset($visible[$key]) ? Caster::PREFIX_VIRTUAL : Caster::PREFIX_PROTECTED).$key] = $value;
97
        }
98
99
        return $results;
100
    }
101
}
102