SelectorsHandler.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /*
  3. * This file is part of the Mink package.
  4. * (c) Konstantin Kudryashov <ever.zet@gmail.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Behat\Mink\Selector;
  10. use Behat\Mink\Selector\Xpath\Escaper;
  11. /**
  12. * Selectors handler.
  13. *
  14. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  15. */
  16. class SelectorsHandler
  17. {
  18. private $selectors;
  19. private $escaper;
  20. /**
  21. * Initializes selectors handler.
  22. *
  23. * @param SelectorInterface[] $selectors default selectors to register
  24. */
  25. public function __construct(array $selectors = array())
  26. {
  27. $this->escaper = new Escaper();
  28. $this->registerSelector('named_partial', new PartialNamedSelector());
  29. $this->registerSelector('named_exact', new ExactNamedSelector());
  30. $this->registerSelector('css', new CssSelector());
  31. foreach ($selectors as $name => $selector) {
  32. $this->registerSelector($name, $selector);
  33. }
  34. }
  35. /**
  36. * Registers new selector engine with specified name.
  37. *
  38. * @param string $name selector engine name
  39. * @param SelectorInterface $selector selector engine instance
  40. */
  41. public function registerSelector($name, SelectorInterface $selector)
  42. {
  43. $this->selectors[$name] = $selector;
  44. }
  45. /**
  46. * Checks whether selector with specified name is registered on handler.
  47. *
  48. * @param string $name selector engine name
  49. *
  50. * @return Boolean
  51. */
  52. public function isSelectorRegistered($name)
  53. {
  54. return isset($this->selectors[$name]);
  55. }
  56. /**
  57. * Returns selector engine with specified name.
  58. *
  59. * @param string $name selector engine name
  60. *
  61. * @return SelectorInterface
  62. *
  63. * @throws \InvalidArgumentException
  64. */
  65. public function getSelector($name)
  66. {
  67. if ('named' === $name) {
  68. @trigger_error(
  69. 'Using the "named" selector directly from the handler is deprecated as of 1.6 and will be removed in 2.0.'
  70. .' Use the "named_partial" or use the "named" selector through the Element API instead.',
  71. E_USER_DEPRECATED
  72. );
  73. $name = 'named_partial';
  74. }
  75. if (!$this->isSelectorRegistered($name)) {
  76. throw new \InvalidArgumentException("Selector \"$name\" is not registered.");
  77. }
  78. return $this->selectors[$name];
  79. }
  80. /**
  81. * Translates selector with specified name to XPath.
  82. *
  83. * @param string $selector selector engine name (registered)
  84. * @param string|array $locator selector locator (an array or a string depending of the selector being used)
  85. *
  86. * @return string
  87. */
  88. public function selectorToXpath($selector, $locator)
  89. {
  90. if ('xpath' === $selector) {
  91. if (!is_string($locator)) {
  92. throw new \InvalidArgumentException('The xpath selector expects to get a string as locator');
  93. }
  94. return $locator;
  95. }
  96. return $this->getSelector($selector)->translateToXPath($locator);
  97. }
  98. /**
  99. * Translates string to XPath literal.
  100. *
  101. * @deprecated since Mink 1.7. Use \Behat\Mink\Selector\Xpath\Escaper::escapeLiteral when building Xpath
  102. * or pass the unescaped value when using the named selector.
  103. *
  104. * @param string $s
  105. *
  106. * @return string
  107. */
  108. public function xpathLiteral($s)
  109. {
  110. @trigger_error(
  111. 'The '.__METHOD__.' method is deprecated as of 1.7 and will be removed in 2.0.'
  112. .' Use \Behat\Mink\Selector\Xpath\Escaper::escapeLiteral instead when building Xpath'
  113. .' or pass the unescaped value when using the named selector.',
  114. E_USER_DEPRECATED
  115. );
  116. return $this->escaper->escapeLiteral($s);
  117. }
  118. }