| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | * This file is part of the League.csv library | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | * @license http://opensource.org/licenses/MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | * @link https://github.com/thephpleague/csv/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | * @version 9.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | * @package League.csv | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace League\Csv; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use League\Csv\Exception\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use php_user_filter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Throwable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  *  A stream filter to conform the CSV field to RFC4180 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @package League.csv | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @since   9.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @author  Ignace Nyamagana Butera <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | class RFC4180Field extends php_user_filter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     use ValidatorTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     const FILTERNAME = 'league.csv.rfc4180.field'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * The value being search for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected $search; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * The replacement value that replace found $search values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     protected $replace; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * Static method to register the class as a stream filter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 6 |  |     public static function register() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 6 |  |         if (!in_array(self::FILTERNAME, stream_get_filters())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  |             stream_filter_register(self::FILTERNAME, __CLASS__); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 6 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * Static method to return the stream filter filtername | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 4 |  |     public static function getFiltername(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 4 |  |         return self::FILTERNAME; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * Static method to add the stream filter to a {@link AbstractCsv} object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param AbstractCsv $csv | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return AbstractCsv | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 6 |  |     public static function addTo(AbstractCsv $csv): AbstractCsv | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 6 |  |         self::register(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 6 |  |         return $csv->addStreamFilter(self::FILTERNAME, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 6 |  |             'enclosure' => $csv->getEnclosure(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 6 |  |             'escape' => $csv->getEscape(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 6 |  |             'mode' => $csv->getStreamFilterMode(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @inheritdoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 14 |  |     public function onCreate() | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 14 |  |         if (!isset($this->params['enclosure'], $this->params['escape'], $this->params['mode'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 2 |  |             return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 12 |  |             $enclosure = $this->filterControl($this->params['enclosure'], 'enclosure', __METHOD__); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 10 |  |             $escape = $this->filterControl($this->params['escape'], 'escape', __METHOD__); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 8 |  |             $mode = $this->filterMode($this->params['mode']); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 6 |  |             $this->search = $escape.$enclosure; | 
            
                                                                        
                            
            
                                    
            
            
                | 101 | 6 |  |             $this->replace = $enclosure.$enclosure; | 
            
                                                                        
                            
            
                                    
            
            
                | 102 | 6 |  |             if (STREAM_FILTER_WRITE === $mode) { | 
            
                                                                        
                            
            
                                    
            
            
                | 103 | 4 |  |                 $this->replace = $escape.$this->replace; | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 106 | 6 |  |             return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 107 | 6 |  |         } catch (Throwable $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 108 | 6 |  |             return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * Filter the stream filter mode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param int $mode stream filter mode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @throws InvalidArgumentException if the stream filter mode is unknown or unsupported | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 8 |  |     protected function filterMode(int $mode) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 8 |  |         static $mode_list = [STREAM_FILTER_READ => 1, STREAM_FILTER_WRITE => 1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 8 |  |         if (isset($mode_list[$mode])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 6 |  |             return $mode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 2 |  |         throw new InvalidArgumentException(sprintf('The given filter mode `%s` is unknown or unsupported', $mode)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 6 |  |     public function filter($in, $out, &$consumed, $closing) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 6 |  |         while ($bucket = stream_bucket_make_writeable($in)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 6 |  |             $bucket->data = str_replace($this->search, $this->replace, $bucket->data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 6 |  |             $consumed += $bucket->datalen; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 6 |  |             stream_bucket_append($out, $bucket); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 6 |  |         return PSFS_PASS_ON; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 144 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |  |