You are here

class Twig_Extension_Core in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/twig/twig/lib/Twig/Extension/Core.php \Twig_Extension_Core

Hierarchy

Expanded class hierarchy of Twig_Extension_Core

File

vendor/twig/twig/lib/Twig/Extension/Core.php, line 16

View source
class Twig_Extension_Core extends Twig_Extension {
  protected $dateFormats = array(
    'F j, Y H:i',
    '%d days',
  );
  protected $numberFormat = array(
    0,
    '.',
    ',',
  );
  protected $timezone = null;
  protected $escapers = array();

  /**
   * Defines a new escaper to be used via the escape filter.
   *
   * @param string   $strategy The strategy name that should be used as a strategy in the escape call
   * @param callable $callable A valid PHP callable
   */
  public function setEscaper($strategy, $callable) {
    $this->escapers[$strategy] = $callable;
  }

  /**
   * Gets all defined escapers.
   *
   * @return array An array of escapers
   */
  public function getEscapers() {
    return $this->escapers;
  }

  /**
   * Sets the default format to be used by the date filter.
   *
   * @param string $format             The default date format string
   * @param string $dateIntervalFormat The default date interval format string
   */
  public function setDateFormat($format = null, $dateIntervalFormat = null) {
    if (null !== $format) {
      $this->dateFormats[0] = $format;
    }
    if (null !== $dateIntervalFormat) {
      $this->dateFormats[1] = $dateIntervalFormat;
    }
  }

  /**
   * Gets the default format to be used by the date filter.
   *
   * @return array The default date format string and the default date interval format string
   */
  public function getDateFormat() {
    return $this->dateFormats;
  }

