Last active
August 11, 2022 08:18
-
-
Save nasrulhazim/aa11c5347e44801a28081d9fc14c334f to your computer and use it in GitHub Desktop.
Custom Exception method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Exceptions; | |
use Exception; | |
class ContractException extends Exception | |
{ | |
public static function throwIf(bool $condition, string $method, ...$args) | |
{ | |
if (! $condition) { | |
if (method_exists(__CLASS__, $method)) { | |
throw self::$method(...$args); | |
} | |
throw new self("Invalid exception method $method"); | |
} | |
} | |
public static function missingContract(string $class, string $contract) | |
{ | |
return new self("$class did not implements $contract"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment