/** * @param string $baseDir * @param string $locale */ public function __construct(string $baseDir, string $locale) { parent::__construct('parseDown', [$this, 'getParsedMarkdown'], ['is_safe' => ['html']]); $this->baseDir = $baseDir; $this->locale = $locale; $this->parser = new \ParsedownExtra(); }
public function __construct() { parent::__construct('age', function (Twig_Environment $env, $date, $acc = 'y', $timezoneDate = null, $timezoneNow = null) { $now = time(); if (!is_string($acc)) { throw new \Twig_Error_Runtime(sprintf('Accuracy must be string, got %s.', is_object($acc) ? get_class($acc) : gettype($acc) . (is_resource($acc) ? '' : '#' . $acc))); } $date = twig_date_converter($env, $date, $timezoneDate); $now = twig_date_converter($env, $now, $timezoneNow); $diff = $now->diff($date, false); switch (strtolower($acc)) { case 'y': $v = $diff->y + $diff->m / 12 + ($diff->d + ($diff->h + ($diff->i + $diff->s / 60) / 60) / 24) / 365; break; // case 'm' is not supported by design // case 'm' is not supported by design case 'd': $v = (int) $diff->format('%a') + ($diff->h + ($diff->i + $diff->s / 60) / 60) / 24; break; case 'h': $v = 24 * (int) $diff->format('%a') + $diff->h + ($diff->i + $diff->s / 60) / 60; break; case 'i': $v = 60 * 24 * (int) $diff->format('%a') + 60 * $diff->h + $diff->i + $diff->s / 60; break; case 's': return (int) $now->format('U') - (int) $date->format('U'); default: throw new \Twig_Error_Runtime(sprintf('Accuracy must be any of "y, d, h, i, s", got "%s".', $acc)); } // (int) cast for HHVM =< 3.9.10 // https://github.com/facebook/hhvm/pull/6134 / https://github.com/facebook/hhvm/issues/5537 return 1 === (int) $diff->invert ? $v : -1 * $v; }, ['needs_environment' => true]); }
public function __construct() { parent::__construct('date', function (Twig_Environment $env, $date, $format = null, $timezone = null) { if (empty($date) && !is_array($date)) { return ''; } return twig_date_format_filter($env, $date, $format, $timezone); }, ['needs_environment' => true]); }
public function __construct() { parent::__construct('filePermissions', function ($string) { if (is_string($string)) { if (ctype_digit($string)) { $permissions = octdec($string); } elseif (is_link($string)) { $permissions = lstat($string)['mode']; } elseif (file_exists($string)) { $permissions = fileperms($string); } else { throw new \Twig_Error_Runtime(sprintf('Cannot determine permissions for "%s".', $string)); } } else { $permissions = octdec($string); } if (($permissions & 0xc000) === 0xc000) { // Socket $info = 's'; } elseif (($permissions & 0xa000) === 0xa000) { // Symbolic Link $info = 'l'; } elseif (($permissions & 0x8000) === 0x8000) { // Regular $info = '-'; } elseif (($permissions & 0x6000) === 0x6000) { // Block special $info = 'b'; } elseif (($permissions & 0x4000) === 0x4000) { // Directory $info = 'd'; } elseif (($permissions & 0x2000) === 0x2000) { // Character special $info = 'c'; } elseif (($permissions & 0x1000) === 0x1000) { // FIFO pipe $info = 'p'; } else { // Unknown $info = 'u'; } // Owner $info .= $permissions & 0x100 ? 'r' : '-'; $info .= $permissions & 0x80 ? 'w' : '-'; $info .= $permissions & 0x40 ? $permissions & 0x800 ? 's' : 'x' : ($permissions & 0x800 ? 'S' : '-'); // Group $info .= $permissions & 0x20 ? 'r' : '-'; $info .= $permissions & 0x10 ? 'w' : '-'; $info .= $permissions & 0x8 ? $permissions & 0x400 ? 's' : 'x' : ($permissions & 0x400 ? 'S' : '-'); // World $info .= $permissions & 0x4 ? 'r' : '-'; $info .= $permissions & 0x2 ? 'w' : '-'; $info .= $permissions & 0x1 ? $permissions & 0x200 ? 't' : 'x' : ($permissions & 0x200 ? 'T' : '-'); return $info; }); }
public function __construct() { parent::__construct('upperRoman', function ($string, $matchMode = 'strict') { return $this->numeralRomanMatchCallBack($string, $matchMode, function (array $matches) { if (empty($matches[1])) { return $matches[1]; } return strtoupper($matches[1]); }); }); }
public function __construct() { parent::__construct('secFormat', function (\Twig_Environment $env, $string) { if (strpos($string, ':')) { return $string; } $seconds = (int) $string; $hours = floor($seconds / 3600); $minutes = floor(($seconds - $hours * 3600) / 60); return str_pad($hours, 2, '0', STR_PAD_LEFT) . ':' . str_pad($minutes, 2, '0', STR_PAD_LEFT) . ':' . str_pad($seconds % 60, 2, '0', STR_PAD_LEFT); }, array('needs_environment' => true)); }
public function __construct() { parent::__construct('upperFirst', function (Twig_Environment $env, $string) { if (is_object($string) || is_resource($string)) { throw new \Twig_Error_Runtime(sprintf('Invalid input, expected string got "%s".', is_object($string) ? get_class($string) : gettype($string))); } if (function_exists('mb_get_info') && null !== ($charset = $env->getCharset())) { return mb_strtoupper(mb_substr($string, 0, 1, $charset), $charset) . mb_substr($string, 1, mb_strlen($string, $charset), $charset); } return ucfirst($string); }, ['needs_environment' => true]); }
public function __construct() { parent::__construct('trans', [$this, 'translateKey'], ['is_safe' => ['html']]); $this->translationService = new TranslationService(new TranslationServiceConfig()); }
/** * @param string $baseDir * @param string $locale */ public function __construct(string $baseDir, string $locale) { parent::__construct('markDownContent', [$this, 'getMarkDownContent']); $this->baseDir = $baseDir; $this->locale = $locale; }
public function __construct() { parent::__construct('authorLink', [$this, 'getAuthorLink'], ['is_safe' => ['html']]); }
public function __construct() { parent::__construct($this->getFilterName(), $this->getFunction(), $this->getOptions()); }
public function __construct() { parent::__construct('SI', function (Twig_Environment $env, $number, $symbol = 'auto', $format = '%number%%symbol%', $decimal = null, $decimalPoint = null, $thousandSep = null) { $symbolMag = ['y', 'z', 'a', 'f', 'p', 'n', 'u', 'μ', 'm', 'c', 'd', 'da', 'h', 'k', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']; if (1 === strlen($symbol) || 'μ' === $symbol) { // note: string length of 'μ' is 2 $index = array_search($symbol, $symbolMag, true); if (false === $index) { throw new \Twig_Error_Runtime(sprintf('Unsupported symbol "%s".', $symbol)); } if ($index > 10) { // division if ($index > 13) { $index -= 3; // double 'k' correction $pow = 1000; } else { $pow = 10; } $number /= pow($pow, $index - 10); // -10 includes the double 'u' correction } else { // multiply if ($index > 8) { $pow = 10; $index -= 3; // includes the double 'u' correction } else { if ($index > 6) { --$index; // double 'u' correction } $pow = 1000; } $number *= pow($pow, 8 - $index); } } elseif ($symbol === 'da') { $number /= 10; } elseif ($symbol === 'auto') { $negative = $number < 0; $number = $negative ? abs($number) : $number; if ($number >= 1 && $number <= 10 || 0 === $number) { $symbol = ''; } elseif ($number < 1) { if ($number < 0.001) { $mag = 8; while ($number < 1 && $mag >= 0) { --$mag; // first decrement is double 'u' correction $number *= 1000; } } else { $mag = 11; while ($number < 1) { --$mag; $number *= 10; } } $symbol = $symbolMag[$mag]; } else { if ($number < 1000) { $mag = 0; while ($number >= 10) { ++$mag; $number /= 10; } } else { $mag = 2; while ($number >= 1000 && $mag <= 10) { ++$mag; $number /= 1000; } } $mag += $mag > 2 ? 11 : 10; $symbol = $symbolMag[$mag]; } if ($negative) { $number *= -1; } } elseif ($symbol !== '') { throw new \Twig_Error_Runtime(sprintf('Unsupported symbol "%s".', $symbol)); } $defaults = $env->getExtension('Twig_Extension_Core')->getNumberFormat(); if (null === $decimal) { $decimal = $defaults[0]; } if (null === $decimalPoint) { $decimalPoint = $defaults[1]; } if (null === $thousandSep) { $thousandSep = $defaults[2]; } $number = number_format((double) $number, $decimal, $decimalPoint, $thousandSep); $format = str_replace('%symbol%', $symbol, $format); if ('' === $symbol) { $format = trim($format); } return str_replace('%number%', $number, $format); }, ['needs_environment' => true]); }
/** * @param string $name * @param \IntlDateFormatter $formatter */ public function __construct(string $name, \IntlDateFormatter $formatter) { parent::__construct($name, [$this, 'getDateFormatted'], []); $this->formatter = $formatter; }
public function __construct() { parent::__construct('bytes', function (Twig_Environment $env, $number, $symbol = 'auto,bin', $format = '%number%%symbol%', $decimal = null, $decimalPoint = null, $thousandSep = null) { $symbolLength = strlen($symbol); $symbolMag = ['k' => 1, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4, 'P' => 5, 'E' => 6, 'Z' => 7, 'Y' => 8]; if ($symbolLength === 1) { if ('b' === $symbol) { $number *= 8; } elseif ('B' !== $symbol) { throw new \Twig_Error_Runtime(sprintf('Unsupported symbol \'%s\'.', $symbol)); } } elseif ($symbolLength <= 3) { // SI vs. bin. switch ($symbol[1]) { case 'i': // Binary if ($symbolLength < 3) { throw new \Twig_Error_Runtime(sprintf('Binary symbol must be end with either \'b\' or \'B\', got "%s".', $symbol)); } elseif ('b' === $symbol[2]) { // binary| bit $number *= 8; } elseif ('B' !== $symbol[2]) { // binary| byte throw new \Twig_Error_Runtime(sprintf('Binary symbol must be end with either \'b\' or \'B\', got "%s".', $symbol)); } $magnitude = 1024; break; case 'b': // SI | bit $magnitude = 1000; $number *= 8; break; case 'B': // SI | byte $magnitude = 1000; break; default: throw new \Twig_Error_Runtime(sprintf('Symbol must be binary (b|B[x]) or SI and must end with either \'b\' or \'B\', got "%s".', $symbol)); } if (!array_key_exists($symbol[0], $symbolMag)) { throw new \Twig_Error_Runtime(sprintf('Symbol must start with \'k\', \'K\', \'M\', \'G\', \'T\', \'P\', \'E\', \'Z\', or \'Y\', got "%s".', $symbol)); } $number /= pow($magnitude, $symbolMag[$symbol[0]]); // ** on PHP 5.6 } elseif ('auto,bin' === $symbol) { if ($number < 1024 && $number > -1024) { $symbol = 'B'; } else { $negative = $number < 0; $number = $negative ? abs($number) : $number; // since it is not guaranteed that array() will set the pointer to the first element reset($symbolMag); $mag = 0; while ($number >= 1023.9999999999 && $mag <= 8) { // large numbers rounding issues ++$mag; $number /= 1024; // $number >>= 10; doesn't work for large numbers (> PHP_INT_MAX) and looses the decimals next($symbolMag); } if ($negative) { $number *= -1; } $symbol = key($symbolMag) . 'iB'; } } elseif ('auto,SI' === $symbol) { if ($number < 1000 && $number > -1000) { $symbol = 'B'; } else { $negative = $number < 0; $number = $negative ? abs($number) : $number; // since it is not guaranteed that array() will set the pointer to the first element reset($symbolMag); $mag = 0; while ($number >= 1000 && $mag <= 8) { ++$mag; $number /= 1000; next($symbolMag); } if ($negative) { $number *= -1; } $symbol = key($symbolMag) . 'B'; } } else { throw new \Twig_Error_Runtime(sprintf('Unsupported symbol "%s".', $symbol)); } $defaults = $env->getExtension('Twig_Extension_Core')->getNumberFormat(); if (null === $decimal) { $decimal = $defaults[0]; } if (null === $decimalPoint) { $decimalPoint = $defaults[1]; } if (null === $thousandSep) { $thousandSep = $defaults[2]; } $number = number_format((double) $number, $decimal, $decimalPoint, $thousandSep); $format = str_replace('%symbol%', $symbol, $format); return str_replace('%number%', $number, $format); }, ['needs_environment' => true]); }