hanze/iwa-panda2

Lollipop/TemplateMethods.php in main
Repositories | Summary | Log | Files | README.md

TemplateMethods.php (3081B) download


  1<?php
  2
  3namespace Lollipop {
  4
  5    /// this class contains all built-in functions for the templater
  6    class TemplateMethods
  7    {
  8        public static function add(array &$tokens)
  9        {
 10            $right = array_pop($tokens);
 11            $left = array_pop($tokens);
 12
 13            if (is_null($left) || is_null($right)) {
 14                throw new ErrorException("Stack is empty");
 15            }
 16
 17            return $left + $right;
 18
 19        }
 20
 21        public static function sub(array &$tokens)
 22        {
 23            $right = array_pop($tokens);
 24            $left = array_pop($tokens);
 25
 26            if (is_null($left) || is_null($right)) {
 27                throw new ErrorException("Stack is empty");
 28            }
 29
 30            return intval($left) - intval($right);
 31        }
 32
 33        public static function mul(array &$tokens)
 34        {
 35            $right = array_pop($tokens);
 36            $left = array_pop($tokens);
 37
 38            if (is_null($left) || is_null($right)) {
 39                throw new ErrorException("Stack is empty");
 40            }
 41
 42            return intval($left) * intval($right);
 43        }
 44
 45        public static function div(array &$tokens)
 46        {
 47            $right = array_pop($tokens);
 48            $left = array_pop($tokens);
 49
 50            if (is_null($left) || is_null($right)) {
 51                throw new ErrorException("Stack is empty");
 52            }
 53
 54            return intval($left) / intval($right);
 55        }
 56
 57        public static function cat(array &$tokens)
 58        {
 59            $right = array_pop($tokens);
 60            $left = array_pop($tokens);
 61
 62            if (is_null($left) || is_null($right)) {
 63                throw new ErrorException("Stack is empty");
 64            }
 65
 66            return $left . $right;
 67        }
 68
 69        public static function to_int(array &$tokens)
 70        {
 71            $val = array_pop($val);
 72
 73            if (is_null($val)) {
 74                throw new ErrorException("Stack is empty");
 75            }
 76
 77            return inval($val);
 78        }
 79
 80        public static function include(array &$tokens)
 81        {
 82            $name = array_pop($tokens);
 83
 84            if ($name  == null) {
 85                throw new ErrorException("Stack is empty");
 86            }
 87
 88            include($name);
 89        }
 90
 91        public static function eval(array &$tokens)
 92        {
 93            $expr = array_pop($tokens);
 94
 95            if (is_null($expr)) {
 96                throw new ErrorException("Stack is empty");
 97            }
 98
 99            return eval("return ($expr);");
100        }
101
102        public static function format_if(array &$stack)
103        {
104            $format_false = array_pop($stack);
105            $format_true = array_pop($stack);
106            $expr = array_pop($stack);
107
108            if (is_null($expr) || is_null($format_true) || is_null($format_false)) {
109                throw new ErrorException("Stack is empty");
110            }
111
112            if ($expr == "") {
113                return $format_false;
114            } else {
115                return str_replace("%%", $expr, $format_true);
116            }
117        }
118    }
119}