]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/libraries/MOBIClass/RecordFactory.php
phpepub via composer
[github/wallabag/wallabag.git] / inc / 3rdparty / libraries / MOBIClass / RecordFactory.php
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 $r = "FLIS";
66 $r .= $this->asString(8, 4);
67 $r .= $this->asString(65, 2);
68 $r .= $this->asString(0, 2);
69 $r .= $this->asString(0, 4);
70 $r .= $this->asString(-1, 4);
71 $r .= $this->asString(1, 2);
72 $r .= $this->asString(3, 2);
73 $r .= $this->asString(3, 4);
74 $r .= $this->asString(1, 4);
75 $r .= $this->asString(-1, 4);
76 return new Record($r);
77 }
78
79 private function asString($int, $size){
80 $out = "";
81 for($i = 0; $i < $size; $i++){
82 if($i > 0) $out = " ".$out;
83 $byte = dechex($int & 0xFF);
84 if(strlen($byte) == 1) $byte = "0".$byte;
85 $out = $byte.$out;
86 $int = $int >> 8;
87 }
88 return $out;
89 }
90
91 public function __toString() {
92 $out = "Record Factory: {\n";
93 $out .= "\tRecord Size: ".$this->settings->get("recordSize")."\n";
94 $out .= "\tCompression: ".$this->settings->get("compression")."\n";
95 $out .= "}";
96 return $out;
97 }
98 }
99 function mb_str_split($string, $split_length = 1){
100 mb_internal_encoding('UTF-8');
101 mb_regex_encoding('UTF-8');
102
103 $split_length = ($split_length <= 0) ? 1 : $split_length;
104
105 $mb_strlen = mb_strlen($string, 'utf-8');
106
107 $array = array();
108
109 for($i = 0; $i < $mb_strlen; $i += $split_length){
110 $array[] = mb_substr($string, $i, $split_length);
111 }
112
113 return $array;
114 }
115 ?>