| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * citeproc-php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @link        http://github.com/seboettg/citeproc-php for the source repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright   Copyright (c) 2016 Sebastian Böttger. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license     https://opensource.org/licenses/MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace Seboettg\CiteProc\Rendering; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Seboettg\CiteProc\CiteProc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Seboettg\CiteProc\Exception\CiteProcException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Seboettg\CiteProc\Styles\AffixesTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Seboettg\CiteProc\Styles\ConsecutivePunctuationCharacterTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Seboettg\CiteProc\Styles\DisplayTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Seboettg\CiteProc\Styles\FormattingTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Seboettg\CiteProc\Styles\QuotesTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Seboettg\CiteProc\Styles\TextCaseTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Seboettg\CiteProc\Util\NumberHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Seboettg\CiteProc\Util\PageHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Seboettg\CiteProc\Util\StringHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * Class Term | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @package Seboettg\CiteProc\Node\Style | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @author Sebastian Böttger <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | class Text implements Rendering | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     use FormattingTrait, | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         AffixesTrait, | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         TextCaseTrait, | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         DisplayTrait, | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         ConsecutivePunctuationCharacterTrait, | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         QuotesTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     private $toRenderType; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     private $toRenderTypeValue; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     private $form = "long"; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * Text constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @param \SimpleXMLElement $node | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function __construct(\SimpleXMLElement $node) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         foreach ($node->attributes() as $attribute) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $name = $attribute->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             if (in_array($name, ['value', 'variable', 'macro', 'term'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $this->toRenderType = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 $this->toRenderTypeValue = (string) $attribute; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             if ($name === "form") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 $this->form = (string) $attribute; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->initFormattingAttributes($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $this->initDisplayAttributes($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->initTextCaseAttributes($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $this->initAffixesAttributes($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->initQuotesAttributes($node); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * @param \stdClass $data | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * @param int|null $citationNumber | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function render($data, $citationNumber = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $lang = (isset($data->language) && $data->language != 'en') ? $data->language : 'en'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $renderedText = ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         switch ($this->toRenderType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             case 'value': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 $renderedText = $this->applyTextCase($this->toRenderTypeValue, $lang); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             case 'variable': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 if ($this->toRenderTypeValue === "citation-number") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                      $var = "citation-number"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                     if (isset($data->$var)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                         $renderedText = $data->$var; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                         $renderedText = $citationNumber + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 if ($this->toRenderTypeValue === "page") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                     $renderedText = $this->renderPage($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                     // for test sort_BibliographyCitationNumberDescending.json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 } else if ($this->toRenderTypeValue === "citation-number" && !is_null($citationNumber)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                     $renderedText = strval($citationNumber + 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                     // check if there is an attribute with prefix short or long e.g. shortTitle or longAbstract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                     // test case group_ShortOutputOnly.json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                     if (in_array($this->form, ["short", "long"])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                         $attrWithPrefix = $this->form . ucfirst($this->toRenderTypeValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                         if (isset($data->{$attrWithPrefix}) && !empty($data->{$attrWithPrefix})) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                             $renderedText = $this->applyTextCase($data->{$attrWithPrefix}, $lang); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                     if (!empty($data->{$this->toRenderTypeValue})) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                         $renderedText = $this->applyTextCase(StringHelper::clearApostrophes($data->{$this->toRenderTypeValue}), $lang); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             case 'macro': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 $macro = CiteProc::getContext()->getMacro($this->toRenderTypeValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 if (is_null($macro)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                     try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                         throw new CiteProcException("Macro \"" . $this->toRenderTypeValue . "\" does not exist."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                     } catch (CiteProcException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                         $renderedText = ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                     $renderedText = $macro->render($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             case 'term': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 $term = CiteProc::getContext()->getLocale()->filter("terms", $this->toRenderTypeValue, $this->form)->single; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                 $renderedText = !empty($term) ? $this->applyTextCase($term, $lang) : ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         if (!empty($renderedText)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             //$renderedText = $this->applyTextCase($renderedText); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             $text = $this->format($renderedText); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             $res = $this->addAffixes($text, $this->quotes); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             if (!empty($res)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 $res = $this->removeConsecutiveChars($res); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             $res = $this->addSurroundingQuotes($res); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             return $this->wrapDisplayBlock($res); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         return ""; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     public function rendersMacro() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         return $this->toRenderType === "macro"; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     public function getSource() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         return $this->toRenderType; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     public function getVariable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         return $this->toRenderTypeValue; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 171 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     private function renderPage($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         if (empty($data->page)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             return ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         if (preg_match(NumberHelper::PATTERN_COMMA_AMPERSAND_RANGE, $data->page)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |             $data->page = $this->normalizeDateRange($data->page); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             $ranges = preg_split("/[-–]/", trim($data->page)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             if (count($ranges) > 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |                 if (!empty(CiteProc::getContext()->getGlobalOptions()) && !empty(CiteProc::getContext()->getGlobalOptions()->getPageRangeFormat())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |                     return PageHelper::processPageRangeFormats($ranges, CiteProc::getContext()->getGlobalOptions()->getPageRangeFormat()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |                 list($from, $to) = $ranges; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |                 return "$from-$to"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         return $data->page; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 191 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     private function normalizeDateRange($page) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         if (preg_match("/^(\d+)--(\d+)$/", trim($page), $matches)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |             return $matches[1]."-".$matches[2]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         return $page; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 199 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 200 |  |  | } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 201 |  |  |  | 
            
                        
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.