<?php
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\TwigTest;
use Doctrine\ORM\EntityManagerInterface;
class HelperExtension extends AbstractExtension
{
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function getFilters(): array
{
return array(
new TwigFilter('time_ago', array($this, 'time_ago')),
new TwigFilter('time_togo', array($this, 'time_togo')),
new TwigFilter('filter', array($this, 'filter')),
new TwigFilter('bbcode', array($this, 'bbcode')),
new TwigFilter('shorten', array($this, 'shorten')),
new TwigFilter('remove_empty', array($this, 'remove_empty')),
new TwigFilter('basename', array($this, 'basename')),
new TwigFilter('remove_element', array($this, 'remove_element')),
new TwigFilter('format_bytes', array($this, 'format_bytes')),
new TwigFilter('join_objects', array($this, 'join_objects')),
new TwigFilter('pluralize', array($this, 'pluralize')),
new TwigFilter('cash', array($this, 'cash')),
new TwigFilter('not_less_than', array($this, 'not_less_than')),
new TwigFilter('filesize_convert', array($this, 'filesize_convert')),
new TwigFilter('resolve_tags', array($this, 'resolve_tags')),
new TwigFilter('json_decode', array($this, 'json_decode'))
);
}
public function getFunctions(): array
{
return array(
new TwigFunction('getRepository', array($this, 'getRepository')),
);
}
public function getTests(): array
{
return array(
new TwigTest('instanceof', array($this, 'isInstanceOf')),
);
}
public function getRepository($repositoryName)
{
return $this->em->getRepository($repositoryName);
}
public function isInstanceOf($var, $instance)
{
if(is_null($var))
return false;
$reflexionClass = new \ReflectionClass($instance);
return $reflexionClass->isInstance($var);
}
public function json_decode($text)
{
return json_decode($text);
}
public function filesize_convert($amount, $from, $to)
{
if($from == "byte" && $to == "gigabyte")
{
return ((($amount / 1024) / 1024) / 1024);
}
}
public function not_less_than($number, $notLessThan)
{
if($number < $notLessThan)
return $notLessThan;
else
return $number;
}
function cash(?float $amount, $currency, $symbol = false)
{
// Null?
if($amount === null)
{
if($currency == "GBP")
return ($symbol ? '£' : '') . number_format(0, 2);
else
throw new \Exception("Unknown currency: " . $currency);
}
else
{
if($currency == "GBP")
return ($symbol ? '£' : '') . number_format($amount, 2);
else
throw new \Exception("Unknown currency: " . $currency);
}
}
function pluralize($count, $single, $multiple)
{
if($count == 1)
return $single;
else
return $multiple;
}
public function join_objects($arr, $method, $glue = ', ')
{
$bits = array();
foreach($arr AS $someObject)
{
$methodName = "get" . ucfirst($method);
$bits[] = $someObject->$methodName();
}
return implode($glue, $bits);
}
public function remove_element($arr, $elem)
{
foreach($arr AS $k => $v)
{
if($v === $elem)
unset($arr[$k]);
}
return $arr;
}
public function basename($val)
{
return basename($val);
}
function shorten($content, $length)
{
return trim(substr($content, 0, $length));
}
function remove_empty($arr)
{
return array_filter($arr);
}
function bbcode($content)
{
preg_match_all('~\[PAGELINK\](.*?)\[/PAGELINK\]~s', $content, $matches);
// Got any?
if(count($matches))
{
// Loop
foreach($matches[0] AS $k => $someString)
{
// Grab the perma ref
$permaRef = $matches[1][$k];
// Valid
if(strlen($permaRef) == 5)
{
// Find the page
$thePage = $this->em->getRepository('App:Pages\Page')->findOneFiltered(array(
"permaRef" => $permaRef
));
// Found it?
if($thePage)
{
// Replace it
$content = str_replace($someString, $thePage->getFriendlyPath(), $content);
}
else
{
// Replace it
$content = str_replace($someString, "" . $permaRef, $content);
}
}
}
}
return $content;
}
function filter($arr, $filters = array())
{
$keepValues = array();
// Loop each
foreach($arr AS $someItem)
{
$match = true;
// Loop the filters
foreach($filters AS $k => $v)
{
// Get the method
$getterMethod = "get" . ucfirst($k);
// Matched?
if($someItem->$getterMethod() != $v)
$match = false;
}
// Matched?
if($match)
{
// Into the array
$keepValues[] = $someItem;
}
}
return $keepValues;
}
function time_ago(\DateTime $ptime, $word = 'ago')
{
$timestamp = $ptime->getTimestamp();
$estimate_time = time() - $timestamp;
if($timestamp < 0)
{
return '---';
}
elseif( $estimate_time < 5 )
{
return 'just now';
}
$condition = array(
12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach( $condition as $secs => $str )
{
$d = $estimate_time / $secs;
if( $d >= 1 )
{
$r = round( $d );
return $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ' . $word;
}
}
}
function time_togo(\DateTime $ptime)
{
$timestamp = $ptime->getTimestamp();
$estimate_time = $timestamp - time();
if($timestamp < 0)
{
return '---';
}
elseif( $estimate_time < 5 )
{
return 'just now';
}
$condition = array(
12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach( $condition as $secs => $str )
{
$d = $estimate_time / $secs;
if( $d >= 1 )
{
$r = round( $d );
return $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . '';
}
}
}
}