Exemplo n.º 1
0
 /**
  * Check numeric is parity.
  *
  * @param string $value
  * @param array $params :
  *
  * - then
  * - else
  *
  * @param Template $template
  * @throws \rock\template\TemplateException
  * @return string
  */
 public static function isParity($value, array $params, Template $template)
 {
     if (empty($params) || count($params) < 1 || !isset($params['then'])) {
         throw new TemplateException(TemplateException::UNKNOWN_PARAM_FILTER, ['name' => __METHOD__]);
     }
     $params['else'] = isset($params['else']) ? $params['else'] : null;
     $template = clone $template;
     $placeholders = [];
     $placeholders['output'] = $value;
     return NumericHelper::isParity($value) ? $template->replace($params['then'], $placeholders) : $template->replace($params['else'], $placeholders);
 }
Exemplo n.º 2
0
 /**
  * Callback to replace variables template.
  *
  * @param array $matches array of variables template.
  * @throws TemplateException
  * @return string
  */
 protected function replaceCallback($matches)
 {
     if (!empty($matches['beforeSkip']) && !empty($matches['afterSkip'])) {
         return trim($matches[0], '{!} ');
     }
     // check: count quotes does not parity
     if (!NumericHelper::isParity(mb_substr_count($matches[0], '`', 'UTF-8'))) {
         return $matches[0];
     }
     $matches[0] = preg_replace_callback('/
             \\s*(?P<sugar> (?!`)\\*(?!`) | (?!`)\\*\\*(?!`) | (?!`)\\/(?!`) | (?!`)\\%(?!`) |
             \\s+(?!`)mod(?!`)\\s+ | (?!`)\\+(?!`) | (?!`)\\-(?!`) | (?!`)\\|(?!`) | (?!`)\\&(?!`) |
             (?!`)\\^(?!`) | (?!`)\\>\\>(?!`) | (?!`)\\<\\<(?!`) |
             (?!`)\\|\\|(?!`) | (?!`)\\&\\&(?!`) | \\s+(?!`)' . $this->_getInlineConditionNames() . '(?!`)\\s+ |`\\s+\\?\\s+|`\\s+\\:\\s+)\\s*`
         /x', [$this, 'replaceSugar'], $matches[0]);
     // replace `=` tpl mnemonics
     $matches[0] = preg_replace('/`([\\!\\<\\>]?)[\\=]+`/', '`$1&#61;`', $matches[0]);
     // replace `text` to ““text””
     $matches[0] = preg_replace(['/=\\s*\\`/', '/\\`/'], ['=““', '””'], $matches[0]);
     // replacement of internal recursion on {{{...}}}
     $i = 0;
     $dataRecursive = [];
     $matches[0] = preg_replace_callback('/\\“\\“(?:[^\\“\\”]++|(?R))*\\”\\”/iu', function ($value) use(&$dataRecursive, &$i) {
         $key = '{{{' . $i . '}}}';
         $value = current($value);
         $dataRecursive[$key] = $value;
         $i++;
         return $key;
     }, $matches[0]);
     // Search params is variable of template
     $params = $this->_searchParams($matches[0], $dataRecursive);
     // Search of filters (modifiers)
     $filters = $this->_searchFilters($matches[0], $dataRecursive);
     $matches['name'] = trim($matches['name']);
     $params = Serialize::unserializeRecursive($params);
     // returns cache
     list($cacheKey, $cacheExpire, $cacheTags) = $this->calculateCacheParams($params);
     if (($resultCache = $this->getCacheContent($cacheKey)) !== false) {
         return $resultCache;
     }
     $filters = Serialize::unserializeRecursive($filters);
     $sanitize = $this->sanitize;
     if (isset($params['sanitize'])) {
         $sanitize = $params['sanitize'];
     } elseif (!empty($matches['sanitizeDisable'])) {
         $sanitize = self::SANITIZE_DISABLE;
     }
     // chunk
     if ($matches['type'] === '$') {
         $result = $this->getChunk($matches['name'], $params);
         // alias
     } elseif ($matches['type'] === '@@') {
         $result = Alias::getAlias("@{$matches['name']}");
         // placeholder
     } elseif ($matches['type'] === '+') {
         $result = $this->getPlaceholder($matches['name'], $sanitize);
         // constant
     } elseif ($matches['type'] === '++') {
         $result = $this->getConst($matches['name'], $sanitize);
         // extension
     } elseif ($matches['type'] === '#') {
         $result = $this->getExtension($matches['name'], $params, $sanitize);
         //  i18n
     } elseif ($matches['type'] === '%') {
         $result = $this->_calculateI18N($matches['name'], Helper::getValue($params['placeholders'], []), Helper::getValue($params['locale']), Helper::getValue($params['category']));
         // link
     } elseif ($matches['type'] === '~') {
         $result = $this->_calculateLink($matches['name'], $params);
         // snippet
     } elseif (empty($matches['type'])) {
         $result = $this->getSnippet($matches['name'], $params, $sanitize);
     } else {
         return $matches[0];
     }
     // make a filter
     if (!empty($filters)) {
         $result = $this->makeFilter($result, $filters);
     }
     if ($this->autoSerialize) {
         if (is_array($result) || is_object($result) && !$result instanceof \Closure) {
             $result = @serialize($result);
         }
     }
     if (!is_scalar($result) && !empty($result)) {
         throw new TemplateException('Wrong type is var: ' . Json::encode($result));
     }
     // sets a content to cache
     $this->setCacheContent($cacheKey, $result, $cacheExpire, $cacheTags ?: []);
     return $result;
 }