NullGuardTrait.php 1006 B

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Stdlib\Guard;
  10. /**
  11. * Provide a guard method against null data
  12. */
  13. trait NullGuardTrait
  14. {
  15. /**
  16. * Verify that the data is not null
  17. *
  18. * @param mixed $data the data to verify
  19. * @param string $dataName the data name
  20. * @param string $exceptionClass FQCN for the exception
  21. * @throws \Exception
  22. */
  23. protected function guardAgainstNull(
  24. $data,
  25. $dataName = 'Argument',
  26. $exceptionClass = 'Zend\Stdlib\Exception\InvalidArgumentException'
  27. ) {
  28. if (null === $data) {
  29. $message = sprintf('%s cannot be null', $dataName);
  30. throw new $exceptionClass($message);
  31. }
  32. }
  33. }