From dae47cd9b711b9d9d572fb1292c1e10fd1494738 Mon Sep 17 00:00:00 2001 From: Adrien Loison Date: Tue, 29 Nov 2016 14:19:22 -0800 Subject: [PATCH] Improve error message when invalid sheet name set Instead of listing all the requirements, only list the requirements that actually failed to be met. --- src/Spout/Writer/Common/Sheet.php | 53 ++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/Spout/Writer/Common/Sheet.php b/src/Spout/Writer/Common/Sheet.php index f6a966b..2c0d1f2 100644 --- a/src/Spout/Writer/Common/Sheet.php +++ b/src/Spout/Writer/Common/Sheet.php @@ -75,14 +75,7 @@ class Sheet */ public function setName($name) { - if (!$this->isNameValid($name)) { - $errorMessage = "The sheet's name is invalid. It did not meet at least one of these requirements:\n"; - $errorMessage .= " - It should not be blank\n"; - $errorMessage .= " - It should not exceed 31 characters\n"; - $errorMessage .= " - It should not contain these characters: \\ / ? * : [ or ]\n"; - $errorMessage .= " - It should be unique"; - throw new InvalidSheetNameException($errorMessage); - } + $this->throwIfNameIsInvalid($name); $this->name = $name; self::$SHEETS_NAME_USED[$this->index] = $name; @@ -91,27 +84,49 @@ class Sheet } /** - * Returns whether the given sheet's name is valid. + * Throws an exception if the given sheet's name is not valid. * @see Sheet::setName for validity rules. * * @param string $name - * @return bool TRUE if the name is valid, FALSE otherwise. + * @return void + * @throws \Box\Spout\Writer\Exception\InvalidSheetNameException If the sheet's name is invalid. */ - protected function isNameValid($name) + protected function throwIfNameIsInvalid($name) { if (!is_string($name)) { - return false; + $actualType = gettype($name); + $errorMessage = "The sheet's name is invalid. It must be a string ($actualType given)."; + throw new InvalidSheetNameException($errorMessage); } + $failedRequirements = []; $nameLength = $this->stringHelper->getStringLength($name); - return ( - $nameLength > 0 && - $nameLength <= self::MAX_LENGTH_SHEET_NAME && - !$this->doesContainInvalidCharacters($name) && - $this->isNameUnique($name) && - !$this->doesStartOrEndWithSingleQuote($name) - ); + if (!$this->isNameUnique($name)) { + $failedRequirements[] = 'It should be unique'; + } else { + if ($nameLength === 0) { + $failedRequirements[] = 'It should not be blank'; + } else { + if ($nameLength > self::MAX_LENGTH_SHEET_NAME) { + $failedRequirements[] = 'It should not exceed 31 characters'; + } + + if ($this->doesContainInvalidCharacters($name)) { + $failedRequirements[] = 'It should not contain these characters: \\ / ? * : [ or ]'; + } + + if ($this->doesStartOrEndWithSingleQuote($name)) { + $failedRequirements[] = 'It should not start or end with a single quote'; + } + } + } + + if (count($failedRequirements) !== 0) { + $errorMessage = "The sheet's name (\"$name\") is invalid. It did not respect these rules:\n - "; + $errorMessage .= implode("\n - ", $failedRequirements); + throw new InvalidSheetNameException($errorMessage); + } } /**