ElementNode.php 1.7 KB

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