@@ -37,7 +37,7 @@ abstract class Format {
37
37
38
38
/** @var \SplFileObject File handle */
39
39
protected $ file ;
40
- /** @var \SplFileObject File handle */
40
+ /** @var \org\majkel\dbase\memo\MemoInterface File handle */
41
41
protected $ memoFile ;
42
42
/** @var \org\majkel\dbase\Header */
43
43
protected $ header ;
@@ -53,6 +53,7 @@ abstract class Format {
53
53
/**
54
54
* @param string $filePath
55
55
* @param string $mode
56
+ * @throws \ReflectionException
56
57
*/
57
58
public function __construct ($ filePath , $ mode ) {
58
59
$ this ->mode = $ mode ;
@@ -61,6 +62,7 @@ public function __construct($filePath, $mode) {
61
62
62
63
/**
63
64
* @return \org\majkel\dbase\Header
65
+ * @throws Exception
64
66
*/
65
67
public function getHeader () {
66
68
if (is_null ($ this ->header )) {
@@ -71,6 +73,7 @@ public function getHeader() {
71
73
72
74
/**
73
75
* @return boolean
76
+ * @throws Exception
74
77
*/
75
78
public function isValid () {
76
79
return $ this ->getHeader ()->isValid ();
@@ -85,6 +88,7 @@ public function getFileInfo() {
85
88
86
89
/**
87
90
* @return \SplFileInfo
91
+ * @throws Exception
88
92
*/
89
93
public function getMemoFileInfo () {
90
94
return $ this ->getMemo ()->getFileInfo ();
@@ -100,6 +104,7 @@ public function getName() {
100
104
/**
101
105
* @param integer $index
102
106
* @return Record
107
+ * @throws Exception
103
108
*/
104
109
public function getRecord ($ index ) {
105
110
$ records = $ this ->getRecords ($ index , 1 );
@@ -117,13 +122,18 @@ public function getRecords($index, $length) {
117
122
list ($ start , $ stop ) = $ this ->getReadBoundaries ($ index , $ length );
118
123
$ file = $ this ->getFile ();
119
124
$ rSz = $ this ->getHeader ()->getRecordSize ();
120
- $ file ->fseek ($ this ->getHeader ()->getHeaderSize () + $ start * $ rSz );
125
+ $ offset = $ this ->getHeader ()->getHeaderSize () + $ start * $ rSz ;
126
+ $ file ->fseek ($ offset );
121
127
$ format = $ this ->getRecordFormat ();
122
128
$ allData = $ file ->fread ($ rSz * $ length );
123
129
$ records = array ();
124
130
for ($ i = 0 ; $ start < $ stop ; ++$ start , ++$ i ) {
125
- $ data = unpack ($ format , strlen ($ allData ) === $ rSz
126
- ? $ allData : substr ($ allData , $ i * $ rSz , $ rSz ));
131
+ if (strlen ($ allData ) === $ rSz ) {
132
+ $ recordData = $ allData ;
133
+ } else {
134
+ $ recordData = substr ($ allData , $ i * $ rSz , $ rSz );
135
+ }
136
+ $ data = unpack ($ format , $ recordData );
127
137
$ records [$ start ] = $ this ->createRecord ($ data );
128
138
}
129
139
return $ records ;
@@ -227,8 +237,8 @@ public function isTransaction() {
227
237
}
228
238
229
239
/**
230
- * @return HeaderInterface
231
240
* @return boolean
241
+ * @throws Exception
232
242
*/
233
243
protected function checkIfTransaction () {
234
244
$ currentHeader = $ this ->readHeader ();
@@ -241,6 +251,7 @@ protected function checkIfTransaction() {
241
251
242
252
/**
243
253
* @param boolean $enabled
254
+ * @throws Exception
244
255
*/
245
256
protected function setTransactionStatus ($ enabled ) {
246
257
$ enabled = (boolean ) $ enabled ;
@@ -312,6 +323,7 @@ public function markDeleted($index, $deleted) {
312
323
313
324
/**
314
325
* @return string
326
+ * @throws Exception
315
327
*/
316
328
protected function getWriteRecordFormat () {
317
329
if (is_null ($ this ->writeRecordFormat )) {
@@ -351,6 +363,7 @@ protected function serializeRecord(Record $record) {
351
363
/**
352
364
* @param integer $index
353
365
* @return integer
366
+ * @throws Exception
354
367
*/
355
368
private function getRecordOffset ($ index ) {
356
369
return $ index * $ this ->getHeader ()->getRecordSize () + $ this ->getHeader ()->getHeaderSize ();
@@ -359,6 +372,7 @@ private function getRecordOffset($index) {
359
372
/**
360
373
* @param \org\majkel\dbase\Record $data
361
374
* @return integer
375
+ * @throws Exception
362
376
*/
363
377
public function insert (Record $ data ) {
364
378
$ header = $ this ->getHeader ();
@@ -378,9 +392,10 @@ public function insert(Record $data) {
378
392
}
379
393
380
394
/**
381
- * @param integer $index
395
+ * @param integer $index
382
396
* @param \org\majkel\dbase\Record $data
383
397
* @return void
398
+ * @throws Exception
384
399
*/
385
400
public function update ($ index , Record $ data ) {
386
401
list ($ offset ) = $ this ->getReadBoundaries ($ index , 0 );
@@ -405,6 +420,7 @@ protected function getWriteHeaderFormat() {
405
420
406
421
/**
407
422
* @return void
423
+ * @throws Exception
408
424
*/
409
425
protected function writeHeader () {
410
426
$ file = $ this ->getFile ();
@@ -427,6 +443,7 @@ protected function writeHeader() {
427
443
428
444
/**
429
445
* @return Header
446
+ * @throws Exception
430
447
*/
431
448
protected function readHeader () {
432
449
$ file = $ this ->getFile ();
@@ -497,6 +514,7 @@ protected function createField($data) {
497
514
498
515
/**
499
516
* @return string
517
+ * @throws Exception
500
518
*/
501
519
protected function getRecordFormat () {
502
520
if (is_null ($ this ->recordFormat )) {
@@ -512,6 +530,7 @@ protected function getRecordFormat() {
512
530
/**
513
531
* @param integer $index
514
532
* @return string
533
+ * @throws Exception
515
534
*/
516
535
protected function readMemoEntry ($ index ) {
517
536
return $ this ->getMemo ()->getEntry ($ index );
@@ -520,6 +539,7 @@ protected function readMemoEntry($index) {
520
539
/**
521
540
* @param array $data
522
541
* @return \org\majkel\dbase\Record
542
+ * @throws Exception
523
543
*/
524
544
protected function createRecord ($ data ) {
525
545
$ record = new Record ;
@@ -553,6 +573,7 @@ abstract public function getType();
553
573
/**
554
574
* @param \org\majkel\dbase\Header $header
555
575
* @return $this
576
+ * @throws Exception
556
577
*/
557
578
public function create (Header $ header ) {
558
579
$ this ->getFile ()->ftruncate (0 );
@@ -575,6 +596,7 @@ public function create(Header $header) {
575
596
576
597
/**
577
598
* @return integer
599
+ * @throws Exception
578
600
*/
579
601
protected function calculateRecordSize () {
580
602
$ result = 1 ;
@@ -586,6 +608,7 @@ protected function calculateRecordSize() {
586
608
587
609
/**
588
610
* @return integer
611
+ * @throws Exception
589
612
*/
590
613
protected function calculateHeaderSize () {
591
614
$ result = self ::HEADER_SIZE + $ this ->getHeader ()->getFieldsCount () * self ::FIELD_SIZE + 2 ;
@@ -601,6 +624,7 @@ protected function getWriteFieldFormat() {
601
624
602
625
/**
603
626
* @return void
627
+ * @throws Exception
604
628
*/
605
629
protected function writeRecords () {
606
630
$ file = $ this ->getFile ();
0 commit comments