Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Behat\Mink\Selector; Chris@0: Chris@0: use Behat\Mink\Selector\Xpath\Escaper; Chris@0: Chris@0: /** Chris@0: * Selectors handler. Chris@0: * Chris@0: * @author Konstantin Kudryashov Chris@0: */ Chris@0: class SelectorsHandler Chris@0: { Chris@0: private $selectors; Chris@0: private $escaper; Chris@0: Chris@0: /** Chris@0: * Initializes selectors handler. Chris@0: * Chris@0: * @param SelectorInterface[] $selectors default selectors to register Chris@0: */ Chris@0: public function __construct(array $selectors = array()) Chris@0: { Chris@0: $this->escaper = new Escaper(); Chris@0: Chris@0: $this->registerSelector('named_partial', new PartialNamedSelector()); Chris@0: $this->registerSelector('named_exact', new ExactNamedSelector()); Chris@0: $this->registerSelector('css', new CssSelector()); Chris@0: Chris@0: foreach ($selectors as $name => $selector) { Chris@0: $this->registerSelector($name, $selector); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Registers new selector engine with specified name. Chris@0: * Chris@0: * @param string $name selector engine name Chris@0: * @param SelectorInterface $selector selector engine instance Chris@0: */ Chris@0: public function registerSelector($name, SelectorInterface $selector) Chris@0: { Chris@0: $this->selectors[$name] = $selector; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether selector with specified name is registered on handler. Chris@0: * Chris@0: * @param string $name selector engine name Chris@0: * Chris@0: * @return Boolean Chris@0: */ Chris@0: public function isSelectorRegistered($name) Chris@0: { Chris@0: return isset($this->selectors[$name]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns selector engine with specified name. Chris@0: * Chris@0: * @param string $name selector engine name Chris@0: * Chris@0: * @return SelectorInterface Chris@0: * Chris@0: * @throws \InvalidArgumentException Chris@0: */ Chris@0: public function getSelector($name) Chris@0: { Chris@0: if ('named' === $name) { Chris@0: @trigger_error( Chris@0: 'Using the "named" selector directly from the handler is deprecated as of 1.6 and will be removed in 2.0.' Chris@0: .' Use the "named_partial" or use the "named" selector through the Element API instead.', Chris@0: E_USER_DEPRECATED Chris@0: ); Chris@0: $name = 'named_partial'; Chris@0: } Chris@0: Chris@0: if (!$this->isSelectorRegistered($name)) { Chris@0: throw new \InvalidArgumentException("Selector \"$name\" is not registered."); Chris@0: } Chris@0: Chris@0: return $this->selectors[$name]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Translates selector with specified name to XPath. Chris@0: * Chris@0: * @param string $selector selector engine name (registered) Chris@0: * @param string|array $locator selector locator (an array or a string depending of the selector being used) Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function selectorToXpath($selector, $locator) Chris@0: { Chris@0: if ('xpath' === $selector) { Chris@0: if (!is_string($locator)) { Chris@0: throw new \InvalidArgumentException('The xpath selector expects to get a string as locator'); Chris@0: } Chris@0: Chris@0: return $locator; Chris@0: } Chris@0: Chris@0: return $this->getSelector($selector)->translateToXPath($locator); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Translates string to XPath literal. Chris@0: * Chris@0: * @deprecated since Mink 1.7. Use \Behat\Mink\Selector\Xpath\Escaper::escapeLiteral when building Xpath Chris@0: * or pass the unescaped value when using the named selector. Chris@0: * Chris@0: * @param string $s Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function xpathLiteral($s) Chris@0: { Chris@0: @trigger_error( Chris@0: 'The '.__METHOD__.' method is deprecated as of 1.7 and will be removed in 2.0.' Chris@0: .' Use \Behat\Mink\Selector\Xpath\Escaper::escapeLiteral instead when building Xpath' Chris@0: .' or pass the unescaped value when using the named selector.', Chris@0: E_USER_DEPRECATED Chris@0: ); Chris@0: Chris@0: return $this->escaper->escapeLiteral($s); Chris@0: } Chris@0: }