]>
Commit | Line | Data |
---|---|---|
4188f38a | 1 | <?php |
2 | ||
3 | /** | |
4 | * Description of FileString | |
5 | * | |
6 | * @author Sander | |
7 | */ | |
8 | class FileString extends FileObject { | |
9 | private $forcedLength; | |
10 | private $data; | |
11 | ||
12 | /** | |
13 | * Make a string to be stored in a file | |
14 | * @param string|int $first Optional, if it is a string, it will be the contents, | |
15 | * if it is a number, it will set the forced length. | |
16 | * @param int $second Optional, will set the forced length. Can only be used when the | |
17 | * first argument is contents. | |
18 | */ | |
19 | public function __construct($first = null, $second = null){ | |
20 | $this->forcedLength = -1; | |
21 | $this->data = ""; | |
22 | ||
23 | if($second != null){ | |
24 | $this->data = $first; | |
25 | $this->forcedLength = $second; | |
26 | }else if($first != null){ | |
27 | if(is_string($first)){ | |
28 | $this->data = $first; | |
29 | }else{ | |
30 | $this->forcedLength = $first; | |
31 | } | |
32 | } | |
33 | } | |
34 | ||
35 | public function getByteLength(){ | |
36 | return $this->getLength(); | |
37 | } | |
38 | ||
39 | public function getLength(){ | |
40 | if($this->forcedLength >= 0){ | |
41 | return $this->forcedLength; | |
42 | } | |
43 | return strlen($this->data); | |
44 | } | |
45 | ||
46 | public function get(){ | |
47 | return $this->data; | |
48 | } | |
49 | ||
50 | public function set($value){ | |
51 | $this->data = $value; | |
52 | } | |
53 | ||
54 | public function serialize() { | |
55 | $output = $this->data; | |
56 | $curLength = strlen($output); | |
57 | ||
58 | if($this->forcedLength >= 0){ | |
59 | if($this->forcedLength > $curLength){ | |
60 | return str_pad($output, $this->forcedLength, "\0", STR_PAD_RIGHT); | |
61 | }elseif($this->forcedLength == $curLength){ | |
62 | return $output; | |
63 | }else{ | |
64 | return substr($output, 0, $this->forcedLength); | |
65 | } | |
66 | } | |
67 | return $output; | |
68 | } | |
69 | ||
70 | public function unserialize($data) { | |
71 | __construct($data); | |
72 | } | |
73 | ||
74 | public function __toString(){ | |
75 | $out = "FileString"; | |
76 | if($this->forcedLength >= 0){ | |
77 | $out .= " ".$this->forcedLength; | |
78 | } | |
79 | $out .= ": {\"".str_replace(array(" ", "\0"), " ", $this->serialize())."\"}"; | |
80 | return $out; | |
81 | } | |
82 | } | |
83 | ?> |