  /**
   * Sets the default timezone to be used by the date filter.
   *
   * @param DateTimeZone|string $timezone The default timezone string or a DateTimeZone object
   */
  public function setTimezone($timezone) {
    $this->timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone);
  }

  /**
   * Gets the default timezone to be used by the date filter.
   *
   * @return DateTimeZone The default timezone currently in use
   */
  public function getTimezone() {
    if (null === $this->timezone) {
      $this->timezone = new DateTimeZone(date_default_timezone_get());
    }
    return $this->timezone;
  }

  /**
   * Sets the default format to be used by the number_format filter.
   *
   * @param int    $decimal      The number of decimal places to use.
   * @param string $decimalPoint The character(s) to use for the decimal point.
   * @param string $thousandSep  The character(s) to use for the thousands separator.
   */
  public function setNumberFormat($decimal, $decimalPoint, $thousandSep) {
    $this->numberFormat = array(
      $decimal,
      $decimalPoint,
      $thousandSep,
    );
  }

  /**
   * Get the default format used by the number_format filter.
   *
   * @return array The arguments for number_format()
   */
  public function getNumberFormat() {
    return $this->numberFormat;
  }
  public function getTokenParsers() {
    return array(
      new Twig_TokenParser_For(),
      new Twig_TokenParser_If(),
      new Twig_TokenParser_Extends(),
      new Twig_TokenParser_Include(),
      new Twig_TokenParser_Block(),
      new Twig_TokenParser_Use(),
      new Twig_TokenParser_Filter(),
      new Twig_TokenParser_Macro(),
      new Twig_TokenParser_Import(),
      new Twig_TokenParser_From(),
      new Twig_TokenParser_Set(),
      new Twig_TokenParser_Spaceless(),
      new Twig_TokenParser_Flush(),
      new Twig_TokenParser_Do(),
      new Twig_TokenParser_Embed(),
    );
  }
  public function getFilters() {
    $filters = array(
      // formatting filters
      new Twig_SimpleFilter('date', 'twig_date_format_filter', array(
        'needs_environment' => true,
      )),
      new Twig_SimpleFilter('date_modify', 'twig_date_modify_filter', array(
        'needs_environment' => true,
      )),
      new Twig_SimpleFilter('format', 'sprintf'),
      new Twig_SimpleFilter('replace', 'twig_replace_filter'),
      new Twig_SimpleFilter('number_format', 'twig_number_format_filter', array(
        'needs_environment' => true,
      )),
      new Twig_SimpleFilter('abs', 'abs'),
      new Twig_SimpleFilter('round', 'twig_round'),
      // encoding
      new Twig_SimpleFilter('url_encode', 'twig_urlencode_filter'),
      new Twig_SimpleFilter('json_encode', 'twig_jsonencode_filter'),
      new Twig_SimpleFilter('convert_encoding', 'twig_convert_encoding'),
      // string filters
      new Twig_SimpleFilter('title', 'twig_title_string_filter', array(
        'needs_environment' => true,
      )),
      new Twig_SimpleFilter('capitalize', 'twig_capitalize_string_filter', array(
        'needs_environment' => true,
      )),
      new Twig_SimpleFilter('upper', 'strtoupper'),
      new Twig_SimpleFilter('lower', 'strtolower'),
      new Twig_SimpleFilter('striptags', 'strip_tags'),
      new Twig_SimpleFilter('trim', 'trim'),
      new Twig_SimpleFilter('nl2br', 'nl2br', array(
        'pre_escape' => 'html',
        'is_safe' => array(
          'html',
        ),
      )),
      // array helpers
      new Twig_SimpleFilter('join', 'twig_join_filter'),
      new Twig_SimpleFilter('split', 'twig_split_filter', array(
        'needs_environment' => true,
      )),
      new Twig_SimpleFilter('sort', 'twig_sort_filter'),
      new Twig_SimpleFilter('merge', 'twig_array_merge'),
      new Twig_SimpleFilter('batch', 'twig_array_batch'),
      // string/array filters
      new Twig_SimpleFilter('reverse', 'twig_reverse_filter', array(
        'needs_environment' => true,
      )),
      new Twig_SimpleFilter('length', 'twig_length_filter', array(
        'needs_environment' => true,
      )),
      new Twig_SimpleFilter('slice', 'twig_slice', array(
        'needs_environment' => true,
      )),
      new Twig_SimpleFilter('first', 'twig_first', array(
        'needs_environment' => true,
      )),
      new Twig_SimpleFilter('last', 'twig_last', array(
        'needs_environment' => true,
      )),
      // iteration and runtime
      new Twig_SimpleFilter('default', '_twig_default_filter', array(
        'node_class' => 'Twig_Node_Expression_Filter_Default',
      )),
      new Twig_SimpleFilter('keys', 'twig_get_array_keys_filter'),
      // escaping
      new Twig_SimpleFilter('escape', 'twig_escape_filter', array(
        'needs_environment' => true,
        'is_safe_callback' => 'twig_escape_filter_is_safe',
      )),
      new Twig_SimpleFilter('e', 'twig_escape_filter', array(
        'needs_environment' => true,
        'is_safe_callback' => 'twig_escape_filter_is_safe',
      )),
    );
    if (function_exists('mb_get_info')) {
      $filters[] = new Twig_SimpleFilter('upper', 'twig_upper_filter', array(
        'needs_environment' => true,
      ));
      $filters[] = new Twig_SimpleFilter('lower', 'twig_lower_filter', array(
        'needs_environment' => true,
      ));
    }
    return $filters;
  }
  public function getFunctions() {
    return array(
      new Twig_SimpleFunction('max', 'max'),
      new Twig_SimpleFunction('min', 'min'),
      new Twig_SimpleFunction('range', 'range'),
      new Twig_SimpleFunction('constant', 'twig_constant'),
      new Twig_SimpleFunction('cycle', 'twig_cycle'),
      new Twig_SimpleFunction('random', 'twig_random', array(
        'needs_environment' => true,
      )),
      new Twig_SimpleFunction('date', 'twig_date_converter', array(
        'needs_environment' => true,
      )),
      new Twig_SimpleFunction('include', 'twig_include', array(
        'needs_environment' => true,
        'needs_context' => true,
        'is_safe' => array(
          'all',
        ),
      )),
      new Twig_SimpleFunction('source', 'twig_source', array(
        'needs_environment' => true,
        'is_safe' => array(
          'all',
        ),
      )),
    );
  }
  public function getTests() {
    return array(
      new Twig_SimpleTest('even', null, array(
        'node_class' => 'Twig_Node_Expression_Test_Even',
      )),
      new Twig_SimpleTest('odd', null, array(
        'node_class' => 'Twig_Node_Expression_Test_Odd',
      )),
      new Twig_SimpleTest('defined', null, array(
        'node_class' => 'Twig_Node_Expression_Test_Defined',
      )),
      new Twig_SimpleTest('sameas', null, array(
        'node_class' => 'Twig_Node_Expression_Test_Sameas',
        'deprecated' => true,
        'alternative' => 'same as',
      )),
      new Twig_SimpleTest('same as', null, array(
        'node_class' => 'Twig_Node_Expression_Test_Sameas',
      )),
      new Twig_SimpleTest('none', null, array(
        'node_class' => 'Twig_Node_Expression_Test_Null',
      )),
      new Twig_SimpleTest('null', null, array(
        'node_class' => 'Twig_Node_Expression_Test_Null',
      )),
      new Twig_SimpleTest('divisibleby', null, array(
        'node_class' => 'Twig_Node_Expression_Test_Divisibleby',
        'deprecated' => true,
        'alternative' => 'divisible by',
      )),
      new Twig_SimpleTest('divisible by', null, array(
        'node_class' => 'Twig_Node_Expression_Test_Divisibleby',
      )),
      new Twig_SimpleTest('constant', null, array(
        'node_class' => 'Twig_Node_Expression_Test_Constant',
      )),
      new Twig_SimpleTest('empty', 'twig_test_empty'),
      new Twig_SimpleTest('iterable', 'twig_test_iterable'),
    );
  }
  public function getOperators() {
    return array(
      array(
        'not' => array(
          'precedence' => 50,
          'class' => 'Twig_Node_Expression_Unary_Not',
        ),
        '-' => array(
          'precedence' => 500,
          'class' => 'Twig_Node_Expression_Unary_Neg',
        ),
        '+' => array(
          'precedence' => 500,
          'class' => 'Twig_Node_Expression_Unary_Pos',
        ),
      ),
      array(
        'or' => array(
          'precedence' => 10,
          'class' => 'Twig_Node_Expression_Binary_Or',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        'and' => array(
          'precedence' => 15,
          'class' => 'Twig_Node_Expression_Binary_And',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        'b-or' => array(
          'precedence' => 16,
          'class' => 'Twig_Node_Expression_Binary_BitwiseOr',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        'b-xor' => array(
          'precedence' => 17,
          'class' => 'Twig_Node_Expression_Binary_BitwiseXor',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        'b-and' => array(
          'precedence' => 18,
          'class' => 'Twig_Node_Expression_Binary_BitwiseAnd',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        '==' => array(
          'precedence' => 20,
          'class' => 'Twig_Node_Expression_Binary_Equal',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        '!=' => array(
          'precedence' => 20,
          'class' => 'Twig_Node_Expression_Binary_NotEqual',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        '<' => array(
          'precedence' => 20,
          'class' => 'Twig_Node_Expression_Binary_Less',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        '>' => array(
          'precedence' => 20,
          'class' => 'Twig_Node_Expression_Binary_Greater',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        '>=' => array(
          'precedence' => 20,
          'class' => 'Twig_Node_Expression_Binary_GreaterEqual',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        '<=' => array(
          'precedence' => 20,
          'class' => 'Twig_Node_Expression_Binary_LessEqual',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        'not in' => array(
          'precedence' => 20,
          'class' => 'Twig_Node_Expression_Binary_NotIn',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        'in' => array(
          'precedence' => 20,
          'class' => 'Twig_Node_Expression_Binary_In',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        'matches' => array(
          'precedence' => 20,
          'class' => 'Twig_Node_Expression_Binary_Matches',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        'starts with' => array(
          'precedence' => 20,
          'class' => 'Twig_Node_Expression_Binary_StartsWith',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        'ends with' => array(
          'precedence' => 20,
          'class' => 'Twig_Node_Expression_Binary_EndsWith',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        '..' => array(
          'precedence' => 25,
          'class' => 'Twig_Node_Expression_Binary_Range',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        '+' => array(
          'precedence' => 30,
          'class' => 'Twig_Node_Expression_Binary_Add',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        '-' => array(
          'precedence' => 30,
          'class' => 'Twig_Node_Expression_Binary_Sub',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        '~' => array(
          'precedence' => 40,
          'class' => 'Twig_Node_Expression_Binary_Concat',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        '*' => array(
          'precedence' => 60,
          'class' => 'Twig_Node_Expression_Binary_Mul',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        '/' => array(
          'precedence' => 60,
          'class' => 'Twig_Node_Expression_Binary_Div',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        '//' => array(
          'precedence' => 60,
          'class' => 'Twig_Node_Expression_Binary_FloorDiv',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        '%' => array(
          'precedence' => 60,
          'class' => 'Twig_Node_Expression_Binary_Mod',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        'is' => array(
          'precedence' => 100,
          'callable' => array(
            $this,
            'parseTestExpression',
          ),
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        'is not' => array(
          'precedence' => 100,
          'callable' => array(
            $this,
            'parseNotTestExpression',
          ),
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        '**' => array(
          'precedence' => 200,
          'class' => 'Twig_Node_Expression_Binary_Power',
          'associativity' => Twig_ExpressionParser::OPERATOR_RIGHT,
        ),
      ),
    );
  }
  public function parseNotTestExpression(Twig_Parser $parser, Twig_NodeInterface $node) {
    return new Twig_Node_Expression_Unary_Not($this
      ->parseTestExpression($parser, $node), $parser
      ->getCurrentToken()
      ->getLine());
  }
  public function parseTestExpression(Twig_Parser $parser, Twig_NodeInterface $node) {
    $stream = $parser
      ->getStream();
    list($name, $test) = $this
      ->getTest($parser, $node
      ->getLine());
    if ($test instanceof Twig_SimpleTest && $test
      ->isDeprecated()) {
      $message = sprintf('Twig Test "%s" is deprecated', $name);
      if ($test
        ->getAlternative()) {
        $message .= sprintf('. Use "%s" instead', $test
          ->getAlternative());
      }
      $message .= sprintf(' in %s at line %d.', $stream
        ->getFilename(), $stream
        ->getCurrent()
        ->getLine());
      @trigger_error($message, E_USER_DEPRECATED);
    }
    $class = $this
      ->getTestNodeClass($parser, $test);
    $arguments = null;
    if ($stream
      ->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
      $arguments = $parser
        ->getExpressionParser()
        ->parseArguments(true);
    }
    return new $class($node, $name, $arguments, $parser
      ->getCurrentToken()
      ->getLine());
  }
  protected function getTest(Twig_Parser $parser, $line) {
    $stream = $parser
      ->getStream();
    $name = $stream
      ->expect(Twig_Token::NAME_TYPE)
      ->getValue();
    $env = $parser
      ->getEnvironment();
    if ($test = $env
      ->getTest($name)) {
      return array(
        $name,
        $test,
      );
    }
    if ($stream
      ->test(Twig_Token::NAME_TYPE)) {

      // try 2-words tests
      $name = $name . ' ' . $parser
        ->getCurrentToken()
        ->getValue();
      if ($test = $env
        ->getTest($name)) {
        $parser
          ->getStream()
          ->next();
        return array(
          $name,
          $test,
        );
      }
    }
    $e = new Twig_Error_Syntax(sprintf('Unknown "%s" test.', $name), $line, $parser
      ->getFilename());
    $e
      ->addSuggestions($name, array_keys($env
      ->getTests()));
    throw $e;
  }
  protected function getTestNodeClass(Twig_Parser $parser, $test) {
    if ($test instanceof Twig_SimpleTest) {
      return $test
        ->getNodeClass();
    }
    return $test instanceof Twig_Test_Node ? $test
      ->getClass() : 'Twig_Node_Expression_Test';
  }
  public function getName() {
    return 'core';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Twig_Extension_Core::$dateFormats protected property
Twig_Extension_Core::$escapers protected property
Twig_Extension_Core::$numberFormat protected property
Twig_Extension_Core::$timezone protected property
Twig_Extension_Core::getDateFormat public function Gets the default format to be used by the date filter.
Twig_Extension_Core::getEscapers public function Gets all defined escapers.
Twig_Extension_Core::getFilters public function
Twig_Extension_Core::getFunctions public function
Twig_Extension_Core::getName public function
Twig_Extension_Core::getNumberFormat public function Get the default format used by the number_format filter.
Twig_Extension_Core::getOperators public function
Twig_Extension_Core::getTest protected function
Twig_Extension_Core::getTestNodeClass protected function
Twig_Extension_Core::getTests public function
Twig_Extension_Core::getTimezone public function Gets the default timezone to be used by the date filter.
Twig_Extension_Core::getTokenParsers public function
Twig_Extension_Core::parseNotTestExpression public function
Twig_Extension_Core::parseTestExpression public function
Twig_Extension_Core::setDateFormat public function Sets the default format to be used by the date filter.
Twig_Extension_Core::setEscaper public function Defines a new escaper to be used via the escape filter.
Twig_Extension_Core::setNumberFormat public function Sets the default format to be used by the number_format filter.
Twig_Extension_Core::setTimezone public function Sets the default timezone to be used by the date filter.