| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the rybakit/msgpack.php package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Eugene Leonovich <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace MessagePack; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use MessagePack\Exception\PackingFailedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class Packer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 74 |  |     public function pack($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 74 |  |         $type = gettype($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         switch ($type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 74 |  |             case 'array': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 13 |  |                 return array_values($value) === $value ? $this->packArr($value) : $this->packMap($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 73 |  |             case 'string': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 23 |  |                 return preg_match('//u', $value) ? $this->packStr($value) : $this->packBin($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 56 |  |             case 'integer': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 41 |  |                 return $this->packInt($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 19 |  |             case 'NULL': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 3 |  |                 return $this->packNil(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 18 |  |             case 'boolean': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 6 |  |                 return $this->packBool($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 13 |  |             case 'double': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 3 |  |                 return $this->packDouble($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 10 |  |         if ($value instanceof Ext) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 9 |  |             return $this->packExt($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |         throw new PackingFailedException($value, 'Unsupported type.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 7 |  |     public function packArr(array $array) | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 7 |  |         $size = count($array); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 7 |  |         $data = self::packArrHeader($size); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 7 |  |         foreach ($array as $val) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 6 |  |             $data .= $this->pack($val); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 7 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 7 |  |         return $data; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 56 | 7 | View Code Duplication |     private static function packArrHeader($size) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 7 |  |         if ($size <= 0xf) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 4 |  |             return chr(0x90 | $size); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 3 |  |         if ($size <= 0xffff) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |             return pack('Cn', 0xdc, $size); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |         return pack('CN', 0xdd, $size); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 8 |  |     public function packMap(array $map) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 8 |  |         $size = count($map); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 8 |  |         $data = self::packMapHeader($size); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 8 |  |         foreach ($map as $key => $val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 8 |  |             $data .= $this->pack($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 8 |  |             $data .= $this->pack($val); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 8 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 8 |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 82 | 8 | View Code Duplication |     private static function packMapHeader($size) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 8 |  |         if ($size <= 0xf) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 5 |  |             return chr(0x80 | $size); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 3 |  |         if ($size <= 0xffff) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  |             return pack('Cn', 0xde, $size); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |         return pack('CN', 0xdf, $size); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 17 |  |     public function packStr($str) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 17 |  |         $len = strlen($str); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 17 |  |         if ($len < 32) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 10 |  |             return chr(0xa0 | $len).$str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 7 |  |         if ($len <= 0xff) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 4 |  |             return pack('CC', 0xd9, $len).$str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 3 |  |         if ($len <= 0xffff) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 2 |  |             return pack('Cn', 0xda, $len).$str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |         return pack('CN', 0xdb, $len).$str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 8 |  |     public function packBin($str) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 8 |  |         $len = strlen($str); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 8 |  |         if ($len <= 0xff) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 6 |  |             return pack('CC', 0xc4, $len).$str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 2 |  |         if ($len <= 0xffff) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  |             return pack('Cn', 0xc5, $len).$str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 1 |  |         return pack('CN', 0xc6, $len).$str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 9 |  |     public function packExt(Ext $ext) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 9 |  |         $type = $ext->getType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 9 |  |         $data = $ext->getData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 9 |  |         $len = strlen($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         switch ($len) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 9 |  |             case 1: return pack('CC', 0xd4, $type).$data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 8 |  |             case 2: return pack('CC', 0xd5, $type).$data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 7 |  |             case 4: return pack('CC', 0xd6, $type).$data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 6 |  |             case 8: return pack('CC', 0xd7, $type).$data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 5 |  |             case 16: return pack('CC', 0xd8, $type).$data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 4 |  |         if ($len <= 0xff) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 2 |  |             return pack('CCC', 0xc7, $len, $type).$data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 2 |  |         if ($len <= 0xffff) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 1 |  |             return pack('CnC', 0xc8, $len, $type).$data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 1 |  |         return pack('CNC', 0xc9, $len, $type).$data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 3 |  |     public function packNil() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 3 |  |         return "\xc0"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 6 |  |     public function packBool($val) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 6 |  |         return $val ? "\xc3" : "\xc2"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 3 |  |     public function packDouble($num) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 3 |  |         return "\xcb".strrev(pack('d', $num)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 41 |  |     public function packInt($num) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 41 |  |         if ($num >= 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 26 |  |             if ($num <= 0x7f) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 14 |  |                 return chr($num); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 16 |  |             if ($num <= 0xff) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 6 |  |                 return pack('CC', 0xcc, $num); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 12 |  |             if ($num <= 0xffff) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 6 |  |                 return pack('Cn', 0xcd, $num); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 7 |  |             if ($num <= 0xffffffff) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 5 |  |                 return pack('CN', 0xce, $num); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 3 |  |             return self::packU64(0xcf, $num); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 | 15 |  |         if ($num >= -0x20) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 3 |  |             return chr(0xe0 | $num); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 | 12 |  |         if ($num >= -0x80) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 | 3 |  |             return pack('CC', 0xd0, $num); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 | 9 |  |         if ($num >= -0x8000) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 | 3 |  |             return pack('Cn', 0xd1, $num); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 6 |  |         if ($num >= -0x80000000) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 | 3 |  |             return pack('CN', 0xd2, $num); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 | 3 |  |         return self::packU64(0xd3, $num); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 | 6 |  |     private static function packU64($code, $num) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 | 6 |  |         $hi = ($num & 0xffffffff00000000) >> 32; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 | 6 |  |         $lo = $num & 0x00000000ffffffff; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 | 6 |  |         return pack('CNN', $code, $hi, $lo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 207 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 208 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.