@@ -37,7 +37,7 @@ abstract class Format {
3737
3838 /** @var \SplFileObject File handle */
3939 protected $ file ;
40- /** @var \SplFileObject File handle */
40+ /** @var \org\majkel\dbase\memo\MemoInterface File handle */
4141 protected $ memoFile ;
4242 /** @var \org\majkel\dbase\Header */
4343 protected $ header ;
@@ -53,6 +53,7 @@ abstract class Format {
5353 /**
5454 * @param string $filePath
5555 * @param string $mode
56+ * @throws \ReflectionException
5657 */
5758 public function __construct ($ filePath , $ mode ) {
5859 $ this ->mode = $ mode ;
@@ -61,6 +62,7 @@ public function __construct($filePath, $mode) {
6162
6263 /**
6364 * @return \org\majkel\dbase\Header
65+ * @throws Exception
6466 */
6567 public function getHeader () {
6668 if (is_null ($ this ->header )) {
@@ -71,6 +73,7 @@ public function getHeader() {
7173
7274 /**
7375 * @return boolean
76+ * @throws Exception
7477 */
7578 public function isValid () {
7679 return $ this ->getHeader ()->isValid ();
@@ -85,6 +88,7 @@ public function getFileInfo() {
8588
8689 /**
8790 * @return \SplFileInfo
91+ * @throws Exception
8892 */
8993 public function getMemoFileInfo () {
9094 return $ this ->getMemo ()->getFileInfo ();
@@ -100,6 +104,7 @@ public function getName() {
100104 /**
101105 * @param integer $index
102106 * @return Record
107+ * @throws Exception
103108 */
104109 public function getRecord ($ index ) {
105110 $ records = $ this ->getRecords ($ index , 1 );
@@ -117,13 +122,18 @@ public function getRecords($index, $length) {
117122 list ($ start , $ stop ) = $ this ->getReadBoundaries ($ index , $ length );
118123 $ file = $ this ->getFile ();
119124 $ rSz = $ this ->getHeader ()->getRecordSize ();
120- $ file ->fseek ($ this ->getHeader ()->getHeaderSize () + $ start * $ rSz );
125+ $ offset = $ this ->getHeader ()->getHeaderSize () + $ start * $ rSz ;
126+ $ file ->fseek ($ offset );
121127 $ format = $ this ->getRecordFormat ();
122128 $ allData = $ file ->fread ($ rSz * $ length );
123129 $ records = array ();
124130 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 );
127137 $ records [$ start ] = $ this ->createRecord ($ data );
128138 }
129139 return $ records ;
@@ -227,8 +237,8 @@ public function isTransaction() {
227237 }
228238
229239 /**
230- * @return HeaderInterface
231240 * @return boolean
241+ * @throws Exception
232242 */
233243 protected function checkIfTransaction () {
234244 $ currentHeader = $ this ->readHeader ();
@@ -241,6 +251,7 @@ protected function checkIfTransaction() {
241251
242252 /**
243253 * @param boolean $enabled
254+ * @throws Exception
244255 */
245256 protected function setTransactionStatus ($ enabled ) {
246257 $ enabled = (boolean ) $ enabled ;
@@ -312,6 +323,7 @@ public function markDeleted($index, $deleted) {
312323
313324 /**
314325 * @return string
326+ * @throws Exception
315327 */
316328 protected function getWriteRecordFormat () {
317329 if (is_null ($ this ->writeRecordFormat )) {
@@ -351,6 +363,7 @@ protected function serializeRecord(Record $record) {
351363 /**
352364 * @param integer $index
353365 * @return integer
366+ * @throws Exception
354367 */
355368 private function getRecordOffset ($ index ) {
356369 return $ index * $ this ->getHeader ()->getRecordSize () + $ this ->getHeader ()->getHeaderSize ();
@@ -359,6 +372,7 @@ private function getRecordOffset($index) {
359372 /**
360373 * @param \org\majkel\dbase\Record $data
361374 * @return integer
375+ * @throws Exception
362376 */
363377 public function insert (Record $ data ) {
364378 $ header = $ this ->getHeader ();
@@ -378,9 +392,10 @@ public function insert(Record $data) {
378392 }
379393
380394 /**
381- * @param integer $index
395+ * @param integer $index
382396 * @param \org\majkel\dbase\Record $data
383397 * @return void
398+ * @throws Exception
384399 */
385400 public function update ($ index , Record $ data ) {
386401 list ($ offset ) = $ this ->getReadBoundaries ($ index , 0 );
@@ -405,6 +420,7 @@ protected function getWriteHeaderFormat() {
405420
406421 /**
407422 * @return void
423+ * @throws Exception
408424 */
409425 protected function writeHeader () {
410426 $ file = $ this ->getFile ();
@@ -427,6 +443,7 @@ protected function writeHeader() {
427443
428444 /**
429445 * @return Header
446+ * @throws Exception
430447 */
431448 protected function readHeader () {
432449 $ file = $ this ->getFile ();
@@ -497,6 +514,7 @@ protected function createField($data) {
497514
498515 /**
499516 * @return string
517+ * @throws Exception
500518 */
501519 protected function getRecordFormat () {
502520 if (is_null ($ this ->recordFormat )) {
@@ -512,6 +530,7 @@ protected function getRecordFormat() {
512530 /**
513531 * @param integer $index
514532 * @return string
533+ * @throws Exception
515534 */
516535 protected function readMemoEntry ($ index ) {
517536 return $ this ->getMemo ()->getEntry ($ index );
@@ -520,6 +539,7 @@ protected function readMemoEntry($index) {
520539 /**
521540 * @param array $data
522541 * @return \org\majkel\dbase\Record
542+ * @throws Exception
523543 */
524544 protected function createRecord ($ data ) {
525545 $ record = new Record ;
@@ -553,6 +573,7 @@ abstract public function getType();
553573 /**
554574 * @param \org\majkel\dbase\Header $header
555575 * @return $this
576+ * @throws Exception
556577 */
557578 public function create (Header $ header ) {
558579 $ this ->getFile ()->ftruncate (0 );
@@ -575,6 +596,7 @@ public function create(Header $header) {
575596
576597 /**
577598 * @return integer
599+ * @throws Exception
578600 */
579601 protected function calculateRecordSize () {
580602 $ result = 1 ;
@@ -586,6 +608,7 @@ protected function calculateRecordSize() {
586608
587609 /**
588610 * @return integer
611+ * @throws Exception
589612 */
590613 protected function calculateHeaderSize () {
591614 $ result = self ::HEADER_SIZE + $ this ->getHeader ()->getFieldsCount () * self ::FIELD_SIZE + 2 ;
@@ -601,6 +624,7 @@ protected function getWriteFieldFormat() {
601624
602625 /**
603626 * @return void
627+ * @throws Exception
604628 */
605629 protected function writeRecords () {
606630 $ file = $ this ->getFile ();
0 commit comments