s-p-ko/multiexception

包含实现多异常概念类的类

1.0.0 2019-04-08 21:48 UTC

This package is auto-updated.

Last update: 2024-09-15 00:45:15 UTC


README

包含实现多异常概念类的类

基本用法

这是使用此类的最简单方法

<?php

require __DIR__ . '/vendor/autoload.php';

use Spko\MultiException;

function checkPassword(string $password) {
    $errors = new MultiException();
    if (strlen($password) < 6) {
        $errors->add(new \Exception('Less than 6'));
    }
    if (false === strpos($password, '!')) {
        $errors->add(new \Exception('Without \'!\''));
    }
    if (!$errors->empty()) {
        throw $errors;
    }
    return true;
}

try {
    $result = checkPassword('123');
} catch (MultiException $exceptions) {
    foreach ($exceptions->all() as $e) {
        echo $e->getMessage() . "\n";
    }
}

结果

Less than 6

Without '!'