Validators.php 3.7 KB

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