Skip to content

Commit 75eb48f

Browse files
committed
Refactor class CI_Format
1 parent 42a6871 commit 75eb48f

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

system/core/Format.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
2-
defined('BASEPATH') OR exit('No direct script access allowed');
2+
defined('BASEPATH') or exit('No direct script access allowed');
33

4-
class Format
4+
class CI_Format
55
{
66
/**
77
* Array output format.
@@ -60,9 +60,9 @@ class Format
6060
/**
6161
* Type to convert from.
6262
*
63-
* @var string
63+
* @var string|null
6464
*/
65-
protected $_from_type = null;
65+
protected $_from_type;
6666

6767
/**
6868
* DO NOT CALL THIS DIRECTLY, USE factory().
@@ -82,10 +82,10 @@ public function __construct($data = null, $from_type = null)
8282

8383
// If the provided data is already formatted we should probably convert it to an array
8484
if ($from_type !== null) {
85-
if (method_exists($this, '_from_'.$from_type)) {
86-
$data = call_user_func([$this, '_from_'.$from_type], $data);
85+
if (method_exists($this, '_from_' . $from_type)) {
86+
$data = $this->{'_from_' . $from_type}($data);
8787
} else {
88-
throw new Exception('Format class does not support conversion from "'.$from_type.'".');
88+
throw new Exception('Format class does not support conversion from "' . $from_type . '".');
8989
}
9090
}
9191

@@ -181,7 +181,7 @@ public function to_xml($data = null, $structure = null, $basenode = 'xml')
181181
// no numeric keys in our xml please!
182182
if (is_numeric($key)) {
183183
// make string key...
184-
$key = (singular($basenode) != $basenode) ? singular($basenode) : 'item';
184+
$key = (singular($basenode) !== $basenode) ? singular($basenode) : 'item';
185185
}
186186

187187
// replace anything not alpha numeric
@@ -196,8 +196,7 @@ public function to_xml($data = null, $structure = null, $basenode = 'xml')
196196
foreach ($attributes as $attribute_name => $attribute_value) {
197197
$structure->addAttribute($attribute_name, $attribute_value);
198198
}
199-
}
200-
// if there is another array found recursively call this function
199+
} // if there is another array found recursively call this function
201200
elseif (is_array($value) || is_object($value)) {
202201
$node = $structure->addChild($key);
203202

@@ -242,7 +241,7 @@ public function to_html($data = null)
242241
} else {
243242
// Single array
244243
$headings = array_keys($data);
245-
$data = [$data];
244+
$data = [$data];
246245
}
247246

248247
// Load the table library
@@ -276,7 +275,7 @@ public function to_html($data = null)
276275
public function to_csv($data = null, $delimiter = ',', $enclosure = '"')
277276
{
278277
// Use a threshold of 1 MB (1024 * 1024)
279-
$handle = fopen('php://temp/maxmemory:1048576', 'w');
278+
$handle = fopen('php://temp/maxmemory:1048576', 'wb');
280279
if ($handle === false) {
281280
return;
282281
}
@@ -309,7 +308,7 @@ public function to_csv($data = null, $delimiter = ',', $enclosure = '"')
309308
} else {
310309
// Single array
311310
$headings = array_keys($data);
312-
$data = [$data];
311+
$data = [$data];
313312
}
314313

315314
// Apply the headings
@@ -368,15 +367,16 @@ public function to_json($data = null)
368367
return json_encode($data, JSON_UNESCAPED_UNICODE);
369368
}
370369

371-
// We only honour a jsonp callback which are valid javascript identifiers
372-
elseif (preg_match('/^[a-z_\$][a-z0-9\$_]*(\.[a-z_\$][a-z0-9\$_]*)*$/i', $callback)) {
370+
if (preg_match('/^[a-z_\$][a-z0-9\$_]*(\.[a-z_\$][a-z0-9\$_]*)*$/i', $callback)) {
373371
// Return the data as encoded json with a callback
374-
return $callback.'('.json_encode($data, JSON_UNESCAPED_UNICODE).');';
372+
return $callback . '(' . json_encode($data, JSON_UNESCAPED_UNICODE) . ');';
375373
}
376374

375+
// We only honour a jsonp callback which are valid javascript identifiers
376+
377377
// An invalid jsonp callback function provided.
378378
// Though I don't believe this should be hardcoded here
379-
$data['warning'] = 'INVALID JSONP CALLBACK: '.$callback;
379+
$data['warning'] = 'INVALID JSONP CALLBACK: ' . $callback;
380380

381381
return json_encode($data, JSON_UNESCAPED_UNICODE);
382382
}

0 commit comments

Comments
 (0)