|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Component\CssSelector\Node; |
|
13 |
|
14 use Symfony\Component\CssSelector\XPathExpr; |
|
15 |
|
16 /** |
|
17 * ElementNode represents a "namespace|element" node. |
|
18 * |
|
19 * This component is a port of the Python lxml library, |
|
20 * which is copyright Infrae and distributed under the BSD license. |
|
21 * |
|
22 * @author Fabien Potencier <fabien@symfony.com> |
|
23 */ |
|
24 class ElementNode implements NodeInterface |
|
25 { |
|
26 protected $namespace; |
|
27 protected $element; |
|
28 |
|
29 /** |
|
30 * Constructor. |
|
31 * |
|
32 * @param string $namespace Namespace |
|
33 * @param string $element Element |
|
34 */ |
|
35 public function __construct($namespace, $element) |
|
36 { |
|
37 $this->namespace = $namespace; |
|
38 $this->element = $element; |
|
39 } |
|
40 |
|
41 /** |
|
42 * {@inheritDoc} |
|
43 */ |
|
44 public function __toString() |
|
45 { |
|
46 return sprintf('%s[%s]', __CLASS__, $this->formatElement()); |
|
47 } |
|
48 |
|
49 /** |
|
50 * Formats the element into a string. |
|
51 * |
|
52 * @return string Element as an XPath string |
|
53 */ |
|
54 public function formatElement() |
|
55 { |
|
56 if ($this->namespace == '*') { |
|
57 return $this->element; |
|
58 } |
|
59 |
|
60 return sprintf('%s|%s', $this->namespace, $this->element); |
|
61 } |
|
62 |
|
63 /** |
|
64 * {@inheritDoc} |
|
65 */ |
|
66 public function toXpath() |
|
67 { |
|
68 if ($this->namespace == '*') { |
|
69 $el = strtolower($this->element); |
|
70 } else { |
|
71 // FIXME: Should we lowercase here? |
|
72 $el = sprintf('%s:%s', $this->namespace, $this->element); |
|
73 } |
|
74 |
|
75 return new XPathExpr(null, null, $el); |
|
76 } |
|
77 } |