Completed
Push — master ( 0e9bc5...7ef25f )
by Ryan
15:00
created

_helpers.php ➔ str_humanize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Anomaly\Streams\Platform\Support\Parser;
4
use Anomaly\Streams\Platform\Support\Str;
5
use Anomaly\Streams\Platform\Support\Template;
6
use Anomaly\Streams\Platform\Support\Value;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Collection;
9
10
if (!function_exists('str_humanize')) {
11
12
    /**
13
     * Humanize the string.
14
     *
15
     * @param        $target
16
     * @param string $separator
17
     * @return string
18
     */
19
    function str_humanize($target, $separator = '_')
20
    {
21
        return app(Str::class)->humanize($target, $separator);
22
    }
23
}
24
25
if (!function_exists('parse')) {
26
27
    /**
28
     * Parse the target with data.
29
     *
30
     * @param       $target
31
     * @param array $data
32
     * @return mixed    The parsed target.
33
     */
34
    function parse($target, array $data = [])
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
35
    {
36
        return app(Parser::class)->parse($target, $data);
37
    }
38
}
39
40
if (!function_exists('render')) {
41
42
    /**
43
     * Render the string template.
44
     *
45
     * @param       $template
46
     * @param array $payload
47
     * @return string
48
     */
49
    function render($template, array $payload = [])
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
50
    {
51
        return app(Template::class)->render($template, $payload);
52
    }
53
}
54
55
if (!function_exists('valuate')) {
56
57
    /**
58
     * Make a valuation.
59
     *
60
     * @param        $parameters
61
     * @param        $entry
62
     * @param string $term
63
     * @param array  $payload
64
     * @return mixed
65
     */
66
    function valuate($parameters, $entry, $term = 'entry', $payload = [])
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
    {
68
        return app(Value::class)->make($parameters, $entry, $term, $payload);
69
    }
70
}
71
72
if (!function_exists('data')) {
73
74
    /**
75
     * Get an item from an array or object using "dot" notation.
76
     *
77
     * @param  mixed        $target
78
     * @param  string|array $key
79
     * @param  mixed        $default
80
     * @return mixed
81
     */
82
    function data($target, $key, $default = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
83
    {
84
        if (is_null($key)) {
85
            return $target;
86
        }
87
88
        $key = is_array($key) ? $key : explode('.', $key);
89
90
        while (!is_null($segment = array_shift($key))) {
91
            if ($segment === '*') {
92
                if ($target instanceof Collection) {
93
                    $target = $target->all();
94
                } elseif (!is_array($target)) {
95
                    return value($default);
96
                }
97
98
                $result = Arr::pluck($target, $key);
99
100
                return in_array('*', $key) ? Arr::collapse($result) : $result;
101
            }
102
103
            if (Arr::accessible($target) && Arr::exists($target, $segment)) {
104
                $target = $target[$segment];
105
            } elseif (is_object($target) && isset($target->{$segment})) {
106
                $target = $target->{$segment};
107
            } elseif (is_object($target) && method_exists($target, $segment)) {
108
                // This is different than laravel..
109
                $target = call_user_func([$target, $segment]);
110
            } else {
111
                return value($default);
112
            }
113
        }
114
115
        return $target;
116
    }
117
}
118