1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the stream package |
4
|
|
|
* |
5
|
|
|
* @author Daniel Schröder <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace GravityMedia\Stream\Reader; |
9
|
|
|
|
10
|
|
|
use GravityMedia\Stream\Enum\Endian; |
11
|
|
|
use GravityMedia\Stream\Exception; |
12
|
|
|
use GravityMedia\Stream\StreamInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Signed short (16-bit integer) reader |
16
|
|
|
* |
17
|
|
|
* @package GravityMedia\Stream\Reader |
18
|
|
|
*/ |
19
|
|
|
class SignedShortReader |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
protected static $endianess; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var StreamInterface |
28
|
|
|
*/ |
29
|
|
|
protected $stream; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $endian; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create signed short (16-bit integer) reader object |
38
|
|
|
* |
39
|
|
|
* @throws Exception\InvalidArgumentException An exception will be thrown for non-readable streams or an invalid |
40
|
|
|
* endian value |
41
|
|
|
* |
42
|
|
|
* @param StreamInterface $stream |
43
|
|
|
* @param int $endian |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function __construct(StreamInterface $stream, $endian) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
if (!$stream->isReadable()) { |
48
|
|
|
throw new Exception\InvalidArgumentException('Stream not readable'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (!in_array($endian, [Endian::ENDIAN_BIG, Endian::ENDIAN_LITTLE])) { |
52
|
|
|
throw new Exception\InvalidArgumentException('Invalid endian'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->stream = $stream; |
56
|
|
|
$this->endian = $endian; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get stream |
61
|
|
|
* |
62
|
|
|
* @return StreamInterface |
63
|
|
|
*/ |
64
|
|
|
public function getStream() |
65
|
|
|
{ |
66
|
|
|
return $this->stream; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get endian |
71
|
|
|
* |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
|
|
public function getEndian() |
75
|
|
|
{ |
76
|
|
|
return $this->endian; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get endianess |
81
|
|
|
* |
82
|
|
|
* @return int |
83
|
|
|
*/ |
84
|
|
|
protected function getEndianess() |
85
|
|
|
{ |
86
|
|
|
if (null === static::$endianess) { |
87
|
|
|
$data = unpack('l*', "\x01\x00\x00\x00"); |
88
|
|
|
|
89
|
|
|
static::$endianess = Endian::ENDIAN_BIG; |
90
|
|
|
if (1 === $data[1]) { |
91
|
|
|
static::$endianess = Endian::ENDIAN_LITTLE; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return static::$endianess; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Read string data from the stream |
100
|
|
|
* |
101
|
|
|
* @throws Exception\IOException An exception will be thrown for invalid stream resources or when the data could |
102
|
|
|
* not be read |
103
|
|
|
* |
104
|
|
|
* @return int |
105
|
|
|
*/ |
106
|
|
|
public function read() |
107
|
|
|
{ |
108
|
|
|
$data = unpack('s', $this->stream->read(2)); |
109
|
|
|
|
110
|
|
|
if (Endian::ENDIAN_BIG === $this->getEndianess() && Endian::ENDIAN_LITTLE === $this->endian) { |
111
|
|
|
$hex = dechex($data[1]); |
112
|
|
|
|
113
|
|
|
if (strlen($hex) <= 2) { |
114
|
|
|
return $data[1]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$data = unpack('H*', strrev(pack('H*', $hex))); |
118
|
|
|
return hexdec($data[1]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $data[1]; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
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.