| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Platine Template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Platine Template is a template engine that has taken a lot of inspiration from Django. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This content is released under the MIT License (MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Copyright (c) 2020 Platine Template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Copyright (c) 2014 Guz Alexander, http://guzalexander.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Copyright (c) 2011, 2012 Harald Hanek, http://www.delacap.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Copyright (c) 2006 Mateo Murphy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * of this software and associated documentation files (the "Software"), to deal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * in the Software without restriction, including without limitation the rights | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * copies of the Software, and to permit persons to whom the Software is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * furnished to do so, subject to the following conditions: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * The above copyright notice and this permission notice shall be included in all | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * copies or substantial portions of the Software. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * SOFTWARE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  *  @file FilterCollection.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  *  The Template Filter collection class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  *  @package    Platine\Template\Parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  *  @author Platine Developers Team | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  *  @copyright  Copyright (c) 2020 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  *  @license    http://opensource.org/licenses/MIT  MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  *  @link   https://www.platine-php.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  *  @version 1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  *  @filesource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | namespace Platine\Template\Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | use Platine\Template\Filter\ArrayFilter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | use Platine\Template\Filter\DatetimeFilter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | use Platine\Template\Filter\HtmlFilter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | use Platine\Template\Filter\NumberFilter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | use Platine\Template\Filter\StringFilter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | use Platine\Template\Util\Helper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | use ReflectionClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | use ReflectionMethod; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  * @class FilterCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  * @package Platine\Template\Parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | class FilterCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * The list of filter with their name and class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @var array<string, class-string> | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     protected array $filters = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * Create new instance | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     public function __construct() | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         $this->addFilter(ArrayFilter::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         $this->addFilter(DatetimeFilter::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         $this->addFilter(HtmlFilter::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         $this->addFilter(NumberFilter::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         $this->addFilter(StringFilter::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * Add filter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param class-string $filter a class the filters will be | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      *  called statically | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     public function addFilter(string $filter): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         // register all its static methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $reflection = new ReflectionClass($filter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $methods = $reflection->getMethods(ReflectionMethod::IS_STATIC); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         if (!empty($methods)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             foreach ($methods as $method) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 /** @var class-string $className */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 $className = $method->class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 $this->filters[$method->name] = $className; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * Invokes the filter with the given name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param array<int, mixed> $args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     public function invoke(string $name, mixed $value, array $args = []): mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         // workaround for a single standard filter being a reserved | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         // keyword - we can't use overloading for static calls | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         if ($name === 'default') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $name = 'defaultValue'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         //convert underscore value to camelcase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         // like sort_key => sortKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $methodName = Helper::dashesToCamelCase($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         array_unshift($args, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         // Consult the mapping | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         if (!isset($this->filters[$methodName])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $className = $this->filters[$methodName]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $callback = $className . '::' . $methodName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         if (is_callable($callback)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             // Call a class method statically | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             return call_user_func_array($callback, $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |  |