diff --git a/src/exception/ZugferdBaseException.php b/src/exception/ZugferdBaseException.php new file mode 100644 index 0000000..96e6548 --- /dev/null +++ b/src/exception/ZugferdBaseException.php @@ -0,0 +1,23 @@ + + * @license https://opensource.org/licenses/MIT MIT + * @link https://github.com/horstoeko/zugferd + */ +class ZugferdBaseException extends \Exception +{ +} diff --git a/src/exception/ZugferdExceptionCodes.php b/src/exception/ZugferdExceptionCodes.php new file mode 100644 index 0000000..d0cba60 --- /dev/null +++ b/src/exception/ZugferdExceptionCodes.php @@ -0,0 +1,32 @@ + + * @license https://opensource.org/licenses/MIT MIT + * @link https://github.com/horstoeko/zugferd + */ +class ZugferdExceptionCodes +{ + public const CANNOTFINDPROFILESTRING = -1101; + public const UNKNOWNPROFILE = -1102; + public const MIMETYPENOTSUPPORTED = -1103; + public const UNKNOWNDATEFORMAT = -1104; + public const NOVALIDATTACHMENTFOUNDINPDF = -1105; + public const UNKNOWNPROFILEPARAMETER = -1106; + public const UNKNOWNSYNTAX = -1107; + public const FILENOTFOUND = -2000; + public const FILENOTREADABLE = -2001; +} diff --git a/src/exception/ZugferdFileNotFoundException.php b/src/exception/ZugferdFileNotFoundException.php index 0072ba1..f514d17 100644 --- a/src/exception/ZugferdFileNotFoundException.php +++ b/src/exception/ZugferdFileNotFoundException.php @@ -9,7 +9,7 @@ namespace horstoeko\zugferd\exception; -use Exception; +use Throwable; /** * Class representing an exception for missing a file @@ -20,42 +20,16 @@ * @license https://opensource.org/licenses/MIT MIT * @link https://github.com/horstoeko/zugferd */ -class ZugferdFileNotFoundException extends Exception +class ZugferdFileNotFoundException extends ZugferdBaseException { - /** - * The context of the type element - * - * @var string - */ - private $filePath = ""; - /** * Constructor * - * @param string $filePath - */ - public function __construct(string $filePath) - { - $this->filePath = $filePath; - - parent::__construct($this->buildMessage()); - } - - /** - * @inheritDoc - */ - public function __toString() - { - return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; - } - - /** - * Build the message - * - * @return string + * @param string $filename + * @param Throwable|null $previous */ - private function buildMessage(): string + public function __construct(string $filename, ?Throwable $previous = null) { - return sprintf("The filer %s is missing", $this->filePath); + parent::__construct(sprintf("The file %s was not found", $filename), ZugferdExceptionCodes::FILENOTFOUND, $previous); } } diff --git a/src/exception/ZugferdFileNotReadableException.php b/src/exception/ZugferdFileNotReadableException.php index c7c081b..7988577 100644 --- a/src/exception/ZugferdFileNotReadableException.php +++ b/src/exception/ZugferdFileNotReadableException.php @@ -9,7 +9,7 @@ namespace horstoeko\zugferd\exception; -use Exception; +use Throwable; /** * Class representing an exception for non-readable a file @@ -20,42 +20,16 @@ * @license https://opensource.org/licenses/MIT MIT * @link https://github.com/horstoeko/zugferd */ -class ZugferdFileNotReadableException extends Exception +class ZugferdFileNotReadableException extends ZugferdBaseException { - /** - * The context of the type element - * - * @var string - */ - private $filePath = ""; - /** * Constructor * - * @param string $filePath - */ - public function __construct(string $filePath) - { - $this->filePath = $filePath; - - parent::__construct($this->buildMessage()); - } - - /** - * @inheritDoc - */ - public function __toString() - { - return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; - } - - /** - * Build the message - * - * @return string + * @param string $filename + * @param Throwable|null $previous */ - private function buildMessage(): string + public function __construct(string $filename, ?Throwable $previous = null) { - return sprintf("The filer %s is not readable", $this->filePath); + parent::__construct(sprintf("The file %s is not readable", $filename), ZugferdExceptionCodes::FILENOTFOUND, $previous); } } diff --git a/src/exception/ZugferdUnknownDateFormatException.php b/src/exception/ZugferdUnknownDateFormatException.php index e02ef12..8c91504 100644 --- a/src/exception/ZugferdUnknownDateFormatException.php +++ b/src/exception/ZugferdUnknownDateFormatException.php @@ -9,6 +9,8 @@ namespace horstoeko\zugferd\exception; +use Throwable; + /** * Class representing an exception for unknown date formates * @@ -18,42 +20,10 @@ * @license https://opensource.org/licenses/MIT MIT * @link https://github.com/horstoeko/zugferd */ -class ZugferdUnknownDateFormatException extends \Exception +class ZugferdUnknownDateFormatException extends ZugferdBaseException { - /** - * The context of the type element - * - * @var string - */ - private $dateFormat = ""; - - /** - * Constructor - * - * @param string $dateFormat - */ - public function __construct(string $dateFormat) - { - $this->dateFormat = $dateFormat; - - parent::__construct($this->buildMessage()); - } - - /** - * @inheritDoc - */ - public function __toString() - { - return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; - } - - /** - * Build the message - * - * @return string - */ - private function buildMessage(): string + public function __construct(string $dateFormatCode, ?Throwable $previous = null) { - return sprintf("The date format identifier %s is unknown or not supported", $this->dateFormat); + parent::__construct(sprintf("Invalid date format %s", $dateFormatCode), ZugferdExceptionCodes::UNKNOWNDATEFORMAT, $previous); } } diff --git a/src/exception/ZugferdUnknownProfileException.php b/src/exception/ZugferdUnknownProfileException.php index 4ab78a1..bc8739d 100644 --- a/src/exception/ZugferdUnknownProfileException.php +++ b/src/exception/ZugferdUnknownProfileException.php @@ -9,7 +9,7 @@ namespace horstoeko\zugferd\exception; -use Exception; +use Throwable; /** * Class representing an exception for unknown profile @@ -20,42 +20,16 @@ * @license https://opensource.org/licenses/MIT MIT * @link https://github.com/horstoeko/zugferd */ -class ZugferdUnknownProfileException extends Exception +class ZugferdUnknownProfileException extends ZugferdBaseException { - /** - * The context of the type element - * - * @var string - */ - private $contextElement = ""; - /** * Constructor * - * @param string $contextElement - */ - public function __construct(string $contextElement) - { - $this->contextElement = $contextElement; - - parent::__construct($this->buildMessage()); - } - - /** - * @inheritDoc - */ - public function __toString() - { - return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; - } - - /** - * Build the message - * - * @return string + * @param string $profileString + * @param Throwable|null $previous */ - private function buildMessage(): string + public function __construct(string $profileString, ?Throwable $previous = null) { - return sprintf("A context parameter was found, but the content of \"%s\" is not a valid profile", $this->contextElement); + parent::__construct(sprintf("Cannot determain the profile by %s", $profileString), ZugferdExceptionCodes::UNKNOWNPROFILE, $previous); } } diff --git a/src/exception/ZugferdUnknownProfileIdException.php b/src/exception/ZugferdUnknownProfileIdException.php index 8292de9..90ae4c1 100644 --- a/src/exception/ZugferdUnknownProfileIdException.php +++ b/src/exception/ZugferdUnknownProfileIdException.php @@ -9,7 +9,7 @@ namespace horstoeko\zugferd\exception; -use Exception; +use Throwable; /** * Class representing an exception for unknown profile id @@ -20,42 +20,16 @@ * @license https://opensource.org/licenses/MIT MIT * @link https://github.com/horstoeko/zugferd */ -class ZugferdUnknownProfileIdException extends Exception +class ZugferdUnknownProfileIdException extends ZugferdBaseException { - /** - * The context of the type element - * - * @var integer - */ - private $profileId = 0; - /** * Constructor * - * @param integer $profileId - */ - public function __construct(int $profileId) - { - $this->profileId = $profileId; - - parent::__construct($this->buildMessage()); - } - - /** - * @inheritDoc - */ - public function __toString() - { - return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; - } - - /** - * Build the message - * - * @return string + * @param int $profileId + * @param Throwable|null $previous */ - private function buildMessage(): string + public function __construct(int $profileId, ?Throwable $previous = null) { - return sprintf("The profile id %s is uknown", $this->profileId); + parent::__construct(sprintf("The profile id %s is uknown", $profileId), ZugferdExceptionCodes::UNKNOWNPROFILE, $previous); } } diff --git a/src/exception/ZugferdUnknownProfileParameterException.php b/src/exception/ZugferdUnknownProfileParameterException.php index cd2b2e4..5b9ece5 100644 --- a/src/exception/ZugferdUnknownProfileParameterException.php +++ b/src/exception/ZugferdUnknownProfileParameterException.php @@ -9,7 +9,7 @@ namespace horstoeko\zugferd\exception; -use Exception; +use Throwable; /** * Class representing an exception for unknown profile parameter @@ -20,42 +20,16 @@ * @license https://opensource.org/licenses/MIT MIT * @link https://github.com/horstoeko/zugferd */ -class ZugferdUnknownProfileParameterException extends Exception +class ZugferdUnknownProfileParameterException extends ZugferdBaseException { - /** - * The context of the type element - * - * @var string - */ - private $profileParameter = ""; - /** * Constructor * - * @param string $profileParameter - */ - public function __construct(string $profileParameter) - { - $this->profileParameter = $profileParameter; - - parent::__construct($this->buildMessage()); - } - - /** - * @inheritDoc - */ - public function __toString() - { - return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; - } - - /** - * Build the message - * - * @return string + * @param string $profileParameter + * @param Throwable|null $previous */ - private function buildMessage(): string + public function __construct(string $profileParameter, ?Throwable $previous = null) { - return sprintf("The profile parameter %s is uknown", $this->profileParameter); + parent::__construct(sprintf("The profile parameter %s is uknown", $profileParameter), ZugferdExceptionCodes::UNKNOWNPROFILEPARAMETER, $previous); } } diff --git a/src/exception/ZugferdUnknownXmlContentException.php b/src/exception/ZugferdUnknownXmlContentException.php index af55eb9..65be861 100644 --- a/src/exception/ZugferdUnknownXmlContentException.php +++ b/src/exception/ZugferdUnknownXmlContentException.php @@ -9,7 +9,6 @@ namespace horstoeko\zugferd\exception; -use Exception; use Throwable; /** @@ -21,31 +20,15 @@ * @license https://opensource.org/licenses/MIT MIT * @link https://github.com/horstoeko/zugferd */ -class ZugferdUnknownXmlContentException extends Exception +class ZugferdUnknownXmlContentException extends ZugferdBaseException { /** * Constructor - */ - public function __construct() - { - parent::__construct($this->buildMessage()); - } - - /** - * @inheritDoc - */ - public function __toString() - { - return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; - } - - /** - * Build the message * - * @return string + * @param Throwable|null $previous */ - private function buildMessage(): string + public function __construct(?Throwable $previous = null) { - return "The XML does not match the requirements for an XML in CII-Syntax"; + parent::__construct("The XML does not match the requirements for an XML in CII-Syntax", ZugferdExceptionCodes::UNKNOWNSYNTAX, $previous); } } diff --git a/tests/testcases/ObjectHelperEn16931Test.php b/tests/testcases/ObjectHelperEn16931Test.php index e7464a4..597dc77 100644 --- a/tests/testcases/ObjectHelperEn16931Test.php +++ b/tests/testcases/ObjectHelperEn16931Test.php @@ -1308,7 +1308,7 @@ public function testToDateTimeGeneral(): void $this->assertNull(self::$objectHelper->toDateTime(null, "")); $this->assertNull(self::$objectHelper->toDateTime(null, null)); $this->expectException(ZugferdUnknownDateFormatException::class); - $this->expectExceptionMessage("The date format identifier 999 is unknown or not supported"); + $this->expectExceptionMessage("Invalid date format 999"); $this->assertNull(self::$objectHelper->toDateTime("20200202", "999")); } diff --git a/tests/testcases/ObjectHelperExtendedTest.php b/tests/testcases/ObjectHelperExtendedTest.php index 0437034..ad4dfe8 100644 --- a/tests/testcases/ObjectHelperExtendedTest.php +++ b/tests/testcases/ObjectHelperExtendedTest.php @@ -1307,7 +1307,7 @@ public function testToDateTimeGeneral(): void $this->assertNull(self::$objectHelper->toDateTime(null, "")); $this->assertNull(self::$objectHelper->toDateTime(null, null)); $this->expectException(ZugferdUnknownDateFormatException::class); - $this->expectExceptionMessage("The date format identifier 999 is unknown or not supported"); + $this->expectExceptionMessage("Invalid date format 999"); $this->assertNull(self::$objectHelper->toDateTime("20200202", "999")); } diff --git a/tests/testcases/ProfileResolverTest.php b/tests/testcases/ProfileResolverTest.php index 8d4662e..a2eef48 100644 --- a/tests/testcases/ProfileResolverTest.php +++ b/tests/testcases/ProfileResolverTest.php @@ -7,6 +7,7 @@ use horstoeko\zugferd\tests\TestCase; use horstoeko\zugferd\ZugferdProfiles; use horstoeko\zugferd\ZugferdProfileResolver; +use horstoeko\zugferd\exception\ZugferdUnknownProfileException; use horstoeko\zugferd\exception\ZugferdUnknownProfileIdException; class ProfileResolverTest extends TestCase @@ -153,8 +154,8 @@ public function testResolveProfileDefEn16931() public function testResolveUnknownProfile() { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('A context parameter was found, but the content of "unknown" is not a valid profile'); + $this->expectException(ZugferdUnknownProfileException::class); + $this->expectExceptionMessage('Cannot determain the profile by unknown'); ZugferdProfileResolver::resolveProfileId($this->deliverUnknownProfile()); }