| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * CSVelte: Slender, elegant CSV for PHP. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Inspired by Python's CSV module and Frictionless Data and the W3C's CSV | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * standardization efforts, CSVelte was written in an effort to take all the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * suck out of working with CSV. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @version   v0.2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @copyright Copyright (c) 2016 Luke Visinoni <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @author    Luke Visinoni <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @license   https://github.com/deni-zen/csvelte/blob/master/LICENSE The MIT License (MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace CSVelte\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use CSVelte\Exception\IOException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * IO IsReadable Trait. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * Read methods shared between CSVelte\IO classes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @package    CSVelte | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @subpackage CSVelte\Traits | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @copyright  (c) 2016, Luke Visinoni <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @author     Luke Visinoni <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @since      v0.2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | trait IsReadable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Read single line. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Read the next line from the file (moving the internal pointer down a line). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * Returns multiple lines if newline character(s) fall within a quoted string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param string|array A string or array of strings to be used as EOL char/sequence | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param int Maximum number of bytes to return (line will be truncated to this -1 if set) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return string A single line read from the file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @throws CSVelte\Exception\IOException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @todo Should this add a newline if maxlength is reached? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @todo I could actually buffer this by reading x chars at a time and doing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      *     the same thing with looping char by char if this is too IO intensive. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 19 |  |     public function readLine($eol = PHP_EOL, $maxLength = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 19 |  |         $size = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 19 |  |         $buffer = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 19 |  |         if (!is_array($eol)) $eol = array($eol); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 19 |  |         while (!$this->eof()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             // Using a loose equality here to match on '' and false. | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 19 |  |             if (null == ($byte = $this->read(1))) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 1 |  |                 return $buffer; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 19 |  |             $buffer .= $byte; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             // Break when a new line is found or the max length - 1 is reached | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 19 |  |             if (array_reduce($eol, function($carry, $eol) use ($buffer) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 19 |  |                 if (!$carry) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 19 |  |                     $eollen = 0 - strlen($eol); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 19 |  |                     return (substr($buffer, $eollen) === $eol); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 1 |  |                 return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 19 |  |             }, false) || ++$size === $maxLength - 1) { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 17 |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 19 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 18 |  |         return $buffer; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * Assert that this file/stream object is readable. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @throws CSVelte\Exception\IOException if stream isn't readable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 24 |  |     protected function assertIsReadable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 24 |  |         if (!$this->isReadable()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |             throw new IOException("Stream not readable: " . $this->getName(), IOException::ERR_NOT_READABLE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 23 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     abstract public function getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     abstract public function isReadable(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     abstract public function read($length); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     abstract public function eof(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  |