|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
namespace WFV\Abstraction; |
|
3
|
|
|
defined( 'ABSPATH' ) || die(); |
|
4
|
|
|
|
|
5
|
|
|
use WFV\Contract\CollectionInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* |
|
9
|
|
|
* |
|
10
|
|
|
* @since 0.10.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class Collectable implements CollectionInterface { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* |
|
16
|
|
|
* |
|
17
|
|
|
* @since 0.10.0 |
|
18
|
|
|
* @access protected |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $data = array(); |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Check if the collection contains a key / value pair |
|
25
|
|
|
* |
|
26
|
|
|
* @since 0.10.0 |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $key |
|
29
|
|
|
* @param string $value |
|
30
|
|
|
* @return bool |
|
31
|
|
|
*/ |
|
32
|
|
|
public function contains( $key = null, $value = null ) { |
|
33
|
|
|
if( $this->has( $key ) ) { |
|
34
|
|
|
return ( is_array( $this->data[ $key ] ) ) |
|
35
|
|
|
? in_array( $value, $this->data[ $key ] ) |
|
36
|
|
|
: $this->data[ $key ] === $value; |
|
37
|
|
|
} |
|
38
|
|
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Returns escaped input value. |
|
43
|
|
|
* |
|
44
|
|
|
* @since 0.10.0 |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $key Index |
|
47
|
|
|
* @param string|array (optional) $callback Context appropriate callable. |
|
48
|
|
|
* @return string|null |
|
49
|
|
|
*/ |
|
50
|
|
|
public function escape( $key, callable $callback = null ) { |
|
51
|
|
|
if( is_string( $key ) ) { |
|
52
|
|
|
$callback = ( null === $callback ) ? 'esc_html' : $callback; |
|
53
|
|
|
return ( true === $this->has( $key ) ) ? $this->call_func( $callback, $this->data[ $key ] ) : null; |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Check if the collection has a given key |
|
60
|
|
|
* |
|
61
|
|
|
* @since 0.10.0 |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $key |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
|
|
public function has( $key = null ) { |
|
67
|
|
|
return array_key_exists( $key, $this->data ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Checks if the collection has data |
|
72
|
|
|
* |
|
73
|
|
|
* @since 0.10.0 |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
|
|
public function is_populated() { |
|
78
|
|
|
return count( $this->data ) > 0; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* If provided key is an array in this collection |
|
83
|
|
|
* pass each leaf through a callback and return new array. |
|
84
|
|
|
* |
|
85
|
|
|
* @since 0.10.0 |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $key |
|
88
|
|
|
* @param callable $callback |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
public function transform( $key = null, callable $callback ) { |
|
92
|
|
|
// WIP |
|
93
|
|
|
if( true === $this->has( $key ) ) { |
|
94
|
|
|
if( is_array( $this->data[ $key ] ) ) { |
|
95
|
|
|
return $this->transform_array_leafs( $this->data[ $key ], $callback ); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Transform $array leafs |
|
102
|
|
|
* |
|
103
|
|
|
* WIP |
|
104
|
|
|
* |
|
105
|
|
|
* @since 0.10.0 |
|
106
|
|
|
* @access private |
|
107
|
|
|
* |
|
108
|
|
|
* @param array $array |
|
109
|
|
|
* @param string|array $callback |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function transform_array_leafs( array $array, $callback ) { |
|
113
|
|
|
array_walk_recursive( $array, function( &$item ) use( $callback ) { |
|
114
|
|
|
$item = $this->call_func( $callback, $item ); |
|
115
|
|
|
} ); |
|
116
|
|
|
return $array; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Trigger a callback function |
|
121
|
|
|
* |
|
122
|
|
|
* @since 0.10.0 |
|
123
|
|
|
* @access private |
|
124
|
|
|
* |
|
125
|
|
|
* @param string|array $callback |
|
126
|
|
|
* @param string (optional) $input The input string |
|
127
|
|
|
* @return |
|
128
|
|
|
*/ |
|
129
|
|
|
private function call_func( $callback, $input = null ) { |
|
130
|
|
|
// WIP - simplify |
|
131
|
|
|
if( is_array( $callback ) ) { |
|
132
|
|
|
$method = $callback[0]; |
|
133
|
|
|
$args = $callback[1]; |
|
134
|
|
|
if ( $input ) { |
|
135
|
|
|
array_unshift( $args, $input ); |
|
136
|
|
|
} |
|
137
|
|
|
return call_user_func_array( $method, $args ); |
|
138
|
|
|
} |
|
139
|
|
|
return $callback( $input ); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.