LightnCandy\Validator::helper PHP Method

helper() public static method

Return true when the name is listed in helper table
public static helper ( array\arraystring | integer> &$context, string $name, boolean $checkSubexp = false ) : boolean
$context array\arraystring | integer>
$name string token name
$checkSubexp boolean true when check for subexpression
return boolean Return true when it is custom helper
    public static function helper(&$context, $name, $checkSubexp = false)
    {
        if (static::resolveHelper($context, $name)) {
            $context['usedFeature']['helper']++;
            return true;
        }
        if ($checkSubexp) {
            switch ($name) {
                case 'if':
                case 'unless':
                case 'with':
                case 'each':
                case 'lookup':
                    return $context['flags']['nohbh'] ? false : true;
            }
        }
        return false;
    }

Usage Example

Beispiel #1
0
 /**
  * Parse a subexpression then return parsed result.
  *
  * @param string $expression the full string of a sub expression
  * @param array<string,array|string|integer> $context current compile context
  *
  * @return array<boolean|integer|array> Return parsed result
  *
  * @expect array(\LightnCandy\Parser::SUBEXP, array(array('a'), array('b')), '(a b)') when input '(a b)', array('usedFeature' => array('subexp' => 0), 'flags' => array('advar' => 0, 'namev' => 0, 'this' => 0, 'exhlp' => 1, 'strpar' => 0))
  */
 public static function subexpression($expression, &$context)
 {
     $context['usedFeature']['subexp']++;
     $vars = static::analyze(substr($expression, 1, -1), $context);
     $avars = static::advancedVariable($vars, $context, $expression);
     if (isset($avars[0][0]) && !$context['flags']['exhlp']) {
         if (!Validator::helper($context, $avars[0][0], true)) {
             $context['error'][] = "Can not find custom helper function defination {$avars[0][0]}() !";
         }
     }
     return array(static::SUBEXP, $avars, $expression);
 }