1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Yolk - Gamer Network's PHP Framework. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2015 Gamer Network Ltd. |
6
|
|
|
* |
7
|
|
|
* Distributed under the MIT License, a copy of which is available in the |
8
|
|
|
* LICENSE file that was bundled with this package, or online at: |
9
|
|
|
* https://github.com/gamernetwork/yolk-support |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace yolk\support; |
13
|
|
|
|
14
|
|
|
class TwigExtension extends \Twig_Extension { |
15
|
|
|
|
16
|
|
|
public function getName() { |
17
|
|
|
return 'Yolk Twig Extension'; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function getTests() { |
21
|
|
|
return [ |
22
|
|
|
new \Twig_SimpleTest('numeric', function( $value ) { |
|
|
|
|
23
|
|
|
return is_numeric($value); |
24
|
|
|
}), |
25
|
|
|
new \Twig_SimpleTest('integer', function( $value ) { |
|
|
|
|
26
|
|
|
return is_numeric($value) && ($value == (int) $value); |
27
|
|
|
}), |
28
|
|
|
new \Twig_SimpleTest('string', function( $value ) { |
|
|
|
|
29
|
|
|
return is_string($value); |
30
|
|
|
}), |
31
|
|
|
new \Twig_SimpleTest('array', function( $value ) { |
|
|
|
|
32
|
|
|
return is_array($value); |
33
|
|
|
}), |
34
|
|
|
new \Twig_SimpleTest('object', function( $value, $class = '' ) { |
|
|
|
|
35
|
|
|
$result = is_object($value); |
36
|
|
|
if( $class ) |
37
|
|
|
$result = $result && ($value instanceof $class); |
38
|
|
|
return $result; |
39
|
|
|
}), |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getFilters() { |
44
|
|
|
|
45
|
|
|
$filters = [ |
46
|
|
|
new \Twig_SimpleFilter('md5', 'md5'), |
|
|
|
|
47
|
|
|
new \Twig_SimpleFilter('sha1', 'sha1'), |
|
|
|
|
48
|
|
|
new \Twig_SimpleFilter('truncate', function( $str, $length, $replace = '...' ) { |
|
|
|
|
49
|
|
|
if( isset($str) ) |
50
|
|
|
return strlen($str) <= $length ? $str : substr($str, 0, $length - strip_tags($replace)) . $replace; |
51
|
|
|
return null; |
52
|
|
|
}), |
53
|
|
|
new \Twig_SimpleFilter('sum', 'array_sum'), |
|
|
|
|
54
|
|
|
new \Twig_SimpleFilter('shuffle', 'shuffle'), |
|
|
|
|
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
// include some helpful shit from yolk-core/StringHelper if we have it available |
58
|
|
|
if( class_exists ('\\yolk\\helpers\\StringHelper') ) { |
59
|
|
|
$filters = array_merge( |
60
|
|
|
$filters, |
61
|
|
|
[ |
62
|
|
|
new \Twig_SimpleFilter('slugify', ['\\yolk\\helpers\\StringHelper', 'slugify']), |
|
|
|
|
63
|
|
|
new \Twig_SimpleFilter('uncamelise', ['\\yolk\\helpers\\StringHelper', 'uncamelise']), |
|
|
|
|
64
|
|
|
new \Twig_SimpleFilter('removeAccents', ['\\yolk\\helpers\\StringHelper', 'removeAccents']), |
|
|
|
|
65
|
|
|
new \Twig_SimpleFilter('ordinal', ['\\yolk\\helpers\\StringHelper', 'ordinal']), |
|
|
|
|
66
|
|
|
new \Twig_SimpleFilter('sizeFormat', ['\\yolk\\helpers\\StringHelper', 'sizeFormat']), |
|
|
|
|
67
|
|
|
new \Twig_SimpleFilter('stripControlChars', ['\\yolk\\helpers\\StringHelper', 'stripControlChars']), |
|
|
|
|
68
|
|
|
new \Twig_SimpleFilter('normaliseLineEndings', ['\\yolk\\helpers\\StringHelper', 'normaliseLineEndings']), |
|
|
|
|
69
|
|
|
] |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// more helpful shit from yolk-core/Inflector if we have it available |
74
|
|
|
if( class_exists ('\\yolk\\helpers\\Inflector') ) { |
75
|
|
|
$filters = array_merge( |
76
|
|
|
$filters, |
77
|
|
|
[ |
78
|
|
|
new \Twig_SimpleFilter('pluralise', ['\\yolk\\helpers\\Inflector', 'pluralise']), |
|
|
|
|
79
|
|
|
new \Twig_SimpleFilter('singularise', ['\\yolk\\helpers\\Inflector', 'singularise']), |
|
|
|
|
80
|
|
|
] |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $filters; |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getFunctions() { |
89
|
|
|
|
90
|
|
|
$functions = [ |
91
|
|
|
new \Twig_SimpleFunction('ceil', 'ceil'), |
|
|
|
|
92
|
|
|
new \Twig_SimpleFunction('floor', 'floor'), |
|
|
|
|
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
// include some helpful shit from yolk-core/StringHelper if we have it available |
96
|
|
|
if( class_exists ('\\yolk\\helpers\\StringHelper') ) { |
97
|
|
|
$functions = array_merge( |
98
|
|
|
$functions, |
99
|
|
|
[ |
100
|
|
|
new \Twig_SimpleFunction('randomHex', ['\\yolk\\helpers\\StringHelper', 'randomHex']), |
|
|
|
|
101
|
|
|
new \Twig_SimpleFunction('randomString', ['\\yolk\\helpers\\StringHelper', 'randomString']), |
|
|
|
|
102
|
|
|
] |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $functions; |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// EOF |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.