| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace OneSheet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use OneSheet\Xml\CellXml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Class DefaultCellBuilder to build xml cell strings. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Wheter a numeric value is written as a number or string type cell | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * is simply determined by type, to allow for some control by typecasting. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @package OneSheet | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | class CellBuilder | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * Array of control characters that should be escaped. | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     private $controlCharacters = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * Array of escape characters to replace control characters. | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     private $escapeCharacters = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * CellBuilder constructor to create escaping arrays. | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 5 |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 5 |  |         foreach (range(chr(0), chr(31)) as $key => $character) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 5 |  |             if (!in_array($character, array("\n", "\r", "\t"))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 5 |  |                 $this->controlCharacters[] = $character; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 5 |  |                 $this->escapeCharacters[] = sprintf('_x%04s_', strtoupper(dechex($key))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 5 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 5 |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 | 5 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * Build and return the string for a single cell. | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @param int   $rowNumber | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @param int   $cellNumber | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @param mixed $cellValue | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @param int   $styleId | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |     public function build($rowNumber, $cellNumber, $cellValue, $styleId = 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  |         $cellId = $this->getCellId($cellNumber, $rowNumber); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  |         if (is_int($cellValue) || is_double($cellValue)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |             return sprintf(CellXml::NUMBER_XML, $cellId, $styleId, $cellValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  |         } elseif (is_numeric($cellValue) || 1 != preg_match('~[^\w]~', $cellValue)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2 |  |             return sprintf(CellXml::STRING_XML, $cellId, $styleId, htmlspecialchars($cellValue, ENT_QUOTES)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } elseif (is_bool($cellValue)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             return sprintf(CellXml::BOOLEAN_XML, $cellId, $styleId, (int)$cellValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         return sprintf(CellXml::STRING_XML, $cellId, $styleId, $this->escape($cellValue)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * Turn a integer cell number + row number into a valid cell identifier | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * like e.g. A1, Z1, AA1 etc and return it. | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      * @param int      $cellNumber | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      * @param int|null $rowNumber | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 2 |  |     public function getCellId($cellNumber, $rowNumber = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 2 |  |         if ($cellNumber / 26 < 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |             return chr(65 + $cellNumber) . $rowNumber; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |         return $this->getCellId(floor($cellNumber / 26) - 1) . chr(65 + $cellNumber % 26) . $rowNumber; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * Escape/replace control characters characters. | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * @param string $value | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     private function escape($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         return str_replace($this->controlCharacters, $this->escapeCharacters, htmlspecialchars($value, ENT_QUOTES)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 94 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  | 
            
                        
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.