From d40315247e0d4ea9b3c0c65e6acad8931da48a7a Mon Sep 17 00:00:00 2001 From: rafageist Date: Tue, 16 Jan 2024 23:44:46 -0300 Subject: [PATCH] workbook --- src/matrix.php | 107 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 97 insertions(+), 10 deletions(-) diff --git a/src/matrix.php b/src/matrix.php index 6aa1e61..9a3ef7f 100644 --- a/src/matrix.php +++ b/src/matrix.php @@ -50,8 +50,8 @@ class matrix public const FORMAT_YAML = "YAML"; public const FORMAT_TXT = "TXT"; public const FORMAT_SQL = "SQL"; - - public static $worksheet = []; + private $defaultFormat = self::FORMAT_TXT; + public static $workbook = []; /** * Constructor @@ -73,7 +73,7 @@ public function __construct(array $matrix) $this->matrix = $matrix; $this->matrix_original = $matrix; - self::$worksheet[] = $this; + self::$workbook[] = $this; self::evaluateAll(); } @@ -668,13 +668,18 @@ public function evaluate(): void } } + /** + * Process all matrixes + * + * @return void + */ public static function evaluateAll(): void { if (self::$disableEvallAll) { return; } - foreach (self::$worksheet as $worksheet) { + foreach (self::$workbook as $worksheet) { $worksheet->evaluate(); } } @@ -801,18 +806,27 @@ public function getTotalRows() public function getTotalColumns() { return count($this->matrix[0]); - } - - public function __toString() - { - return $this->format(self::FORMAT_TXT); - } + } + /** + * Get a row + * + * @param int $row + * + * @return array + */ public function getRow(int $row): array { return $this->matrix[$row]; } + /** + * Get a column + * + * @param int $column + * + * @return array + */ public function getColumn(int $column): array { $result = []; @@ -822,11 +836,25 @@ public function getColumn(int $column): array return $result; } + /** + * Get a row with formulas + * + * @param int $row + * + * @return array + */ public function getRowWithFormulas(int $row): array { return $this->matrix_original[$row]; } + /** + * Get a column with formulas + * + * @param int $column + * + * @return array + */ public function getColumnWithFormulas(int $column): array { $result = []; @@ -836,4 +864,63 @@ public function getColumnWithFormulas(int $column): array return $result; } + /** + * Validate a format + * + * @param string $format + * + * @throws InvalidArgumentException + * + * @return void + */ + private function validateFormat(string $format): void + { + if (!in_array($format, [ + self::FORMAT_CSV, + self::FORMAT_XML, + self::FORMAT_JSON, + self::FORMAT_JSON_OBJECTS, + self::FORMAT_SERIALIZE, + self::FORMAT_HTML, + self::FORMAT_MARKDOWN, + self::FORMAT_YAML, + self::FORMAT_TXT, + self::FORMAT_SQL, + ])) { + throw new InvalidArgumentException('Invalid format'); + } + } + + /** + * Get the default format + * + * @return string + */ + public function getDefaultFormat() + { + return $this->defaultFormat; + } + + /** + * Set the default format + * + * @param string $format + * + * @return void + */ + public function setDefaultFormat(string $format) + { + $this->validateFormat($format); + $this->defaultFormat = $format; + } + + /** + * Magic method to convert the matrix to string + * + * @return string + */ + public function __toString() + { + return $this->format($this->defaultFormat); + } }