| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace allejo\stakx\Twig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Twig_Environment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Twig_Extension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Twig_SimpleFilter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * This file is part of Twig. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * (c) 2009 Fabien Potencier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @author Henrik Bjornskov <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class TextExtension extends Twig_Extension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * Returns a list of filters. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return array | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     public function getFilters () | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         return array( | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |             new Twig_SimpleFilter('summary',  'twig_summary_filter'), | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |             new Twig_SimpleFilter('truncate', 'twig_truncate_filter', array('needs_environment' => true)), | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             new Twig_SimpleFilter('wordwrap', 'twig_wordwrap_filter', array('needs_environment' => true)), | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Name of this extension. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function getName () | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         return 'Text'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | function twig_summary_filter ($value, $paragraphCount = 1) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     if (function_exists('simplexml_load_string')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $content = simplexml_load_string('<html>' . $value . '</html>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $count = min($paragraphCount, $content->count()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $children = $content->children(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $summary = ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         for ($i = 0; $i < $count; $i++) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $summary .= $children[$i]->asXml(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return $summary; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | function twig_truncate_filter (Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     if (mb_strlen($value, $env->getCharset()) > $length) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if ($preserve) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             // If breakpoint is on the last word, return the value without separator. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             if (false === ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset()))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $length = $breakpoint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return rtrim(mb_substr($value, 0, $length, $env->getCharset())).$separator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | function twig_wordwrap_filter (Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     $sentences = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     $previous = mb_regex_encoding(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     mb_regex_encoding($env->getCharset()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     $pieces = mb_split($separator, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     mb_regex_encoding($previous); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     foreach ($pieces as $piece) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         while (!$preserve && mb_strlen($piece, $env->getCharset()) > $length) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $sentences[] = mb_substr($piece, 0, $length, $env->getCharset()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $piece = mb_substr($piece, $length, 2048, $env->getCharset()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $sentences[] = $piece; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     return implode($separator, $sentences); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |  | 
            
                        
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.