customer41/multiexception

It's a simple package for managing multiple exceptions

Maintainers

Package info

github.com/customer41/multiexception

pkg:composer/customer41/multiexception

Statistics

Installs: 8

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.1 2025-05-20 16:48 UTC

This package is not auto-updated.

Last update: 2026-05-05 21:18:05 UTC


README

MultiException - это простой контейнер для ваших исключений, который сам по себе является исключением

Требования

  • php >= 8.0

Установка

composer require customer41/multiexception

Использование

<?php

class Article
{
    public string $title;
    public string $description;
    
    public function __construct(string $title, string $description)
    {
        $errors = new MultiException();
        
        $this->setTitle($title);
        $this->setDescription($description);
        
        if (count($errors) > 0) {
            throw $errors;
        }
    }
    
    private function setTitle(string $title): void
    {
        if (!empty($title)) {
            $this->title = $title;
        } else {
            $errors->add(new \Exception('Пустой заголовок'));
        }
    }
    
    private function setDescription(string $description): void
    {
        if (!empty($description)) {
            $this->description = $description;
        } else {
            $errors->add(new \Exception('Пустое описание'));
        }
    }
}

В контроллере:

class ArticleController
{
    public function createArticle(): void
    {
        try {
            $article = new Article($_POST['title'], $_POST['description']);
        } catch (MultiException $errors) {
            ...
        }
    }
}

В шаблоне:

<?php foreach ($errors as $error): ?>
    <div class="alert alert-danger">
        <?php echo $error->getMessage(); ?>
    </div>
<?php endforeach; ?>