| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace BitWasp\Bitcoin\Serializer\Bloom; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use BitWasp\Bitcoin\Bitcoin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use BitWasp\Bitcoin\Bloom\BloomFilter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use BitWasp\Bitcoin\Serializer\Types; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use BitWasp\Buffertools\BufferInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use BitWasp\Buffertools\Buffertools; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use BitWasp\Buffertools\Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use BitWasp\Buffertools\Template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class BloomFilterSerializer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var Template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 48 |  |     private $template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 48 |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 48 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 36 |  |         $this->uint32le = Types::uint32le(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 48 |  |         $this->uint8le = Types::uint8le(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 48 |  |         $this->template = new Template([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 48 |  |             Types::vector(function (Parser $parser) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 48 |  |                 return $parser->readBytes(1)->getInt(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 48 |  |             }), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             $this->uint32le, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             $this->uint32le, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $this->uint8le | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 24 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param BloomFilter $filter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 24 |  |      * @return BufferInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 24 |  |     public function serialize(BloomFilter $filter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 24 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 24 |  |         $parser = new Parser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 12 |  |         $parser->appendBuffer(Buffertools::numToVarInt(count($filter->getData()))); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         foreach ($filter->getData() as $i) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 24 |  |             $parser->writeRawBinary(1, pack('c', $i)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 24 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 24 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 24 |  |         $parser->writeRawBinary(4, $this->uint32le->write($filter->getNumHashFuncs())); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 24 |  |         $parser->writeRawBinary(4, $this->uint32le->write($filter->getTweak())); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 12 |  |         $parser->writeRawBinary(1, $this->uint8le->write($filter->getFlags())); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return $parser->getBuffer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param Parser $parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 36 |  |      * @return BloomFilter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 36 |  |     public function fromParser(Parser $parser) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 36 |  |         list ($vData, $numHashFuncs, $nTweak, $flags) = $this->template->parse($parser); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 36 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 18 |  |         return new BloomFilter( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 18 |  |             Bitcoin::getMath(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 18 |  |             $vData, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $numHashFuncs, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 18 |  |             $nTweak, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $flags | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param string|BufferInterface $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 36 |  |      * @return BloomFilter | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 74 | 36 |  |     public function parse($data) | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |         return $this->fromParser(new Parser($data)); | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |  | 
            
                        
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: