@@ -135,14 +135,14 @@ discard block |
||
135 | 135 | */ |
136 | 136 | private static function persistenceLoadDefault($pk, $table, $options){ |
137 | 137 | if ( $data = SQL::single("SELECT * FROM $table WHERE {$options['key']}=? LIMIT 1",[$pk]) ){ |
138 | - $obj = new static; |
|
139 | - foreach ((array)$data as $key => $value) { |
|
140 | - $obj->$key = $value; |
|
141 | - } |
|
142 | - return $obj; |
|
143 | - } else { |
|
144 | - return null; |
|
145 | - } |
|
138 | + $obj = new static; |
|
139 | + foreach ((array)$data as $key => $value) { |
|
140 | + $obj->$key = $value; |
|
141 | + } |
|
142 | + return $obj; |
|
143 | + } else { |
|
144 | + return null; |
|
145 | + } |
|
146 | 146 | } |
147 | 147 | |
148 | 148 | /** |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | * Private Standard Save Method |
164 | 164 | */ |
165 | 165 | private function persistenceSaveDefault($table,$options){ |
166 | - return SQL::insertOrUpdate($table,array_filter((array)$this),$options['key']); |
|
166 | + return SQL::insertOrUpdate($table,array_filter((array)$this),$options['key']); |
|
167 | 167 | } |
168 | 168 | |
169 | 169 |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | * @param mixed $options The options passed to the persistence layer. |
21 | 21 | * @return mixed All options array or a single value |
22 | 22 | */ |
23 | - protected static function persistenceOptions($options=null){ |
|
23 | + protected static function persistenceOptions($options = null) { |
|
24 | 24 | static $_options = []; |
25 | 25 | |
26 | 26 | if ($options === null) return $_options; |
@@ -29,18 +29,18 @@ discard block |
||
29 | 29 | } else { |
30 | 30 | if (empty($_options['table'])) { |
31 | 31 | $self = get_called_class(); |
32 | - if (defined("$self::_PRIMARY_KEY_")){ |
|
32 | + if (defined("$self::_PRIMARY_KEY_")) { |
|
33 | 33 | $x = explode('.', $self::_PRIMARY_KEY_); |
34 | 34 | $_options = [ |
35 | 35 | 'table' => current($x), |
36 | - 'key' => isset($x[1])?$x[1]:'id', |
|
36 | + 'key' => isset($x[1]) ? $x[1] : 'id', |
|
37 | 37 | ]; |
38 | 38 | } else { |
39 | 39 | // User pluralized class name as default table |
40 | - switch(substr($s = strtolower($self),-1)){ |
|
41 | - case 'y': $table = substr($s,0,-1).'ies'; break; |
|
42 | - case 's': $table = substr($s,0,-1).'es'; break; |
|
43 | - default: $table = $s.'s'; break; |
|
40 | + switch (substr($s = strtolower($self), -1)) { |
|
41 | + case 'y': $table = substr($s, 0, -1).'ies';break; |
|
42 | + case 's': $table = substr($s, 0, -1).'es';break; |
|
43 | + default: $table = $s.'s';break; |
|
44 | 44 | } |
45 | 45 | // Default ID |
46 | 46 | $_options = [ |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | * @param callable $callback The callback to use on model save |
62 | 62 | * @return callable Current save callback |
63 | 63 | */ |
64 | - protected static function persistenceSave(callable $callback=null){ |
|
64 | + protected static function persistenceSave(callable $callback = null) { |
|
65 | 65 | static $save_cb = null; |
66 | 66 | return $callback ? $save_cb = $callback : $save_cb; |
67 | 67 | } |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | * @param callable $callback The callback to use on model load |
75 | 75 | * @return callable Current load callback |
76 | 76 | */ |
77 | - protected static function persistenceLoad(callable $callback=null){ |
|
77 | + protected static function persistenceLoad(callable $callback = null) { |
|
78 | 78 | static $retrieve_cb = null; |
79 | 79 | return $callback ? $retrieve_cb = $callback : $retrieve_cb; |
80 | 80 | } |
@@ -89,8 +89,8 @@ discard block |
||
89 | 89 | * @param array $options An associative array with options for the persistance layer. |
90 | 90 | * @return void |
91 | 91 | */ |
92 | - public static function persistOn($table, array $options=[]){ |
|
93 | - $options = array_merge($options,[ |
|
92 | + public static function persistOn($table, array $options = []) { |
|
93 | + $options = array_merge($options, [ |
|
94 | 94 | 'key' => 'id' |
95 | 95 | ]); |
96 | 96 | $options['table'] = $table; |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | * @param callable $callback The callback to use on model save |
104 | 104 | * @return void |
105 | 105 | */ |
106 | - public static function onSave(callable $callback){ |
|
106 | + public static function onSave(callable $callback) { |
|
107 | 107 | static::persistenceSave($callback); |
108 | 108 | } |
109 | 109 | |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | * @param callable $callback The callback to use on model load |
113 | 113 | * @return void |
114 | 114 | */ |
115 | - public static function onLoad(callable $callback){ |
|
115 | + public static function onLoad(callable $callback) { |
|
116 | 116 | static::persistenceLoad($callback); |
117 | 117 | } |
118 | 118 | |
@@ -120,23 +120,23 @@ discard block |
||
120 | 120 | * Load the model from the persistence layer |
121 | 121 | * @return mixed The retrieved object |
122 | 122 | */ |
123 | - public static function load($pk){ |
|
123 | + public static function load($pk) { |
|
124 | 124 | $table = static::persistenceOptions('table'); |
125 | 125 | $cb = static::persistenceLoad(); |
126 | 126 | $op = static::persistenceOptions(); |
127 | 127 | |
128 | 128 | // Use standard persistence on DB layer |
129 | - return ( false == is_callable($cb) ) ? |
|
130 | - static::persistenceLoadDefault($pk,$table,$op) : $cb($pk,$table,$op); |
|
129 | + return (false == is_callable($cb)) ? |
|
130 | + static::persistenceLoadDefault($pk, $table, $op) : $cb($pk, $table, $op); |
|
131 | 131 | } |
132 | 132 | |
133 | 133 | /** |
134 | 134 | * Private Standard Load Method |
135 | 135 | */ |
136 | - private static function persistenceLoadDefault($pk, $table, $options){ |
|
137 | - if ( $data = SQL::single("SELECT * FROM $table WHERE {$options['key']}=? LIMIT 1",[$pk]) ){ |
|
136 | + private static function persistenceLoadDefault($pk, $table, $options) { |
|
137 | + if ($data = SQL::single("SELECT * FROM $table WHERE {$options['key']}=? LIMIT 1", [$pk])) { |
|
138 | 138 | $obj = new static; |
139 | - foreach ((array)$data as $key => $value) { |
|
139 | + foreach ((array) $data as $key => $value) { |
|
140 | 140 | $obj->$key = $value; |
141 | 141 | } |
142 | 142 | return $obj; |
@@ -149,21 +149,21 @@ discard block |
||
149 | 149 | * Save the model to the persistence layer |
150 | 150 | * @return mixed The results from the save callback. (default: lastInsertID) |
151 | 151 | */ |
152 | - public function save(){ |
|
152 | + public function save() { |
|
153 | 153 | $table = static::persistenceOptions('table'); |
154 | 154 | $op = static::persistenceOptions(); |
155 | 155 | $cb = static::persistenceSave(); |
156 | 156 | |
157 | 157 | // Use standard persistence on DB layer |
158 | - $cb = $cb ? Closure::bind($cb, $this) : [$this,'persistenceSaveDefault']; |
|
159 | - return $cb($table,$op); |
|
158 | + $cb = $cb ? Closure::bind($cb, $this) : [$this, 'persistenceSaveDefault']; |
|
159 | + return $cb($table, $op); |
|
160 | 160 | } |
161 | 161 | |
162 | 162 | /** |
163 | 163 | * Private Standard Save Method |
164 | 164 | */ |
165 | - private function persistenceSaveDefault($table,$options){ |
|
166 | - return SQL::insertOrUpdate($table,array_filter((array)$this),$options['key']); |
|
165 | + private function persistenceSaveDefault($table, $options) { |
|
166 | + return SQL::insertOrUpdate($table, array_filter((array) $this), $options['key']); |
|
167 | 167 | } |
168 | 168 | |
169 | 169 |
@@ -20,16 +20,18 @@ discard block |
||
20 | 20 | * @param mixed $options The options passed to the persistence layer. |
21 | 21 | * @return mixed All options array or a single value |
22 | 22 | */ |
23 | - protected static function persistenceOptions($options=null){ |
|
23 | + protected static function persistenceOptions($options=null) { |
|
24 | 24 | static $_options = []; |
25 | 25 | |
26 | - if ($options === null) return $_options; |
|
26 | + if ($options === null) { |
|
27 | + return $_options; |
|
28 | + } |
|
27 | 29 | if (is_array($options)) { |
28 | 30 | return $_options = $options; |
29 | 31 | } else { |
30 | 32 | if (empty($_options['table'])) { |
31 | 33 | $self = get_called_class(); |
32 | - if (defined("$self::_PRIMARY_KEY_")){ |
|
34 | + if (defined("$self::_PRIMARY_KEY_")) { |
|
33 | 35 | $x = explode('.', $self::_PRIMARY_KEY_); |
34 | 36 | $_options = [ |
35 | 37 | 'table' => current($x), |
@@ -37,7 +39,7 @@ discard block |
||
37 | 39 | ]; |
38 | 40 | } else { |
39 | 41 | // User pluralized class name as default table |
40 | - switch(substr($s = strtolower($self),-1)){ |
|
42 | + switch(substr($s = strtolower($self),-1)) { |
|
41 | 43 | case 'y': $table = substr($s,0,-1).'ies'; break; |
42 | 44 | case 's': $table = substr($s,0,-1).'es'; break; |
43 | 45 | default: $table = $s.'s'; break; |
@@ -61,7 +63,7 @@ discard block |
||
61 | 63 | * @param callable $callback The callback to use on model save |
62 | 64 | * @return callable Current save callback |
63 | 65 | */ |
64 | - protected static function persistenceSave(callable $callback=null){ |
|
66 | + protected static function persistenceSave(callable $callback=null) { |
|
65 | 67 | static $save_cb = null; |
66 | 68 | return $callback ? $save_cb = $callback : $save_cb; |
67 | 69 | } |
@@ -74,7 +76,7 @@ discard block |
||
74 | 76 | * @param callable $callback The callback to use on model load |
75 | 77 | * @return callable Current load callback |
76 | 78 | */ |
77 | - protected static function persistenceLoad(callable $callback=null){ |
|
79 | + protected static function persistenceLoad(callable $callback=null) { |
|
78 | 80 | static $retrieve_cb = null; |
79 | 81 | return $callback ? $retrieve_cb = $callback : $retrieve_cb; |
80 | 82 | } |
@@ -89,7 +91,7 @@ discard block |
||
89 | 91 | * @param array $options An associative array with options for the persistance layer. |
90 | 92 | * @return void |
91 | 93 | */ |
92 | - public static function persistOn($table, array $options=[]){ |
|
94 | + public static function persistOn($table, array $options=[]) { |
|
93 | 95 | $options = array_merge($options,[ |
94 | 96 | 'key' => 'id' |
95 | 97 | ]); |
@@ -103,7 +105,7 @@ discard block |
||
103 | 105 | * @param callable $callback The callback to use on model save |
104 | 106 | * @return void |
105 | 107 | */ |
106 | - public static function onSave(callable $callback){ |
|
108 | + public static function onSave(callable $callback) { |
|
107 | 109 | static::persistenceSave($callback); |
108 | 110 | } |
109 | 111 | |
@@ -112,7 +114,7 @@ discard block |
||
112 | 114 | * @param callable $callback The callback to use on model load |
113 | 115 | * @return void |
114 | 116 | */ |
115 | - public static function onLoad(callable $callback){ |
|
117 | + public static function onLoad(callable $callback) { |
|
116 | 118 | static::persistenceLoad($callback); |
117 | 119 | } |
118 | 120 | |
@@ -120,7 +122,7 @@ discard block |
||
120 | 122 | * Load the model from the persistence layer |
121 | 123 | * @return mixed The retrieved object |
122 | 124 | */ |
123 | - public static function load($pk){ |
|
125 | + public static function load($pk) { |
|
124 | 126 | $table = static::persistenceOptions('table'); |
125 | 127 | $cb = static::persistenceLoad(); |
126 | 128 | $op = static::persistenceOptions(); |
@@ -133,8 +135,8 @@ discard block |
||
133 | 135 | /** |
134 | 136 | * Private Standard Load Method |
135 | 137 | */ |
136 | - private static function persistenceLoadDefault($pk, $table, $options){ |
|
137 | - if ( $data = SQL::single("SELECT * FROM $table WHERE {$options['key']}=? LIMIT 1",[$pk]) ){ |
|
138 | + private static function persistenceLoadDefault($pk, $table, $options) { |
|
139 | + if ( $data = SQL::single("SELECT * FROM $table WHERE {$options['key']}=? LIMIT 1",[$pk]) ) { |
|
138 | 140 | $obj = new static; |
139 | 141 | foreach ((array)$data as $key => $value) { |
140 | 142 | $obj->$key = $value; |
@@ -149,7 +151,7 @@ discard block |
||
149 | 151 | * Save the model to the persistence layer |
150 | 152 | * @return mixed The results from the save callback. (default: lastInsertID) |
151 | 153 | */ |
152 | - public function save(){ |
|
154 | + public function save() { |
|
153 | 155 | $table = static::persistenceOptions('table'); |
154 | 156 | $op = static::persistenceOptions(); |
155 | 157 | $cb = static::persistenceSave(); |
@@ -162,7 +164,7 @@ discard block |
||
162 | 164 | /** |
163 | 165 | * Private Standard Save Method |
164 | 166 | */ |
165 | - private function persistenceSaveDefault($table,$options){ |
|
167 | + private function persistenceSaveDefault($table,$options) { |
|
166 | 168 | return SQL::insertOrUpdate($table,array_filter((array)$this),$options['key']); |
167 | 169 | } |
168 | 170 |
@@ -17,15 +17,15 @@ discard block |
||
17 | 17 | $encoded_payload = implode('.', [rtrim(strtr(base64_encode(json_encode([ |
18 | 18 | 'typ' => 'JWT', |
19 | 19 | 'alg' => $algo, |
20 | - ])), '+/', '-_'),'='), |
|
21 | - rtrim(strtr(base64_encode(json_encode($payload)), '+/', '-_'),'='), |
|
20 | + ])), '+/', '-_'), '='), |
|
21 | + rtrim(strtr(base64_encode(json_encode($payload)), '+/', '-_'), '='), |
|
22 | 22 | ]); |
23 | - return $encoded_payload . '.' . static::sign($encoded_payload, $secret, $algo); |
|
23 | + return $encoded_payload.'.'.static::sign($encoded_payload, $secret, $algo); |
|
24 | 24 | } |
25 | 25 | |
26 | - public static function decode($jwt, $secret = null, $verify = true){ |
|
26 | + public static function decode($jwt, $secret = null, $verify = true) { |
|
27 | 27 | |
28 | - if (substr_count($jwt,'.') != 2) throw new \Exception('Token not valid'); |
|
28 | + if (substr_count($jwt, '.') != 2) throw new \Exception('Token not valid'); |
|
29 | 29 | |
30 | 30 | list($encoded_header, $encoded_payload, $client_sig) = explode('.', $jwt); |
31 | 31 | |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | 'HS256' => 'sha256', |
54 | 54 | ]; |
55 | 55 | if (empty($algos[$algo])) throw new \Exception('Signing algorithm not supported'); |
56 | - return rtrim(strtr(base64_encode(hash_hmac($algos[$algo], $payload, $secret, true)), '+/', '-_'),'='); |
|
56 | + return rtrim(strtr(base64_encode(hash_hmac($algos[$algo], $payload, $secret, true)), '+/', '-_'), '='); |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | } |
@@ -23,24 +23,31 @@ discard block |
||
23 | 23 | return $encoded_payload . '.' . static::sign($encoded_payload, $secret, $algo); |
24 | 24 | } |
25 | 25 | |
26 | - public static function decode($jwt, $secret = null, $verify = true){ |
|
26 | + public static function decode($jwt, $secret = null, $verify = true) { |
|
27 | 27 | |
28 | - if (substr_count($jwt,'.') != 2) throw new \Exception('Token not valid'); |
|
28 | + if (substr_count($jwt,'.') != 2) { |
|
29 | + throw new \Exception('Token not valid'); |
|
30 | + } |
|
29 | 31 | |
30 | 32 | list($encoded_header, $encoded_payload, $client_sig) = explode('.', $jwt); |
31 | 33 | |
32 | - if (null === ($payload = json_decode(base64_decode(strtr($encoded_payload, '-_', '+/'))))) |
|
33 | - throw new \Exception('Invalid encoding'); |
|
34 | + if (null === ($payload = json_decode(base64_decode(strtr($encoded_payload, '-_', '+/'))))) { |
|
35 | + throw new \Exception('Invalid encoding'); |
|
36 | + } |
|
34 | 37 | |
35 | 38 | |
36 | 39 | if ($verify) { |
37 | - if (null === ($header = json_decode(base64_decode(strtr($encoded_header, '-_', '+/'))))) |
|
38 | - throw new \Exception('Invalid encoding'); |
|
40 | + if (null === ($header = json_decode(base64_decode(strtr($encoded_header, '-_', '+/'))))) { |
|
41 | + throw new \Exception('Invalid encoding'); |
|
42 | + } |
|
39 | 43 | |
40 | - if (empty($header->alg)) throw new \Exception('Invalid encoding'); |
|
44 | + if (empty($header->alg)) { |
|
45 | + throw new \Exception('Invalid encoding'); |
|
46 | + } |
|
41 | 47 | |
42 | - if ($client_sig != static::sign("$encoded_header.$encoded_payload", $secret, $header->alg)) |
|
43 | - throw new \Exception('Token verification failed'); |
|
48 | + if ($client_sig != static::sign("$encoded_header.$encoded_payload", $secret, $header->alg)) { |
|
49 | + throw new \Exception('Token verification failed'); |
|
50 | + } |
|
44 | 51 | } |
45 | 52 | |
46 | 53 | return $payload; |
@@ -52,7 +59,9 @@ discard block |
||
52 | 59 | 'HS384' => 'sha384', |
53 | 60 | 'HS256' => 'sha256', |
54 | 61 | ]; |
55 | - if (empty($algos[$algo])) throw new \Exception('Signing algorithm not supported'); |
|
62 | + if (empty($algos[$algo])) { |
|
63 | + throw new \Exception('Signing algorithm not supported'); |
|
64 | + } |
|
56 | 65 | return rtrim(strtr(base64_encode(hash_hmac($algos[$algo], $payload, $secret, true)), '+/', '-_'),'='); |
57 | 66 | } |
58 | 67 |
@@ -15,40 +15,40 @@ |
||
15 | 15 | |
16 | 16 | protected static $_listeners = []; |
17 | 17 | |
18 | - public static function on($name,callable $listener){ |
|
18 | + public static function on($name, callable $listener) { |
|
19 | 19 | static::$_listeners[$name][] = $listener; |
20 | 20 | } |
21 | 21 | |
22 | - public static function single($name,callable $listener){ |
|
22 | + public static function single($name, callable $listener) { |
|
23 | 23 | static::$_listeners[$name] = [$listener]; |
24 | 24 | } |
25 | 25 | |
26 | - public static function off($name,callable $listener = null){ |
|
27 | - if($listener === null) { |
|
26 | + public static function off($name, callable $listener = null) { |
|
27 | + if ($listener === null) { |
|
28 | 28 | unset(static::$_listeners[$name]); |
29 | 29 | } else { |
30 | - if ($idx = array_search($listener,static::$_listeners[$name],true)) |
|
30 | + if ($idx = array_search($listener, static::$_listeners[$name], true)) |
|
31 | 31 | unset(static::$_listeners[$name][$idx]); |
32 | 32 | } |
33 | 33 | } |
34 | 34 | |
35 | - public static function alias($source,$alias){ |
|
36 | - static::$_listeners[$alias] =& static::$_listeners[$source]; |
|
35 | + public static function alias($source, $alias) { |
|
36 | + static::$_listeners[$alias] = & static::$_listeners[$source]; |
|
37 | 37 | } |
38 | 38 | |
39 | - public static function trigger($name){ |
|
40 | - if (false === empty(static::$_listeners[$name])){ |
|
39 | + public static function trigger($name) { |
|
40 | + if (false === empty(static::$_listeners[$name])) { |
|
41 | 41 | $args = func_get_args(); |
42 | 42 | array_shift($args); |
43 | 43 | $results = []; |
44 | 44 | foreach (static::$_listeners[$name] as $listener) { |
45 | - $results[] = call_user_func_array($listener,$args); |
|
45 | + $results[] = call_user_func_array($listener, $args); |
|
46 | 46 | } |
47 | 47 | return $results; |
48 | 48 | }; |
49 | 49 | } |
50 | 50 | |
51 | - public static function triggerOnce($name){ |
|
51 | + public static function triggerOnce($name) { |
|
52 | 52 | $res = static::trigger($name); |
53 | 53 | unset(static::$_listeners[$name]); |
54 | 54 | return $res; |
@@ -15,29 +15,30 @@ discard block |
||
15 | 15 | |
16 | 16 | protected static $_listeners = []; |
17 | 17 | |
18 | - public static function on($name,callable $listener){ |
|
18 | + public static function on($name,callable $listener) { |
|
19 | 19 | static::$_listeners[$name][] = $listener; |
20 | 20 | } |
21 | 21 | |
22 | - public static function single($name,callable $listener){ |
|
22 | + public static function single($name,callable $listener) { |
|
23 | 23 | static::$_listeners[$name] = [$listener]; |
24 | 24 | } |
25 | 25 | |
26 | - public static function off($name,callable $listener = null){ |
|
26 | + public static function off($name,callable $listener = null) { |
|
27 | 27 | if($listener === null) { |
28 | 28 | unset(static::$_listeners[$name]); |
29 | 29 | } else { |
30 | - if ($idx = array_search($listener,static::$_listeners[$name],true)) |
|
31 | - unset(static::$_listeners[$name][$idx]); |
|
30 | + if ($idx = array_search($listener,static::$_listeners[$name],true)) { |
|
31 | + unset(static::$_listeners[$name][$idx]); |
|
32 | + } |
|
32 | 33 | } |
33 | 34 | } |
34 | 35 | |
35 | - public static function alias($source,$alias){ |
|
36 | + public static function alias($source,$alias) { |
|
36 | 37 | static::$_listeners[$alias] =& static::$_listeners[$source]; |
37 | 38 | } |
38 | 39 | |
39 | - public static function trigger($name){ |
|
40 | - if (false === empty(static::$_listeners[$name])){ |
|
40 | + public static function trigger($name) { |
|
41 | + if (false === empty(static::$_listeners[$name])) { |
|
41 | 42 | $args = func_get_args(); |
42 | 43 | array_shift($args); |
43 | 44 | $results = []; |
@@ -48,7 +49,7 @@ discard block |
||
48 | 49 | }; |
49 | 50 | } |
50 | 51 | |
51 | - public static function triggerOnce($name){ |
|
52 | + public static function triggerOnce($name) { |
|
52 | 53 | $res = static::trigger($name); |
53 | 54 | unset(static::$_listeners[$name]); |
54 | 55 | return $res; |
@@ -13,15 +13,15 @@ discard block |
||
13 | 13 | class Negotiation { |
14 | 14 | protected $list; |
15 | 15 | |
16 | - public static function parse($query){ |
|
16 | + public static function parse($query) { |
|
17 | 17 | $list = new \SplPriorityQueue(); |
18 | 18 | array_map(function($e) use ($list) { |
19 | - preg_match_all('(([^;]+)(?=\s*;\s*(\w+)\s*=\s*([^;]+))*)',$e,$p); |
|
20 | - $params = array_map('trim',array_merge( |
|
21 | - [ 'type' => current($p[0]) ], array_combine($p[2], $p[3])) |
|
19 | + preg_match_all('(([^;]+)(?=\s*;\s*(\w+)\s*=\s*([^;]+))*)', $e, $p); |
|
20 | + $params = array_map('trim', array_merge( |
|
21 | + ['type' => current($p[0])], array_combine($p[2], $p[3])) |
|
22 | 22 | ); |
23 | 23 | unset($params['']); |
24 | - $params['q'] = isset($params['q']) ? 1.0*$params['q'] : $params['q'] = 1.0; |
|
24 | + $params['q'] = isset($params['q']) ? 1.0 * $params['q'] : $params['q'] = 1.0; |
|
25 | 25 | $list->insert($params, $params['q']); |
26 | 26 | },preg_split('(\s*,\s*)', $query)); |
27 | 27 | return array_values(iterator_to_array($list)); |
@@ -35,26 +35,26 @@ discard block |
||
35 | 35 | $this->list = self::parse(trim($query)); |
36 | 36 | } |
37 | 37 | |
38 | - public function preferred(){ |
|
38 | + public function preferred() { |
|
39 | 39 | return self::encodeParsedValue(current($this->list)); |
40 | 40 | } |
41 | 41 | |
42 | - protected static function encodeParsedValue($parsed){ |
|
43 | - unset($parsed['q']); // Hide quality key from output |
|
44 | - $type = $parsed['type']; // Extract type |
|
42 | + protected static function encodeParsedValue($parsed) { |
|
43 | + unset($parsed['q']);// Hide quality key from output |
|
44 | + $type = $parsed['type'];// Extract type |
|
45 | 45 | unset($parsed['type']); |
46 | - return implode(';', array_merge([$type], array_map(function($k,$v){ |
|
46 | + return implode(';', array_merge([$type], array_map(function($k, $v) { |
|
47 | 47 | return "$k=$v"; |
48 | 48 | }, array_keys($parsed), $parsed))); |
49 | 49 | } |
50 | 50 | |
51 | - public function best($choices){ |
|
52 | - $_choices = self::parse(trim($choices)); |
|
53 | - foreach ($this->list as $accept){ |
|
54 | - foreach ($_choices as $choice){ |
|
51 | + public function best($choices) { |
|
52 | + $_choices = self::parse(trim($choices)); |
|
53 | + foreach ($this->list as $accept) { |
|
54 | + foreach ($_choices as $choice) { |
|
55 | 55 | if (preg_match('('.strtr($accept["type"], |
56 | - [ '.' => '\.', '+' => '\+', '*' => '.+' ] |
|
57 | - ).')', $choice["type"])){ |
|
56 | + ['.' => '\.', '+' => '\+', '*' => '.+'] |
|
57 | + ).')', $choice["type"])) { |
|
58 | 58 | return self::encodeParsedValue($choice); |
59 | 59 | } |
60 | 60 | } |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | class Negotiation { |
14 | 14 | protected $list; |
15 | 15 | |
16 | - public static function parse($query){ |
|
16 | + public static function parse($query) { |
|
17 | 17 | $list = new \SplPriorityQueue(); |
18 | 18 | array_map(function($e) use ($list) { |
19 | 19 | preg_match_all('(([^;]+)(?=\s*;\s*(\w+)\s*=\s*([^;]+))*)',$e,$p); |
@@ -35,26 +35,26 @@ discard block |
||
35 | 35 | $this->list = self::parse(trim($query)); |
36 | 36 | } |
37 | 37 | |
38 | - public function preferred(){ |
|
38 | + public function preferred() { |
|
39 | 39 | return self::encodeParsedValue(current($this->list)); |
40 | 40 | } |
41 | 41 | |
42 | - protected static function encodeParsedValue($parsed){ |
|
42 | + protected static function encodeParsedValue($parsed) { |
|
43 | 43 | unset($parsed['q']); // Hide quality key from output |
44 | 44 | $type = $parsed['type']; // Extract type |
45 | 45 | unset($parsed['type']); |
46 | - return implode(';', array_merge([$type], array_map(function($k,$v){ |
|
46 | + return implode(';', array_merge([$type], array_map(function($k,$v) { |
|
47 | 47 | return "$k=$v"; |
48 | 48 | }, array_keys($parsed), $parsed))); |
49 | 49 | } |
50 | 50 | |
51 | - public function best($choices){ |
|
51 | + public function best($choices) { |
|
52 | 52 | $_choices = self::parse(trim($choices)); |
53 | - foreach ($this->list as $accept){ |
|
54 | - foreach ($_choices as $choice){ |
|
53 | + foreach ($this->list as $accept) { |
|
54 | + foreach ($_choices as $choice) { |
|
55 | 55 | if (preg_match('('.strtr($accept["type"], |
56 | 56 | [ '.' => '\.', '+' => '\+', '*' => '.+' ] |
57 | - ).')', $choice["type"])){ |
|
57 | + ).')', $choice["type"])) { |
|
58 | 58 | return self::encodeParsedValue($choice); |
59 | 59 | } |
60 | 60 | } |
@@ -10,7 +10,7 @@ |
||
10 | 10 | * @copyright Caffeina srl - 2015 - http://caffeina.it |
11 | 11 | */ |
12 | 12 | |
13 | - class Password { |
|
13 | + class Password { |
|
14 | 14 | use Module; |
15 | 15 | |
16 | 16 | /** |
@@ -18,12 +18,12 @@ discard block |
||
18 | 18 | * @param string $password |
19 | 19 | * @return string |
20 | 20 | */ |
21 | - public static function make($password){ |
|
21 | + public static function make($password) { |
|
22 | 22 | // Pre PHP 5.5 support |
23 | 23 | if (!defined('PASSWORD_DEFAULT')) { |
24 | - return '$5h$'.hash('sha1',$password); |
|
24 | + return '$5h$'.hash('sha1', $password); |
|
25 | 25 | } else { |
26 | - return password_hash($password,PASSWORD_BCRYPT,['cost' => 12]); |
|
26 | + return password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]); |
|
27 | 27 | } |
28 | 28 | } |
29 | 29 | |
@@ -33,12 +33,12 @@ discard block |
||
33 | 33 | * @param string $hash The hash to match against |
34 | 34 | * @return bool Returns `true` if hash match password |
35 | 35 | */ |
36 | - public static function verify($password, $hash){ |
|
36 | + public static function verify($password, $hash) { |
|
37 | 37 | // Pre PHP 5.5 support |
38 | - if (!defined('PASSWORD_DEFAULT') || substr($hash,0,4)=='$5h$') { |
|
39 | - return '$5h$'.hash('sha1',$password) == $hash; |
|
38 | + if (!defined('PASSWORD_DEFAULT') || substr($hash, 0, 4) == '$5h$') { |
|
39 | + return '$5h$'.hash('sha1', $password) == $hash; |
|
40 | 40 | } else { |
41 | - return password_verify($password,$hash); |
|
41 | + return password_verify($password, $hash); |
|
42 | 42 | } |
43 | 43 | } |
44 | 44 | |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | * @param string $b Second string to compare |
50 | 50 | * @return boll Returns `true` if strings are the same |
51 | 51 | */ |
52 | - public static function compare($a, $b){ |
|
52 | + public static function compare($a, $b) { |
|
53 | 53 | return hash_equals($a, $b); |
54 | 54 | } |
55 | 55 | |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | |
59 | 59 | // Polyfill hash_equals (PHP < 5.6.0) |
60 | 60 | // http://php.net/manual/en/function.hash-equals.php |
61 | -if(!function_exists('hash_equals')) { |
|
61 | +if (!function_exists('hash_equals')) { |
|
62 | 62 | function hash_equals($a, $b) { |
63 | 63 | return substr_count("$a" ^ "$b", "\0") * 2 === strlen("$a$b"); |
64 | 64 | } |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | * @param string $password |
19 | 19 | * @return string |
20 | 20 | */ |
21 | - public static function make($password){ |
|
21 | + public static function make($password) { |
|
22 | 22 | // Pre PHP 5.5 support |
23 | 23 | if (!defined('PASSWORD_DEFAULT')) { |
24 | 24 | return '$5h$'.hash('sha1',$password); |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | * @param string $hash The hash to match against |
34 | 34 | * @return bool Returns `true` if hash match password |
35 | 35 | */ |
36 | - public static function verify($password, $hash){ |
|
36 | + public static function verify($password, $hash) { |
|
37 | 37 | // Pre PHP 5.5 support |
38 | 38 | if (!defined('PASSWORD_DEFAULT') || substr($hash,0,4)=='$5h$') { |
39 | 39 | return '$5h$'.hash('sha1',$password) == $hash; |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | * @param string $b Second string to compare |
50 | 50 | * @return boll Returns `true` if strings are the same |
51 | 51 | */ |
52 | - public static function compare($a, $b){ |
|
52 | + public static function compare($a, $b) { |
|
53 | 53 | return hash_equals($a, $b); |
54 | 54 | } |
55 | 55 |
@@ -14,8 +14,8 @@ |
||
14 | 14 | use Module; |
15 | 15 | |
16 | 16 | public $file, |
17 | - $name, |
|
18 | - $zip; |
|
17 | + $name, |
|
18 | + $zip; |
|
19 | 19 | |
20 | 20 | public static function create($name=''){ |
21 | 21 | return new ZIP($name); |
@@ -17,42 +17,42 @@ discard block |
||
17 | 17 | $name, |
18 | 18 | $zip; |
19 | 19 | |
20 | - public static function create($name=''){ |
|
20 | + public static function create($name = '') { |
|
21 | 21 | return new ZIP($name); |
22 | 22 | } |
23 | 23 | |
24 | - public function __construct($name=''){ |
|
25 | - $this->name = preg_replace('/\.zip$/','',($name?:tempnam(sys_get_temp_dir(), 'ZExp').'-archive')); |
|
26 | - $this->file = $this->name . '.zip'; |
|
27 | - if (!preg_match('~^/|\./|\.\./~',$this->file)) $this->file = './'.$this->file; |
|
24 | + public function __construct($name = '') { |
|
25 | + $this->name = preg_replace('/\.zip$/', '', ($name ?: tempnam(sys_get_temp_dir(), 'ZExp').'-archive')); |
|
26 | + $this->file = $this->name.'.zip'; |
|
27 | + if (!preg_match('~^/|\./|\.\./~', $this->file)) $this->file = './'.$this->file; |
|
28 | 28 | $this->zip = new \ZipArchive; |
29 | - if ( true !== ($e = $this->zip->open($this->file, |
|
29 | + if (true !== ($e = $this->zip->open($this->file, |
|
30 | 30 | \ZipArchive::CREATE || \ZipArchive::OVERWRITE |
31 | 31 | ))) { |
32 | 32 | throw new Exception("Error opening temp ZIP file [".($this->file)."] Code $e", 1); |
33 | 33 | } |
34 | 34 | } |
35 | 35 | |
36 | - public function __destruct(){ |
|
36 | + public function __destruct() { |
|
37 | 37 | $this->close(); |
38 | 38 | } |
39 | 39 | |
40 | - public function path(){ |
|
40 | + public function path() { |
|
41 | 41 | return $this->file; |
42 | 42 | } |
43 | 43 | |
44 | - public function write($filename, $data){ |
|
44 | + public function write($filename, $data) { |
|
45 | 45 | $this->zip->addFromString($filename, $data); |
46 | 46 | return $this; |
47 | 47 | } |
48 | 48 | |
49 | - public function close(){ |
|
50 | - if($this->zip) @$this->zip->close(); |
|
49 | + public function close() { |
|
50 | + if ($this->zip) @$this->zip->close(); |
|
51 | 51 | return $this; |
52 | 52 | } |
53 | 53 | |
54 | - public function addDirectory($folder, $root=null) { |
|
55 | - $folder = rtrim($folder,'/'); |
|
54 | + public function addDirectory($folder, $root = null) { |
|
55 | + $folder = rtrim($folder, '/'); |
|
56 | 56 | if (null === $root) { |
57 | 57 | $root = dirname($folder); |
58 | 58 | $folder = basename($folder); |
@@ -60,25 +60,25 @@ discard block |
||
60 | 60 | $this->zip->addEmptyDir($folder); |
61 | 61 | foreach (glob("$root/$folder/*") as $item) { |
62 | 62 | if (is_dir($item)) { |
63 | - $this->addDirectory(str_replace($root,'',$item),$root); |
|
64 | - } else if (is_file($item)) { |
|
65 | - $this->zip->addFile($item, str_replace($root,'',$item)); |
|
63 | + $this->addDirectory(str_replace($root, '', $item), $root); |
|
64 | + } else if (is_file($item)) { |
|
65 | + $this->zip->addFile($item, str_replace($root, '', $item)); |
|
66 | 66 | } |
67 | 67 | } |
68 | 68 | |
69 | 69 | return $this; |
70 | 70 | } |
71 | 71 | |
72 | - public function download(){ |
|
72 | + public function download() { |
|
73 | 73 | @$this->zip->close(); |
74 | 74 | header('Content-Type: application/zip'); |
75 | - header('Content-Disposition: attachment;filename="'.$this->name.'"',true); |
|
75 | + header('Content-Disposition: attachment;filename="'.$this->name.'"', true); |
|
76 | 76 | header('Content-Transfer-Encoding: binary'); |
77 | 77 | header('Expires: 0'); |
78 | 78 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
79 | 79 | header('Pragma: public'); |
80 | 80 | header('Content-Length: '.filesize($this->file)); |
81 | - while(ob_get_level()) ob_end_clean(); |
|
81 | + while (ob_get_level()) ob_end_clean(); |
|
82 | 82 | readfile($this->file); |
83 | 83 | exit; |
84 | 84 | } |
@@ -17,14 +17,16 @@ discard block |
||
17 | 17 | $name, |
18 | 18 | $zip; |
19 | 19 | |
20 | - public static function create($name=''){ |
|
20 | + public static function create($name='') { |
|
21 | 21 | return new ZIP($name); |
22 | 22 | } |
23 | 23 | |
24 | - public function __construct($name=''){ |
|
24 | + public function __construct($name='') { |
|
25 | 25 | $this->name = preg_replace('/\.zip$/','',($name?:tempnam(sys_get_temp_dir(), 'ZExp').'-archive')); |
26 | 26 | $this->file = $this->name . '.zip'; |
27 | - if (!preg_match('~^/|\./|\.\./~',$this->file)) $this->file = './'.$this->file; |
|
27 | + if (!preg_match('~^/|\./|\.\./~',$this->file)) { |
|
28 | + $this->file = './'.$this->file; |
|
29 | + } |
|
28 | 30 | $this->zip = new \ZipArchive; |
29 | 31 | if ( true !== ($e = $this->zip->open($this->file, |
30 | 32 | \ZipArchive::CREATE || \ZipArchive::OVERWRITE |
@@ -33,21 +35,23 @@ discard block |
||
33 | 35 | } |
34 | 36 | } |
35 | 37 | |
36 | - public function __destruct(){ |
|
38 | + public function __destruct() { |
|
37 | 39 | $this->close(); |
38 | 40 | } |
39 | 41 | |
40 | - public function path(){ |
|
42 | + public function path() { |
|
41 | 43 | return $this->file; |
42 | 44 | } |
43 | 45 | |
44 | - public function write($filename, $data){ |
|
46 | + public function write($filename, $data) { |
|
45 | 47 | $this->zip->addFromString($filename, $data); |
46 | 48 | return $this; |
47 | 49 | } |
48 | 50 | |
49 | - public function close(){ |
|
50 | - if($this->zip) @$this->zip->close(); |
|
51 | + public function close() { |
|
52 | + if($this->zip) { |
|
53 | + @$this->zip->close(); |
|
54 | + } |
|
51 | 55 | return $this; |
52 | 56 | } |
53 | 57 | |
@@ -61,7 +65,7 @@ discard block |
||
61 | 65 | foreach (glob("$root/$folder/*") as $item) { |
62 | 66 | if (is_dir($item)) { |
63 | 67 | $this->addDirectory(str_replace($root,'',$item),$root); |
64 | - } else if (is_file($item)) { |
|
68 | + } else if (is_file($item)) { |
|
65 | 69 | $this->zip->addFile($item, str_replace($root,'',$item)); |
66 | 70 | } |
67 | 71 | } |
@@ -69,7 +73,7 @@ discard block |
||
69 | 73 | return $this; |
70 | 74 | } |
71 | 75 | |
72 | - public function download(){ |
|
76 | + public function download() { |
|
73 | 77 | @$this->zip->close(); |
74 | 78 | header('Content-Type: application/zip'); |
75 | 79 | header('Content-Disposition: attachment;filename="'.$this->name.'"',true); |
@@ -31,7 +31,7 @@ |
||
31 | 31 | } |
32 | 32 | } |
33 | 33 | } |
34 | - parent::__construct($data, static::ARRAY_AS_PROPS); |
|
34 | + parent::__construct($data, static::ARRAY_AS_PROPS); |
|
35 | 35 | } else { |
36 | 36 | throw new InvalidArgumentException( |
37 | 37 | 'Argument must be a string containing valid JSON, an array or an stdClass.' |
@@ -21,12 +21,12 @@ discard block |
||
21 | 21 | * @param mixed $input The object/array/json_encoded object to wrap |
22 | 22 | * @param boolean $deep Wrap also deep branches as Objects |
23 | 23 | */ |
24 | - public function __construct($input=[], $deep=true){ |
|
24 | + public function __construct($input=[], $deep=true) { |
|
25 | 25 | $data = is_string($input) ? json_decode($input,true) : (array)$input; |
26 | - if (is_array($data)){ |
|
26 | + if (is_array($data)) { |
|
27 | 27 | if ($deep) { |
28 | 28 | foreach ($data as $key => &$value) { |
29 | - if (is_array($value) || is_a($value,'stdClass')){ |
|
29 | + if (is_array($value) || is_a($value,'stdClass')) { |
|
30 | 30 | $value = new self($value); |
31 | 31 | } |
32 | 32 | } |
@@ -42,17 +42,18 @@ discard block |
||
42 | 42 | /** |
43 | 43 | * ArrayObject::offsetSet |
44 | 44 | */ |
45 | - public function offsetSet($key, $value){ |
|
46 | - if ( is_array($value) ) |
|
47 | - parent::offsetSet($key, new static($value)); |
|
48 | - else |
|
49 | - parent::offsetSet($key, $value); |
|
45 | + public function offsetSet($key, $value) { |
|
46 | + if ( is_array($value) ) { |
|
47 | + parent::offsetSet($key, new static($value)); |
|
48 | + } else { |
|
49 | + parent::offsetSet($key, $value); |
|
50 | + } |
|
50 | 51 | } |
51 | 52 | |
52 | 53 | /** |
53 | 54 | * ArrayObject::offsetGet |
54 | 55 | */ |
55 | - public function offsetGet($key){ |
|
56 | + public function offsetGet($key) { |
|
56 | 57 | $raw = parent::offsetGet($key); |
57 | 58 | return is_callable($raw) ? call_user_func($raw) : $raw; |
58 | 59 | } |
@@ -60,10 +61,12 @@ discard block |
||
60 | 61 | /** |
61 | 62 | * Emulate object methods |
62 | 63 | */ |
63 | - public function __call($method, $args){ |
|
64 | + public function __call($method, $args) { |
|
64 | 65 | $raw = parent::offsetGet($method); |
65 | 66 | if (is_callable($raw)) { |
66 | - if ($raw instanceof \Closure) $raw->bindTo($this); |
|
67 | + if ($raw instanceof \Closure) { |
|
68 | + $raw->bindTo($this); |
|
69 | + } |
|
67 | 70 | return call_user_func_array($raw, $args); |
68 | 71 | } |
69 | 72 | } |
@@ -72,7 +75,7 @@ discard block |
||
72 | 75 | * If casted as a string, return a JSON rappresentation of the wrapped payload |
73 | 76 | * @return string |
74 | 77 | */ |
75 | - public function __toString(){ |
|
78 | + public function __toString() { |
|
76 | 79 | return json_encode($this,JSON_NUMERIC_CHECK); |
77 | 80 | } |
78 | 81 | |
@@ -103,7 +106,7 @@ discard block |
||
103 | 106 | return $frag ? '' : $ptr; |
104 | 107 | } |
105 | 108 | |
106 | - public static function create($class, $args = null){ |
|
109 | + public static function create($class, $args = null) { |
|
107 | 110 | return is_array($args) ? (new ReflectionClass($class))->newInstanceArgs($args) : new $class; |
108 | 111 | } |
109 | 112 |
@@ -21,12 +21,12 @@ discard block |
||
21 | 21 | * @param mixed $input The object/array/json_encoded object to wrap |
22 | 22 | * @param boolean $deep Wrap also deep branches as Objects |
23 | 23 | */ |
24 | - public function __construct($input=[], $deep=true){ |
|
25 | - $data = is_string($input) ? json_decode($input,true) : (array)$input; |
|
26 | - if (is_array($data)){ |
|
24 | + public function __construct($input = [], $deep = true) { |
|
25 | + $data = is_string($input) ? json_decode($input, true) : (array) $input; |
|
26 | + if (is_array($data)) { |
|
27 | 27 | if ($deep) { |
28 | 28 | foreach ($data as $key => &$value) { |
29 | - if (is_array($value) || is_a($value,'stdClass')){ |
|
29 | + if (is_array($value) || is_a($value, 'stdClass')) { |
|
30 | 30 | $value = new self($value); |
31 | 31 | } |
32 | 32 | } |
@@ -42,8 +42,8 @@ discard block |
||
42 | 42 | /** |
43 | 43 | * ArrayObject::offsetSet |
44 | 44 | */ |
45 | - public function offsetSet($key, $value){ |
|
46 | - if ( is_array($value) ) |
|
45 | + public function offsetSet($key, $value) { |
|
46 | + if (is_array($value)) |
|
47 | 47 | parent::offsetSet($key, new static($value)); |
48 | 48 | else |
49 | 49 | parent::offsetSet($key, $value); |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | /** |
53 | 53 | * ArrayObject::offsetGet |
54 | 54 | */ |
55 | - public function offsetGet($key){ |
|
55 | + public function offsetGet($key) { |
|
56 | 56 | $raw = parent::offsetGet($key); |
57 | 57 | return is_callable($raw) ? call_user_func($raw) : $raw; |
58 | 58 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | /** |
61 | 61 | * Emulate object methods |
62 | 62 | */ |
63 | - public function __call($method, $args){ |
|
63 | + public function __call($method, $args) { |
|
64 | 64 | $raw = parent::offsetGet($method); |
65 | 65 | if (is_callable($raw)) { |
66 | 66 | if ($raw instanceof \Closure) $raw->bindTo($this); |
@@ -72,8 +72,8 @@ discard block |
||
72 | 72 | * If casted as a string, return a JSON rappresentation of the wrapped payload |
73 | 73 | * @return string |
74 | 74 | */ |
75 | - public function __toString(){ |
|
76 | - return json_encode($this,JSON_NUMERIC_CHECK); |
|
75 | + public function __toString() { |
|
76 | + return json_encode($this, JSON_NUMERIC_CHECK); |
|
77 | 77 | } |
78 | 78 | |
79 | 79 | /** |
@@ -84,22 +84,22 @@ discard block |
||
84 | 84 | */ |
85 | 85 | |
86 | 86 | public static function fetch($path, $root) { |
87 | - $_ = (array)$root; |
|
88 | - if (strpos($path,'.') === false) { |
|
87 | + $_ = (array) $root; |
|
88 | + if (strpos($path, '.') === false) { |
|
89 | 89 | return isset($_[$path]) ? $_[$path] : null; |
90 | 90 | } else { |
91 | - list($frag,$rest) = explode('.', $path, 2); |
|
91 | + list($frag, $rest) = explode('.', $path, 2); |
|
92 | 92 | if ($rest) { |
93 | 93 | return isset($_[$frag]) ? self::fetch($rest, $_[$frag]) : null; |
94 | 94 | } elseif ($frag) { |
95 | - return (array)$_[$frag]; |
|
95 | + return (array) $_[$frag]; |
|
96 | 96 | } else { |
97 | 97 | return null; |
98 | 98 | } |
99 | 99 | } |
100 | 100 | } |
101 | 101 | |
102 | - public static function create($class, $args = null){ |
|
102 | + public static function create($class, $args = null) { |
|
103 | 103 | return is_array($args) ? (new ReflectionClass($class))->newInstanceArgs($args) : new $class; |
104 | 104 | } |
105 | 105 |
@@ -15,41 +15,41 @@ discard block |
||
15 | 15 | protected static $loaded = false; |
16 | 16 | protected static $fields = []; |
17 | 17 | |
18 | - protected static function init(){ |
|
19 | - if(false===static::$loaded){ |
|
20 | - static::load(Session::get('core.messages',[])); |
|
18 | + protected static function init() { |
|
19 | + if (false === static::$loaded) { |
|
20 | + static::load(Session::get('core.messages', [])); |
|
21 | 21 | static::$loaded = true; |
22 | 22 | } |
23 | 23 | } |
24 | 24 | |
25 | - public static function & get($key,$default=null){ |
|
25 | + public static function & get($key, $default = null){ |
|
26 | 26 | static::init(); |
27 | - $value = parent::get($key,''); |
|
28 | - parent::delete($key,''); |
|
29 | - Session::set('core.messages',parent::all()); |
|
27 | + $value = parent::get($key, ''); |
|
28 | + parent::delete($key, ''); |
|
29 | + Session::set('core.messages', parent::all()); |
|
30 | 30 | return $value; |
31 | 31 | } |
32 | 32 | |
33 | - public static function set($key,$data=null){ |
|
33 | + public static function set($key, $data = null) { |
|
34 | 34 | static::init(); |
35 | - parent::set($key,$data); |
|
36 | - return Session::set('core.messages',parent::all()); |
|
35 | + parent::set($key, $data); |
|
36 | + return Session::set('core.messages', parent::all()); |
|
37 | 37 | } |
38 | 38 | |
39 | - public static function add($key,$data=null){ |
|
39 | + public static function add($key, $data = null) { |
|
40 | 40 | static::init(); |
41 | - $d = parent::get($key,[]); |
|
41 | + $d = parent::get($key, []); |
|
42 | 42 | $d[] = $data; |
43 | - parent::set($key,$d); |
|
44 | - return Session::set('core.messages',parent::all()); |
|
43 | + parent::set($key, $d); |
|
44 | + return Session::set('core.messages', parent::all()); |
|
45 | 45 | } |
46 | 46 | |
47 | - public static function & all($key=null){ |
|
47 | + public static function & all($key = null){ |
|
48 | 48 | static::init(); |
49 | - if($key){ |
|
50 | - $all = parent::get($key,[]); |
|
49 | + if ($key) { |
|
50 | + $all = parent::get($key, []); |
|
51 | 51 | parent::delete($key); |
52 | - Session::set('core.messages',parent::all()); |
|
52 | + Session::set('core.messages', parent::all()); |
|
53 | 53 | } else { |
54 | 54 | $all = parent::all(); |
55 | 55 | static::clear(); |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | return $all; |
58 | 58 | } |
59 | 59 | |
60 | - public static function clear(){ |
|
60 | + public static function clear() { |
|
61 | 61 | static::init(); |
62 | 62 | parent::clear(); |
63 | 63 | Session::delete('core.messages'); |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | * Return a read-only accessor to messages variables for in-view use. |
69 | 69 | * @return MessageReadOnly |
70 | 70 | */ |
71 | - public static function readOnly(){ |
|
71 | + public static function readOnly() { |
|
72 | 72 | return new MessageReadOnly(); |
73 | 73 | } |
74 | 74 | |
@@ -81,11 +81,11 @@ discard block |
||
81 | 81 | |
82 | 82 | class MessageReadOnly { |
83 | 83 | |
84 | - public function __get($key){ |
|
84 | + public function __get($key) { |
|
85 | 85 | return Message::get($key); |
86 | 86 | } |
87 | 87 | |
88 | - public function __isset($key){ |
|
88 | + public function __isset($key) { |
|
89 | 89 | return true; |
90 | 90 | } |
91 | 91 |
@@ -15,8 +15,8 @@ discard block |
||
15 | 15 | protected static $loaded = false; |
16 | 16 | protected static $fields = []; |
17 | 17 | |
18 | - protected static function init(){ |
|
19 | - if(false===static::$loaded){ |
|
18 | + protected static function init() { |
|
19 | + if(false===static::$loaded) { |
|
20 | 20 | static::load(Session::get('core.messages',[])); |
21 | 21 | static::$loaded = true; |
22 | 22 | } |
@@ -30,13 +30,13 @@ discard block |
||
30 | 30 | return $value; |
31 | 31 | } |
32 | 32 | |
33 | - public static function set($key,$data=null){ |
|
33 | + public static function set($key,$data=null) { |
|
34 | 34 | static::init(); |
35 | 35 | parent::set($key,$data); |
36 | 36 | return Session::set('core.messages',parent::all()); |
37 | 37 | } |
38 | 38 | |
39 | - public static function add($key,$data=null){ |
|
39 | + public static function add($key,$data=null) { |
|
40 | 40 | static::init(); |
41 | 41 | $d = parent::get($key,[]); |
42 | 42 | $d[] = $data; |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | |
47 | 47 | public static function & all($key=null){ |
48 | 48 | static::init(); |
49 | - if($key){ |
|
49 | + if($key) { |
|
50 | 50 | $all = parent::get($key,[]); |
51 | 51 | parent::delete($key); |
52 | 52 | Session::set('core.messages',parent::all()); |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | return $all; |
58 | 58 | } |
59 | 59 | |
60 | - public static function clear(){ |
|
60 | + public static function clear() { |
|
61 | 61 | static::init(); |
62 | 62 | parent::clear(); |
63 | 63 | Session::delete('core.messages'); |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | * Return a read-only accessor to messages variables for in-view use. |
69 | 69 | * @return MessageReadOnly |
70 | 70 | */ |
71 | - public static function readOnly(){ |
|
71 | + public static function readOnly() { |
|
72 | 72 | return new MessageReadOnly(); |
73 | 73 | } |
74 | 74 | |
@@ -81,11 +81,11 @@ discard block |
||
81 | 81 | |
82 | 82 | class MessageReadOnly { |
83 | 83 | |
84 | - public function __get($key){ |
|
84 | + public function __get($key) { |
|
85 | 85 | return Message::get($key); |
86 | 86 | } |
87 | 87 | |
88 | - public function __isset($key){ |
|
88 | + public function __isset($key) { |
|
89 | 89 | return true; |
90 | 90 | } |
91 | 91 |
@@ -27,54 +27,54 @@ discard block |
||
27 | 27 | $format = self::STANDARD, |
28 | 28 | $savedheaders = false; |
29 | 29 | |
30 | - public static function open($file, $format=self::AUTO){ |
|
31 | - return new static($file,self::READ,$format); |
|
30 | + public static function open($file, $format = self::AUTO) { |
|
31 | + return new static($file, self::READ, $format); |
|
32 | 32 | } |
33 | 33 | |
34 | - public static function create($file, $format=self::STANDARD){ |
|
35 | - return new static($file,self::WRITE, $format); |
|
34 | + public static function create($file, $format = self::STANDARD) { |
|
35 | + return new static($file, self::WRITE, $format); |
|
36 | 36 | } |
37 | 37 | |
38 | - public static function fromSQL($sql, $format=self::AUTO){ |
|
38 | + public static function fromSQL($sql, $format = self::AUTO) { |
|
39 | 39 | $csv = static::create(tempnam(sys_get_temp_dir(), 'CSVx'), $format); |
40 | - SQL::each($sql,function($row) use (&$csv){ |
|
40 | + SQL::each($sql, function($row) use (&$csv){ |
|
41 | 41 | $csv->write($row); |
42 | 42 | }); |
43 | 43 | return $csv; |
44 | 44 | } |
45 | 45 | |
46 | - public static function fromTable($table, $format=self::AUTO){ |
|
46 | + public static function fromTable($table, $format = self::AUTO) { |
|
47 | 47 | $csv = static::create(tempnam(sys_get_temp_dir(), 'CSVx'), $format); |
48 | - foreach($table as $row){ |
|
48 | + foreach ($table as $row) { |
|
49 | 49 | $csv->write($row); |
50 | 50 | } |
51 | 51 | return $csv; |
52 | 52 | } |
53 | 53 | |
54 | - public function __construct($file, $mode=self::READ, $format=self::AUTO){ |
|
54 | + public function __construct($file, $mode = self::READ, $format = self::AUTO) { |
|
55 | 55 | $this->mode = $mode; |
56 | - $this->file = new \SplFileObject($file,'c+'); |
|
56 | + $this->file = new \SplFileObject($file, 'c+'); |
|
57 | 57 | if (!$this->file->valid()) throw new Exception("Error opening CSV file [$file]", 1); |
58 | 58 | $this->file->setFlags( |
59 | - \SplFileObject::READ_CSV | // set file reading mode to csv |
|
60 | - \SplFileObject::SKIP_EMPTY | // ignore empty lines |
|
59 | + \SplFileObject::READ_CSV | // set file reading mode to csv |
|
60 | + \SplFileObject::SKIP_EMPTY | // ignore empty lines |
|
61 | 61 | \SplFileObject::DROP_NEW_LINE // drop new line from last column in record |
62 | 62 | ); |
63 | - $this->format = ($format==self::AUTO ? $this->guessSeparator() : $format); |
|
64 | - $this->file->setCsvControl($this->format,'"',"\\"); |
|
63 | + $this->format = ($format == self::AUTO ? $this->guessSeparator() : $format); |
|
64 | + $this->file->setCsvControl($this->format, '"', "\\"); |
|
65 | 65 | } |
66 | 66 | |
67 | - private function guessSeparator($checkLines = 2){ |
|
67 | + private function guessSeparator($checkLines = 2) { |
|
68 | 68 | if ($this->mode == self::WRITE) return self::STANDARD; |
69 | - $delimiters = [",","\t",";"]; |
|
69 | + $delimiters = [",", "\t", ";"]; |
|
70 | 70 | $results = []; |
71 | 71 | $this->file->rewind(); |
72 | 72 | while ($checkLines--) { |
73 | 73 | $line = $this->file->fgets(); |
74 | - foreach ($delimiters as $delimiter){ |
|
74 | + foreach ($delimiters as $delimiter) { |
|
75 | 75 | $fields = preg_split('/['.$delimiter.']/', $line); |
76 | - if(count($fields) > 1){ |
|
77 | - if(empty($results[$delimiter])){ |
|
76 | + if (count($fields) > 1) { |
|
77 | + if (empty($results[$delimiter])) { |
|
78 | 78 | $results[$delimiter] = 1; |
79 | 79 | } else { |
80 | 80 | $results[$delimiter]++; |
@@ -87,9 +87,9 @@ discard block |
||
87 | 87 | return $results[0]; |
88 | 88 | } |
89 | 89 | |
90 | - public function write($row){ |
|
90 | + public function write($row) { |
|
91 | 91 | if ($this->mode != self::WRITE) return; |
92 | - $row = (array)$row; |
|
92 | + $row = (array) $row; |
|
93 | 93 | if (false === $this->savedheaders) { |
94 | 94 | $this->schema(array_keys($row)); |
95 | 95 | } |
@@ -100,11 +100,11 @@ discard block |
||
100 | 100 | $this->file->fputcsv($row_t); |
101 | 101 | } |
102 | 102 | |
103 | - public function read(){ |
|
103 | + public function read() { |
|
104 | 104 | if ($this->mode != self::READ) return; |
105 | - foreach($this->file as $row){ |
|
106 | - if ($row){ |
|
107 | - if(!$this->headers) { |
|
105 | + foreach ($this->file as $row) { |
|
106 | + if ($row) { |
|
107 | + if (!$this->headers) { |
|
108 | 108 | $this->headers = $row; |
109 | 109 | continue; |
110 | 110 | } |
@@ -114,18 +114,18 @@ discard block |
||
114 | 114 | return; |
115 | 115 | } |
116 | 116 | |
117 | - public function each(callable $looper = null){ |
|
117 | + public function each(callable $looper = null) { |
|
118 | 118 | if ($looper) { |
119 | - foreach($this->read() as $row) $looper($row); |
|
119 | + foreach ($this->read() as $row) $looper($row); |
|
120 | 120 | return $this; |
121 | 121 | } else { |
122 | 122 | $results = []; |
123 | - foreach($this->read() as $row) $results[] = $row; |
|
123 | + foreach ($this->read() as $row) $results[] = $row; |
|
124 | 124 | return $results; |
125 | 125 | } |
126 | 126 | } |
127 | 127 | |
128 | - public function convert($filename, $format=self::STANDARD){ |
|
128 | + public function convert($filename, $format = self::STANDARD) { |
|
129 | 129 | if ($this->mode != self::READ) return; |
130 | 130 | if ($format == self::AUTO) $format = self::STANDARD; |
131 | 131 | $csv = CSV::create($filename, CSV::EXCEL); |
@@ -135,18 +135,18 @@ discard block |
||
135 | 135 | return $csv; |
136 | 136 | } |
137 | 137 | |
138 | - public function flush(){ |
|
138 | + public function flush() { |
|
139 | 139 | if ($this->mode == self::WRITE) { |
140 | 140 | $this->file->fflush(); |
141 | 141 | } |
142 | 142 | } |
143 | 143 | |
144 | - public function schema($schema=null){ |
|
145 | - if($schema){ |
|
146 | - $this->headers = array_values((array)$schema); |
|
144 | + public function schema($schema = null) { |
|
145 | + if ($schema) { |
|
146 | + $this->headers = array_values((array) $schema); |
|
147 | 147 | if ($this->mode == self::WRITE) { |
148 | 148 | $this->savedheaders = true; |
149 | - $this->template = array_combine($this->headers, array_pad([],count($this->headers),'')); |
|
149 | + $this->template = array_combine($this->headers, array_pad([], count($this->headers), '')); |
|
150 | 150 | $this->file->fputcsv($this->headers); |
151 | 151 | } |
152 | 152 | return $this; |
@@ -155,13 +155,13 @@ discard block |
||
155 | 155 | } |
156 | 156 | } |
157 | 157 | |
158 | - public function asString(){ |
|
158 | + public function asString() { |
|
159 | 159 | $this->flush(); |
160 | 160 | return file_get_contents($this->file->getPathname()); |
161 | 161 | } |
162 | 162 | |
163 | - public function __toString(){ |
|
164 | - try { return $this->asString(); } catch(\Exception $e) { return ''; } |
|
163 | + public function __toString() { |
|
164 | + try { return $this->asString();} catch (\Exception $e) { return '';} |
|
165 | 165 | } |
166 | 166 | |
167 | 167 | } |
@@ -27,34 +27,36 @@ discard block |
||
27 | 27 | $format = self::STANDARD, |
28 | 28 | $savedheaders = false; |
29 | 29 | |
30 | - public static function open($file, $format=self::AUTO){ |
|
30 | + public static function open($file, $format=self::AUTO) { |
|
31 | 31 | return new static($file,self::READ,$format); |
32 | 32 | } |
33 | 33 | |
34 | - public static function create($file, $format=self::STANDARD){ |
|
34 | + public static function create($file, $format=self::STANDARD) { |
|
35 | 35 | return new static($file,self::WRITE, $format); |
36 | 36 | } |
37 | 37 | |
38 | - public static function fromSQL($sql, $format=self::AUTO){ |
|
38 | + public static function fromSQL($sql, $format=self::AUTO) { |
|
39 | 39 | $csv = static::create(tempnam(sys_get_temp_dir(), 'CSVx'), $format); |
40 | - SQL::each($sql,function($row) use (&$csv){ |
|
40 | + SQL::each($sql,function($row) use (&$csv) { |
|
41 | 41 | $csv->write($row); |
42 | 42 | }); |
43 | 43 | return $csv; |
44 | 44 | } |
45 | 45 | |
46 | - public static function fromTable($table, $format=self::AUTO){ |
|
46 | + public static function fromTable($table, $format=self::AUTO) { |
|
47 | 47 | $csv = static::create(tempnam(sys_get_temp_dir(), 'CSVx'), $format); |
48 | - foreach($table as $row){ |
|
48 | + foreach($table as $row) { |
|
49 | 49 | $csv->write($row); |
50 | 50 | } |
51 | 51 | return $csv; |
52 | 52 | } |
53 | 53 | |
54 | - public function __construct($file, $mode=self::READ, $format=self::AUTO){ |
|
54 | + public function __construct($file, $mode=self::READ, $format=self::AUTO) { |
|
55 | 55 | $this->mode = $mode; |
56 | 56 | $this->file = new \SplFileObject($file,'c+'); |
57 | - if (!$this->file->valid()) throw new Exception("Error opening CSV file [$file]", 1); |
|
57 | + if (!$this->file->valid()) { |
|
58 | + throw new Exception("Error opening CSV file [$file]", 1); |
|
59 | + } |
|
58 | 60 | $this->file->setFlags( |
59 | 61 | \SplFileObject::READ_CSV | // set file reading mode to csv |
60 | 62 | \SplFileObject::SKIP_EMPTY | // ignore empty lines |
@@ -64,17 +66,19 @@ discard block |
||
64 | 66 | $this->file->setCsvControl($this->format,'"',"\\"); |
65 | 67 | } |
66 | 68 | |
67 | - private function guessSeparator($checkLines = 2){ |
|
68 | - if ($this->mode == self::WRITE) return self::STANDARD; |
|
69 | + private function guessSeparator($checkLines = 2) { |
|
70 | + if ($this->mode == self::WRITE) { |
|
71 | + return self::STANDARD; |
|
72 | + } |
|
69 | 73 | $delimiters = [",","\t",";"]; |
70 | 74 | $results = []; |
71 | 75 | $this->file->rewind(); |
72 | 76 | while ($checkLines--) { |
73 | 77 | $line = $this->file->fgets(); |
74 | - foreach ($delimiters as $delimiter){ |
|
78 | + foreach ($delimiters as $delimiter) { |
|
75 | 79 | $fields = preg_split('/['.$delimiter.']/', $line); |
76 | - if(count($fields) > 1){ |
|
77 | - if(empty($results[$delimiter])){ |
|
80 | + if(count($fields) > 1) { |
|
81 | + if(empty($results[$delimiter])) { |
|
78 | 82 | $results[$delimiter] = 1; |
79 | 83 | } else { |
80 | 84 | $results[$delimiter]++; |
@@ -87,23 +91,29 @@ discard block |
||
87 | 91 | return $results[0]; |
88 | 92 | } |
89 | 93 | |
90 | - public function write($row){ |
|
91 | - if ($this->mode != self::WRITE) return; |
|
94 | + public function write($row) { |
|
95 | + if ($this->mode != self::WRITE) { |
|
96 | + return; |
|
97 | + } |
|
92 | 98 | $row = (array)$row; |
93 | 99 | if (false === $this->savedheaders) { |
94 | 100 | $this->schema(array_keys($row)); |
95 | 101 | } |
96 | 102 | $row_t = $this->template; |
97 | 103 | foreach ($this->headers as $key) { |
98 | - if (isset($row[$key])) $row_t[$key] = $row[$key]; |
|
104 | + if (isset($row[$key])) { |
|
105 | + $row_t[$key] = $row[$key]; |
|
106 | + } |
|
99 | 107 | } |
100 | 108 | $this->file->fputcsv($row_t); |
101 | 109 | } |
102 | 110 | |
103 | - public function read(){ |
|
104 | - if ($this->mode != self::READ) return; |
|
105 | - foreach($this->file as $row){ |
|
106 | - if ($row){ |
|
111 | + public function read() { |
|
112 | + if ($this->mode != self::READ) { |
|
113 | + return; |
|
114 | + } |
|
115 | + foreach($this->file as $row) { |
|
116 | + if ($row) { |
|
107 | 117 | if(!$this->headers) { |
108 | 118 | $this->headers = $row; |
109 | 119 | continue; |
@@ -114,7 +124,7 @@ discard block |
||
114 | 124 | return; |
115 | 125 | } |
116 | 126 | |
117 | - public function each(callable $looper = null){ |
|
127 | + public function each(callable $looper = null) { |
|
118 | 128 | if ($looper) { |
119 | 129 | foreach($this->read() as $row) $looper($row); |
120 | 130 | return $this; |
@@ -125,9 +135,13 @@ discard block |
||
125 | 135 | } |
126 | 136 | } |
127 | 137 | |
128 | - public function convert($filename, $format=self::STANDARD){ |
|
129 | - if ($this->mode != self::READ) return; |
|
130 | - if ($format == self::AUTO) $format = self::STANDARD; |
|
138 | + public function convert($filename, $format=self::STANDARD) { |
|
139 | + if ($this->mode != self::READ) { |
|
140 | + return; |
|
141 | + } |
|
142 | + if ($format == self::AUTO) { |
|
143 | + $format = self::STANDARD; |
|
144 | + } |
|
131 | 145 | $csv = CSV::create($filename, CSV::EXCEL); |
132 | 146 | $this->each(function($row) use ($csv) { |
133 | 147 | $csv->write($row); |
@@ -135,14 +149,14 @@ discard block |
||
135 | 149 | return $csv; |
136 | 150 | } |
137 | 151 | |
138 | - public function flush(){ |
|
152 | + public function flush() { |
|
139 | 153 | if ($this->mode == self::WRITE) { |
140 | 154 | $this->file->fflush(); |
141 | 155 | } |
142 | 156 | } |
143 | 157 | |
144 | - public function schema($schema=null){ |
|
145 | - if($schema){ |
|
158 | + public function schema($schema=null) { |
|
159 | + if($schema) { |
|
146 | 160 | $this->headers = array_values((array)$schema); |
147 | 161 | if ($this->mode == self::WRITE) { |
148 | 162 | $this->savedheaders = true; |
@@ -155,12 +169,12 @@ discard block |
||
155 | 169 | } |
156 | 170 | } |
157 | 171 | |
158 | - public function asString(){ |
|
172 | + public function asString() { |
|
159 | 173 | $this->flush(); |
160 | 174 | return file_get_contents($this->file->getPathname()); |
161 | 175 | } |
162 | 176 | |
163 | - public function __toString(){ |
|
177 | + public function __toString() { |
|
164 | 178 | try { return $this->asString(); } catch(\Exception $e) { return ''; } |
165 | 179 | } |
166 | 180 |