]>
Commit | Line | Data |
---|---|---|
4188f38a | 1 | <?php |
2 | ||
3 | /** | |
4 | * Description of FileObject | |
5 | * | |
6 | * @author Sander | |
7 | */ | |
8 | abstract class FileObject { | |
9 | private $byteLength = -1; | |
10 | ||
11 | public function __construct($byteLength = -1){ | |
12 | $this->byteLength = $byteLength; | |
13 | } | |
14 | ||
15 | public function getByteLength(){ | |
16 | if($this->byteLength >= 0){ | |
17 | return $this->byteLength; | |
18 | } | |
19 | return $this->getLength(); | |
20 | } | |
21 | ||
22 | public function getLength(){ | |
23 | throw new Exception("Sub-class needs to implement this if it doesn't have a fixed length"); | |
24 | } | |
25 | ||
26 | /** | |
27 | * Convert a string to byte format (maximum 4 bytes) | |
28 | * @param string $string Input string | |
29 | * @return int Output integer | |
30 | */ | |
31 | public function toInt($string){ | |
32 | $out = 0; | |
33 | for($i = 0, $len = min(4, strlen($string)); $i < $len; $i++){ | |
34 | $out = $out | (ord($string[$i]) << (($len-$i-1)*8)); | |
35 | } | |
36 | return $out; | |
37 | } | |
38 | ||
39 | /** | |
40 | * Convert a byte (stored in an integer) to a string | |
41 | * @param byte $int | |
42 | * @return string | |
43 | */ | |
44 | public function byteToString($int){ | |
45 | return $this->toString($int, 1); | |
46 | } | |
47 | ||
48 | /** | |
49 | * Convert a byte (stored in an integer) to a string | |
50 | * @param byte $int | |
51 | * @return string | |
52 | */ | |
53 | public function byteAsString($int){ | |
54 | return $this->asString($int, 1); | |
55 | } | |
56 | ||
57 | /** | |
58 | * Convert a short (stored in an integer) to a string | |
59 | * @param short $int | |
60 | * @return string | |
61 | */ | |
62 | public function shortToString($int){ | |
63 | return $this->toString($int, 2); | |
64 | } | |
65 | ||
66 | /** | |
67 | * Convert a short (stored in an integer) to a string | |
68 | * @param short $int | |
69 | * @return string | |
70 | */ | |
71 | public function shortAsString($int){ | |
72 | return $this->asString($int, 2); | |
73 | } | |
74 | ||
75 | /** | |
76 | * Convert a tri-byte (stored in an integer) to a string | |
77 | * @param tri-byte $int | |
78 | * @return string | |
79 | */ | |
80 | public function triToString($int){ | |
81 | return $this->toString($int, 3); | |
82 | } | |
83 | ||
84 | /** | |
85 | * Convert a tri-byte (stored in an integer) to a string | |
86 | * @param tri-byte $int | |
87 | * @return string | |
88 | */ | |
89 | public function triAsString($int){ | |
90 | return $this->asString($int, 3); | |
91 | } | |
92 | ||
93 | /** | |
94 | * Convert an integer to a string | |
95 | * @param int $int | |
96 | * @return string | |
97 | */ | |
98 | public function intToString($int){ | |
99 | return $this->toString($int, 4); | |
100 | } | |
101 | ||
102 | /** | |
103 | * Convert an integer to a string | |
104 | * @param int $int | |
105 | * @return string | |
106 | */ | |
107 | public function intAsString($int){ | |
108 | return $this->asString($int, 4); | |
109 | } | |
110 | ||
111 | /** | |
112 | * Convert a number of n bytes to a string | |
113 | * @param int $int Number that should be converted | |
114 | * @param int $size Number of bytes to convert | |
115 | * @return string Output string | |
116 | */ | |
117 | private function toString($int, $size){ | |
118 | $out = ""; | |
119 | for($i = 0; $i < $size; $i++){ | |
120 | $out = chr($int & 0xFF).$out; | |
121 | $int = $int >> 8; | |
122 | } | |
123 | return $out; | |
124 | } | |
125 | ||
126 | /** | |
127 | * Convert a number of n bytes to a string | |
128 | * @param int $int Number that should be converted | |
129 | * @param int $size Number of bytes to convert | |
130 | * @return string Output string | |
131 | */ | |
132 | private function asString($int, $size){ | |
133 | $out = ""; | |
134 | for($i = 0; $i < $size; $i++){ | |
135 | if($i > 0) $out = " ".$out; | |
136 | $byte = dechex($int & 0xFF); | |
137 | if(strlen($byte) == 1) $byte = "0".$byte; | |
138 | $out = $byte.$out; | |
139 | $int = $int >> 8; | |
140 | } | |
141 | return $out; | |
142 | } | |
143 | ||
144 | /** | |
145 | * Get the value | |
146 | * @return mixed Value to get | |
147 | */ | |
148 | abstract public function get(); | |
149 | ||
150 | /** | |
151 | * Set the value | |
152 | * @return mixed Value to set | |
153 | */ | |
154 | abstract public function set($value); | |
155 | ||
156 | /** | |
157 | * Serialize the object | |
158 | * @return string String representation | |
159 | */ | |
160 | abstract public function serialize(); | |
161 | ||
162 | /** | |
163 | * Unserialize the object | |
164 | * @param string $data String representation | |
165 | */ | |
166 | abstract public function unserialize($data); | |
167 | } | |
168 | ?> |