| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of graze/console-diff-renderer. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (c) 2017 Nature Delivered Ltd. <https://www.graze.com> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @license https://github.com/graze/console-diff-renderer/blob/master/LICENSE.md | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @link    https://github.com/graze/console-diff-renderer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Graze\DiffRenderer\Diff; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class ConsoleDiff extends FirstDiff | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     const UNCLOSED_TAGS = '/<(?P<tag>[a-z;=]+)>(?!.*?<\/(?:(?P=tag)|)>)/i'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** @var FirstDiff */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $diff; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * ConsoleDiff constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param FirstDiff|null $differ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function __construct($differ = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->diff = $differ ?: new FirstDiff(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param string[] $old | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param string[] $new | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param int      $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return string[] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     public function lines(array $old, array $new, $options = OutputInterface::OUTPUT_NORMAL) | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $diff = $this->diff->lines($old, $new); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         if (($options & OutputInterface::OUTPUT_NORMAL) == OutputInterface::OUTPUT_NORMAL) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             // replace col number with strip_tags version to represent what is outputted to the user | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             $len = count($new); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             for ($i = 0; $i < $len; $i++) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                 if (isset($diff[$i]) && !is_null($new[$i]) && $diff[$i]['col'] > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                     $tags = $this->getUnclosedTags(mb_substr($new[$i], 0, $diff[$i]['col'])); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                     if (count($tags) > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                         $diff[$i]['str'] = '<' . implode('><', $tags) . '>' . $diff[$i]['str']; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                     $diff[$i]['col'] = mb_strlen(strip_tags(mb_substr($new[$i], 0, $diff[$i]['col']))); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         return $diff; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Find a list of unclosed tags | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param string $string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     private function getUnclosedTags($string) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         if (preg_match_all(static::UNCLOSED_TAGS, $string, $matches)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             return $matches['tag']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  |