Validators.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Command;
  11. class Validators
  12. {
  13. /**
  14. * @static
  15. *
  16. * @param string $username
  17. *
  18. * @return mixed
  19. * @throws \InvalidArgumentException
  20. */
  21. static public function validateUsername($username)
  22. {
  23. if (is_null($username)) {
  24. throw new \InvalidArgumentException('The username must be set');
  25. }
  26. return $username;
  27. }
  28. /**
  29. * @static
  30. *
  31. * @param string $shortcut
  32. *
  33. * @return array
  34. * @throws \InvalidArgumentException
  35. */
  36. static public function validateEntityName($shortcut)
  37. {
  38. $entity = str_replace('/', '\\', $shortcut);
  39. if (false === $pos = strpos($entity, ':')) {
  40. throw new \InvalidArgumentException(sprintf('The entity name must contain a : ("%s" given, expecting something like AcmeBlogBundle:Post)', $entity));
  41. }
  42. return array(substr($entity, 0, $pos), substr($entity, $pos + 1));
  43. }
  44. }