NeedsTrait.php 1.0 KB

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