1<?php
2/**
3* PhpCss provides several integrative functions to use this library
4*
5* @license http://www.opensource.org/licenses/mit-license.php The MIT License
6* @copyright Copyright 2010-2014 PhpCss Team
7*/
8
9use PhpCss\Exception\ParserException;
10
11/**
12* PhpCss provides several integrative functions to use this library
13*/
14abstract class PhpCss {
15
16  /**
17   * Parses a css selector and compiles it into an css selector again
18   *
19   * @param string $cssSelector
20   * @return string
21   * @throws ParserException
22   */
23  public static function reformat(string $cssSelector): string {
24    $ast = self::getAst($cssSelector);
25    $visitor = new PhpCss\Ast\Visitor\Css();
26    $ast->accept($visitor);
27    return (string)$visitor;
28  }
29
30  /**
31   * Parses a css selector and transforms it into an xpath expression
32   *
33   * @param string $cssSelector
34   * @param int $options
35   * @return string
36   * @throws ParserException
37   */
38  public static function toXpath(string $cssSelector, int $options = 0): string {
39    $ast = self::getAst($cssSelector);
40    $visitor = new PhpCss\Ast\Visitor\Xpath($options);
41    $ast->accept($visitor);
42    return (string)$visitor;
43  }
44
45  /**
46   * Parses a css selector and returns the AST
47   *
48   * @param string $cssSelector
49   * @return PhpCss\Ast\Node
50   * @throws ParserException
51   */
52  public static function getAst(string $cssSelector): PhpCss\Ast\Node {
53    $tokens = array();
54    $scanner = new PhpCss\Scanner(new PhpCss\Scanner\Status\Selector());
55    $scanner->scan($tokens, $cssSelector);
56    $parser = new PhpCss\Parser\Standard($tokens);
57    return $parser->parse();
58  }
59}
60