From c129ec0be50974f990d21d0f3337a439f9324a5c Mon Sep 17 00:00:00 2001 From: Wector975 Date: Sun, 22 Dec 2024 15:37:57 +0400 Subject: [PATCH] License. --- LICENSE | 21 +++ README.md | 6 +- composer.json | 1 + src/Contract/ExceptionRepositoryInterface.php | 31 +++++ src/Service/ExceptionRepository.php | 121 +++++++++--------- 5 files changed, 119 insertions(+), 61 deletions(-) create mode 100644 LICENSE create mode 100644 src/Contract/ExceptionRepositoryInterface.php diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c408340 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Aleksandr Zakhozhiy + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index f3b53e4..6280077 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -# azakhozhiy/laravel-exceptions \ No newline at end of file +# Laravel Exceptions + +## License + +This library is licensed under the [MIT License](./LICENSE). \ No newline at end of file diff --git a/composer.json b/composer.json index 45c3116..4c84ef8 100644 --- a/composer.json +++ b/composer.json @@ -2,6 +2,7 @@ "name": "azakhozhiy/laravel-exceptions", "version": "1.0.0", "description": "Laravel Exceptions", + "license": "MIT", "authors": [ { "name": "Aleksandr Zakhozhiy", diff --git a/src/Contract/ExceptionRepositoryInterface.php b/src/Contract/ExceptionRepositoryInterface.php new file mode 100644 index 0000000..48bb068 --- /dev/null +++ b/src/Contract/ExceptionRepositoryInterface.php @@ -0,0 +1,31 @@ +categories); } - public function assertCategoryNotExists(string $catSlug): void - { - if (isset($this->categories[$catSlug])) { - throw new ExceptionCategoryAlreadyRegistered( - "Category $catSlug already registered." - ); - } - } - - public function assertCategoryExists(string $catSlug): void - { - if (!isset($this->categories[$catSlug])) { - throw new UnknownExceptionCategory( - "Unknown exception category $catSlug." - ); - } - } - /** * @param class-string $catClass * @return $this @@ -82,25 +65,6 @@ public function registerException(string $catClass, string $exceptionClass): sta return $this; } - protected function assertCategoryAndExceptionNotExist(string $catSlug, int $errorCode): void - { - if (isset($this->exceptions[$catSlug][$errorCode])) { - throw new ExceptionCodeAlreadyRegistered( - "Exception code [$errorCode] already " . - "registered for the [$catSlug] category." - ); - } - } - - protected function assertCategoryAndExceptionExist(string $catSlug, int $errorCode): void - { - if (!isset($this->exceptions[$catSlug][$errorCode])) { - throw new UnknownExceptionCode( - "Unknown exception code $errorCode for category $catSlug." - ); - } - } - /** * @param string $catSlug * @return BaseExceptionObject[] @@ -117,28 +81,6 @@ public function getExceptionObjectsByCategory(string $catSlug): array return $items; } - public function getExceptionObject(string $catSlug, int $errorCode): BaseExceptionObject - { - $this->assertCategoryExists($catSlug); - $this->assertCategoryAndExceptionExist($catSlug, $errorCode); - - return $this->exceptions[$catSlug][$errorCode](); - } - - public function getExceptionCategory(string $catSlug, bool $withCodes = false): ExceptionCategoryItem - { - $this->assertCategoryExists($catSlug); - - /** @var ExceptionCategoryItem $cat */ - $cat = $this->categories[$catSlug](); - - if ($withCodes) { - $cat->addCodes($this->getExceptionObjectsByCategory($catSlug)); - } - - return $cat; - } - public function buildException( string $catSlug, int $errorCode, @@ -170,4 +112,63 @@ public function buildException( $previous ); } + + public function getExceptionObject(string $catSlug, int $errorCode): BaseExceptionObject + { + $this->assertCategoryExists($catSlug); + $this->assertCategoryAndExceptionExist($catSlug, $errorCode); + + return $this->exceptions[$catSlug][$errorCode](); + } + + public function getExceptionCategory(string $catSlug, bool $withCodes = false): ExceptionCategoryItem + { + $this->assertCategoryExists($catSlug); + + /** @var ExceptionCategoryItem $cat */ + $cat = $this->categories[$catSlug](); + + if ($withCodes) { + $cat->addCodes($this->getExceptionObjectsByCategory($catSlug)); + } + + return $cat; + } + + protected function assertCategoryNotExists(string $catSlug): void + { + if (isset($this->categories[$catSlug])) { + throw new ExceptionCategoryAlreadyRegistered( + "Category $catSlug already registered." + ); + } + } + + protected function assertCategoryExists(string $catSlug): void + { + if (!isset($this->categories[$catSlug])) { + throw new UnknownExceptionCategory( + "Unknown exception category $catSlug." + ); + } + } + + protected function assertCategoryAndExceptionNotExist(string $catSlug, int $errorCode): void + { + if (isset($this->exceptions[$catSlug][$errorCode])) { + throw new ExceptionCodeAlreadyRegistered( + "Exception code [$errorCode] already " . + "registered for the [$catSlug] category." + ); + } + } + + protected function assertCategoryAndExceptionExist(string $catSlug, int $errorCode): void + { + if (!isset($this->exceptions[$catSlug][$errorCode])) { + throw new UnknownExceptionCode( + "Unknown exception code $errorCode for category $catSlug." + ); + } + } }