-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.php
174 lines (142 loc) · 3.58 KB
/
helpers.php
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* All these functions are designed to work in PHP >= 5.2 primarily to ensure
* that they can be used in WordPress
*/
// Array Heleprs
/**
* array_column was added in PHP >= 5.5.
* It's really useful and is nice if it's availible in lower versions.
*
* @example
* $people = array(
* array( 'id' => 2, 'name' => 'Tom' ),
* array( 'id' => 3, 'name' => 'Dick' ),
* array( 'id' => 5, 'name' => 'Harry' ),
* );
*
* $ids = array_column( $people, 'id' );
*
* // Returns
* array( 2, 3, 5 );
*
* @param array $collection The array to pull values from
* @param string $columnKey The value to pull from each array
* @param string $indexKey Optional argument for specifiying the index key
* @return array A new array of values plucked from the original
*/
if ( ! function_exists( "array_column" ) )
{
function array_column( array $collection, $columnKey, $indexKey = null)
{
$newCollection = array();
foreach ( $collection as $item )
{
// If an indexKey is specified && it isset && the columnKey isset
if ( ! is_null( $indexKey ) && isset( $item[ $indexKey ] ) && isset( $item[ $columnKey ] ) )
{
$newCollection[ $item[ $indexKey ] ] = $item[ $columnKey ];
}
// Else if just the columnKey isset
elseif ( isset( $item[ $columnKey ] ) )
{
$newCollection[] = $item[ $columnKey ];
}
}
return $newCollection;
}
}
// Object helpers
/**
* object_column doesn't exist but would be useful. Vry similair to array_column,
* given an array of objects it will pull a value from each of them and return
* an array with the new values.
*
* @example
* $people = array(
* array( 'id' => 2, 'name' => 'Tom' ),
* array( 'id' => 3, 'name' => 'Dick' ),
* array( 'id' => 5, 'name' => 'Harry' ),
* );
*
* $ids = object_column( $people, 'id' );
*
* // Returns
* array( 2, 3, 5 );
*
* @param array $collection The array of objects to pull values from
* @param string $columnKey The value to pull from each object
* @param string $indexKey Optional argument for specifiying the index key
* @return array A new array of values plucked from the original
*/
if ( ! function_exists( "object_column" ) )
{
function object_column( array $collection, $columnKey, $indexKey = null)
{
$newCollection = array();
foreach ( $collection as $item )
{
// If an indexKey is specified && it isset && the columnKey isset
if ( ! is_null( $indexKey ) && isset( $item->$indexKey ) && isset( $item->$columnKey ) )
{
$newCollection[ $item->$indexKey ] = $item->$columnKey;
}
// Else if just the columnKey isset
elseif ( isset( $item->$columnKey ) )
{
$newCollection[] = $item->$columnKey;
}
}
return $newCollection;
}
}
function head()
{
}
function last()
{
}
// String helpers
function snake_case()
{
}
function camel_case()
{
}
function studly_case()
{
}
function starts_with()
{
}
function ends_with()
{
}
function str_contains()
{
}
function str_is()
{
}
function str_quick_random()
{
}
function str_secure_random()
{
}
// Miscellaneous heleprs
/**
* Dump & die. Really usefull for debugging. Does exactly what it says, prints
* to screen then dies.
*
* @param mixed $anything Anything you wish to view on screen
* @return void
*/
if ( ! function_exists("dd") )
{
function dd($anything)
{
var_dump($anything);
die;
}
}