|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Zikula package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright Zikula - https://ziku.la/ |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Zikula\Bundle\CoreBundle\Twig\Runtime; |
|
15
|
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
18
|
|
|
use Twig\Extension\RuntimeExtensionInterface; |
|
19
|
|
|
|
|
20
|
|
|
class CoreRuntime implements RuntimeExtensionInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var TranslatorInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $translator; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(TranslatorInterface $translator) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->translator = $translator; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Delete a key of an array. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function arrayUnset(array $array, string $key): array |
|
36
|
|
|
{ |
|
37
|
|
|
unset($array[$key]); |
|
38
|
|
|
|
|
39
|
|
|
return $array; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function yesNo($string): string |
|
43
|
|
|
{ |
|
44
|
|
|
if (null !== $string && !in_array($string, [true, false, '', '0', '1'], true)) { |
|
45
|
|
|
return $string; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return (bool) $string ? $this->translator->trans('Yes') : $this->translator->trans('No'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Apply an existing function (e.g. php's `md5`) to a string. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string|object $subject |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
*/ |
|
57
|
|
|
public function applyPhp($subject, string $func) |
|
58
|
|
|
{ |
|
59
|
|
|
if (function_exists($func)) { |
|
60
|
|
|
return $func($subject); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $subject; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Protect a given mail address by finding the text 'x@y' and replacing |
|
68
|
|
|
* it with HTML entities. This provides protection against email harvesters. |
|
69
|
|
|
*/ |
|
70
|
|
|
public function protectMailAddress(string $string): string |
|
71
|
|
|
{ |
|
72
|
|
|
$string = preg_replace_callback( |
|
73
|
|
|
'/(.)@(.)/s', |
|
74
|
|
|
static function($m) { |
|
75
|
|
|
return '&#' . sprintf('%03d', ord($m[1])) . ';@&#' . sprintf('%03d', ord($m[2])) . ';'; |
|
76
|
|
|
}, |
|
77
|
|
|
$string |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
return $string; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Call a php callable with parameters. |
|
85
|
|
|
* |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
*/ |
|
88
|
|
|
public function callFunc(callable $callable, array $parameters = []) |
|
89
|
|
|
{ |
|
90
|
|
|
if (function_exists($callable)) { |
|
|
|
|
|
|
91
|
|
|
return call_user_func_array($callable, $parameters); |
|
92
|
|
|
} |
|
93
|
|
|
throw new InvalidArgumentException($this->translator->trans('Function does not exist or is not callable.')); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|