NeedsTrait.php 1017 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace Aws\Crypto\Polyfill;
  3. use Aws\Exception\CryptoPolyfillException;
  4. /**
  5. * Trait NeedsTrait
  6. */
  7. trait NeedsTrait
  8. {
  9. /**
  10. * Preconditions, postconditions, and loop invariants are very
  11. * useful for safe programing. They also document the specifications.
  12. * This function is to help simplify the semantic burden of parsing
  13. * these constructions.
  14. *
  15. * Instead of constructions like
  16. * if (!(GOOD CONDITION)) {
  17. * throw new \Exception('condition not true');
  18. * }
  19. *
  20. * you can write:
  21. * needs(GOOD CONDITION, 'condition not true');
  22. * @param $condition
  23. * @param $errorMessage
  24. * @param null $exceptionClass
  25. */
  26. public static function needs($condition, $errorMessage, $exceptionClass = null)
  27. {
  28. if (!$condition) {
  29. if (!$exceptionClass) {
  30. $exceptionClass = CryptoPolyfillException::class;
  31. }
  32. throw new $exceptionClass($errorMessage);
  33. }
  34. }
  35. }