Skip to content

Commit

Permalink
Code refactoring. Added Export to Pdf. Well, nothing has changed in f…
Browse files Browse the repository at this point in the history
…act.
  • Loading branch information
Sidorovich Nikolay committed May 3, 2023
1 parent d527a9e commit db87e88
Show file tree
Hide file tree
Showing 15 changed files with 725 additions and 739 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
},
"require": {
"php": ">=7.2",
"php": "^7.4 || ^8.0",
"phpoffice/phpspreadsheet": ">=1.22"
},
"require-dev": {
Expand Down
369 changes: 170 additions & 199 deletions src/InsertedCells.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,206 +6,177 @@

class InsertedCells
{
/**
* @var integer[][] Inserted rows, where
* key 1 - index of column,
* key 2 - index of row,
* value - count of inserted rows in the row of template variable
*/
public $inserted_rows;

/**
* @var integer[][] Inserted columns, where
* ключ 1 - index of row,
* ключ 2 - index of column,
public array $inserted_cols = [];
public array $inserted_rows = [];

/**
* @param integer[][] $inserted_cols Inserted columns, where
* key 1 - index of column,
* key 2 - index of row,
* value - count of inserted rows in the row of template variable
*
* @param integer[][] $inserted_rows Inserted rows, where
* key 1 - index of row,
* key 2 - index of column,
* value - count of inserted columns in the column of template variable
*/
public $inserted_cols;

/**
* @param integer[] $inserted_cols
* @param integer[] $inserted_rows
*/
public function __construct($inserted_cols=[], $inserted_rows=[])
{
$this->inserted_cols = $inserted_cols;
$this->inserted_rows = $inserted_rows;
}

/**
* @param $row_key integer Current index of row
* @param $col_key integer Current index of column
* @return integer Total count of inserted rows, that were inserted from 0 row to current row
*/
public function getInsertedRowsCount($row_key, $col_key): int
{
$this->_addKeysTo2DArrayIfNotExists($this->inserted_rows, $col_key, $row_key);

$result = 0;
for($i=0; $i < $row_key; $i++) {
$result += $this->inserted_rows[$col_key][$i];
}
return $result;
}

/**
* @param $row_key integer Current index of row
* @param $col_key integer Current index of column
* @return integer Total count of inserted columns, that were inserted from 0 column to current column
*/
public function getInsertedColsCount($row_key, $col_key): int
{
$this->_addKeysTo2DArrayIfNotExists($this->inserted_cols, $row_key, $col_key);

$result = 0;
for($i=0; $i < $col_key; $i++) {
$result += $this->inserted_cols[$row_key][$i];
}
return $result;
}

/**
* @param $row_key integer The row index, where was template variable
* @param $col_key integer The column index, where was template variable
* @param $inserted_cols_count integer
*/
public function setInsertedCols($row_key, $col_key, $inserted_cols_count): void
{
$this->_addKeysTo2DArrayIfNotExists($this->inserted_cols, $row_key, $col_key);
$this->inserted_cols[$row_key][$col_key] = $inserted_cols_count;
}

/**
* @param $row_key integer The row index, where was template variable
* @param $col_key integer The column index, where was template variable
* @param $inserted_rows_count integer
*/
public function setInsertedRows($row_key, $col_key, $inserted_rows_count): void
{
$this->_addKeysTo2DArrayIfNotExists($this->inserted_rows, $col_key, $row_key);
$this->inserted_rows[$col_key][$row_key] = $inserted_rows_count;
}

/**
* @param $row_key integer The row index, where was template variable
* @param $col_key integer The column index, where was template variable
* @param $inserted_cols_count integer
*/
public function addInsertedCols($row_key, $col_key, $inserted_cols_count): void
{
$this->_addKeysTo2DArrayIfNotExists($this->inserted_cols, $row_key, $col_key);
$this->inserted_cols[$row_key][$col_key] += $inserted_cols_count;
}

/**
* @param $row_key integer
* @param $col_key integer
* @param $inserted_rows_count integer
*/
public function addInsertedRows($row_key, $col_key, $inserted_rows_count): void
{
$this->_addKeysTo2DArrayIfNotExists($this->inserted_rows, $col_key, $row_key);
$this->inserted_rows[$col_key][$row_key] += $inserted_rows_count;
}

/**
* @param $row_key integer
* @param $col_key integer
* @return integer
*/
public function getInsertedRows($row_key, $col_key): int
{
$this->_addKeysTo2DArrayIfNotExists($this->inserted_rows, $col_key, $row_key);
return $this->inserted_rows[$col_key][$row_key];
}

/**
* @param $row_key integer
* @param $col_key integer
* @return integer
*/
public function getInsertedCols($row_key, $col_key): int
{
$this->_addKeysTo2DArrayIfNotExists($this->inserted_cols, $row_key, $col_key);
return $this->inserted_cols[$row_key][$col_key];
}

/**
*/
public function __construct(array $inserted_cols = [], array $inserted_rows = [])
{
$this->inserted_rows = $inserted_rows;
$this->inserted_cols = $inserted_cols;
}

/**
* @param $rowKey integer Current index of row
* @param $colKey integer Current index of column
*
* @return integer Total count of inserted rows, that were inserted from 0 row to current row
*/
public function getInsertedRowsCount(int $rowKey, int $colKey): int
{
$this->_addKeysTo2DArrayIfNotExists($this->inserted_rows, $colKey, $rowKey);

$result = 0;
for ($i = 0; $i < $rowKey; $i++) {
$result += $this->inserted_rows[$colKey][$i];
}
return $result;
}

/**
* @param $rowKey integer Current index of row
* @param $colKey integer Current index of column
*
* @return integer Total count of inserted columns, that were inserted from 0 column to current column
*/
public function getInsertedColsCount(int $rowKey, int $colKey): int
{
$this->_addKeysTo2DArrayIfNotExists($this->inserted_cols, $rowKey, $colKey);

$result = 0;
for ($i = 0; $i < $colKey; $i++) {
$result += $this->inserted_cols[$rowKey][$i];
}
return $result;
}

/**
* @param $rowKey integer The row index, where was template variable
* @param $colKey integer The column index, where was template variable
* @param $insertedColsCount integer
*/
public function setInsertedCols(int $rowKey, int $colKey, int $insertedColsCount): void
{
$this->_addKeysTo2DArrayIfNotExists($this->inserted_cols, $rowKey, $colKey);
$this->inserted_cols[$rowKey][$colKey] = $insertedColsCount;
}

/**
* @param $rowKey integer The row index, where was template variable
* @param $colKey integer The column index, where was template variable
* @param $insertedRowsCount integer
*/
public function setInsertedRows(int $rowKey, int $colKey, int $insertedRowsCount): void
{
$this->_addKeysTo2DArrayIfNotExists($this->inserted_rows, $colKey, $rowKey);
$this->inserted_rows[$colKey][$rowKey] = $insertedRowsCount;
}

/**
* @param $rowKey integer The row index, where was template variable
* @param $colKey integer The column index, where was template variable
* @param $insertedColsCount integer
*/
public function addInsertedCols(int $rowKey, int $colKey, int $insertedColsCount): void
{
$this->_addKeysTo2DArrayIfNotExists($this->inserted_cols, $rowKey, $colKey);
$this->inserted_cols[$rowKey][$colKey] += $insertedColsCount;
}

public function addInsertedRows(int $rowKey, int $colKey, int $insertedRowsCount): void
{
$this->_addKeysTo2DArrayIfNotExists($this->inserted_rows, $colKey, $rowKey);
$this->inserted_rows[$colKey][$rowKey] += $insertedRowsCount;
}

public function getInsertedRows(int $rowKey, int $colKey): int
{
$this->_addKeysTo2DArrayIfNotExists($this->inserted_rows, $colKey, $rowKey);
return $this->inserted_rows[$colKey][$rowKey];
}

public function getInsertedCols(int $rowKey, int $colKey): int
{
$this->_addKeysTo2DArrayIfNotExists($this->inserted_cols, $rowKey, $colKey);
return $this->inserted_cols[$rowKey][$colKey];
}

/**
* Inserts indexes to specified array. The array filled with indexes from 0 to $key_index, if the index does not exist.
* @param $array array
* @param $key_index integer
*/
private function _addKeysToArrayIfNotExists(&$array, $key_index): void
{
for($i=0; $i <= $key_index; $i++) {
if ( ! array_key_exists($i, $array)) {
$array[$i] = 0;
}
}
}

/**
*/
private function _addKeysToArrayIfNotExists(array &$array, int $keyIndex): void
{
for ($i = 0; $i <= $keyIndex; $i++) {
if (!array_key_exists($i, $array)) {
$array[$i] = 0;
}
}
}

/**
* Inserts indexes to specified 2D array. The array filled with indexes from 0 to $key_index, if the indexes does not exist.
* @param $array array
* @param $i_max integer
* @param $j_max integer
*/
private function _addKeysTo2DArrayIfNotExists(&$array, $i_max, $j_max): void
{
for($i=0; $i <= $i_max; $i++) {
for($j=0; $j <= $j_max; $j++) {
if ( ! array_key_exists($i, $array)) {
$array[$i] = [];
}
if ( ! array_key_exists($j, $array[$i])) {
$array[$i][$j] = 0;
}
}
}
}

/**
* @param $row_key integer The row index, where was template variable
* @param $col_key integer The column index, where was template variable
* @return integer Index of row, considering the count of inserted columns and rows
*/
public function getCurrentRowIndex($row_key, $col_key): int
{
$inserted_rows = $this->getInsertedRowsCount($row_key, $col_key);
return $row_key + 1 + $inserted_rows;
}

/**
* @param $row_key integer
* @param $col_key integer
* @return integer Index of column, considering the count of inserted columns and rows
*/
public function getCurrentColIndex($row_key, $col_key): int
{
$inserted_cols = $this->getInsertedColsCount($row_key, $col_key);
return $col_key + 1 + $inserted_cols;
}

/**
* @param $row_key integer
* @param $col_key integer
* @return string Index of column (as a letter), considering the count of inserted columns and rows
*/
public function getCurrentCol($row_key, $col_key): string
{
$col_index = $this->getCurrentColIndex($row_key, $col_key);
return Coordinate::stringFromColumnIndex($col_index);
}

/**
* @param $row_key integer
* @param $col_key integer
* @return string Cell coordinate, considering the count of inserted columns and rows
*/
public function getCurrentCellCoordinate($row_key, $col_key): string
{
$col = $this->getCurrentCol($row_key, $col_key);
$row_index = $this->getCurrentRowIndex($row_key, $col_key);
return $col . $row_index;
}
*/
private function _addKeysTo2DArrayIfNotExists(array &$array, int $i_max, int $j_max): void
{
for ($i = 0; $i <= $i_max; $i++) {
for ($j = 0; $j <= $j_max; $j++) {
if (!array_key_exists($i, $array)) {
$array[$i] = [];
}
if (!array_key_exists($j, $array[$i])) {
$array[$i][$j] = 0;
}
}
}
}

/**
* @param $rowKey integer The row index, where was template variable
* @param $colKey integer The column index, where was template variable
*
* @return integer Index of row, considering the count of inserted columns and rows
*/
public function getCurrentRowIndex(int $rowKey, int $colKey): int
{
$inserted_rows = $this->getInsertedRowsCount($rowKey, $colKey);
return $rowKey + 1 + $inserted_rows;
}

/**
* @return integer Index of column, considering the count of inserted columns and rows
*/
public function getCurrentColIndex(int $rowKey, int $colKey): int
{
$inserted_cols = $this->getInsertedColsCount($rowKey, $colKey);
return $colKey + 1 + $inserted_cols;
}

/**
* @return string Index of column (as a letter), considering the count of inserted columns and rows
*/
public function getCurrentCol(int $rowKey, int $colKey): string
{
$col_index = $this->getCurrentColIndex($rowKey, $colKey);
return Coordinate::stringFromColumnIndex($col_index);
}

/**
* @return string Cell coordinate, considering the count of inserted columns and rows
*/
public function getCurrentCellCoordinate(int $rowKey, int $colKey): string
{
$col = $this->getCurrentCol($rowKey, $colKey);
$row_index = $this->getCurrentRowIndex($rowKey, $colKey);
return $col . $row_index;
}
}
Loading

0 comments on commit db87e88

Please sign in to comment.