]>
Commit | Line | Data |
---|---|---|
4188f38a | 1 | <?php |
2 | ||
3 | /** | |
4 | * Helper class to help with creating records from a | |
5 | * long data stream | |
6 | * | |
7 | * @author Sander | |
8 | */ | |
9 | class RecordFactory { | |
10 | /** | |
11 | * Settings for the record factory | |
12 | * @var Settings | |
13 | */ | |
14 | private $settings; | |
15 | ||
16 | /** | |
17 | * Create the helper class | |
18 | * @param Settings $settings The Settings to be used for the records | |
19 | */ | |
20 | public function __construct($settings){ | |
21 | $this->settings = $settings; | |
22 | } | |
23 | ||
24 | /** | |
25 | * Create records from a data string | |
26 | * @param string $data | |
27 | * @return array(Record) | |
28 | */ | |
29 | public function createRecords($data){ | |
30 | $records = array(); | |
31 | $size = $this->settings->get("recordSize"); | |
32 | $compression = $this->settings->get("compression"); | |
33 | ||
34 | $dataEntries = mb_str_split($data, $size); | |
35 | ||
36 | for($i = 0, $len = sizeof($dataEntries); $i < $len; $i++){ | |
37 | $records[$i] = new Record($dataEntries[$i]); | |
38 | $records[$i]->compress($compression); | |
39 | } | |
40 | ||
41 | return $records; | |
42 | } | |
43 | ||
44 | public function createEOFRecord(){ | |
45 | return new Record(0xe98e0d0a); | |
46 | } | |
47 | ||
48 | public function createFCISRecord($textLength){ | |
49 | $r = "FCIS"; | |
50 | $r .= $this->asString(20, 4); | |
51 | $r .= $this->asString(16, 4); | |
52 | $r .= $this->asString(1, 4); | |
53 | $r .= $this->asString(0, 4); | |
54 | $r .= $this->asString($textLength, 4); | |
55 | $r .= $this->asString(0, 4); | |
56 | $r .= $this->asString(32, 4); | |
57 | $r .= $this->asString(8, 4); | |
58 | $r .= $this->asString(1, 2); | |
59 | $r .= $this->asString(1, 2); | |
60 | $r .= $this->asString(0, 4); | |
61 | return new Record($r); | |
62 | } | |
63 | ||
64 | public function createFLISRecord(){ | |
65 | ||
66 | $this->elements = new FileElement(array( | |
67 | "offsetL0"=>new FileString("FLIS", 4), //FLIS | |
68 | "offsetL4"=>new FileInt(0x08), | |
69 | "offsetL8"=>new FileShort(0x41), | |
70 | "offsetL10"=>new FileTri(), | |
71 | "offsetL16"=>new FileInt(0xFFFFFFFF), | |
72 | "offsetL20"=>new FileShort(0x01), | |
73 | "offsetL22"=>new FileShort(0x03), | |
74 | "offsetL24"=>new FileInt(0x03), | |
75 | "offsetL28"=>new FileInt(0x01), | |
76 | "offsetL32"=>new FileInt(0xFFFFFFFF) | |
77 | )); | |
78 | ||
79 | /*$r = "FLIS"; | |
80 | $r .= $this->asString(8, 4); | |
81 | $r .= $this->asString(65, 2); | |
82 | $r .= $this->asString(0, 2); | |
83 | $r .= $this->asString(0, 4); | |
84 | $r .= $this->asString(-1, 4); | |
85 | $r .= $this->asString(1, 2); | |
86 | $r .= $this->asString(3, 2); | |
87 | $r .= $this->asString(3, 4); | |
88 | $r .= $this->asString(1, 4); | |
89 | $r .= $this->asString(-1, 4); | |
90 | return new Record($r);*/ | |
91 | } | |
92 | ||
93 | private function asString($int, $size){ | |
94 | $out = ""; | |
95 | for($i = 0; $i < $size; $i++){ | |
96 | if($i > 0) $out = " ".$out; | |
97 | $byte = dechex($int & 0xFF); | |
98 | if(strlen($byte) == 1) $byte = "0".$byte; | |
99 | $out = $byte.$out; | |
100 | $int = $int >> 8; | |
101 | } | |
102 | return $out; | |
103 | } | |
104 | ||
105 | public function __toString() { | |
106 | $out = "Record Factory: {\n"; | |
107 | $out .= "\tRecord Size: ".$this->settings->get("recordSize")."\n"; | |
108 | $out .= "\tCompression: ".$this->settings->get("compression")."\n"; | |
109 | $out .= "}"; | |
110 | return $out; | |
111 | } | |
112 | } | |
113 | function mb_str_split($string, $split_length = 1){ | |
114 | mb_internal_encoding('UTF-8'); | |
115 | mb_regex_encoding('UTF-8'); | |
116 | ||
117 | $split_length = ($split_length <= 0) ? 1 : $split_length; | |
118 | ||
119 | $mb_strlen = mb_strlen($string, 'utf-8'); | |
120 | ||
121 | $array = array(); | |
122 | ||
123 | for($i = 0; $i < $mb_strlen; $i += $split_length){ | |
124 | $array[] = mb_substr($string, $i, $split_length); | |
125 | } | |
126 | ||
127 | return $array; | |
128 | } | |
129 | ?> |