-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlagsHelper.php
202 lines (181 loc) · 3.95 KB
/
FlagsHelper.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
* @author Hazuli Fidastian <hazulifidastian@gmail.com>
* @package Helpers
*/
/**
* Membantu mendefinisikan, validasi, pengecekan flags
*
*/
class FlagsHelper {
/**
* Separator
* @var string
*/
private $sep = '|';
/**
* Hanya menerima flags yang valid
* @var array
*/
private $valid_flags = array(
'LOCKED',
'UNLOCKED',
'ORIGINAL',
'REPLACEMENT',
);
/**
* Flag-flag
*
* @var string
*/
private $flags = array();
/**
*
* @var Singleton
*/
private static $instance;
/**
* Inisialisasi
*
* @param string $flags [description]
* @return instance
*/
public static function flags( $flags='' )
{
self::$instance = new self( $flags );
return self::$instance;
}
/**
* Constructor with flags
*
* @param string $flags
*/
private function __construct( $flags = '' )
{
$this->extract( $flags );
}
/**
* Magic method to filter flag parameter from method insert, delete, exists and update
* @param string $name Method name
* @param array $args Arguments
* @return call a function | $this
*/
public function __call( $name, $args )
{
$validate_flag = array( 'insert', 'delete', 'exists' );
if ( method_exists($this, "_{$name}") )
{
if( in_array($name, $validate_flag) and $this->validate($args[0]) )
return call_user_func_array(array($this, "_{$name}"), $args);
else if( $name == 'update' and $this->validate($args[0]) and $this->validate($args[1]) )
return call_user_func_array(array($this, "_{$name}"), $args);
}
return $this;
}
/**
* Extract to array and validate flags
*
* @param string $flags
* @return void
*/
private function extract( $flags )
{
$flags = explode( $this->sep, $flags );
foreach( $flags as $fl )
if( $this->validate($fl) )
$this->flags[] = $fl;
}
/**
* Validasi apakah flag sudah dideklarasikan
*
* @param string $flag
* @return boolean Tersedia/tidak
*/
private function validate( $flag )
{
return in_array( $flag, $this->valid_flags );
}
/**
* Cek apakah flag ada didalam flags
*
* @param string $flag
* @return boolean
*/
public function _exists( $flag )
{
return in_array( $flag, $this->flags );
}
/**
* Tambah
*
* @param string $flag
* @return FlagsHelper
*/
public function _insert( $flag )
{
if( !$this->exists( $flag ) )
{
array_push( $this->flags, $flag );
$this->flags = array_unique( $this->flags, SORT_STRING );
}
return $this;
}
/**
* Hapus
*
* @param string $flag
* @return FlagsHelper
*/
public function _delete( $flag )
{
$this->flags = array_diff( $this->flags, array($flag) );
return $this;
}
/**
* Update
*
* @param string $flag
* @return FlagsHelper
*/
public function _update( $from_flag, $to_flag )
{
$this->delete( $from_flag );
$this->insert( $to_flag );
return $this;
}
/**
* Convert to string
*
* @return string
*/
public function to_str()
{
return implode( $this->sep, $this->flags );
}
/**
* Get flags string
*
* @return string
*/
public function select()
{
return $this->to_str();
}
/**
* Convert to array
*
* @return array
*/
public function to_array()
{
return $this->flags;
}
/**
* Convert to json
* @return json string
*/
public function to_json()
{
return json_encode( $this->flags );
}
}