Validators.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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('The entity name must contain a : ("%s" given, expecting something like AcmeBlogBundle:Post)', $entity));
  48. }
  49. return array(substr($entity, 0, $pos), substr($entity, $pos + 1));
  50. }
  51. /**
  52. * @static
  53. *
  54. * @param string $class
  55. *
  56. * @return string
  57. *
  58. * @throws \InvalidArgumentException
  59. */
  60. public static function validateClass($class)
  61. {
  62. $class = str_replace('/', '\\', $class);
  63. if (!class_exists($class)) {
  64. throw new \InvalidArgumentException(sprintf('The class "%s" does not exist.', $class));
  65. }
  66. return $class;
  67. }
  68. /**
  69. * @static
  70. *
  71. * @param string $adminClassBasename
  72. *
  73. * @return string
  74. *
  75. * @throws \InvalidArgumentException
  76. */
  77. public static function validateAdminClassBasename($adminClassBasename)
  78. {
  79. $adminClassBasename = str_replace('/', '\\', $adminClassBasename);
  80. if (false !== strpos($adminClassBasename, ':')) {
  81. throw new \InvalidArgumentException(sprintf('The admin class name must not contain a : ("%s" given, expecting something like PostAdmin")', $adminClassBasename));
  82. }
  83. return $adminClassBasename;
  84. }
  85. /**
  86. * @static
  87. *
  88. * @param string $controllerClassBasename
  89. *
  90. * @return string
  91. *
  92. * @throws \InvalidArgumentException
  93. */
  94. public static function validateControllerClassBasename($controllerClassBasename)
  95. {
  96. $controllerClassBasename = str_replace('/', '\\', $controllerClassBasename);
  97. if (false !== strpos($controllerClassBasename, ':')) {
  98. throw new \InvalidArgumentException(sprintf('The controller class name must not contain a : ("%s" given, expecting something like PostAdminController")', $controllerClassBasename));
  99. }
  100. if (substr($controllerClassBasename, -10) != 'Controller') {
  101. throw new \InvalidArgumentException('The controller class name must end with Controller.');
  102. }
  103. return $controllerClassBasename;
  104. }
  105. /**
  106. * @static
  107. *
  108. * @param string $servicesFile
  109. *
  110. * @return string
  111. */
  112. public static function validateServicesFile($servicesFile)
  113. {
  114. return trim($servicesFile, '/');
  115. }
  116. /**
  117. * @static
  118. *
  119. * @param string $serviceId
  120. *
  121. * @return string
  122. *
  123. * @throws \InvalidArgumentException
  124. */
  125. public static function validateServiceId($serviceId)
  126. {
  127. if (preg_match('/[^A-Za-z\._0-9]/', $serviceId, $matches)) {
  128. throw new \InvalidArgumentException(sprintf(
  129. 'Service ID "%s" contains invalid character "%s".',
  130. $serviceId,
  131. $matches[0]
  132. ));
  133. }
  134. return $serviceId;
  135. }
  136. }