Validators.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /**
  12. * Class Validators
  13. *
  14. * @package Sonata\AdminBundle\Command
  15. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  16. */
  17. class Validators
  18. {
  19. /**
  20. * @static
  21. *
  22. * @param string $username
  23. *
  24. * @return mixed
  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. * @throws \InvalidArgumentException
  41. */
  42. public static function validateEntityName($shortcut)
  43. {
  44. $entity = str_replace('/', '\\', $shortcut);
  45. if (false === $pos = strpos($entity, ':')) {
  46. throw new \InvalidArgumentException(sprintf('The entity name must contain a : ("%s" given, expecting something like AcmeBlogBundle:Post)', $entity));
  47. }
  48. return array(substr($entity, 0, $pos), substr($entity, $pos + 1));
  49. }
  50. /**
  51. * @static
  52. *
  53. * @param string $class
  54. *
  55. * @return string
  56. * @throws \InvalidArgumentException
  57. */
  58. public static function validateClass($class)
  59. {
  60. $class = str_replace('/', '\\', $class);
  61. if (!class_exists($class)) {
  62. throw new \InvalidArgumentException(sprintf('The class "%s" does not exist.', $class));
  63. }
  64. return $class;
  65. }
  66. /**
  67. * @static
  68. *
  69. * @param string $adminClassBasename
  70. *
  71. * @return string
  72. * @throws \InvalidArgumentException
  73. */
  74. public static function validateAdminClassBasename($adminClassBasename)
  75. {
  76. $adminClassBasename = str_replace('/', '\\', $adminClassBasename);
  77. if (false !== strpos($adminClassBasename, ':')) {
  78. throw new \InvalidArgumentException(sprintf('The admin class name must not contain a : ("%s" given, expecting something like PostAdmin")', $adminClassBasename));
  79. }
  80. return $adminClassBasename;
  81. }
  82. /**
  83. * @static
  84. *
  85. * @param string $controllerClassBasename
  86. *
  87. * @return string
  88. * @throws \InvalidArgumentException
  89. */
  90. public static function validateControllerClassBasename($controllerClassBasename)
  91. {
  92. $controllerClassBasename = str_replace('/', '\\', $controllerClassBasename);
  93. if (false !== strpos($controllerClassBasename, ':')) {
  94. throw new \InvalidArgumentException(sprintf('The controller class name must not contain a : ("%s" given, expecting something like PostAdminController")', $controllerClassBasename));
  95. }
  96. if (substr($controllerClassBasename, -10) != 'Controller') {
  97. throw new \InvalidArgumentException('The controller class name must end with Controller.');
  98. }
  99. return $controllerClassBasename;
  100. }
  101. /**
  102. * @static
  103. *
  104. * @param string $servicesFile
  105. *
  106. * @return string
  107. */
  108. public static function validateServicesFile($servicesFile)
  109. {
  110. return trim($servicesFile, '/');
  111. }
  112. /**
  113. * @static
  114. *
  115. * @param string $serviceId
  116. *
  117. * @return string
  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. }