/** * @inheritdoc */ public function sanitize($input) { $input = NumericHelper::toNumeric($input); return $input < 0 ? 0 : $input; }
/** * 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=`', $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; }
/** * Set hash for attributes. * * @param array $attributes */ public function setHash(array $attributes) { foreach ($attributes as $attribute) { $this->{$attribute . '_hash'} = NumericHelper::hexToBin(md5($this->{$attribute})); } }
protected function hash($value) { return NumericHelper::hexToBin(md5($value)); }
/** * Number convert to positive. * * @param int $value * @return int */ public static function positive($value) { return NumericHelper::toPositive($value); }