From 4cfb71f1f5c302061a7188081a17f0366a2da963 Mon Sep 17 00:00:00 2001 From: Silvio Date: Thu, 19 Aug 2010 16:29:40 -0300 Subject: Adding IsisEntryIterator --- apps/field.php | 4 +- apps/repetition.php | 4 +- apps/subfield.php | 4 +- classes/IsisFinder.php | 51 ++++++------------------- classes/iterators/IsisEntryIterator.php | 66 +++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 45 deletions(-) create mode 100644 classes/iterators/IsisEntryIterator.php diff --git a/apps/field.php b/apps/field.php index 8d78f89..193d351 100644 --- a/apps/field.php +++ b/apps/field.php @@ -26,12 +26,12 @@ if ($isis) { list($entry, $result) = $isis->nextField($entry, $field); // Navigation bar. - $display->navbar($entry, $isis->entries, $repetition, '&fid='. $fid); + $display->navbar($entry, $isis->entries, 'field.php', '&fid='. $fid); // Format output. echo "
\n";
   echo "Selected field: $fid: ". $field['name'] ."\n";
-  echo "Showing entry ". $display->entry_link($entry) ." from $entries total entries.\n";
+  echo "Showing entry ". $display->entry_link($entry) ." from ". $isis->entries ." total entries.\n";
   echo "Repetitions found: ". count($result[$field['name']]) .".\n";
   echo "\n";
   print_r($result[$field['name']]);
diff --git a/apps/repetition.php b/apps/repetition.php
index 3fce88f..0e2718c 100644
--- a/apps/repetition.php
+++ b/apps/repetition.php
@@ -26,12 +26,12 @@ if ($isis) {
   list($entry, $result) = $isis->nextRepetition($entry, $field);
 
   // Navigation bar.
-  $display->navbar($entry, $isis->entries, $repetition, '&fid='. $fid);
+  $display->navbar($entry, $isis->entries, 'repetition.php', '&fid='. $fid);
 
   // Format output.
   echo "
\n";
   echo "Selected field: $fid: ". $field['name'] ."\n";
-  echo "Showing entry ". $display->entry_link($entry) ." from $entries total entries.\n";
+  echo "Showing entry ". $display->entry_link($entry) ." from ". $isis->entries ." total entries.\n";
   echo "Repetitions found: ". count($result[$field['name']]) .".\n";
   echo "\n";
   print_r($result[$field['name']]);
diff --git a/apps/subfield.php b/apps/subfield.php
index 352a9f5..ca7557e 100644
--- a/apps/subfield.php
+++ b/apps/subfield.php
@@ -29,13 +29,13 @@ if ($isis) {
   list($entry, $result) = $isis->nextSubfield($entry, $field, $subfield);
 
   // Navigation bar.
-  $display->navbar($entry, $isis->entries, $repetition, '&fid='. $fid . '&sid='. $sid);
+  $display->navbar($entry, $isis->entries, 'subfield.php', '&fid='. $fid . '&sid='. $sid);
 
   // Format output.
   echo "
\n";
   echo "Selected field: $fid: ". $field['name'] .".\n";
   echo "Selected subfield: $sid: $subfield.\n";
-  echo "Showing entry ". $display->entry_link($entry) ." from $entries total entries.\n";
+  echo "Showing entry ". $display->entry_link($entry) ." from ". $isis->entries ." total entries.\n";
   echo "Repetitions found: ". count($result[$field['name']]) .".\n";
   echo "\n";
   print_r($result[$field['name']]);
diff --git a/classes/IsisFinder.php b/classes/IsisFinder.php
index e4f228d..26430c6 100644
--- a/classes/IsisFinder.php
+++ b/classes/IsisFinder.php
@@ -17,22 +17,13 @@ class IsisFinder extends IsisConnector {
    *   Next repetition entry and result.
    */
   public function nextRepetition($entry = 1, $field) {
-    $entry--;
-
-    // Query database.
-    do {
-      $result = $this->read(++$entry);
-      if ($entry == $entries) {
-        break;
+    foreach(new IsisEntryIterator($this, $entry) as $entry => $result) {
+      if (count($this->getValues($field)) >= 2) {
+        return array($entry, $result);
       }
-      $values = $this->getValues($field);
-    } while (count($values) < 2);
-
-    if (count($values) < 2) {
-      return FALSE;
     }
 
-    return array($entry, $result);
+    return FALSE;
   }
 
   /**
@@ -48,22 +39,13 @@ class IsisFinder extends IsisConnector {
    *   Next occurrence.
    */
   public function nextField($entry = 1, $field) {
-    $entry--;
-
-    // Query database.
-    do {
-      $result = $this->read(++$entry);
-      if ($entry == $entries) {
-        break;
+    foreach(new IsisEntryIterator($this, $entry) as $entry => $result) {
+      if (count($this->getValues($field)) > 0) {
+        return array($entry, $result);
       }
-      $values = $this->getValues($field);
-    } while (empty($values));
-
-    if (empty($values)) {
-      return FALSE;
     }
 
-    return array($entry, $result);
+    return FALSE;
   }
 
   /**
@@ -82,22 +64,13 @@ class IsisFinder extends IsisConnector {
    *   Next occurrence.
    */
   public function nextSubfield($entry = 1, $field, $subfield) {
-    $entry--;
-
-    // Query database.
-    do {
-      $result = $this->read(++$entry);
-      if ($entry == $entries) {
-        break;
+    foreach(new IsisEntryIterator($this, $entry) as $entry => $result) {
+      if ($this->hasSubfieldInRows($field, $subfield) !== FALSE) {
+        return array($entry, $result);
       }
-      $has = $this->hasSubfieldInRows($field, $subfield);
-    } while ($has === FALSE);
-
-    if (!$this->hasSubfield($field, $subfield, $has)) {
-      return FALSE;
     }
 
-    return array($entry, $result);
+    return FALSE;
   }
 
   /**
diff --git a/classes/iterators/IsisEntryIterator.php b/classes/iterators/IsisEntryIterator.php
new file mode 100644
index 0000000..6a1d72c
--- /dev/null
+++ b/classes/iterators/IsisEntryIterator.php
@@ -0,0 +1,66 @@
+read($entry);
+
+    // Setup.
+    $this->class   = $class;
+    $this->entry   = $this->start = $entry;
+    $this->entries = $class->entries;
+  }
+
+  /**
+   * Rewind the Iterator to the first element.
+   */
+  function rewind() {
+    $this->entry = $this->start;
+  }
+
+  /**
+   * Return the key of the current element.
+   */
+  function key() {
+    return $this->entry;
+  }
+
+  /**
+   * Return the current element.
+   */
+  function current() {
+    return $this->class->result;
+  }
+
+  /**
+   * Move forward to next element.
+   */
+  function next() {
+    $this->class->read(++$this->entry);
+  }
+
+  /**
+   * Check if there is a current element after calls to rewind() or next().
+   */
+  function valid() {
+    return $this->entry <= $this->entries;
+  }  
+}
-- 
cgit v1.2.3