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