@@ -21,7 +21,7 @@ |
||
| 21 | 21 | |
| 22 | 22 | interface ViewrInterface |
| 23 | 23 | { |
| 24 | - public function render(object $controller , string $view); |
|
| 24 | + public function render(object $controller, string $view); |
|
| 25 | 25 | |
| 26 | 26 | public function fetch($data = null); |
| 27 | 27 | |
@@ -96,7 +96,7 @@ |
||
| 96 | 96 | |
| 97 | 97 | public function add(array $data) : void |
| 98 | 98 | { |
| 99 | - $this->data = array_merge( $this->data, $data ); |
|
| 99 | + $this->data = array_merge($this->data, $data); |
|
| 100 | 100 | |
| 101 | 101 | } |
| 102 | 102 | |
@@ -180,16 +180,16 @@ |
||
| 180 | 180 | */ |
| 181 | 181 | public function merge($key, $value = null) |
| 182 | 182 | { |
| 183 | - if (is_array( $key )) { |
|
| 184 | - $this->elements = array_merge( $this->elements , $key ); |
|
| 183 | + if (is_array($key)) { |
|
| 184 | + $this->elements = array_merge($this->elements, $key); |
|
| 185 | 185 | } |
| 186 | - if (is_string( $key )) { |
|
| 187 | - $items = (array)$this->get( $key ); |
|
| 188 | - $value = array_merge( $items , $this->getArrayItems( $value ) ); |
|
| 189 | - $this->set( $key , $value ); |
|
| 186 | + if (is_string($key)) { |
|
| 187 | + $items = (array)$this->get($key); |
|
| 188 | + $value = array_merge($items, $this->getArrayItems($value)); |
|
| 189 | + $this->set($key, $value); |
|
| 190 | 190 | } |
| 191 | - if (is_object( $key ) || $key instanceof IteratorDot) { |
|
| 192 | - $this->elements = array_merge( $this->elements , $key->all() ); |
|
| 191 | + if (is_object($key) || $key instanceof IteratorDot) { |
|
| 192 | + $this->elements = array_merge($this->elements, $key->all()); |
|
| 193 | 193 | } |
| 194 | 194 | } |
| 195 | 195 | } |
| 196 | 196 | \ No newline at end of file |
@@ -42,13 +42,13 @@ discard block |
||
| 42 | 42 | * @param $arguments |
| 43 | 43 | * @return $this |
| 44 | 44 | */ |
| 45 | - function __call($name , $arguments) |
|
| 45 | + function __call($name, $arguments) |
|
| 46 | 46 | { |
| 47 | 47 | $clausule = $arguments[0]; |
| 48 | - if (count( $arguments ) > 1) { |
|
| 48 | + if (count($arguments) > 1) { |
|
| 49 | 49 | $clausule = $arguments; |
| 50 | 50 | } |
| 51 | - $this->clausules[strtolower( $name )] = $clausule; |
|
| 51 | + $this->clausules[strtolower($name)] = $clausule; |
|
| 52 | 52 | |
| 53 | 53 | return $this; |
| 54 | 54 | } |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | public function __construct(array $options) |
| 61 | 61 | { |
| 62 | - parent::__construct( $options ); |
|
| 62 | + parent::__construct($options); |
|
| 63 | 63 | $this->conn = $this->connection(); |
| 64 | 64 | |
| 65 | 65 | } |
@@ -68,15 +68,15 @@ discard block |
||
| 68 | 68 | * @param array $values |
| 69 | 69 | * @return string |
| 70 | 70 | */ |
| 71 | - public function insert(array $values = [] ) |
|
| 71 | + public function insert(array $values = []) |
|
| 72 | 72 | { |
| 73 | 73 | // recupera o nome da tabela |
| 74 | 74 | // ou deixa uma marcação para mostrar que faltou informar esse campo |
| 75 | - $table = isset( $this->clausules['table'] ) ? $this->clausules['table'] : '<table>'; |
|
| 75 | + $table = isset($this->clausules['table']) ? $this->clausules['table'] : '<table>'; |
|
| 76 | 76 | |
| 77 | 77 | // recupera o array dos campos |
| 78 | 78 | // ou deixa uma marcação para mostrar que faltou informar esse campo |
| 79 | - $_fields = isset( $this->clausules['fields'] ) ? $this->clausules['fields'] : '<fields>'; |
|
| 79 | + $_fields = isset($this->clausules['fields']) ? $this->clausules['fields'] : '<fields>'; |
|
| 80 | 80 | // $fields = implode( ', ' , $_fields ); |
| 81 | 81 | // |
| 82 | 82 | // // cria uma lista de rótulos para usar "prepared statement" |
@@ -100,9 +100,9 @@ discard block |
||
| 100 | 100 | |
| 101 | 101 | // INSERT INTO {table} ({fields}) VALUES ({values}); |
| 102 | 102 | // junta o comando |
| 103 | - $sql = implode( ' ' , $command ); |
|
| 103 | + $sql = implode(' ', $command); |
|
| 104 | 104 | |
| 105 | - return $this->executeInsert( $sql , $values ); |
|
| 105 | + return $this->executeInsert($sql, $values); |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | /** |
@@ -113,14 +113,14 @@ discard block |
||
| 113 | 113 | { |
| 114 | 114 | // recupera o nome da tabela |
| 115 | 115 | // ou deixa uma marcação para mostrar que faltou informar esse campo |
| 116 | - $table = isset( $this->clausules['table'] ) ? $this->clausules['table'] : '<table>'; |
|
| 116 | + $table = isset($this->clausules['table']) ? $this->clausules['table'] : '<table>'; |
|
| 117 | 117 | |
| 118 | 118 | // recupera o array dos campos |
| 119 | 119 | // ou deixa uma marcação para mostrar que faltou informar esse campo |
| 120 | - $_fields = isset( $this->clausules['fields'] ) ? $this->clausules['fields'] : '*'; |
|
| 121 | - $fields = implode( ', ' , $_fields ); |
|
| 120 | + $_fields = isset($this->clausules['fields']) ? $this->clausules['fields'] : '*'; |
|
| 121 | + $fields = implode(', ', $_fields); |
|
| 122 | 122 | |
| 123 | - $join = isset( $this->clausules['join'] ) ? $this->clausules['join'] : ''; |
|
| 123 | + $join = isset($this->clausules['join']) ? $this->clausules['join'] : ''; |
|
| 124 | 124 | |
| 125 | 125 | $command = []; |
| 126 | 126 | $command[] = 'SELECT'; |
@@ -133,31 +133,31 @@ discard block |
||
| 133 | 133 | |
| 134 | 134 | $clausules = [ |
| 135 | 135 | 'where' => [ |
| 136 | - 'instruction' => 'WHERE' , |
|
| 137 | - 'separator' => ' ' , |
|
| 138 | - ] , |
|
| 136 | + 'instruction' => 'WHERE', |
|
| 137 | + 'separator' => ' ', |
|
| 138 | + ], |
|
| 139 | 139 | 'group' => [ |
| 140 | - 'instruction' => 'GROUP BY' , |
|
| 141 | - 'separator' => ', ' , |
|
| 142 | - ] , |
|
| 140 | + 'instruction' => 'GROUP BY', |
|
| 141 | + 'separator' => ', ', |
|
| 142 | + ], |
|
| 143 | 143 | 'order' => [ |
| 144 | - 'instruction' => 'ORDER BY' , |
|
| 145 | - 'separator' => ', ' , |
|
| 146 | - ] , |
|
| 144 | + 'instruction' => 'ORDER BY', |
|
| 145 | + 'separator' => ', ', |
|
| 146 | + ], |
|
| 147 | 147 | 'having' => [ |
| 148 | - 'instruction' => 'HAVING' , |
|
| 149 | - 'separator' => ' AND ' , |
|
| 150 | - ] , |
|
| 148 | + 'instruction' => 'HAVING', |
|
| 149 | + 'separator' => ' AND ', |
|
| 150 | + ], |
|
| 151 | 151 | 'limit' => [ |
| 152 | - 'instruction' => 'LIMIT' , |
|
| 153 | - 'separator' => ',' , |
|
| 154 | - ] , |
|
| 152 | + 'instruction' => 'LIMIT', |
|
| 153 | + 'separator' => ',', |
|
| 154 | + ], |
|
| 155 | 155 | ]; |
| 156 | 156 | foreach ($clausules as $key => $clausule) { |
| 157 | - if (isset( $this->clausules[$key] )) { |
|
| 157 | + if (isset($this->clausules[$key])) { |
|
| 158 | 158 | $value = $this->clausules[$key]; |
| 159 | - if (is_array( $value )) { |
|
| 160 | - $value = implode( $clausule['separator'] , $this->clausules[$key] ); |
|
| 159 | + if (is_array($value)) { |
|
| 160 | + $value = implode($clausule['separator'], $this->clausules[$key]); |
|
| 161 | 161 | } |
| 162 | 162 | $command[] = $clausule['instruction'] . ' ' . $value; |
| 163 | 163 | } |
@@ -165,10 +165,10 @@ discard block |
||
| 165 | 165 | |
| 166 | 166 | // SELECT {fields} FROM <JOIN> {table} <WHERE> <GROUP> <ORDER> <HAVING> <LIMIT>; |
| 167 | 167 | // junta o comando |
| 168 | - $sql = implode( ' ' , $command ); |
|
| 168 | + $sql = implode(' ', $command); |
|
| 169 | 169 | var_dump($sql); |
| 170 | 170 | |
| 171 | - return $this->executeSelect( $sql , $values ); |
|
| 171 | + return $this->executeSelect($sql, $values); |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | /** |
@@ -178,13 +178,13 @@ discard block |
||
| 178 | 178 | { |
| 179 | 179 | // recupera o nome da tabela |
| 180 | 180 | // ou deixa uma marcação para mostrar que faltou informar esse campo |
| 181 | - $table = isset( $this->clausules['table'] ) ? $this->clausules['table'] : '<table>'; |
|
| 181 | + $table = isset($this->clausules['table']) ? $this->clausules['table'] : '<table>'; |
|
| 182 | 182 | |
| 183 | - $join = isset( $this->clausules['join'] ) ? $this->clausules['join'] : ''; |
|
| 183 | + $join = isset($this->clausules['join']) ? $this->clausules['join'] : ''; |
|
| 184 | 184 | |
| 185 | 185 | // recupera o array dos campos |
| 186 | 186 | // ou deixa uma marcação para mostrar que faltou informar esse campo |
| 187 | - $_fields = isset( $this->clausules['fields'] ) ? $this->clausules['fields'] : '<fields>'; |
|
| 187 | + $_fields = isset($this->clausules['fields']) ? $this->clausules['fields'] : '<fields>'; |
|
| 188 | 188 | |
| 189 | 189 | |
| 190 | 190 | ksort($_fields); |
@@ -208,19 +208,19 @@ discard block |
||
| 208 | 208 | |
| 209 | 209 | $clausules = [ |
| 210 | 210 | 'where' => [ |
| 211 | - 'instruction' => 'WHERE' , |
|
| 212 | - 'separator' => ' ' , |
|
| 211 | + 'instruction' => 'WHERE', |
|
| 212 | + 'separator' => ' ', |
|
| 213 | 213 | ], |
| 214 | 214 | 'limit' => [ |
| 215 | - 'instruction' => 'LIMIT' , |
|
| 216 | - 'separator' => ',' , |
|
| 217 | - ] , |
|
| 215 | + 'instruction' => 'LIMIT', |
|
| 216 | + 'separator' => ',', |
|
| 217 | + ], |
|
| 218 | 218 | ]; |
| 219 | 219 | foreach ($clausules as $key => $clausule) { |
| 220 | - if (isset( $this->clausules[$key] )) { |
|
| 220 | + if (isset($this->clausules[$key])) { |
|
| 221 | 221 | $value = $this->clausules[$key]; |
| 222 | - if (is_array( $value )) { |
|
| 223 | - $value = implode( $clausule['separator'] , $this->clausules[$key] ); |
|
| 222 | + if (is_array($value)) { |
|
| 223 | + $value = implode($clausule['separator'], $this->clausules[$key]); |
|
| 224 | 224 | } |
| 225 | 225 | $command[] = $clausule['instruction'] . ' ' . $value; |
| 226 | 226 | } |
@@ -228,22 +228,22 @@ discard block |
||
| 228 | 228 | |
| 229 | 229 | // UPDATE {table} SET {set} <WHERE> |
| 230 | 230 | // junta o comando |
| 231 | - $sql = implode( ' ' , $command ); |
|
| 231 | + $sql = implode(' ', $command); |
|
| 232 | 232 | |
| 233 | - return $this->executeUpdate( $sql , $_fields ); |
|
| 233 | + return $this->executeUpdate($sql, $_fields); |
|
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | /** |
| 237 | 237 | * @param $filters |
| 238 | 238 | * @return int |
| 239 | 239 | */ |
| 240 | - public function delete($filters = null ) |
|
| 240 | + public function delete($filters = null) |
|
| 241 | 241 | { |
| 242 | 242 | // recupera o nome da tabela |
| 243 | 243 | // ou deixa uma marcação para mostrar que faltou informar esse campo |
| 244 | - $table = isset( $this->clausules['table'] ) ? $this->clausules['table'] : '<table>'; |
|
| 244 | + $table = isset($this->clausules['table']) ? $this->clausules['table'] : '<table>'; |
|
| 245 | 245 | |
| 246 | - $join = isset( $this->clausules['join'] ) ? $this->clausules['join'] : ''; |
|
| 246 | + $join = isset($this->clausules['join']) ? $this->clausules['join'] : ''; |
|
| 247 | 247 | |
| 248 | 248 | $command = []; |
| 249 | 249 | $command[] = 'DELETE FROM'; |
@@ -254,20 +254,20 @@ discard block |
||
| 254 | 254 | |
| 255 | 255 | $clausules = [ |
| 256 | 256 | 'where' => [ |
| 257 | - 'instruction' => 'WHERE' , |
|
| 258 | - 'separator' => ' ' , |
|
| 257 | + 'instruction' => 'WHERE', |
|
| 258 | + 'separator' => ' ', |
|
| 259 | 259 | ], |
| 260 | 260 | 'limit' => [ |
| 261 | - 'instruction' => 'LIMIT' , |
|
| 262 | - 'separator' => ',' , |
|
| 263 | - ] , |
|
| 261 | + 'instruction' => 'LIMIT', |
|
| 262 | + 'separator' => ',', |
|
| 263 | + ], |
|
| 264 | 264 | |
| 265 | 265 | ]; |
| 266 | 266 | foreach ($clausules as $key => $clausule) { |
| 267 | - if (isset( $this->clausules[$key] )) { |
|
| 267 | + if (isset($this->clausules[$key])) { |
|
| 268 | 268 | $value = $this->clausules[$key]; |
| 269 | - if (is_array( $value )) { |
|
| 270 | - $value = implode( $clausule['separator'] , $this->clausules[$key] ); |
|
| 269 | + if (is_array($value)) { |
|
| 270 | + $value = implode($clausule['separator'], $this->clausules[$key]); |
|
| 271 | 271 | } |
| 272 | 272 | $command[] = $clausule['instruction'] . ' ' . $value; |
| 273 | 273 | } |
@@ -275,30 +275,30 @@ discard block |
||
| 275 | 275 | |
| 276 | 276 | // DELETE FROM {table} <JOIN> <USING> <WHERE> |
| 277 | 277 | // junta o comando |
| 278 | - $sql = implode( ' ' , $command ); |
|
| 278 | + $sql = implode(' ', $command); |
|
| 279 | 279 | |
| 280 | - return $this->executeDelete( $sql , $filters ); |
|
| 280 | + return $this->executeDelete($sql, $filters); |
|
| 281 | 281 | } |
| 282 | 282 | |
| 283 | - private function executeSelect($sql , $value) |
|
| 283 | + private function executeSelect($sql, $value) |
|
| 284 | 284 | { |
| 285 | 285 | |
| 286 | - $stmt = $this->conn->prepare( $sql ); |
|
| 286 | + $stmt = $this->conn->prepare($sql); |
|
| 287 | 287 | |
| 288 | 288 | foreach ($value as $key => $values) { |
| 289 | - return $stmt->bindValue( "$key" , $values ); |
|
| 289 | + return $stmt->bindValue("$key", $values); |
|
| 290 | 290 | } |
| 291 | 291 | $stmt->execute(); |
| 292 | 292 | |
| 293 | 293 | do { |
| 294 | - return $stmt->fetchAll( $fetchMode ); |
|
| 294 | + return $stmt->fetchAll($fetchMode); |
|
| 295 | 295 | } while ( |
| 296 | 296 | $stmt->nextRowset()); |
| 297 | 297 | $stmt->close(); |
| 298 | 298 | |
| 299 | 299 | } |
| 300 | 300 | |
| 301 | - private function executeInsert($sql , $value) |
|
| 301 | + private function executeInsert($sql, $value) |
|
| 302 | 302 | { |
| 303 | 303 | try { |
| 304 | 304 | |
@@ -306,11 +306,11 @@ discard block |
||
| 306 | 306 | |
| 307 | 307 | $stmt = $this->conn->prepare($sql); |
| 308 | 308 | foreach ($value as $key => $values) { |
| 309 | - $stmt->bindValue( ":$key" , $values ); |
|
| 309 | + $stmt->bindValue(":$key", $values); |
|
| 310 | 310 | } |
| 311 | 311 | $this->_commit(); |
| 312 | 312 | $stmt->execute(); |
| 313 | - unset( $stmt ); |
|
| 313 | + unset($stmt); |
|
| 314 | 314 | } catch (\Exception $e) { |
| 315 | 315 | $this->_Rollback(); |
| 316 | 316 | echo 'error insert ' . $e->getMessage(); |
@@ -319,7 +319,7 @@ discard block |
||
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | |
| 322 | - private function executeUpdate($sql , $data) |
|
| 322 | + private function executeUpdate($sql, $data) |
|
| 323 | 323 | { |
| 324 | 324 | $stmt = $this->conn->prepare($sql); |
| 325 | 325 | foreach ($data as $key => $values) { |
@@ -329,7 +329,7 @@ discard block |
||
| 329 | 329 | return $stmt->execute(); |
| 330 | 330 | } |
| 331 | 331 | |
| 332 | - private function executeDelete($sql , $value) |
|
| 332 | + private function executeDelete($sql, $value) |
|
| 333 | 333 | { |
| 334 | 334 | return $this->conn->exec($sql); |
| 335 | 335 | |
@@ -58,7 +58,7 @@ |
||
| 58 | 58 | public function save($file) |
| 59 | 59 | { |
| 60 | 60 | $this->createWritableFolder($file); |
| 61 | - $file = DIR_STORAGE. $file; |
|
| 61 | + $file = DIR_STORAGE . $file; |
|
| 62 | 62 | $handle = strcasecmp(substr($file, -3), '.gz') ? fopen($file, 'wb') : gzopen($file, 'wb'); |
| 63 | 63 | if (!$handle) { |
| 64 | 64 | throw new \Exception("ERROR: Cannot write file '$file'."); |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | |
| 19 | 19 | namespace Ballybran\Helpers\Http; |
| 20 | -use Ballybran\Core\Http\{Request , Response , RestUtilities}; |
|
| 20 | +use Ballybran\Core\Http\{Request, Response, RestUtilities}; |
|
| 21 | 21 | |
| 22 | 22 | |
| 23 | 23 | /** |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | |
| 110 | 110 | if (!is_null($maxage)) { |
| 111 | 111 | $maxage = intval($maxage); |
| 112 | - $this->data['maxage'] = 'Expires=' .gmdate("D, d M Y H:i:s", $maxage > 0 ? time() + $maxage : 0)." GMT" . 'Max-Age=' . $maxage; |
|
| 112 | + $this->data['maxage'] = 'Expires=' . gmdate("D, d M Y H:i:s", $maxage > 0 ? time() + $maxage : 0) . " GMT" . 'Max-Age=' . $maxage; |
|
| 113 | 113 | } |
| 114 | 114 | $this->data['maxage'] = $maxage; |
| 115 | 115 | return $this; |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | */ |
| 50 | 50 | public function write($message, string $type = null) |
| 51 | 51 | { |
| 52 | - if ($this->handle = fopen(getcwd() . DS. DIR_STORAGE.$this->filename, 'a')) { |
|
| 52 | + if ($this->handle = fopen(getcwd() . DS . DIR_STORAGE . $this->filename, 'a')) { |
|
| 53 | 53 | if (is_array($message) && $type == 'A') { |
| 54 | 54 | foreach ($message as $key => $value) { |
| 55 | 55 | fwrite($this->handle, date('Y-m-d G:i:s') . ' - ' . print_r($key . " -> " . $value, true) . "\n"); |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | |
| 73 | 73 | fclose($this->handle); |
| 74 | 74 | if ($this->filename == true) { |
| 75 | - chmod(getcwd() . DS. DIR_STORAGE.$this->filename, 0755); |
|
| 75 | + chmod(getcwd() . DS . DIR_STORAGE . $this->filename, 0755); |
|
| 76 | 76 | } |
| 77 | 77 | } |
| 78 | 78 | |
@@ -14,10 +14,10 @@ |
||
| 14 | 14 | private function createWritableFolder($file) |
| 15 | 15 | { |
| 16 | 16 | $currPath = ''; |
| 17 | - $path = explode( "/" , $file ); |
|
| 18 | - $currPath .= trim( $path[0] ); |
|
| 19 | - if($currPath != '.' && $currPath != '/' ) { |
|
| 20 | - @mkdir( DIR_STORAGE.$currPath , 0777 , true ); |
|
| 17 | + $path = explode("/", $file); |
|
| 18 | + $currPath .= trim($path[0]); |
|
| 19 | + if ($currPath != '.' && $currPath != '/') { |
|
| 20 | + @mkdir(DIR_STORAGE . $currPath, 0777, true); |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | } |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | // Populated as-needed. |
| 25 | 25 | public $decoded_response; // Decoded response body. |
| 26 | 26 | |
| 27 | - public function __construct($options=[]){ |
|
| 27 | + public function __construct($options = []) { |
|
| 28 | 28 | $default_options = [ |
| 29 | 29 | 'headers' => [], |
| 30 | 30 | 'parameters' => [], |
@@ -43,94 +43,94 @@ discard block |
||
| 43 | 43 | ]; |
| 44 | 44 | |
| 45 | 45 | $this->options = array_merge($default_options, $options); |
| 46 | - if(array_key_exists('decoders', $options)) |
|
| 46 | + if (array_key_exists('decoders', $options)) |
|
| 47 | 47 | $this->options['decoders'] = array_merge( |
| 48 | 48 | $default_options['decoders'], $options['decoders']); |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | - public function set_option($key, $value){ |
|
| 51 | + public function set_option($key, $value) { |
|
| 52 | 52 | $this->options[$key] = $value; |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | - public function register_decoder($format, $method){ |
|
| 55 | + public function register_decoder($format, $method) { |
|
| 56 | 56 | // Decoder callbacks must adhere to the following pattern: |
| 57 | 57 | // array my_decoder(string $data) |
| 58 | 58 | $this->options['decoders'][$format] = $method; |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | // Iterable methods: |
| 62 | - public function rewind(){ |
|
| 62 | + public function rewind() { |
|
| 63 | 63 | $this->decode_response(); |
| 64 | 64 | return reset($this->decoded_response); |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - public function current(){ |
|
| 67 | + public function current() { |
|
| 68 | 68 | return current($this->decoded_response); |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - public function key(){ |
|
| 71 | + public function key() { |
|
| 72 | 72 | return key($this->decoded_response); |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - public function next(){ |
|
| 75 | + public function next() { |
|
| 76 | 76 | return next($this->decoded_response); |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - public function valid(){ |
|
| 79 | + public function valid() { |
|
| 80 | 80 | return is_array($this->decoded_response) |
| 81 | 81 | && (key($this->decoded_response) !== NULL); |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | // ArrayAccess methods: |
| 85 | - public function offsetExists($key){ |
|
| 85 | + public function offsetExists($key) { |
|
| 86 | 86 | $this->decode_response(); |
| 87 | - return is_array($this->decoded_response)? |
|
| 87 | + return is_array($this->decoded_response) ? |
|
| 88 | 88 | isset($this->decoded_response[$key]) : isset($this->decoded_response->{$key}); |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | - public function offsetGet($key){ |
|
| 91 | + public function offsetGet($key) { |
|
| 92 | 92 | $this->decode_response(); |
| 93 | - if(!$this->offsetExists($key)) |
|
| 93 | + if (!$this->offsetExists($key)) |
|
| 94 | 94 | return NULL; |
| 95 | 95 | |
| 96 | - return is_array($this->decoded_response)? |
|
| 96 | + return is_array($this->decoded_response) ? |
|
| 97 | 97 | $this->decoded_response[$key] : $this->decoded_response->{$key}; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - public function offsetSet($key, $value){ |
|
| 100 | + public function offsetSet($key, $value) { |
|
| 101 | 101 | throw new RestClientException("Decoded response data is immutable."); |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | - public function offsetUnset($key){ |
|
| 104 | + public function offsetUnset($key) { |
|
| 105 | 105 | throw new RestClientException("Decoded response data is immutable."); |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | // Request methods: |
| 109 | - public function get($url, $parameters=[], $headers=[]){ |
|
| 109 | + public function get($url, $parameters = [], $headers = []) { |
|
| 110 | 110 | return $this->execute($url, 'GET', $parameters, $headers); |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - public function post($url, $parameters=[], $headers=[]){ |
|
| 113 | + public function post($url, $parameters = [], $headers = []) { |
|
| 114 | 114 | return $this->execute($url, 'POST', $parameters, $headers); |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - public function put($url, $parameters=[], $headers=[]){ |
|
| 117 | + public function put($url, $parameters = [], $headers = []) { |
|
| 118 | 118 | return $this->execute($url, 'PUT', $parameters, $headers); |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - public function patch($url, $parameters=[], $headers=[]){ |
|
| 121 | + public function patch($url, $parameters = [], $headers = []) { |
|
| 122 | 122 | return $this->execute($url, 'PATCH', $parameters, $headers); |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | - public function delete($url, $parameters=[], $headers=[]){ |
|
| 125 | + public function delete($url, $parameters = [], $headers = []) { |
|
| 126 | 126 | return $this->execute($url, 'DELETE', $parameters, $headers); |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | - public function head($url, $parameters=[], $headers=[]){ |
|
| 129 | + public function head($url, $parameters = [], $headers = []) { |
|
| 130 | 130 | return $this->execute($url, 'HEAD', $parameters, $headers); |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | - public function execute($url, $method='GET', $parameters=[], $headers=[]){ |
|
| 133 | + public function execute($url, $method = 'GET', $parameters = [], $headers = []) { |
|
| 134 | 134 | $client = clone $this; |
| 135 | 135 | $client->url = $url; |
| 136 | 136 | $client->handle = curl_init(); |
@@ -140,89 +140,89 @@ discard block |
||
| 140 | 140 | CURLOPT_USERAGENT => $client->options['user_agent'] |
| 141 | 141 | ]; |
| 142 | 142 | |
| 143 | - if($client->options['username'] && $client->options['password']) |
|
| 143 | + if ($client->options['username'] && $client->options['password']) |
|
| 144 | 144 | $curlopt[CURLOPT_USERPWD] = sprintf("%s:%s", |
| 145 | 145 | $client->options['username'], $client->options['password']); |
| 146 | 146 | |
| 147 | - if(count($client->options['headers']) || count($headers)){ |
|
| 147 | + if (count($client->options['headers']) || count($headers)) { |
|
| 148 | 148 | $curlopt[CURLOPT_HTTPHEADER] = []; |
| 149 | 149 | $headers = array_merge($client->options['headers'], $headers); |
| 150 | - foreach($headers as $key => $values){ |
|
| 151 | - foreach(is_array($values)? $values : [$values] as $value){ |
|
| 150 | + foreach ($headers as $key => $values) { |
|
| 151 | + foreach (is_array($values) ? $values : [$values] as $value) { |
|
| 152 | 152 | $curlopt[CURLOPT_HTTPHEADER][] = sprintf("%s:%s", $key, $value); |
| 153 | 153 | } |
| 154 | 154 | } |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | - if($client->options['format']) |
|
| 158 | - $client->url .= '.'.$client->options['format']; |
|
| 157 | + if ($client->options['format']) |
|
| 158 | + $client->url .= '.' . $client->options['format']; |
|
| 159 | 159 | |
| 160 | 160 | // Allow passing parameters as a pre-encoded string (or something that |
| 161 | 161 | // allows casting to a string). Parameters passed as strings will not be |
| 162 | 162 | // merged with parameters specified in the default options. |
| 163 | - if(is_array($parameters)){ |
|
| 163 | + if (is_array($parameters)) { |
|
| 164 | 164 | $parameters = array_merge($client->options['parameters'], $parameters); |
| 165 | 165 | $parameters_string = http_build_query($parameters); |
| 166 | 166 | |
| 167 | 167 | // http_build_query automatically adds an array index to repeated |
| 168 | 168 | // parameters which is not desirable on most systems. This hack |
| 169 | 169 | // reverts "key[0]=foo&key[1]=bar" to "key[]=foo&key[]=bar" |
| 170 | - if(!$client->options['build_indexed_queries']) |
|
| 170 | + if (!$client->options['build_indexed_queries']) |
|
| 171 | 171 | $parameters_string = preg_replace( |
| 172 | 172 | "/%5B[0-9]+%5D=/simU", "%5B%5D=", $parameters_string); |
| 173 | 173 | } |
| 174 | 174 | else |
| 175 | - $parameters_string = (string) $parameters; |
|
| 175 | + $parameters_string = (string)$parameters; |
|
| 176 | 176 | |
| 177 | - if(strtoupper($method) == 'POST'){ |
|
| 177 | + if (strtoupper($method) == 'POST') { |
|
| 178 | 178 | $curlopt[CURLOPT_POST] = TRUE; |
| 179 | 179 | $curlopt[CURLOPT_POSTFIELDS] = $parameters_string; |
| 180 | 180 | } |
| 181 | - elseif(strtoupper($method) == 'HEAD'){ |
|
| 181 | + elseif (strtoupper($method) == 'HEAD') { |
|
| 182 | 182 | $curlopt[CURLOPT_NOBODY] = TRUE; |
| 183 | 183 | } |
| 184 | - elseif(strtoupper($method) != 'GET'){ |
|
| 184 | + elseif (strtoupper($method) != 'GET') { |
|
| 185 | 185 | $curlopt[CURLOPT_CUSTOMREQUEST] = strtoupper($method); |
| 186 | 186 | $curlopt[CURLOPT_POSTFIELDS] = $parameters_string; |
| 187 | 187 | } |
| 188 | - elseif($parameters_string){ |
|
| 189 | - $client->url .= strpos($client->url, '?')? '&' : '?'; |
|
| 188 | + elseif ($parameters_string) { |
|
| 189 | + $client->url .= strpos($client->url, '?') ? '&' : '?'; |
|
| 190 | 190 | $client->url .= $parameters_string; |
| 191 | 191 | } |
| 192 | 192 | |
| 193 | - if($client->options['base_url']){ |
|
| 194 | - if($client->url[0] != '/' && substr($client->options['base_url'], -1) != '/') |
|
| 193 | + if ($client->options['base_url']) { |
|
| 194 | + if ($client->url[0] != '/' && substr($client->options['base_url'], -1) != '/') |
|
| 195 | 195 | $client->url = '/' . $client->url; |
| 196 | 196 | $client->url = $client->options['base_url'] . $client->url; |
| 197 | 197 | } |
| 198 | 198 | $curlopt[CURLOPT_URL] = $client->url; |
| 199 | 199 | |
| 200 | - if($client->options['curl_options']){ |
|
| 200 | + if ($client->options['curl_options']) { |
|
| 201 | 201 | // array_merge would reset our numeric keys. |
| 202 | - foreach($client->options['curl_options'] as $key => $value){ |
|
| 202 | + foreach ($client->options['curl_options'] as $key => $value) { |
|
| 203 | 203 | $curlopt[$key] = $value; |
| 204 | 204 | } |
| 205 | 205 | } |
| 206 | 206 | curl_setopt_array($client->handle, $curlopt); |
| 207 | 207 | |
| 208 | 208 | $client->parse_response(curl_exec($client->handle)); |
| 209 | - $client->info = (object) curl_getinfo($client->handle); |
|
| 209 | + $client->info = (object)curl_getinfo($client->handle); |
|
| 210 | 210 | $client->error = curl_error($client->handle); |
| 211 | 211 | |
| 212 | 212 | curl_close($client->handle); |
| 213 | 213 | return $client; |
| 214 | 214 | } |
| 215 | 215 | |
| 216 | - public function parse_response($response){ |
|
| 216 | + public function parse_response($response) { |
|
| 217 | 217 | $headers = []; |
| 218 | 218 | $this->response_status_lines = []; |
| 219 | 219 | $line = strtok($response, "\n"); |
| 220 | 220 | do { |
| 221 | - if(strlen(trim($line)) == 0){ |
|
| 221 | + if (strlen(trim($line)) == 0) { |
|
| 222 | 222 | // Since we tokenize on \n, use the remaining \r to detect empty lines. |
| 223 | - if(count($headers) > 0) break; // Must be the newline after headers, move on to response body |
|
| 223 | + if (count($headers) > 0) break; // Must be the newline after headers, move on to response body |
|
| 224 | 224 | } |
| 225 | - elseif(strpos($line, 'HTTP') === 0){ |
|
| 225 | + elseif (strpos($line, 'HTTP') === 0) { |
|
| 226 | 226 | // One or more HTTP status lines |
| 227 | 227 | $this->response_status_lines[] = trim($line); |
| 228 | 228 | } |
@@ -232,42 +232,42 @@ discard block |
||
| 232 | 232 | $key = trim(strtolower(str_replace('-', '_', $key))); |
| 233 | 233 | $value = trim($value); |
| 234 | 234 | |
| 235 | - if(empty($headers[$key])) |
|
| 235 | + if (empty($headers[$key])) |
|
| 236 | 236 | $headers[$key] = $value; |
| 237 | - elseif(is_array($headers[$key])) |
|
| 237 | + elseif (is_array($headers[$key])) |
|
| 238 | 238 | $headers[$key][] = $value; |
| 239 | 239 | else |
| 240 | 240 | $headers[$key] = [$headers[$key], $value]; |
| 241 | 241 | } |
| 242 | - } while($line = strtok("\n")); |
|
| 242 | + } while ($line = strtok("\n")); |
|
| 243 | 243 | |
| 244 | - $this->headers = (object) $headers; |
|
| 244 | + $this->headers = (object)$headers; |
|
| 245 | 245 | $this->response = strtok(""); |
| 246 | 246 | } |
| 247 | 247 | |
| 248 | - public function get_response_format(){ |
|
| 249 | - if(!$this->response) |
|
| 248 | + public function get_response_format() { |
|
| 249 | + if (!$this->response) |
|
| 250 | 250 | throw new RestClientException( |
| 251 | 251 | "A response must exist before it can be decoded."); |
| 252 | 252 | |
| 253 | 253 | // User-defined format. |
| 254 | - if(!empty($this->options['format'])) |
|
| 254 | + if (!empty($this->options['format'])) |
|
| 255 | 255 | return $this->options['format']; |
| 256 | 256 | |
| 257 | 257 | // Extract format from response content-type header. |
| 258 | - if(!empty($this->headers->content_type)) |
|
| 259 | - if(preg_match($this->options['format_regex'], $this->headers->content_type, $matches)) |
|
| 258 | + if (!empty($this->headers->content_type)) |
|
| 259 | + if (preg_match($this->options['format_regex'], $this->headers->content_type, $matches)) |
|
| 260 | 260 | return $matches[2]; |
| 261 | 261 | |
| 262 | 262 | throw new RestClientException( |
| 263 | 263 | "Response format could not be determined."); |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | - public function decode_response(){ |
|
| 267 | - if(empty($this->decoded_response)){ |
|
| 266 | + public function decode_response() { |
|
| 267 | + if (empty($this->decoded_response)) { |
|
| 268 | 268 | $format = $this->get_response_format(); |
| 269 | - if(!array_key_exists($format, $this->options['decoders'])) |
|
| 270 | - throw new RestClientException("'${format}' is not a supported ". |
|
| 269 | + if (!array_key_exists($format, $this->options['decoders'])) |
|
| 270 | + throw new RestClientException("'${format}' is not a supported " . |
|
| 271 | 271 | "format, register a decoder to handle this response."); |
| 272 | 272 | |
| 273 | 273 | $this->decoded_response = call_user_func( |
@@ -43,9 +43,10 @@ discard block |
||
| 43 | 43 | ]; |
| 44 | 44 | |
| 45 | 45 | $this->options = array_merge($default_options, $options); |
| 46 | - if(array_key_exists('decoders', $options)) |
|
| 47 | - $this->options['decoders'] = array_merge( |
|
| 46 | + if(array_key_exists('decoders', $options)) { |
|
| 47 | + $this->options['decoders'] = array_merge( |
|
| 48 | 48 | $default_options['decoders'], $options['decoders']); |
| 49 | + } |
|
| 49 | 50 | } |
| 50 | 51 | |
| 51 | 52 | public function set_option($key, $value){ |
@@ -90,8 +91,9 @@ discard block |
||
| 90 | 91 | |
| 91 | 92 | public function offsetGet($key){ |
| 92 | 93 | $this->decode_response(); |
| 93 | - if(!$this->offsetExists($key)) |
|
| 94 | - return NULL; |
|
| 94 | + if(!$this->offsetExists($key)) { |
|
| 95 | + return NULL; |
|
| 96 | + } |
|
| 95 | 97 | |
| 96 | 98 | return is_array($this->decoded_response)? |
| 97 | 99 | $this->decoded_response[$key] : $this->decoded_response->{$key}; |
@@ -140,9 +142,10 @@ discard block |
||
| 140 | 142 | CURLOPT_USERAGENT => $client->options['user_agent'] |
| 141 | 143 | ]; |
| 142 | 144 | |
| 143 | - if($client->options['username'] && $client->options['password']) |
|
| 144 | - $curlopt[CURLOPT_USERPWD] = sprintf("%s:%s", |
|
| 145 | + if($client->options['username'] && $client->options['password']) { |
|
| 146 | + $curlopt[CURLOPT_USERPWD] = sprintf("%s:%s", |
|
| 145 | 147 | $client->options['username'], $client->options['password']); |
| 148 | + } |
|
| 146 | 149 | |
| 147 | 150 | if(count($client->options['headers']) || count($headers)){ |
| 148 | 151 | $curlopt[CURLOPT_HTTPHEADER] = []; |
@@ -154,8 +157,9 @@ discard block |
||
| 154 | 157 | } |
| 155 | 158 | } |
| 156 | 159 | |
| 157 | - if($client->options['format']) |
|
| 158 | - $client->url .= '.'.$client->options['format']; |
|
| 160 | + if($client->options['format']) { |
|
| 161 | + $client->url .= '.'.$client->options['format']; |
|
| 162 | + } |
|
| 159 | 163 | |
| 160 | 164 | // Allow passing parameters as a pre-encoded string (or something that |
| 161 | 165 | // allows casting to a string). Parameters passed as strings will not be |
@@ -167,32 +171,31 @@ discard block |
||
| 167 | 171 | // http_build_query automatically adds an array index to repeated |
| 168 | 172 | // parameters which is not desirable on most systems. This hack |
| 169 | 173 | // reverts "key[0]=foo&key[1]=bar" to "key[]=foo&key[]=bar" |
| 170 | - if(!$client->options['build_indexed_queries']) |
|
| 171 | - $parameters_string = preg_replace( |
|
| 174 | + if(!$client->options['build_indexed_queries']) { |
|
| 175 | + $parameters_string = preg_replace( |
|
| 172 | 176 | "/%5B[0-9]+%5D=/simU", "%5B%5D=", $parameters_string); |
| 177 | + } |
|
| 178 | + } else { |
|
| 179 | + $parameters_string = (string) $parameters; |
|
| 173 | 180 | } |
| 174 | - else |
|
| 175 | - $parameters_string = (string) $parameters; |
|
| 176 | 181 | |
| 177 | 182 | if(strtoupper($method) == 'POST'){ |
| 178 | 183 | $curlopt[CURLOPT_POST] = TRUE; |
| 179 | 184 | $curlopt[CURLOPT_POSTFIELDS] = $parameters_string; |
| 180 | - } |
|
| 181 | - elseif(strtoupper($method) == 'HEAD'){ |
|
| 185 | + } elseif(strtoupper($method) == 'HEAD'){ |
|
| 182 | 186 | $curlopt[CURLOPT_NOBODY] = TRUE; |
| 183 | - } |
|
| 184 | - elseif(strtoupper($method) != 'GET'){ |
|
| 187 | + } elseif(strtoupper($method) != 'GET'){ |
|
| 185 | 188 | $curlopt[CURLOPT_CUSTOMREQUEST] = strtoupper($method); |
| 186 | 189 | $curlopt[CURLOPT_POSTFIELDS] = $parameters_string; |
| 187 | - } |
|
| 188 | - elseif($parameters_string){ |
|
| 190 | + } elseif($parameters_string){ |
|
| 189 | 191 | $client->url .= strpos($client->url, '?')? '&' : '?'; |
| 190 | 192 | $client->url .= $parameters_string; |
| 191 | 193 | } |
| 192 | 194 | |
| 193 | 195 | if($client->options['base_url']){ |
| 194 | - if($client->url[0] != '/' && substr($client->options['base_url'], -1) != '/') |
|
| 195 | - $client->url = '/' . $client->url; |
|
| 196 | + if($client->url[0] != '/' && substr($client->options['base_url'], -1) != '/') { |
|
| 197 | + $client->url = '/' . $client->url; |
|
| 198 | + } |
|
| 196 | 199 | $client->url = $client->options['base_url'] . $client->url; |
| 197 | 200 | } |
| 198 | 201 | $curlopt[CURLOPT_URL] = $client->url; |
@@ -220,24 +223,26 @@ discard block |
||
| 220 | 223 | do { |
| 221 | 224 | if(strlen(trim($line)) == 0){ |
| 222 | 225 | // Since we tokenize on \n, use the remaining \r to detect empty lines. |
| 223 | - if(count($headers) > 0) break; // Must be the newline after headers, move on to response body |
|
| 224 | - } |
|
| 225 | - elseif(strpos($line, 'HTTP') === 0){ |
|
| 226 | + if(count($headers) > 0) { |
|
| 227 | + break; |
|
| 228 | + } |
|
| 229 | + // Must be the newline after headers, move on to response body |
|
| 230 | + } elseif(strpos($line, 'HTTP') === 0){ |
|
| 226 | 231 | // One or more HTTP status lines |
| 227 | 232 | $this->response_status_lines[] = trim($line); |
| 228 | - } |
|
| 229 | - else { |
|
| 233 | + } else { |
|
| 230 | 234 | // Has to be a header |
| 231 | 235 | list($key, $value) = explode(':', $line, 2); |
| 232 | 236 | $key = trim(strtolower(str_replace('-', '_', $key))); |
| 233 | 237 | $value = trim($value); |
| 234 | 238 | |
| 235 | - if(empty($headers[$key])) |
|
| 236 | - $headers[$key] = $value; |
|
| 237 | - elseif(is_array($headers[$key])) |
|
| 238 | - $headers[$key][] = $value; |
|
| 239 | - else |
|
| 240 | - $headers[$key] = [$headers[$key], $value]; |
|
| 239 | + if(empty($headers[$key])) { |
|
| 240 | + $headers[$key] = $value; |
|
| 241 | + } elseif(is_array($headers[$key])) { |
|
| 242 | + $headers[$key][] = $value; |
|
| 243 | + } else { |
|
| 244 | + $headers[$key] = [$headers[$key], $value]; |
|
| 245 | + } |
|
| 241 | 246 | } |
| 242 | 247 | } while($line = strtok("\n")); |
| 243 | 248 | |
@@ -246,18 +251,21 @@ discard block |
||
| 246 | 251 | } |
| 247 | 252 | |
| 248 | 253 | public function get_response_format(){ |
| 249 | - if(!$this->response) |
|
| 250 | - throw new RestClientException( |
|
| 254 | + if(!$this->response) { |
|
| 255 | + throw new RestClientException( |
|
| 251 | 256 | "A response must exist before it can be decoded."); |
| 257 | + } |
|
| 252 | 258 | |
| 253 | 259 | // User-defined format. |
| 254 | - if(!empty($this->options['format'])) |
|
| 255 | - return $this->options['format']; |
|
| 260 | + if(!empty($this->options['format'])) { |
|
| 261 | + return $this->options['format']; |
|
| 262 | + } |
|
| 256 | 263 | |
| 257 | 264 | // Extract format from response content-type header. |
| 258 | - if(!empty($this->headers->content_type)) |
|
| 259 | - if(preg_match($this->options['format_regex'], $this->headers->content_type, $matches)) |
|
| 265 | + if(!empty($this->headers->content_type)) { |
|
| 266 | + if(preg_match($this->options['format_regex'], $this->headers->content_type, $matches)) |
|
| 260 | 267 | return $matches[2]; |
| 268 | + } |
|
| 261 | 269 | |
| 262 | 270 | throw new RestClientException( |
| 263 | 271 | "Response format could not be determined."); |
@@ -266,9 +274,10 @@ discard block |
||
| 266 | 274 | public function decode_response(){ |
| 267 | 275 | if(empty($this->decoded_response)){ |
| 268 | 276 | $format = $this->get_response_format(); |
| 269 | - if(!array_key_exists($format, $this->options['decoders'])) |
|
| 270 | - throw new RestClientException("'${format}' is not a supported ". |
|
| 277 | + if(!array_key_exists($format, $this->options['decoders'])) { |
|
| 278 | + throw new RestClientException("'${format}' is not a supported ". |
|
| 271 | 279 | "format, register a decoder to handle this response."); |
| 280 | + } |
|
| 272 | 281 | |
| 273 | 282 | $this->decoded_response = call_user_func( |
| 274 | 283 | $this->options['decoders'][$format], $this->response); |