CssSelector.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /*
  3. * This file is part of the Mink package.
  4. * (c) Konstantin Kudryashov <ever.zet@gmail.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Behat\Mink\Selector;
  10. use Symfony\Component\CssSelector\CssSelector as CSS;
  11. use Symfony\Component\CssSelector\CssSelectorConverter;
  12. /**
  13. * CSS selector engine. Transforms CSS to XPath.
  14. *
  15. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  16. */
  17. class CssSelector implements SelectorInterface
  18. {
  19. /**
  20. * Translates CSS into XPath.
  21. *
  22. * @param string|array $locator current selector locator
  23. *
  24. * @return string
  25. */
  26. public function translateToXPath($locator)
  27. {
  28. if (!is_string($locator)) {
  29. throw new \InvalidArgumentException('The CssSelector expects to get a string as locator');
  30. }
  31. // Symfony 2.8+ API
  32. if (class_exists('Symfony\Component\CssSelector\CssSelectorConverter')) {
  33. $converter = new CssSelectorConverter();
  34. return $converter->toXPath($locator);
  35. }
  36. // old static API for Symfony 2.7 and older
  37. return CSS::toXPath($locator);
  38. }
  39. }