Validators.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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. *
  20. * @throws \InvalidArgumentException
  21. */
  22. public static function validateUsername($username)
  23. {
  24. if (is_null($username)) {
  25. throw new \InvalidArgumentException('The username must be set');
  26. }
  27. return $username;
  28. }
  29. /**
  30. * @static
  31. *
  32. * @param string $shortcut
  33. *
  34. * @return array
  35. *
  36. * @throws \InvalidArgumentException
  37. */
  38. public static function validateEntityName($shortcut)
  39. {
  40. $entity = str_replace('/', '\\', $shortcut);
  41. if (false === $pos = strpos($entity, ':')) {
  42. throw new \InvalidArgumentException(sprintf('The entity name must contain a : ("%s" given, expecting something like AcmeBlogBundle:Post)', $entity));
  43. }
  44. return array(substr($entity, 0, $pos), substr($entity, $pos + 1));
  45. }
  46. /**
  47. * @static
  48. *
  49. * @param string $class
  50. *
  51. * @return string
  52. *
  53. * @throws \InvalidArgumentException
  54. */
  55. public static function validateClass($class)
  56. {
  57. $class = str_replace('/', '\\', $class);
  58. if (!class_exists($class)) {
  59. throw new \InvalidArgumentException(sprintf('The class "%s" does not exist.', $class));
  60. }
  61. return $class;
  62. }
  63. /**
  64. * @static
  65. *
  66. * @param string $adminClassBasename
  67. *
  68. * @return string
  69. *
  70. * @throws \InvalidArgumentException
  71. */
  72. public static function validateAdminClassBasename($adminClassBasename)
  73. {
  74. $adminClassBasename = str_replace('/', '\\', $adminClassBasename);
  75. if (false !== strpos($adminClassBasename, ':')) {
  76. throw new \InvalidArgumentException(sprintf('The admin class name must not contain a : ("%s" given, expecting something like PostAdmin")', $adminClassBasename));
  77. }
  78. return $adminClassBasename;
  79. }
  80. /**
  81. * @static
  82. *
  83. * @param string $controllerClassBasename
  84. *
  85. * @return string
  86. *
  87. * @throws \InvalidArgumentException
  88. */
  89. public static function validateControllerClassBasename($controllerClassBasename)
  90. {
  91. $controllerClassBasename = str_replace('/', '\\', $controllerClassBasename);
  92. if (false !== strpos($controllerClassBasename, ':')) {
  93. throw new \InvalidArgumentException(sprintf('The controller class name must not contain a : ("%s" given, expecting something like PostAdminController")', $controllerClassBasename));
  94. }
  95. if (substr($controllerClassBasename, -10) != 'Controller') {
  96. throw new \InvalidArgumentException('The controller class name must end with Controller.');
  97. }
  98. return $controllerClassBasename;
  99. }
  100. /**
  101. * @static
  102. *
  103. * @param string $servicesFile
  104. *
  105. * @return string
  106. */
  107. public static function validateServicesFile($servicesFile)
  108. {
  109. return trim($servicesFile, '/');
  110. }
  111. /**
  112. * @static
  113. *
  114. * @param string $serviceId
  115. *
  116. * @return string
  117. *
  118. * @throws \InvalidArgumentException
  119. */
  120. public static function validateServiceId($serviceId)
  121. {
  122. if (preg_match('/[^A-Za-z\._0-9]/', $serviceId, $matches)) {
  123. throw new \InvalidArgumentException(sprintf(
  124. 'Service ID "%s" contains invalid character "%s".',
  125. $serviceId,
  126. $matches[0]
  127. ));
  128. }
  129. return $serviceId;
  130. }
  131. }