1 | <?php |
||
12 | class BlockHeaderSerializer |
||
13 | { |
||
14 | 414 | public function __construct() |
|
15 | { |
||
16 | 414 | $this->hash = Types::bytestringle(32); |
|
|
|||
17 | 414 | $this->uint32le = Types::uint32le(); |
|
18 | 414 | $this->int32le = Types::int32le(); |
|
19 | 414 | } |
|
20 | |||
21 | /** |
||
22 | * @param \BitWasp\Buffertools\BufferInterface|string $string |
||
23 | * @return BlockHeader |
||
24 | * @throws ParserOutOfRange |
||
25 | */ |
||
26 | 10 | public function parse($string) |
|
27 | { |
||
28 | 10 | return $this->fromParser(new Parser($string)); |
|
29 | } |
||
30 | |||
31 | /** |
||
32 | * @param Parser $parser |
||
33 | * @return BlockHeader |
||
34 | * @throws ParserOutOfRange |
||
35 | */ |
||
36 | 36 | public function fromParser(Parser $parser) |
|
37 | { |
||
38 | try { |
||
39 | 36 | return new BlockHeader( |
|
40 | 36 | $this->int32le->read($parser), |
|
41 | 32 | $this->hash->read($parser), |
|
42 | 32 | $this->hash->read($parser), |
|
43 | 30 | (int) $this->uint32le->read($parser), |
|
44 | 30 | (int) $this->uint32le->read($parser), |
|
45 | 30 | (int) $this->uint32le->read($parser) |
|
46 | ); |
||
47 | 6 | } catch (ParserOutOfRange $e) { |
|
48 | 6 | throw new ParserOutOfRange('Failed to extract full block header from parser'); |
|
49 | } |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param BlockHeaderInterface $header |
||
54 | * @return \BitWasp\Buffertools\BufferInterface |
||
55 | */ |
||
56 | 396 | public function serialize(BlockHeaderInterface $header) |
|
67 | } |
||
68 |
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: