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