Validators.php 4.1 KB

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