first commit

This commit is contained in:
2018-04-11 17:57:57 +02:00
commit fdfcfbd2a6
4073 changed files with 501866 additions and 0 deletions
@@ -0,0 +1,45 @@
<?php
namespace League\CLImate\TerminalObject\Basic;
use League\CLImate\Decorator\Parser\ParserImporter;
use League\CLImate\Settings\SettingsImporter;
use League\CLImate\Util\UtilImporter;
abstract class BasicTerminalObject implements BasicTerminalObjectInterface
{
use SettingsImporter, ParserImporter, UtilImporter;
/**
* Set the property if there is a valid value
*
* @param string $key
* @param string $value
*/
protected function set($key, $value)
{
if (strlen($value)) {
$this->$key = $value;
}
}
/**
* Get the parser for the current object
*
* @return \League\CLImate\Decorator\Parser\Parser
*/
public function getParser()
{
return $this->parser;
}
/**
* Check if this object requires a new line to be added after the output
*
* @return boolean
*/
public function sameLine()
{
return false;
}
}
@@ -0,0 +1,34 @@
<?php
namespace League\CLImate\TerminalObject\Basic;
use League\CLImate\Decorator\Parser\Parser;
use League\CLImate\Util\UtilFactory;
interface BasicTerminalObjectInterface
{
public function result();
public function settings();
/**
* @param $setting
* @return void
*/
public function importSetting($setting);
/**
* @return boolean
*/
public function sameLine();
/**
* @param \League\CLImate\Decorator\Parser\Parser $parser
*/
public function parser(Parser $parser);
/**
* @param UtilFactory $util
*/
public function util(UtilFactory $util);
}
@@ -0,0 +1,67 @@
<?php
namespace League\CLImate\TerminalObject\Basic;
class Border extends BasicTerminalObject
{
/**
* The character to repeat for the border
*
* @var string $char
*/
protected $char = '-';
/**
* The length of the border
*
* @var integer $length
*/
protected $length;
public function __construct($char = null, $length = null)
{
$this->char($char)->length($length);
}
/**
* Set the character to repeat for the border
*
* @param string $char
*
* @return Border
*/
public function char($char)
{
$this->set('char', $char);
return $this;
}
/**
* Set the length of the border
*
* @param integer $length
*
* @return Border
*/
public function length($length)
{
$this->set('length', $length);
return $this;
}
/**
* Return the border
*
* @return string
*/
public function result()
{
$length = $this->length ?: $this->util->width() ?: 100;
$str = str_repeat($this->char, $length);
$str = substr($str, 0, $length);
return $str;
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace League\CLImate\TerminalObject\Basic;
class Br extends Repeatable
{
/**
* Return an empty string
*
* @return string
*/
public function result()
{
return array_fill(0, $this->count, '');
}
}
@@ -0,0 +1,21 @@
<?php
namespace League\CLImate\TerminalObject\Basic;
class Clear extends BasicTerminalObject
{
/**
* Clear the terminal
*
* @return string
*/
public function result()
{
return "\e[H\e[2J";
}
public function sameLine()
{
return true;
}
}
@@ -0,0 +1,207 @@
<?php
namespace League\CLImate\TerminalObject\Basic;
use League\CLImate\TerminalObject\Helper\StringLength;
class Columns extends BasicTerminalObject
{
use StringLength;
/**
* Number of columns
*
* @var integer $column_count
*/
protected $column_count;
/**
* Data to columnize
*
* @var array $data
*/
protected $data;
public function __construct($data, $column_count = null)
{
$this->data = $data;
$this->column_count = $column_count;
}
/**
* Calculate the number of columns organize data
*
* @return array
*/
public function result()
{
$keys = array_keys($this->data);
$first_key = reset($keys);
return (!is_int($first_key)) ? $this->associativeColumns() : $this->columns();
}
/**
* Get columns for a regular array
*
* @return array
*/
protected function columns()
{
$this->data = $this->setData();
$column_widths = $this->getColumnWidths();
$output = [];
$count = count(reset($this->data));
for ($i = 0; $i < $count; $i++) {
$output[] = $this->getRow($i, $column_widths);
}
return $output;
}
/**
* Re-configure the data into it's final form
*/
protected function setData()
{
// If it's already an array of arrays, we're good to go
if (is_array(reset($this->data))) {
return $this->setArrayOfArraysData();
}
$column_width = $this->getColumnWidth($this->data);
$row_count = $this->getMaxRows($column_width);
return array_chunk($this->data, $row_count);
}
/**
* Re-configure an array of arrays into column arrays
*/
protected function setArrayOfArraysData()
{
$this->setColumnCountViaArray($this->data);
$new_data = array_fill(0, $this->column_count, []);
foreach ($this->data as $items) {
for ($i = 0; $i < $this->column_count; $i++) {
$new_data[$i][] = (array_key_exists($i, $items)) ? $items[$i] : null;
}
}
return $new_data;
}
/**
* Get columns for an associative array
*
* @return array
*/
protected function associativeColumns()
{
$column_width = $this->getColumnWidth(array_keys($this->data));
$output = [];
foreach ($this->data as $key => $value) {
$output[] = $this->pad($key, $column_width) . $value;
}
return $output;
}
/**
* Get the row of data
*
* @param integer $key
* @param array $column_widths
*
* @return string
*/
protected function getRow($key, $column_widths)
{
$row = [];
for ($j = 0; $j < $this->column_count; $j++) {
if (isset($this->data[$j]) && array_key_exists($key, $this->data[$j])) {
$row[] = $this->pad($this->data[$j][$key], $column_widths[$j]);
}
}
return trim(implode('', $row));
}
/**
* Get the standard column width
*
* @param array $data
*
* @return integer
*/
protected function getColumnWidth($data)
{
// Return the maximum width plus a buffer
return $this->maxStrLen($data) + 5;
}
/**
* Get an array of each column's width
*
* @return array
*/
protected function getColumnWidths()
{
$column_widths = [];
for ($i = 0; $i < $this->column_count; $i++) {
if (!isset($this->data[$i])) {
$column_widths[] = 0;
continue;
}
$column_widths[] = $this->getColumnWidth($this->data[$i]);
}
return $column_widths;
}
/**
* Set the count property
*
* @param integer $column_width
*/
protected function setColumnCount($column_width)
{
$this->column_count = (int) floor($this->util->width() / $column_width);
}
/**
* Set the count property via an array
*
* @param array $items
*/
protected function setColumnCountViaArray($items)
{
$counts = array_map(function ($arr) {
return count($arr);
}, $items);
$this->column_count = max($counts);
}
/**
* Get the number of rows per column
*
* @param integer $column_width
*
* @return integer
*/
protected function getMaxRows($column_width)
{
if (!$this->column_count) {
$this->setColumnCount($column_width);
}
return ceil(count($this->data) / $this->column_count);
}
}
+30
View File
@@ -0,0 +1,30 @@
<?php
namespace League\CLImate\TerminalObject\Basic;
use League\CLImate\TerminalObject\Helper\Art;
class Draw extends BasicTerminalObject
{
use Art;
public function __construct($art)
{
// Add the default art directory
$this->addDir(__DIR__ . '/../../ASCII');
$this->art = $art;
}
/**
* Return the art
*
* @return array
*/
public function result()
{
$file = $this->artFile($this->art) ?: $this->artFile($this->default_art);
return $this->parse($file);
}
}
+36
View File
@@ -0,0 +1,36 @@
<?php
namespace League\CLImate\TerminalObject\Basic;
class Dump extends BasicTerminalObject
{
/**
* The data to convert to JSON
*
* @var mixed $data
*/
protected $data;
public function __construct($data)
{
$this->data = $data;
}
/**
* Return the data as JSON
*
* @return string
*/
public function result()
{
ob_start();
var_dump($this->data);
$result = ob_get_contents();
ob_end_clean();
return $result;
}
}
@@ -0,0 +1,74 @@
<?php
namespace League\CLImate\TerminalObject\Basic;
class Flank extends BasicTerminalObject
{
/**
* The string that will be flanked
*
* @var string $str
*/
protected $str;
/**
* The character(s) to repeat on either side of the string
*
* @var string $char
*/
protected $char = '#';
/**
* How many times the character(s) should be repeated on either side
*
* @var integer $repeat
*/
protected $repeat = 3;
public function __construct($str, $char = null, $repeat = null)
{
$this->str = $str;
$this->char($char)->repeat($repeat);
}
/**
* Set the character(s) to repeat on either side
*
* @param string $char
*
* @return Flank
*/
public function char($char)
{
$this->set('char', $char);
return $this;
}
/**
* Set the repeat of the flank character(s)
*
* @param integer $repeat
*
* @return Flank
*/
public function repeat($repeat)
{
$this->set('repeat', $repeat);
return $this;
}
/**
* Return the flanked string
*
* @return string
*/
public function result()
{
$flank = str_repeat($this->char, $this->repeat);
return "{$flank} {$this->str} {$flank}";
}
}
@@ -0,0 +1,16 @@
<?php
namespace League\CLImate\TerminalObject\Basic;
class Inline extends Out
{
/**
* Check if this object requires a new line should be added after the output
*
* @return boolean
*/
public function sameLine()
{
return true;
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace League\CLImate\TerminalObject\Basic;
class Json extends BasicTerminalObject
{
/**
* The data to convert to JSON
*
* @var mixed $data
*/
protected $data;
public function __construct($data)
{
$this->data = $data;
}
/**
* Return the data as JSON
*
* @return string
*/
public function result()
{
return json_encode($this->data, JSON_PRETTY_PRINT);
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace League\CLImate\TerminalObject\Basic;
class Out extends BasicTerminalObject
{
/**
* The content to output
*
* @var string $content
*/
protected $content;
public function __construct($content)
{
$this->content = $content;
}
/**
* Return the content to output
*
* @return string
*/
public function result()
{
return $this->content;
}
}
@@ -0,0 +1,18 @@
<?php
namespace League\CLImate\TerminalObject\Basic;
abstract class Repeatable extends BasicTerminalObject
{
/**
* How many times the element should be repeated
*
* @var integer
*/
protected $count;
public function __construct($count = 1)
{
$this->count = (int) round(max((int) $count, 1));
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace League\CLImate\TerminalObject\Basic;
/**
* Tab class to enable tabs to be output without using the escape character.
*/
class Tab extends Repeatable
{
/**
* Check if this object requires a new line should be added after the output.
*
* @return boolean
*/
public function sameLine()
{
return true;
}
/**
* Return the relevant number of tab characters.
*
* @return string
*/
public function result()
{
return str_repeat("\t", $this->count);
}
}
+225
View File
@@ -0,0 +1,225 @@
<?php
namespace League\CLImate\TerminalObject\Basic;
use League\CLImate\TerminalObject\Helper\StringLength;
class Table extends BasicTerminalObject
{
use StringLength;
/**
* The data for the table, an array of (arrays|objects)
*
* @var array $data
*/
protected $data = [];
/**
* An array of the widths of each column in the table
*
* @var array $column_widths
*/
protected $column_widths = [];
/**
* The width of the table
*
* @var integer $table_width
*/
protected $table_width = 0;
/**
* The divider between table cells
*
* @var string $column_divider
*/
protected $column_divider = ' | ';
/**
* The border to divide each row of the table
*
* @var string $border
*/
protected $border;
/**
* The array of rows that will ultimately be returned
*
* @var array $rows
*/
protected $rows = [];
public function __construct(array $data)
{
$this->data = $data;
}
/**
* Return the built rows
*
* @return array
*/
public function result()
{
$this->column_widths = $this->getColumnWidths();
$this->table_width = $this->getWidth();
$this->border = $this->getBorder();
$this->buildHeaderRow();
foreach ($this->data as $key => $columns) {
$this->rows[] = $this->buildRow($columns);
$this->rows[] = $this->border;
}
return $this->rows;
}
/**
* Determine the width of the table
*
* @return integer
*/
protected function getWidth()
{
$first_row = reset($this->data);
$first_row = $this->buildRow($first_row);
return $this->lengthWithoutTags($first_row);
}
/**
* Get the border for each row based on the table width
*/
protected function getBorder()
{
return (new Border())->length($this->table_width)->result();
}
/**
* Check for a header row (if it's an array of associative arrays or objects),
* if there is one, tack it onto the front of the rows array
*/
protected function buildHeaderRow()
{
$header_row = $this->getHeaderRow();
if ($header_row) {
$this->rows[] = $this->border;
$this->rows[] = $this->buildRow($header_row);
$this->rows[] = (new Border())->char('=')->length($this->table_width)->result();
} else {
$this->rows[] = $this->border;
}
}
/**
* Get table row
*
* @param mixed $columns
*
* @return string
*/
protected function buildRow($columns)
{
$row = [];
foreach ($columns as $key => $column) {
$row[] = $this->buildCell($key, $column);
}
$row = implode($this->column_divider, $row);
return trim($this->column_divider . $row . $this->column_divider);
}
/**
* Build the string for this particular table cell
*
* @param mixed $key
* @param string $column
*
* @return string
*/
protected function buildCell($key, $column)
{
return $this->pad($column, $this->column_widths[$key]);
}
/**
* Get the header row for the table if it's an associative array or object
*
* @return mixed
*/
protected function getHeaderRow()
{
$first_item = reset($this->data);
if (is_object($first_item)) {
$first_item = get_object_vars($first_item);
}
$keys = array_keys($first_item);
$first_key = reset($keys);
// We have an associative array (probably), let's have a header row
if (!is_int($first_key)) {
return array_combine($keys, $keys);
}
return false;
}
/**
* Determine the width of each column
*
* @return array
*/
protected function getColumnWidths()
{
$first_row = reset($this->data);
if (is_object($first_row)) {
$first_row = get_object_vars($first_row);
}
// Create an array with the columns as keys and values of zero
$column_widths = $this->getDefaultColumnWidths($first_row);
foreach ($this->data as $columns) {
foreach ($columns as $key => $column) {
$column_widths[$key] = $this->getCellWidth($column_widths[$key], $column);
}
}
return $column_widths;
}
/**
* Set up an array of default column widths
*
* @param array $columns
*
* @return array
*/
protected function getDefaultColumnWidths(array $columns)
{
$widths = $this->arrayOfStrLens(array_keys($columns));
return array_combine(array_keys($columns), $widths);
}
/**
* Determine the width of the columns without tags
*
* @param array $current_width
* @param string $str
*
* @return integer
*/
protected function getCellWidth($current_width, $str)
{
return max($current_width, $this->lengthWithoutTags($str));
}
}