Expression.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Component\Finder\Expression;
  11. /**
  12. * @author Jean-François Simon <contact@jfsimon.fr>
  13. */
  14. class Expression implements ValueInterface
  15. {
  16. const TYPE_REGEX = 1;
  17. const TYPE_GLOB = 2;
  18. /**
  19. * @var ValueInterface
  20. */
  21. private $value;
  22. /**
  23. * @param string $expr
  24. *
  25. * @return Expression
  26. */
  27. public static function create($expr)
  28. {
  29. return new self($expr);
  30. }
  31. /**
  32. * @param string $expr
  33. */
  34. public function __construct($expr)
  35. {
  36. try {
  37. $this->value = Regex::create($expr);
  38. } catch (\InvalidArgumentException $e) {
  39. $this->value = new Glob($expr);
  40. }
  41. }
  42. /**
  43. * @return string
  44. */
  45. public function __toString()
  46. {
  47. return $this->render();
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function render()
  53. {
  54. return $this->value->render();
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function renderPattern()
  60. {
  61. return $this->value->renderPattern();
  62. }
  63. /**
  64. * @return bool
  65. */
  66. public function isCaseSensitive()
  67. {
  68. return $this->value->isCaseSensitive();
  69. }
  70. /**
  71. * @return int
  72. */
  73. public function getType()
  74. {
  75. return $this->value->getType();
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function prepend($expr)
  81. {
  82. $this->value->prepend($expr);
  83. return $this;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function append($expr)
  89. {
  90. $this->value->append($expr);
  91. return $this;
  92. }
  93. /**
  94. * @return bool
  95. */
  96. public function isRegex()
  97. {
  98. return self::TYPE_REGEX === $this->value->getType();
  99. }
  100. /**
  101. * @return bool
  102. */
  103. public function isGlob()
  104. {
  105. return self::TYPE_GLOB === $this->value->getType();
  106. }
  107. /**
  108. * @throws \LogicException
  109. *
  110. * @return Glob
  111. */
  112. public function getGlob()
  113. {
  114. if (self::TYPE_GLOB !== $this->value->getType()) {
  115. throw new \LogicException('Regex cant be transformed to glob.');
  116. }
  117. return $this->value;
  118. }
  119. /**
  120. * @return Regex
  121. */
  122. public function getRegex()
  123. {
  124. return self::TYPE_REGEX === $this->value->getType() ? $this->value : $this->value->toRegex();
  125. }
  126. }