1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakePHPify : CakePHP Plugin for Shopify API Authentication |
4
|
|
|
* Copyright (c) Multidimension.al (http://multidimension.al) |
5
|
|
|
* Github : https://github.com/multidimension-al/cakephpify |
6
|
|
|
* |
7
|
|
|
* Licensed under The MIT License |
8
|
|
|
* For full copyright and license information, please see the LICENSE file |
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
10
|
|
|
* |
11
|
|
|
* @copyright (c) Multidimension.al (http://multidimension.al) |
12
|
|
|
* @link https://github.com/multidimension-al/cakephpify CakePHPify Github |
13
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Multidimensional\Cakephpify\Shell\Helper; |
17
|
|
|
|
18
|
|
|
use Cake\Console\Shell; |
19
|
|
|
use Cake\Console\Helper; |
20
|
|
|
|
21
|
|
|
class TableHelper extends Helper |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
public function output($data, $columns = 10, $terminalWidth = 80) |
25
|
|
|
{ |
26
|
|
|
while ((count($data) % $columns != 0) && ($columns > 5)) { |
27
|
|
|
$columns--; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$maxLength = max(array_map('strlen', $data)); |
31
|
|
|
|
32
|
|
|
while (((($columns * ($maxLength + 3)) + 3) > $terminalWidth) && ($columns >= 1)) { |
33
|
|
|
$columns--; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->_io->out(' ' . str_repeat('+-' . str_repeat('-', $maxLength) . '-', $columns) . '+ '); |
37
|
|
|
|
38
|
|
|
for ($i = 0; $i < count($data);) { |
|
|
|
|
39
|
|
|
$output = " "; |
40
|
|
|
$j = $i; |
|
|
|
|
41
|
|
|
for ($k = $i; $k < ($columns + $j); $k++) { |
42
|
|
|
if (!isset($data[$i])) { |
43
|
|
|
$data[$i] = ''; |
44
|
|
|
} |
45
|
|
|
$output .= '| ' . str_repeat(' ', floor(($maxLength - strlen($data[$i])) / 2)) . $data[$i] . str_repeat(' ', ceil(($maxLength - strlen($data[$i])) / 2)) . ' '; |
46
|
|
|
$i++; |
47
|
|
|
} |
48
|
|
|
$this->_io->out($output . '|'); |
49
|
|
|
$this->_io->out(' ' . str_repeat('+-' . str_repeat('-', $maxLength) . '-', $columns) . '+ '); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: