NullGuardTrait.php 831 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. declare(strict_types=1);
  3. namespace Laminas\Stdlib\Guard;
  4. use Exception;
  5. use Laminas\Stdlib\Exception\InvalidArgumentException;
  6. use function sprintf;
  7. /**
  8. * Provide a guard method against null data
  9. */
  10. trait NullGuardTrait
  11. {
  12. /**
  13. * Verify that the data is not null
  14. *
  15. * @param mixed $data the data to verify
  16. * @param string $dataName the data name
  17. * @param string $exceptionClass FQCN for the exception
  18. * @return void
  19. * @throws Exception
  20. */
  21. protected function guardAgainstNull(
  22. mixed $data,
  23. $dataName = 'Argument',
  24. $exceptionClass = InvalidArgumentException::class
  25. ) {
  26. if (null === $data) {
  27. $message = sprintf('%s cannot be null', $dataName);
  28. throw new $exceptionClass($message);
  29. }
  30. }
  31. }