Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Gich committed Mar 7, 2018
1 parent 7dd4360 commit 9d828a5
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/AbstractFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ abstract class AbstractFormatter implements FormatterInterface {
* @var boolean
*/
protected $useHeader = false;

/**
* @var boolean
*/
protected $useSidebar = false;

/**
* Formatting parameters
Expand Down Expand Up @@ -92,8 +97,8 @@ protected function init(Table $table, $params = []) {
if ($this->getParam('rotate') !== false) {
$this->table->rotate($this->getParam('rotate'));
}
$this->initHeader();
$this->initSidebar();
$this->initHeader();
$maxLength = $this->getParam('max_cell_length');
$ending = $this->getParam('max_cell_ending');
$this->table->truncate($maxLength, $ending);
Expand All @@ -114,12 +119,12 @@ protected function initHeader() {
}

if (($mode & self::HEADER_ABC_MODE) === self::HEADER_ABC_MODE) {
$data = array_merge([$this->getAbcRange($x)], $data);
$data = array_merge([$this->getAbcRange($x, $this->useSidebar)], $data);
$count++;
}

if (($mode & self::HEADER_NUMERIC_MODE) === self::HEADER_NUMERIC_MODE) {
$data = array_merge([range(1, $x)], $data);
$data = array_merge([$this->getNumRange($x, $this->useSidebar)], $data);
$count++;
}

Expand All @@ -141,19 +146,21 @@ protected function initSidebar() {
$count = 0;
$mode = $this->getParam('mode');
$data = $this->table->getData();
$headerMode = ($mode & self::HEADER_FIRST_ROW_MODE) === self::HEADER_FIRST_ROW_MODE;
$y = $this->table->getDimensionY();

if (($mode & self::SIDEBAR_ABC_MODE) === self::SIDEBAR_ABC_MODE) {
$y = $this->table->getDimensionY();
$abc = $this->getAbcRange($y);
$abc = $this->getAbcRange($y, $headerMode);
foreach ($data as $key => $val) {
$data[$key] = array_merge([array_shift($abc)], $val);
}
$count++;
}

if (($mode & self::SIDEBAR_NUMERIC_MODE) === self::SIDEBAR_NUMERIC_MODE) {
$num = $this->getNumRange($y, $headerMode);
foreach ($data as $key => $val) {
$data[$key] = array_merge([$key ? $key : ''], $val);
$data[$key] = array_merge([array_shift($num)], $val);
}
$count++;
}
Expand All @@ -163,6 +170,7 @@ protected function initSidebar() {
}

if ($count) {
$this->useSidebar = true;
$this->table->setData($data);
}
}
Expand Down Expand Up @@ -194,7 +202,8 @@ protected function getParam($key) {
* @param int $length
* @return array
*/
final protected function getAbcRange($length) {
final protected function getAbcRange($length, $firstEmpty = false) {
$length = $firstEmpty ? $length - 1 : $length;
$baseRange = range('A', 'Z');
$range = range('A', 'Z');
while (count($range) < $length && count($baseRange) > 1) {
Expand All @@ -203,7 +212,25 @@ final protected function getAbcRange($length) {
array_walk($tmpRange, function(&$e) use ($letter) { $e = $letter . $e; });
$range = array_merge($range, $tmpRange);
}
return array_slice($range, 0, $length);
$range = array_slice($range, 0, $length);
if ($firstEmpty) {
$range = array_merge([''], $range);
}
return $range;
}

/**
* Returns an array of digits for a spreadsheet header
* @param int $length
* @return array
*/
final protected function getNumRange($length, $firstEmpty = false) {
$length = $firstEmpty ? $length - 1 : $length;
$range = range(1, $length);
if ($firstEmpty) {
$range = array_merge([''], $range);
}
return $range;
}

/**
Expand Down

0 comments on commit 9d828a5

Please sign in to comment.