Validators.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. public static 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. public static 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. /**
  45. * @static
  46. *
  47. * @param string $controllerClassName
  48. *
  49. * @return string
  50. * @throws \InvalidArgumentException
  51. */
  52. public static function validateControllerClassName($controllerClassName)
  53. {
  54. $controllerClassName = str_replace('/', '\\', $controllerClassName);
  55. if (false !== strpos($controllerClassName, ':')) {
  56. throw new \InvalidArgumentException(sprintf('The controller class name must not contain a : ("%s" given, expecting something like PostAdminController")', $controllerClassName));
  57. }
  58. if (substr($controllerClassName, -10) != 'Controller') {
  59. throw new \InvalidArgumentException('The controller class name must end with Controller.');
  60. }
  61. return $controllerClassName;
  62. }
  63. /**
  64. * @static
  65. *
  66. * @param string $servicesFile
  67. *
  68. * @return string
  69. */
  70. public static function validateServicesFile($servicesFile)
  71. {
  72. return trim($servicesFile, '/');
  73. }
  74. /**
  75. * @static
  76. *
  77. * @param string $serviceId
  78. *
  79. * @return string
  80. * @throws \InvalidArgumentException
  81. */
  82. public static function validateServiceId($serviceId)
  83. {
  84. if (preg_match('/[^A-z\._0-9]/', $serviceId, $matches)) {
  85. throw new \InvalidArgumentException(sprintf(
  86. 'Service ID "%s" contains invalid character "%s".',
  87. $serviceId,
  88. $matches[0]
  89. ));
  90. }
  91. return $serviceId;
  92. }
  93. }