|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Nozavroni/Collections |
|
5
|
|
|
* Just another collections library for PHP5.6+. |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Copyright (c) 2016 Luke Visinoni <[email protected]> |
|
8
|
|
|
* @author Luke Visinoni <[email protected]> |
|
9
|
|
|
* @license https://github.com/nozavroni/collections/blob/master/LICENSE The MIT License (MIT) |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Noz; |
|
12
|
|
|
|
|
13
|
|
|
use Iterator; |
|
14
|
|
|
use Noz\Collection\AbstractCollection; |
|
15
|
|
|
use Noz\Collection\Collection; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Collection factory. |
|
19
|
|
|
* |
|
20
|
|
|
* Simply an alias to (new Collection($in)). Allows for a little more concise and |
|
21
|
|
|
* simpler instantiation of a collection. Also I plan to eventually support |
|
22
|
|
|
* additional input types that will make this function more flexible and forgiving |
|
23
|
|
|
* than simply instantiating a Collection object, but for now the two are identical. |
|
24
|
|
|
* |
|
25
|
|
|
* @param array|Iterator $in Either an array or an iterator of data |
|
26
|
|
|
* |
|
27
|
|
|
* @return AbstractCollection A collection object containing data from $in |
|
28
|
|
|
* |
|
29
|
|
|
* @see AbstractCollection::__construct() (alias) |
|
30
|
|
|
*/ |
|
31
|
|
|
function collect($in = null) |
|
32
|
|
|
{ |
|
33
|
2 |
|
return Collection::factory($in); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Invoke a callable and return result. |
|
38
|
|
|
* |
|
39
|
|
|
* Pass in a callable followed by whatever arguments you want passed to |
|
40
|
|
|
* it and this function will invoke it with your arguments and return |
|
41
|
|
|
* the result. |
|
42
|
|
|
* |
|
43
|
|
|
* @param callable $callback The callback function to invoke |
|
44
|
|
|
* @param array ...$args The args to pass to your callable |
|
45
|
|
|
* |
|
46
|
|
|
* @return mixed The result of your invoked callable |
|
47
|
|
|
*/ |
|
48
|
|
|
function invoke(callable $callback, ...$args) |
|
49
|
|
|
{ |
|
50
|
1 |
|
return $callback(...$args); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Determine if data is traversable. |
|
55
|
|
|
* |
|
56
|
|
|
* Pass in any variable and this function will tell you whether or not it |
|
57
|
|
|
* is traversable. Basically this just means that it is either an array or an iterator. |
|
58
|
|
|
* This function was written simply because I was tired of if statements that checked |
|
59
|
|
|
* whether a variable was an array or a descendant of \Iterator. So I wrote this guy. |
|
60
|
|
|
* |
|
61
|
|
|
* @param mixed $input The variable to determine traversability |
|
62
|
|
|
* |
|
63
|
|
|
* @return bool True if $input is an array or an Iterator |
|
64
|
|
|
*/ |
|
65
|
|
|
function is_traversable($input) |
|
66
|
|
|
{ |
|
67
|
100 |
|
return is_array($input) || $input instanceof Iterator; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Dump and die. |
|
72
|
|
|
* |
|
73
|
|
|
* @param mixed $input Data to dump |
|
74
|
|
|
* @param bool $exit Should we exit after dump? |
|
75
|
|
|
* @param bool $label Should we print a label? |
|
76
|
|
|
* @codeCoverageIgnore |
|
77
|
|
|
*/ |
|
78
|
|
|
function dd($input, $exit = true, $label = null) |
|
79
|
|
|
{ |
|
80
|
|
|
if (is_null($label)) { |
|
81
|
|
|
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1); |
|
82
|
|
|
$label = 'File: '; |
|
83
|
|
|
$label .= pathinfo($trace[0]['file'], PATHINFO_FILENAME); |
|
84
|
|
|
$label .= ':' . $trace[0]['line']; |
|
85
|
|
|
echo $label . "\n"; |
|
86
|
|
|
} else { |
|
87
|
|
|
echo $label . "\n" . implode( |
|
88
|
|
|
array_map( |
|
89
|
|
|
function () { |
|
90
|
|
|
return '-'; |
|
91
|
|
|
}, |
|
92
|
|
|
str_split($label) |
|
93
|
|
|
) |
|
94
|
|
|
) . "\n"; |
|
95
|
|
|
} |
|
96
|
|
|
var_dump($input); |
|
97
|
|
|
echo "\n"; |
|
98
|
|
|
if ($exit) { |
|
99
|
|
|
exit; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|