Completed
Push — master ( a51ab2...247c5c )
by Antonio Carlos
03:22
created

helpers.php ➔ collect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
use IlluminateAgnostic\Str\Support\Arr;
4
use IlluminateAgnostic\Str\Support\Str;
5
use IlluminateAgnostic\Str\Support\Collection;
6
use IlluminateAgnostic\Str\Support\Debug\Dumper;
7
8
if (! function_exists('camel_case')) {
9
    /**
10
     * Convert a value to camel case.
11
     *
12
     * @param  string  $value
13
     * @return string
14
     */
15
    function camel_case($value)
16
    {
17
        return Str::camel($value);
18
    }
19
}
20
21
if (! function_exists('collect')) {
22
    /**
23
     * Create a collection from the given value.
24
     *
25
     * @param  mixed  $value
26
     * @return \IlluminateAgnostic\Str\Support\Collection|\Illuminate\Support\Collection
27
     */
28
    function collect($value = null)
29
    {
30
        return new Collection($value);
31
    }
32
}
33
34
if (! function_exists('data_get')) {
35
    /**
36
     * Get an item from an array or object using "dot" notation.
37
     *
38
     * @param  mixed   $target
39
     * @param  string|array  $key
40
     * @param  mixed   $default
41
     * @return mixed
42
     */
43
    function data_get($target, $key, $default = null)
44
    {
45
        if (is_null($key)) {
46
            return $target;
47
        }
48
49
        $key = is_array($key) ? $key : explode('.', $key);
50
51
        while (! is_null($segment = array_shift($key))) {
52
            if ($segment === '*') {
53
                if ($target instanceof Collection) {
54
                    $target = $target->all();
55
                } elseif (! is_array($target)) {
56
                    return value($default);
57
                }
58
59
                $result = Arr::pluck($target, $key);
60
61
                return in_array('*', $key) ? Arr::collapse($result) : $result;
62
            }
63
64
            if (Arr::accessible($target) && Arr::exists($target, $segment)) {
65
                $target = $target[$segment];
66
            } elseif (is_object($target) && isset($target->{$segment})) {
67
                $target = $target->{$segment};
68
            } else {
69
                return value($default);
70
            }
71
        }
72
73
        return $target;
74
    }
75
}
76
77
if (! function_exists('ends_with')) {
78
    /**
79
     * Determine if a given string ends with a given substring.
80
     *
81
     * @param  string  $haystack
82
     * @param  string|array  $needles
83
     * @return bool
84
     */
85
    function ends_with($haystack, $needles)
86
    {
87
        return Str::endsWith($haystack, $needles);
88
    }
89
}
90
91
if (! function_exists('kebab_case')) {
92
    /**
93
     * Convert a string to kebab case.
94
     *
95
     * @param  string  $value
96
     * @return string
97
     */
98
    function kebab_case($value)
99
    {
100
        return Str::kebab($value);
101
    }
102
}
103
104
105
if (! function_exists('snake_case')) {
106
    /**
107
     * Convert a string to snake case.
108
     *
109
     * @param  string  $value
110
     * @param  string  $delimiter
111
     * @return string
112
     */
113
    function snake_case($value, $delimiter = '_')
114
    {
115
        return Str::snake($value, $delimiter);
116
    }
117
}
118
119
if (! function_exists('starts_with')) {
120
    /**
121
     * Determine if a given string starts with a given substring.
122
     *
123
     * @param  string  $haystack
124
     * @param  string|array  $needles
125
     * @return bool
126
     */
127
    function starts_with($haystack, $needles)
128
    {
129
        return Str::startsWith($haystack, $needles);
130
    }
131
}
132
133
if (! function_exists('str_after')) {
134
    /**
135
     * Return the remainder of a string after a given value.
136
     *
137
     * @param  string  $subject
138
     * @param  string  $search
139
     * @return string
140
     */
141
    function str_after($subject, $search)
142
    {
143
        return Str::after($subject, $search);
144
    }
145
}
146
147
if (! function_exists('str_before')) {
148
    /**
149
     * Get the portion of a string before a given value.
150
     *
151
     * @param  string  $subject
152
     * @param  string  $search
153
     * @return string
154
     */
155
    function str_before($subject, $search)
156
    {
157
        return Str::before($subject, $search);
158
    }
159
}
160
161
if (! function_exists('str_contains')) {
162
    /**
163
     * Determine if a given string contains a given substring.
164
     *
165
     * @param  string  $haystack
166
     * @param  string|array  $needles
167
     * @return bool
168
     */
169
    function str_contains($haystack, $needles)
170
    {
171
        return Str::contains($haystack, $needles);
172
    }
173
}
174
175
if (! function_exists('str_finish')) {
176
    /**
177
     * Cap a string with a single instance of a given value.
178
     *
179
     * @param  string  $value
180
     * @param  string  $cap
181
     * @return string
182
     */
183
    function str_finish($value, $cap)
184
    {
185
        return Str::finish($value, $cap);
186
    }
187
}
188
189
if (! function_exists('str_is')) {
190
    /**
191
     * Determine if a given string matches a given pattern.
192
     *
193
     * @param  string|array  $pattern
194
     * @param  string  $value
195
     * @return bool
196
     */
197
    function str_is($pattern, $value)
198
    {
199
        return Str::is($pattern, $value);
200
    }
201
}
202
203
if (! function_exists('str_limit')) {
204
    /**
205
     * Limit the number of characters in a string.
206
     *
207
     * @param  string  $value
208
     * @param  int     $limit
209
     * @param  string  $end
210
     * @return string
211
     */
212
    function str_limit($value, $limit = 100, $end = '...')
213
    {
214
        return Str::limit($value, $limit, $end);
215
    }
216
}
217
218
if (! function_exists('str_plural')) {
219
    /**
220
     * Get the plural form of an English word.
221
     *
222
     * @param  string  $value
223
     * @param  int     $count
224
     * @return string
225
     */
226
    function str_plural($value, $count = 2)
227
    {
228
        return Str::plural($value, $count);
229
    }
230
}
231
232
if (! function_exists('str_random')) {
233
    /**
234
     * Generate a more truly "random" alpha-numeric string.
235
     *
236
     * @param  int  $length
237
     * @return string
238
     *
239
     * @throws \RuntimeException
240
     */
241
    function str_random($length = 16)
242
    {
243
        return Str::random($length);
244
    }
245
}
246
247
if (! function_exists('str_replace_array')) {
248
    /**
249
     * Replace a given value in the string sequentially with an array.
250
     *
251
     * @param  string  $search
252
     * @param  array   $replace
253
     * @param  string  $subject
254
     * @return string
255
     */
256
    function str_replace_array($search, array $replace, $subject)
257
    {
258
        return Str::replaceArray($search, $replace, $subject);
259
    }
260
}
261
262
if (! function_exists('str_replace_first')) {
263
    /**
264
     * Replace the first occurrence of a given value in the string.
265
     *
266
     * @param  string  $search
267
     * @param  string  $replace
268
     * @param  string  $subject
269
     * @return string
270
     */
271
    function str_replace_first($search, $replace, $subject)
272
    {
273
        return Str::replaceFirst($search, $replace, $subject);
274
    }
275
}
276
277
if (! function_exists('str_replace_last')) {
278
    /**
279
     * Replace the last occurrence of a given value in the string.
280
     *
281
     * @param  string  $search
282
     * @param  string  $replace
283
     * @param  string  $subject
284
     * @return string
285
     */
286
    function str_replace_last($search, $replace, $subject)
287
    {
288
        return Str::replaceLast($search, $replace, $subject);
289
    }
290
}
291
292
if (! function_exists('str_singular')) {
293
    /**
294
     * Get the singular form of an English word.
295
     *
296
     * @param  string  $value
297
     * @return string
298
     */
299
    function str_singular($value)
300
    {
301
        return Str::singular($value);
302
    }
303
}
304
305
if (! function_exists('str_slug')) {
306
    /**
307
     * Generate a URL friendly "slug" from a given string.
308
     *
309
     * @param  string  $title
310
     * @param  string  $separator
311
     * @param  string  $language
312
     * @return string
313
     */
314
    function str_slug($title, $separator = '-', $language = 'en')
315
    {
316
        return Str::slug($title, $separator, $language);
317
    }
318
}
319
320
if (! function_exists('str_start')) {
321
    /**
322
     * Begin a string with a single instance of a given value.
323
     *
324
     * @param  string  $value
325
     * @param  string  $prefix
326
     * @return string
327
     */
328
    function str_start($value, $prefix)
329
    {
330
        return Str::start($value, $prefix);
331
    }
332
}
333
334
if (! function_exists('studly_case')) {
335
    /**
336
     * Convert a value to studly caps case.
337
     *
338
     * @param  string  $value
339
     * @return string
340
     */
341
    function studly_case($value)
342
    {
343
        return Str::studly($value);
344
    }
345
}
346
347
348
if (! function_exists('title_case')) {
349
    /**
350
     * Convert a value to title case.
351
     *
352
     * @param  string  $value
353
     * @return string
354
     */
355
    function title_case($value)
356
    {
357
        return Str::title($value);
358
    }
359
}
360
361
362
if (! function_exists('value')) {
363
    /**
364
     * Return the default value of the given value.
365
     *
366
     * @param  mixed  $value
367
     * @return mixed
368
     */
369
    function value($value)
370
    {
371
        return $value instanceof Closure ? $value() : $value;
372
    }
373
}
374
375
376
if (! function_exists('dd')) {
377
    /**
378
     * Dump the passed variables and end the script.
379
     *
380
     * @param  mixed  $args
381
     * @return void
382
     */
383
    function dd(...$args)
384
    {
385
        http_response_code(500);
386
387
        foreach ($args as $x) {
388
            (new Dumper)->dump($x);
389
        }
390
391
        die(1);
392
    }
393
}
394