From 5976bf332aae7389e73333067bc18af9cd5ccf67 Mon Sep 17 00:00:00 2001 From: Silvio Date: Thu, 19 Aug 2010 11:09:16 -0300 Subject: Breaking IsisConnector into smaller classes --- classes/IsisConnector.php | 448 +--------------------------------------------- classes/IsisFinder.php | 3 - classes/IsisMap.php | 293 ++++++++++++++++++++++++++++++ classes/IsisReader.php | 165 +++++++++++++++++ 4 files changed, 459 insertions(+), 450 deletions(-) create mode 100644 classes/IsisMap.php create mode 100644 classes/IsisReader.php (limited to 'classes') diff --git a/classes/IsisConnector.php b/classes/IsisConnector.php index 09bb8ea..ff5a576 100644 --- a/classes/IsisConnector.php +++ b/classes/IsisConnector.php @@ -4,78 +4,7 @@ * IsisConnector: provides an easy interface to connect an * application with Cinisis. */ -class IsisConnector { - /** - * Constructor. - */ - public function __construct($config = null) { - return $this->open($config); - } - - /** - * Open a database. - * - * @param $config - * Config file or array. - */ - public function open($config) { - $this->isis = new Cinisis($config); - - if ($this->isis->db) { - $this->entries = $this->isis->db->entries(); - $this->format = $this->isis->db->format; - $this->fields = $this->format['fields']; - } - else { - return FALSE; - } - } - - /** - * Alias to $isis->db->read(). - * - * @param $entry - * Row number. - * - * @return - * Resulting data. - */ - public function read($entry) { - // Always store the last result. - $this->result = $this->isis->db->read($entry); - - // Return the result. - return $this->result; - } - - /** - * Get the main field name. - * - * @param $field - * Field data from ISIS database schema. - * - * @return - * Main field name. - */ - public function getMainItemName($field) { - $key = $this->getFieldKey($field); - return Cinisis::main_field_name($this->format, $key); - } - - /** - * Whether to join field and subfields in a single array. - * - * @return - * Boolean. - */ - public function joinSubfields() { - if (Cinisis::join_subfields($this->format)) { - return TRUE; - } - - return FALSE; - } - +class IsisConnector extends IsisMap { /** * Get all values of a given field. * @@ -250,322 +179,6 @@ class IsisConnector { return $values; } - /** - * Get the list of subfields from a given field. - * - * @param $field - * Field array. - */ - public function getSubfieldList($field) { - if (isset($field['subfields'])) { - return $field['subfields']; - } - - return array(); - } - - /** - * Determine which model field an ISIS db field should be mapped to. - * When importing an ISIS database to another system, a mapping - * provided in the database schema can be used to put the originating - * entries (fields and subfields) in the right place at the destination - * database. - * - * Map format: - * - * map: - * type: relation - * - * map: - * type: value - * field: dest - * subfields: - * a: dest - * b: dest - * - * Examples: - * - * map: - * type: Performer - * - * map: - * type: value - * field: title - * subfields: - * a: subtitle - * - * @param $field - * Field array. - * - * @param $subfield - * Subfield name. - * - * @return - * A map destination to the field or subfield. - */ - public function getMap($field, $subfield = NULL) { - if ($subfield == NULL) { - if (isset($field['map']['main'])) { - // Custom map provided for the main item. - $dest = $this->mapName($field['map']['main']); - } - else { - // Default map. - $dest = $this->mapName($field['name']); - } - } - else { - $key = $this->getSubfieldKey($field, $subfield); - - if (isset($field['map']['subfields'][$key])) { - // Custom map provided. - $dest = $this->mapName($field['map']['subfields'][$key]); - } - else { - // Default map. - $dest = $this->mapName($subfield); - } - } - - return $dest; - } - - /** - * Get the mapping type of a given field. - * - * @param $field - * Field array. - * - * @return - * The mapping type. - */ - public function getMapType($field) { - return isset($field['map']['type']) ? $field['map']['type'] : FALSE; - } - - /** - * Guess a method name from a type. - * - * @param $type - * Mapping type. - * - * @return - * Method name. - */ - static function methodName($type) { - return 'import'. ucfirst($type); - } - - /** - * Check on an ISIS schema whether a field has a map. - * - * @param $field - * Field array. - * - * @return - * TRUE if field has a map, FALSE otherwise. - */ - public function fieldHasMap($field) { - if (isset($field['map']['main'])) { - return TRUE; - } - return FALSE; - } - - /** - * Check on an ISIS schema whether a subfield has a map. - * - * @param $field - * Field array. - * - * @param $subfield - * Subfield name. - * - * @return - * TRUE if subfield has a map, FALSE otherwise. - */ - public function subfieldHasMap($field, $subfield) { - if (isset($field['map']['subfields'])) { - $key = $this->getSubfieldKey($field, $subfield); - if (isset($field['map']['subfields'][$key])) { - return TRUE; - } - } - return FALSE; - } - - /** - * Get the key of a subfield entry. - * - * @param $field - * Field array. - * - * @param $subfield - * Subfield name. - * - * @return - * Subfield key. - */ - public function getSubfieldKey($field, $subfield) { - $keys = array_flip($field['subfields']); - if (isset($keys[$subfield])) { - return $keys[$subfield]; - } - } - - /** - * Get the item key. - * - * @param $field - * Field array. - * - * @param $item - * Item name. - * - * @return - * Item key. - */ - public function getItemKey($field, $item) { - if ($item == 'main') { - return $item; - } - else { - return $this->getSubfieldKey($field, $item); - } - } - - /** - * Get the key of a field entry. - * - * @param $field - * Field array. - * - * @return - * Field key. - */ - public function getFieldKey($field) { - return array_search($field, $this->format['fields']); - } - - /** - * Get the array which defines a field. - * - * @param $field_key - * Field key. - * - * @return - * Field array. - */ - public function getFieldArray($field_key) { - if (isset($this->format['fields'][$field_key])) { - return $this->format['fields'][$field_key]; - } - - return NULL; - } - - /** - * Remove brackets from strings whithin an array. - * - * @param $value - * Array with bracketed strings. - */ - public function removeBrackets($value) { - $value = str_replace('<', '', $value); - $value = str_replace('>', '', $value); - return $value; - } - - /** - * Remove brackets from strings whithin an array. - * - * @param &$values - * Array with bracketed strings. - */ - public function removeBracketsFromArray(&$values) { - foreach ($values as $key => $value) { - $values[$key] = $this->removeBrackets($value); - } - } - - /** - * Explode a bracketed string into values. Just strings - * inside brackets are returned. - * - * @param $subject - * Strings containing brackets. - * - * @return - * Array of matched strings. - */ - public function explodeBrackets($subject) { - preg_match_all('/<[^<>]*>/', $subject, $values); - return $this->filterBrackets($values[0]); - } - - /** - * Filter out brackets from strings. - * - * @param $values - * String (or array filled with strings) to be filtered. - * - * @result - * Filtered string or array. - */ - public function filterBrackets($values) { - if (is_array($values)) { - foreach ($values as $key => $value) { - $values[$key] = $this->filterBrackets($value); - } - } - else { - $values = preg_replace(array('//'), '', $values); - } - - return $values; - } - - /** - * Check if a string has brackets. - * - * @param $value - * String to be compared. - * - * @return - * True if string has brackets, false otherwise. - */ - public function hasBrackets($value) { - if (strstr($value, '<') && strstr($value, '>')) { - return TRUE; - } - - return FALSE; - } - - /** - * Explode values from fields or subfields. Split values - * inside brackets if needed, but then doesn't return any - * value outside brackets. - * - * @param $value - * String with values. - * - * @return - * Array with values. - */ - public function explodeValue($value) { - if ($this->hasBrackets($value)) { - return $this->explodeBrackets($value); - } - else { - if (!is_array($value)) { - $value = array($value); - } - } - - return $value; - } - /** * Explode brackets for a given subfield, avoiding null entries. * @@ -623,32 +236,6 @@ class IsisConnector { return $this->filterBrackets($this->getSubfield($field, $subfield, $row)); } - /** - * Normalize field names. - * - * @param $name - * Field name - * - * @return - * Normalized field name - */ - static function normalizeFieldName($name) { - return ucfirst(preg_replace('/[^a-z0-9_]/', '', strtolower($name))); - } - - /** - * Build a map name. - * - * @param $name - * Field name - * - * @return - * Map name - */ - static function mapName($name) { - return 'set'. self::normalizeFieldName($name); - } - /** * Check if a field result and row has a given subfield. * @@ -752,39 +339,6 @@ class IsisConnector { return $existing; } - /** - * Get a subfield name. - * - * @param $field_key - * Field key. - * - * @param $subfield_key - * Subfield key. - * - * @return - * Subfield name. - */ - public function getSubfieldName($field_key, $subfield_key) { - if (isset($this->format['fields'][$field_key]['subfields'][$subfield_key])) { - return $this->format['fields'][$field_key]['subfields'][$subfield_key]; - } - - return FALSE; - } - - /** - * Get a field name. - * - * @param $field_key - * Field key. - * - * @return - * Field name. - */ - public function getFieldName($field_key) { - return $this->format['fields'][$field_key]['name']; - } - /** * Check if a field and subfield match a given condition. * diff --git a/classes/IsisFinder.php b/classes/IsisFinder.php index a87488f..f877630 100644 --- a/classes/IsisFinder.php +++ b/classes/IsisFinder.php @@ -78,9 +78,6 @@ class IsisFinder extends IsisConnector { * * @return * Next occurrence. - * - * @todo - * Test. */ public function nextSubfield($entry = 1, $field, $subfield) { $entry--; diff --git a/classes/IsisMap.php b/classes/IsisMap.php new file mode 100644 index 0000000..e73e178 --- /dev/null +++ b/classes/IsisMap.php @@ -0,0 +1,293 @@ +getFieldKey($field); + return Cinisis::main_field_name($this->format, $key); + } + + /** + * Get the list of subfields from a given field. + * + * @param $field + * Field array. + */ + public function getSubfieldList($field) { + if (isset($field['subfields'])) { + return $field['subfields']; + } + + return array(); + } + + /** + * Determine which model field an ISIS db field should be mapped to. + * When importing an ISIS database to another system, a mapping + * provided in the database schema can be used to put the originating + * entries (fields and subfields) in the right place at the destination + * database. + * + * Map format: + * + * map: + * type: relation + * + * map: + * type: value + * field: dest + * subfields: + * a: dest + * b: dest + * + * Examples: + * + * map: + * type: Performer + * + * map: + * type: value + * field: title + * subfields: + * a: subtitle + * + * @param $field + * Field array. + * + * @param $subfield + * Subfield name. + * + * @return + * A map destination to the field or subfield. + */ + public function getMap($field, $subfield = NULL) { + if ($subfield == NULL) { + if (isset($field['map']['main'])) { + // Custom map provided for the main item. + $dest = $this->mapName($field['map']['main']); + } + else { + // Default map. + $dest = $this->mapName($field['name']); + } + } + else { + $key = $this->getSubfieldKey($field, $subfield); + + if (isset($field['map']['subfields'][$key])) { + // Custom map provided. + $dest = $this->mapName($field['map']['subfields'][$key]); + } + else { + // Default map. + $dest = $this->mapName($subfield); + } + } + + return $dest; + } + + /** + * Get the mapping type of a given field. + * + * @param $field + * Field array. + * + * @return + * The mapping type. + */ + public function getMapType($field) { + return isset($field['map']['type']) ? $field['map']['type'] : FALSE; + } + + /** + * Guess a method name from a type. + * + * @param $type + * Mapping type. + * + * @return + * Method name. + */ + static function methodName($type) { + return 'import'. ucfirst($type); + } + + /** + * Check on an ISIS schema whether a field has a map. + * + * @param $field + * Field array. + * + * @return + * TRUE if field has a map, FALSE otherwise. + */ + public function fieldHasMap($field) { + if (isset($field['map']['main'])) { + return TRUE; + } + return FALSE; + } + + /** + * Check on an ISIS schema whether a subfield has a map. + * + * @param $field + * Field array. + * + * @param $subfield + * Subfield name. + * + * @return + * TRUE if subfield has a map, FALSE otherwise. + */ + public function subfieldHasMap($field, $subfield) { + if (isset($field['map']['subfields'])) { + $key = $this->getSubfieldKey($field, $subfield); + if (isset($field['map']['subfields'][$key])) { + return TRUE; + } + } + return FALSE; + } + + /** + * Get the key of a subfield entry. + * + * @param $field + * Field array. + * + * @param $subfield + * Subfield name. + * + * @return + * Subfield key. + */ + public function getSubfieldKey($field, $subfield) { + $keys = array_flip($field['subfields']); + if (isset($keys[$subfield])) { + return $keys[$subfield]; + } + } + + /** + * Get the item key. + * + * @param $field + * Field array. + * + * @param $item + * Item name. + * + * @return + * Item key. + */ + public function getItemKey($field, $item) { + if ($item == 'main') { + return $item; + } + else { + return $this->getSubfieldKey($field, $item); + } + } + + /** + * Get the key of a field entry. + * + * @param $field + * Field array. + * + * @return + * Field key. + */ + public function getFieldKey($field) { + return array_search($field, $this->format['fields']); + } + + /** + * Get the array which defines a field. + * + * @param $field_key + * Field key. + * + * @return + * Field array. + */ + public function getFieldArray($field_key) { + if (isset($this->format['fields'][$field_key])) { + return $this->format['fields'][$field_key]; + } + + return NULL; + } + + /** + * Normalize field names. + * + * @param $name + * Field name + * + * @return + * Normalized field name + */ + static function normalizeFieldName($name) { + return ucfirst(preg_replace('/[^a-z0-9_]/', '', strtolower($name))); + } + + /** + * Build a map name. + * + * @param $name + * Field name + * + * @return + * Map name + */ + static function mapName($name) { + return 'set'. self::normalizeFieldName($name); + } + + /** + * Get a subfield name. + * + * @param $field_key + * Field key. + * + * @param $subfield_key + * Subfield key. + * + * @return + * Subfield name. + */ + public function getSubfieldName($field_key, $subfield_key) { + if (isset($this->format['fields'][$field_key]['subfields'][$subfield_key])) { + return $this->format['fields'][$field_key]['subfields'][$subfield_key]; + } + + return FALSE; + } + + /** + * Get a field name. + * + * @param $field_key + * Field key. + * + * @return + * Field name. + */ + public function getFieldName($field_key) { + return $this->format['fields'][$field_key]['name']; + } +} diff --git a/classes/IsisReader.php b/classes/IsisReader.php new file mode 100644 index 0000000..bb818a5 --- /dev/null +++ b/classes/IsisReader.php @@ -0,0 +1,165 @@ +open($config); + } + + /** + * Open a database. + * + * @param $config + * Config file or array. + */ + public function open($config) { + $this->isis = new Cinisis($config); + + if ($this->isis->db) { + $this->entries = $this->isis->db->entries(); + $this->format = $this->isis->db->format; + $this->fields = $this->format['fields']; + } + else { + return FALSE; + } + } + + /** + * Alias to $isis->db->read(). + * + * @param $entry + * Row number. + * + * @return + * Resulting data. + */ + public function read($entry) { + // Always store the last result. + $this->result = $this->isis->db->read($entry); + + // Return the result. + return $this->result; + } + + /** + * Remove brackets from strings whithin an array. + * + * @param $value + * Array with bracketed strings. + */ + public function removeBrackets($value) { + $value = str_replace('<', '', $value); + $value = str_replace('>', '', $value); + return $value; + } + + /** + * Remove brackets from strings whithin an array. + * + * @param &$values + * Array with bracketed strings. + */ + public function removeBracketsFromArray(&$values) { + foreach ($values as $key => $value) { + $values[$key] = $this->removeBrackets($value); + } + } + + /** + * Explode a bracketed string into values. Just strings + * inside brackets are returned. + * + * @param $subject + * Strings containing brackets. + * + * @return + * Array of matched strings. + */ + public function explodeBrackets($subject) { + preg_match_all('/<[^<>]*>/', $subject, $values); + return $this->filterBrackets($values[0]); + } + + /** + * Filter out brackets from strings. + * + * @param $values + * String (or array filled with strings) to be filtered. + * + * @result + * Filtered string or array. + */ + public function filterBrackets($values) { + if (is_array($values)) { + foreach ($values as $key => $value) { + $values[$key] = $this->filterBrackets($value); + } + } + else { + $values = preg_replace(array('//'), '', $values); + } + + return $values; + } + + /** + * Check if a string has brackets. + * + * @param $value + * String to be compared. + * + * @return + * True if string has brackets, false otherwise. + */ + public function hasBrackets($value) { + if (strstr($value, '<') && strstr($value, '>')) { + return TRUE; + } + + return FALSE; + } + + /** + * Explode values from fields or subfields. Split values + * inside brackets if needed, but then doesn't return any + * value outside brackets. + * + * @param $value + * String with values. + * + * @return + * Array with values. + */ + public function explodeValue($value) { + if ($this->hasBrackets($value)) { + return $this->explodeBrackets($value); + } + else { + if (!is_array($value)) { + $value = array($value); + } + } + + return $value; + } + + /** + * Whether to join field and subfields in a single array. + * + * @return + * Boolean. + */ + public function joinSubfields() { + if (Cinisis::join_subfields($this->format)) { + return TRUE; + } + + return FALSE; + } +} -- cgit v1.2.3