From 1d84217ed97be237d2696c85a997c9e52114b984 Mon Sep 17 00:00:00 2001 From: Silvio Date: Mon, 19 Jul 2010 18:04:40 -0300 Subject: Lots of API changes at IsisConnector and iterators --- classes/IsisConnector.php | 66 ++++++++++++++-- classes/iterators/IsisFieldIterator.php | 81 -------------------- classes/iterators/IsisItemIterator.php | 85 +++++++++++++++++++++ classes/iterators/IsisMainFieldIterator.php | 87 ---------------------- classes/iterators/IsisMainItemIterator.php | 87 ++++++++++++++++++++++ classes/iterators/IsisNormalItemFilterIterator.php | 18 +++++ .../iterators/IsisNormalSubfieldFilterIterator.php | 18 ----- classes/iterators/IsisSubfieldIterator.php | 78 +++++++++++++++++++ 8 files changed, 327 insertions(+), 193 deletions(-) delete mode 100644 classes/iterators/IsisFieldIterator.php create mode 100644 classes/iterators/IsisItemIterator.php delete mode 100644 classes/iterators/IsisMainFieldIterator.php create mode 100644 classes/iterators/IsisMainItemIterator.php create mode 100644 classes/iterators/IsisNormalItemFilterIterator.php delete mode 100644 classes/iterators/IsisNormalSubfieldFilterIterator.php create mode 100644 classes/iterators/IsisSubfieldIterator.php diff --git a/classes/IsisConnector.php b/classes/IsisConnector.php index da19fde..c82d479 100644 --- a/classes/IsisConnector.php +++ b/classes/IsisConnector.php @@ -47,7 +47,7 @@ class IsisConnector { * @return * Main field name. */ - public function getMainFieldName($field) { + public function getMainItemName($field) { $key = $this->getFieldKey($field); return $this->isis->db->main_field_name($key); } @@ -108,8 +108,8 @@ class IsisConnector { * @return * Field data. */ - public function getMainField($field, $row = 0) { - $name = $this->getMainFieldName($field); + public function getMainItem($field, $row = 0) { + $name = $this->getMainItemName($field); if (isset($this->result[$field['name']][$row][$name])) { return $this->result[$field['name']][$row][$name]; @@ -125,9 +125,9 @@ class IsisConnector { * @return * Field data. */ - public function getMainFields($field) { + public function getMainItems($field) { foreach (new IsisRowIterator($this, $field) as $row) { - $values[$row] = $this->getMainField($field, $row); + $values[$row] = $this->getMainItem($field, $row); } return $values; @@ -181,6 +181,58 @@ class IsisConnector { return $values; } + /** + * Get both main field or subfields from a given field and row. + * + * @param $field + * field array. + * + * @param $item + * item name (field or subfield). + * + * @param $row + * row number. + * + * @return + * Item data. + */ + public function getItem($field, $item, $row) { + $main_field = $this->getMainItemName($field); + + if ($field == $main_field) { + return $this->getMainItem($field, $row); + } + else { + return $this->getSubfield($field, $item, $row); + } + } + + /** + * Get all rows both main field or subfields from a given field. + * + * @param $field + * field array. + * + * @param $item + * item name (field or subfield). + * + * @param $row + * row number. + * + * @return + * Item data. + * + * @todo + * Rename to getItem? + */ + public function getItems($field, $item) { + foreach (new IsisRowIterator($this, $field) as $row) { + $values[$row] = $this->getItem($field, $item, $row); + } + + return $values; + } + /** * Get the list of subfields from a given field. * @@ -582,7 +634,7 @@ class IsisConnector { } /** - * Deal with special subfields. + * Deal with special items. * * @param $field * Field data from ISIS database schema. @@ -596,7 +648,7 @@ class IsisConnector { * @return * True if special subfield, false otherwise of special return type */ - public function specialSubfield($field, $subfield, $return = 'boolean') { + public function specialItem($field, $subfield, $return = 'boolean') { if (isset($field['special'])) { $field_key = $this->getFieldKey($field); $subfield_key = $this->getSubfieldKey($field, $subfield); diff --git a/classes/iterators/IsisFieldIterator.php b/classes/iterators/IsisFieldIterator.php deleted file mode 100644 index 26c5e88..0000000 --- a/classes/iterators/IsisFieldIterator.php +++ /dev/null @@ -1,81 +0,0 @@ -class = $class; - $this->field = $field; - $this->rows = $class->getRows($field); - $this->fieldset = $class->getSubfieldList($field); - $this->keys = array_keys($this->fieldset); - $this->subfields = count($this->keys); - } - - /** - * Rewind the Iterator to the first element. - */ - function rewind() { - $this->row = 0; - $this->subfield = 0; - } - - /** - * Return the key of the current element. - */ - function key() { - return $this->row; - } - - /** - * Return the current element. - */ - function current() { - return $this->fieldset[$this->keys[$this->subfield]]; - } - - /** - * Move forward to next element. - */ - function next() { - if ($this->subfield >= $this->subfields) { - $this->subfield = 0; - ++$this->row; - } - else { - ++$this->subfield; - } - } - - /** - * Check if there is a current element after calls to rewind() or next(). - */ - function valid() { - return $this->row <= $this->rows; - } -} diff --git a/classes/iterators/IsisItemIterator.php b/classes/iterators/IsisItemIterator.php new file mode 100644 index 0000000..5c28b2c --- /dev/null +++ b/classes/iterators/IsisItemIterator.php @@ -0,0 +1,85 @@ +class = $class; + $this->field = $field; + $this->rows = $class->getRows($field); + + // Handle subfields + $this->fieldset = $class->getSubfieldList($field); + $this->keys = array_keys($this->fieldset); + $this->items = count($this->keys); + + // Sum up main item + $this->fieldset[] = $class->getMainItem($field); + $this->keys[] = $class->getMainItemName($field); + $this->items++; + } + + /** + * Rewind the Iterator to the first element. + */ + function rewind() { + $this->row = 0; + $this->item = 0; + } + + /** + * Return the key of the current element. + */ + function key() { + return $this->row; + } + + /** + * Return the current element. + */ + function current() { + return $this->fieldset[$this->keys[$this->item]]; + } + + /** + * Move forward to next element. + */ + function next() { + if ($this->item >= $this->items) { + $this->item = 0; + ++$this->row; + } + else { + ++$this->item; + } + } + + /** + * Check if there is a current element after calls to rewind() or next(). + */ + function valid() { + return $this->row <= $this->rows; + } +} diff --git a/classes/iterators/IsisMainFieldIterator.php b/classes/iterators/IsisMainFieldIterator.php deleted file mode 100644 index 5ec1cbc..0000000 --- a/classes/iterators/IsisMainFieldIterator.php +++ /dev/null @@ -1,87 +0,0 @@ -rows = $class->getRows($field); - $this->valueset = $class->getValues($field); - $this->class = $class; - $this->field = $field; - } - - /** - * Rewind the Iterator to the first element. - */ - function rewind() { - $this->row = 0; - $this->value = 0; - } - - /** - * Return the key of the current element. - */ - function key() { - return $this->row; - } - - /** - * Return the current element. - */ - function current() { - $field = $this->class->getMainFieldName($this->field); - return $this->valueset[$this->row][$field]; - } - - /** - * Move forward to next element. - */ - function next() { - do { - ++$this->row; - } - while ($this->current_null() && $this->has_more_rows()); - } - - /** - * Check if there are more rows. - */ - function has_more_rows() { - return $this->row <= $this->rows; - } - - /** - * Check if the current value is null. - */ - function current_null() { - return $this->current() == NULL; - } - - /** - * Check if there is a current element after calls to rewind() or next(). - */ - function valid() { - return $this->has_more_rows() && !$this->current_null(); - } -} diff --git a/classes/iterators/IsisMainItemIterator.php b/classes/iterators/IsisMainItemIterator.php new file mode 100644 index 0000000..02ab8d1 --- /dev/null +++ b/classes/iterators/IsisMainItemIterator.php @@ -0,0 +1,87 @@ +rows = $class->getRows($field); + $this->valueset = $class->getValues($field); + $this->class = $class; + $this->field = $field; + } + + /** + * Rewind the Iterator to the first element. + */ + function rewind() { + $this->row = 0; + $this->value = 0; + } + + /** + * Return the key of the current element. + */ + function key() { + return $this->row; + } + + /** + * Return the current element. + */ + function current() { + $field = $this->class->getMainItemName($this->field); + return $this->valueset[$this->row][$field]; + } + + /** + * Move forward to next element. + */ + function next() { + do { + ++$this->row; + } + while ($this->current_null() && $this->has_more_rows()); + } + + /** + * Check if there are more rows. + */ + function has_more_rows() { + return $this->row <= $this->rows; + } + + /** + * Check if the current value is null. + */ + function current_null() { + return $this->current() == NULL; + } + + /** + * Check if there is a current element after calls to rewind() or next(). + */ + function valid() { + return $this->has_more_rows() && !$this->current_null(); + } +} diff --git a/classes/iterators/IsisNormalItemFilterIterator.php b/classes/iterators/IsisNormalItemFilterIterator.php new file mode 100644 index 0000000..e59d7c6 --- /dev/null +++ b/classes/iterators/IsisNormalItemFilterIterator.php @@ -0,0 +1,18 @@ +getInnerIterator()->field; + $class = $this->getInnerIterator()->class; + $item = $this->getInnerIterator()->current(); + return !$class->specialItem($field, $item); + } +} diff --git a/classes/iterators/IsisNormalSubfieldFilterIterator.php b/classes/iterators/IsisNormalSubfieldFilterIterator.php deleted file mode 100644 index 5a3f4ce..0000000 --- a/classes/iterators/IsisNormalSubfieldFilterIterator.php +++ /dev/null @@ -1,18 +0,0 @@ -getInnerIterator()->field; - $class = $this->getInnerIterator()->class; - $subfield = $this->getInnerIterator()->current(); - return !$class->specialSubfield($field, $subfield); - } -} diff --git a/classes/iterators/IsisSubfieldIterator.php b/classes/iterators/IsisSubfieldIterator.php new file mode 100644 index 0000000..2e213cf --- /dev/null +++ b/classes/iterators/IsisSubfieldIterator.php @@ -0,0 +1,78 @@ +class = $class; + $this->field = $field; + $this->rows = $class->getRows($field); + $this->fieldset = $class->getSubfieldList($field); + $this->keys = array_keys($this->fieldset); + $this->subfields = count($this->keys); + } + + /** + * Rewind the Iterator to the first element. + */ + function rewind() { + $this->row = 0; + $this->subfield = 0; + } + + /** + * Return the key of the current element. + */ + function key() { + return $this->row; + } + + /** + * Return the current element. + */ + function current() { + return $this->fieldset[$this->keys[$this->subfield]]; + } + + /** + * Move forward to next element. + */ + function next() { + if ($this->subfield >= $this->subfields) { + $this->subfield = 0; + ++$this->row; + } + else { + ++$this->subfield; + } + } + + /** + * Check if there is a current element after calls to rewind() or next(). + */ + function valid() { + return $this->row <= $this->rows; + } +} -- cgit v1.2.3