Validators.php 4.1 KB

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