<?php

namespace RxConnexionBundle\Twig;

class AppExtension extends \Twig_Extension
{
    public function getFilters(){
        return array(
            new \Twig_SimpleFilter('base64Encode', array($this, 'base64EncodeFilter')),
            new \Twig_SimpleFilter('base64Decode', array($this, 'base64DecodeFilter')),
            new \Twig_SimpleFilter('isArray', array($this, 'isArrayFilter')),
            new \Twig_SimpleFilter('inArray', array($this, 'inArrayFilter')),
            new \Twig_SimpleFilter('ordinal', array($this, 'ordinal')),
            new \Twig_SimpleFilter('strPad', array($this, 'strPad')),
        );
    }
    public function base64EncodeFilter($str) {
        return base64_encode($str);
    }

    public function base64DecodeFilter($str) {
        return base64_decode($str);
    }

    public function isArrayFilter($var) {
        return is_array($var);
    }

    public function inArrayFilter($array, $var) {
        return in_array($var, $array);
    }

    public function getName(){
        return 'app_extension';
    }

    public function ordinal($number) {
        $ends = array('th','st','nd','rd','th','th','th','th','th','th');
        if ((($number % 100) >= 11) && (($number%100) <= 13))
            return $number. 'th';
        else
            return $number. $ends[$number % 10];
    }

    public function strPad($str, $length, $padString){
        return str_pad($str,$length,$padString, STR_PAD_LEFT);
    }


}