Пример #1
0
 /**
  * This method iterates over each record in the file, yielding each item to the procedure function.
  *
  * @access public
  * @param callable $procedure                               the procedure function to be used
  * @throws Throwable\Parse\Exception                        indicates that an invalid record was
  *                                                          encountered
  */
 public function each(callable $procedure)
 {
     $self = $this;
     IO\FileReader::read($this->file, function ($reader, $data, $index) use($self, $procedure) {
         $line = trim($data);
         if (strlen($line) > 0) {
             if ($index == 0 && $self->bom) {
                 $line = preg_replace('/^' . pack('H*', 'EFBBBF') . '/', '', $line);
             }
             if (!preg_match('/^\\s*#.*$/', $line)) {
                 if (!preg_match('/^[^=]+=.+$/', $line)) {
                     throw new Throwable\Parse\Exception('Unable to parse file. File ":uri" contains an invalid pattern on line :line.', array(':uri' => $self->file, ':line' => $index));
                 }
                 $position = strpos($line, '=');
                 $key = trim(substr($line, 0, $position));
                 $value = trim(substr($line, $position + 1));
                 $type = isset($self->schema[$key]) ? $self->schema[$key] : 'string';
                 $value = Core\Convert::changeType($value, $type);
                 $record = new Common\Mutable\HashMap();
                 $record->putEntry($key, $value);
                 $procedure($record);
             }
         }
     });
 }
Пример #2
0
 /**
  * This method iterates over each record in the file, yielding each item to the procedure function.
  *
  * @access public
  * @param callable $procedure                               the procedure function to be used
  * @throws Throwable\Parse\Exception                        indicates that an invalid record was
  *                                                          encountered
  */
 public function each(callable $procedure)
 {
     $self = $this;
     IO\FileReader::read($this->file, function ($reader, $data, $index) use($self, $procedure) {
         $line = trim($data);
         if (strlen($line) > 0) {
             if ($index == 0 && $self->bom) {
                 $line = preg_replace('/^' . pack('H*', 'EFBBBF') . '/', '', $line);
             }
             $query_string = parse_url($line, PHP_URL_QUERY);
             $properties = array();
             parse_str($query_string, $properties);
             if (!empty($properties)) {
                 $source_encoding = $self->encoder !== null ? call_user_func($self->encoder . "::getEncoding", $properties) : $self->encoding[0];
                 $target_encoding = $self->encoding[1];
                 foreach ($properties as $key => &$value) {
                     $value = urldecode($value);
                     $value = Core\Data\Charset::encode($value, $source_encoding, $target_encoding);
                     $type = isset($self->schema[$key]) ? $self->schema[$key] : 'string';
                     $value = Core\Convert::changeType($value, $type);
                     $record = new Common\Mutable\HashMap();
                     $record->putEntry($key, $value);
                     $procedure($record);
                 }
             }
         }
     });
 }
Пример #3
0
 /**
  * This method returns a new object with the value converted to the specified type.
  *
  * @access public
  * @param string $type                                      the data type to be applied
  * @return MappingService\Data\Field\Item                   a new object with the value converted
  *                                                          to the specified type
  * @throws \Unicity\Throwable\Parse\Exception               indicates that the value could not
  *                                                          be converted
  */
 public function changeType($type)
 {
     if ($this->info->type != 'unknown') {
         $value = Core\Convert::changeType($this->value, $type);
         return static::factory($this->name, $value);
     }
     return $this;
 }
Пример #4
0
 /**
  * This method adds a value to the query string.
  *
  * @access public
  * @param string $key                                       the key to be used
  * @param mixed $value                                      the value to be added
  * @return string                                           the query string segment
  */
 public function addValue($key, $value)
 {
     if (!Core\Data\Undefined::instance()->__equals($value)) {
         $field = preg_replace('/\\[[^\\]]*\\]/', '[]', $key);
         $type = isset($this->metadata['schema'][$field]) ? $this->metadata['schema'][$field] : 'string';
         $value = Core\Convert::changeType($value, $type);
         $value = Core\Convert::toString($value);
         $value = Core\Data\Charset::encode($value, $this->metadata['encoding'][0], $this->metadata['encoding'][1]);
         $value = urlencode($value);
         return $key . '=' . $value;
     }
     return null;
 }
Пример #5
0
 /**
  * This method returns an object matching the description specified by the element.
  *
  * @access public
  * @param Spring\Object\Parser $parser                      a reference to the parser
  * @param \SimpleXMLElement $element                        the element to be parsed
  * @return mixed                                            an object matching the description
  *                                                          specified by the element
  * @throws Throwable\Parse\Exception                        indicates that a problem occurred
  *                                                          when parsing
  */
 public function getObject(Spring\Object\Parser $parser, \SimpleXMLElement $element)
 {
     $children = $parser->getElementChildren($element, null);
     if (!empty($children)) {
         $value = '';
         foreach ($children as $child) {
             $name = $parser->getElementPrefixedName($child);
             switch ($name) {
                 case 'spring:null':
                     $value = $parser->getObjectFromElement($child);
                     break;
                 default:
                     throw new Throwable\Parse\Exception('Unable to process Spring XML. Tag ":tag" has invalid child node ":child"', array(':tag' => 'value', ':child' => $name));
                     break;
             }
         }
         if (is_string($value)) {
             return Core\Data\Charset::encode($value, $parser->getEncoding($parser->getResource()), Core\Data\Charset::UTF_8_ENCODING);
         }
         return $value;
     } else {
         $attributes = $element->attributes();
         $value = dom_import_simplexml($element)->textContent;
         if (isset($attributes['type'])) {
             $type = $parser->valueOf($attributes['type']);
             if (!$parser->isPrimitiveType($type)) {
                 throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid primitive type, but got ":type".', array(':type' => $type));
             }
             $value = Core\Convert::changeType($value, $type);
         }
         if (is_string($value)) {
             $attributes = $element->attributes('xml', true);
             if (isset($attributes['space'])) {
                 $space = $parser->valueOf($attributes['space']);
                 if (!$parser->isSpacePreserved($space)) {
                     throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid space token, but got ":token".', array(':token' => $space));
                 }
             } else {
                 $value = trim($value);
             }
         }
         if (is_string($value)) {
             return Core\Data\Charset::encode($value, $parser->getEncoding($parser->getResource()), Core\Data\Charset::UTF_8_ENCODING);
         }
         return $value;
     }
 }
Пример #6
0
 /**
  * This method recursively adds each entry as a property.
  *
  * @access protected
  * @param string &$buffer                                   the string buffer
  * @param string $key                                       the key to be used
  * @param mixed $value                                      the value to be added
  */
 protected function addProperty(&$buffer, $key, $value)
 {
     if (!is_array($value)) {
         if ($value == null || is_object($value) && $value instanceof Core\Data\Undefined) {
             $buffer .= $key . '=' . '';
         } else {
             $type = isset($this->metadata['schema'][$key]) ? $this->metadata['schema'][$key] : 'string';
             $datum = Core\Convert::changeType($value, $type);
             $datum = Core\Convert::toString($datum);
             $datum = Core\Data\Charset::encode($datum, $this->metadata['encoding'][0], $this->metadata['encoding'][1]);
             $buffer .= $key . '=' . $datum;
         }
         $buffer .= $this->metadata['eol'];
     } else {
         foreach ($value as $k => $v) {
             $this->addProperty($buffer, $key . '.' . Core\Convert::toString($k), $v);
         }
     }
 }
Пример #7
0
 /**
  * This method returns an object matching the description specified by the element.
  *
  * @access public
  * @param Spring\Object\Parser $parser                      a reference to the parser
  * @param \SimpleXMLElement $element                        the element to be parsed
  * @return mixed                                            an object matching the description
  *                                                          specified by the element
  * @throws Throwable\Parse\Exception                        indicates that a problem occurred
  *                                                          when parsing
  */
 public function getObject(Spring\Object\Parser $parser, \SimpleXMLElement $element)
 {
     $object = array();
     $attributes = $element->attributes();
     if (!isset($attributes['key'])) {
         throw new Throwable\Parse\Exception('Unable to process Spring XML. Tag ":tag" is missing ":attribute" attribute.', array(':tag' => $parser->getElementPrefixedName($element), ':attribute' => 'key'));
     }
     $key = $parser->valueOf($attributes['key']);
     if (!$parser->isKey($key)) {
         throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid entry key, but got ":key".', array(':key' => $key));
     }
     $children = $parser->getElementChildren($element, null);
     if (!empty($children)) {
         foreach ($children as $child) {
             $object[$key] = $parser->getObjectFromElement($child);
         }
     } else {
         if (isset($attributes['value-ref'])) {
             $object[$key] = $parser->getObjectFromIdRef($parser->valueOf($attributes['value-ref']));
         } else {
             if (isset($attributes['value'])) {
                 $value = $parser->valueOf($attributes['value']);
                 if (isset($attributes['value-type'])) {
                     $type = $parser->valueOf($attributes['value-type']);
                     if (!$parser->isPrimitiveType($type)) {
                         throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid primitive type, but got ":type".', array(':type' => $type));
                     }
                     $value = Core\Convert::changeType($value, $type);
                 }
                 $object[$key] = $value;
             } else {
                 throw new Throwable\Parse\Exception('Unable to process Spring XML. Tag ":tag" is missing ":attribute" attribute.', array(':tag' => 'entry', ':attribute' => 'value'));
             }
         }
     }
     return $object;
 }
Пример #8
0
 /**
  * This method iterates over each record in the file, yielding each item to the procedure function.
  *
  * @access public
  * @param callable $procedure                               the procedure function to be used
  * @throws Throwable\Parse\Exception                        indicates that an invalid record was
  *                                                          encountered
  */
 public function each(callable $procedure)
 {
     $self = $this;
     $headers = array();
     IO\FileReader::read($this->file, function ($reader, $data, $index) use($self, $procedure, &$headers) {
         $line = trim($data);
         if (strlen($line) > 0) {
             if ($index == 0) {
                 if ($self->bom) {
                     $line = preg_replace('/^' . pack('H*', 'EFBBBF') . '/', '', $line);
                 }
                 $headers = str_getcsv($line, $self->delimiter, $self->enclosure, $self->escape);
                 $headers = array_map('trim', $headers);
                 if ($self->key_case !== null) {
                     switch ($self->key_case) {
                         case CASE_LOWER:
                             $headers = array_map('strtolower', $headers);
                             break;
                         case CASE_UPPER:
                             $headers = array_map('strtoupper', $headers);
                             break;
                     }
                 }
             } else {
                 $record = str_getcsv($line, $self->delimiter, $self->enclosure, $self->escape);
                 if (!is_array($record)) {
                     throw new Throwable\Runtime\Exception('Failed to process record. Expected an array, but got ":type".', array(':type' => gettype($record)));
                 }
                 $record = array_combine($headers, $record);
                 if ($self->strict_mapping && count($headers) != count($record)) {
                     throw new Throwable\Runtime\Exception('Failed to process record. Headers could not be mapped properly.');
                 }
                 $source_encoding = $self->encoder !== null ? call_user_func($self->encoder . "::getEncoding", $record) : $self->encoding[0];
                 $target_encoding = $self->encoding[1];
                 foreach ($record as $key => &$value) {
                     $value = Core\Data\Charset::encode($value, $source_encoding, $target_encoding);
                     if ($self->strip_invalid_chars) {
                         $value = $self->removeNonUTF8Characters($value);
                     }
                     $type = isset($self->schema[$key]) ? $self->schema[$key] : 'string';
                     $value = Core\Convert::changeType($value, $type);
                 }
                 $map = new Common\Mutable\HashMap($record);
                 if ($self->filter === null || call_user_func_array(array($self->filter, 'isQualified'), array($map))) {
                     $procedure($map);
                 }
             }
         }
     });
 }
Пример #9
0
 /**
  * This method processes a "value" node.
  *
  * @access protected
  * @param \SimpleXMLElement $node                           a reference to the "value" node
  * @return mixed                                            the value
  * @throws Throwable\Instantiation\Exception                indicates that problem occurred during
  *                                                          the instantiation
  */
 protected function parseValueElement(\SimpleXMLElement $node)
 {
     $children = $node->children();
     if (count($children) > 0) {
         $value = '';
         foreach ($children as $child) {
             switch ($child->getName()) {
                 case 'null':
                     $value = $this->parseNullElement($child);
                     break;
                 default:
                     throw new Throwable\Instantiation\Exception('Unable to initial class.');
                     break;
             }
         }
         return $value;
     } else {
         $attributes = $node->attributes();
         $value = dom_import_simplexml($node)->textContent;
         if (isset($attributes['type'])) {
             $type = $this->__valueOf($attributes['type']);
             if (!Spring\Data\XML\Syntax::isPrimitiveType($type)) {
                 throw new Throwable\Instantiation\Exception('Unable to initial class.');
             }
             $value = Core\Convert::changeType($value, $type);
         }
         if (is_string($value)) {
             $attributes = $node->attributes('xml', true);
             if (isset($attributes['space'])) {
                 $space = $this->__valueOf($attributes['space']);
                 if (!Spring\Data\XML\Syntax::isSpacePreserved($space)) {
                     throw new Throwable\Instantiation\Exception('Unable to initial class.');
                 }
             } else {
                 $value = trim($value);
             }
         }
         return $value;
     }
 }
Пример #10
0
 /**
  * This method assigns any property values to the specified object.
  *
  * @access protected
  * @param \SimpleXMLElement $element                        a reference to the "object" node
  * @param mixed &$object                                    a reference to the object
  * @throws Throwable\Parse\Exception                        indicates that a problem occurred
  *                                                          when parsing
  */
 protected function getProperties(Spring\Object\Parser $parser, \SimpleXMLElement $element, &$object)
 {
     $class = new \ReflectionClass($object);
     $element->registerXPathNamespace('spring', Spring\Data\XML::NAMESPACE_URI);
     $fields = $element->xpath('./spring:property');
     foreach ($fields as $field) {
         $attributes = $field->attributes();
         if (!isset($attributes['name'])) {
             throw new Throwable\Parse\Exception('Unable to process Spring XML. Tag ":tag" is missing ":attribute" attribute.', array(':tag' => 'property', ':attribute' => 'name'));
         }
         $name = $parser->valueOf($attributes['name']);
         if (!$parser->isPropertyName($name)) {
             throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid property name, but got ":name".', array(':name' => $name));
         }
         $value = null;
         $children = $parser->getElementChildren($field, null);
         if (!empty($children)) {
             foreach ($children as $child) {
                 $value = $parser->getObjectFromElement($child);
             }
         } else {
             if (isset($attributes['expression'])) {
                 $expression = $parser->valueOf($attributes['expression']);
                 $value = null;
                 /*
                 @eval('$value = ' . $expression . ';');
                 if (isset($attributes['type'])) {
                 	$type = $parser->valueOf($attributes['type']);
                 	if (!$parser->isPrimitiveType($type)) {
                 		throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid primitive type, but got ":type".', array(':type' => $type));
                 	}
                 	if (!isset($value)) {
                 		$type = 'NULL';
                 		$value = null;
                 	}
                 	$value = Core\Convert::changeType($value, $type);
                 }
                 */
             } else {
                 if (isset($attributes['ref'])) {
                     $value = $parser->getObjectFromIdRef($parser->valueOf($attributes['ref']));
                 } else {
                     if (isset($attributes['value'])) {
                         $value = $parser->valueOf($attributes['value']);
                         if (isset($attributes['type'])) {
                             $type = $parser->valueOf($attributes['type']);
                             if (!$parser->isPrimitiveType($type)) {
                                 throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid primitive type, but got ":type".', array(':type' => $type));
                             }
                             $value = Core\Convert::changeType($value, $type);
                         }
                     } else {
                         throw new Throwable\Parse\Exception('Unable to process Spring XML. Tag ":tag" is missing ":attribute" attribute.', array(':tag' => 'property', ':attribute' => 'value'));
                     }
                 }
             }
         }
         if ($class->hasProperty($name)) {
             $property = $class->getProperty($name);
             if (!$property->isPublic()) {
                 throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid property name, but got ":name".', array(':name' => $name));
             }
             $property->setValue($object, $value);
         } else {
             if ($object instanceof \stdClass) {
                 $object->{$name} = $value;
             } else {
                 if ($class->hasMethod('__set')) {
                     $method = $class->getMethod('__set');
                     if ($method->isAbstract() || !$method->isPublic()) {
                         throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid property name, but got ":name".', array(':name' => $name));
                     }
                     $method->invoke($object, $name, $value);
                 } else {
                     throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid property name, but got ":name" for class ":class".', array(':class' => get_class($object), ':name' => $name));
                 }
             }
         }
     }
 }
Пример #11
0
 /**
  * This method parses a "value" node.
  *
  * @access protected
  * @param \SimpleXMLElement $root                           a reference to the root node
  * @param \SimpleXMLElement $node                           a reference to the "value" node
  * @return mixed                                            the value
  * @throws Throwable\Parse\Exception                        indicates that problem occurred while
  *                                                          parsing
  */
 protected function parseValueElement(\SimpleXMLElement $root, \SimpleXMLElement $node)
 {
     $children = $node->children();
     if (count($children) > 0) {
         $value = '';
         foreach ($children as $child) {
             $name = $child->getName();
             switch ($name) {
                 case 'null':
                     $value = $this->parseNullElement($root, $child);
                     break;
                 default:
                     throw new Throwable\Parse\Exception('Unable to process Spring XML. Tag ":tag" has invalid child node ":child".', array(':tag' => 'value', ':child' => $name));
                     break;
             }
         }
         return $value;
     } else {
         $attributes = $node->attributes();
         $value = dom_import_simplexml($node)->textContent;
         if (isset($attributes['type'])) {
             $type = Spring\Data\XML::valueOf($attributes['type']);
             if (!Spring\Data\XML\Syntax::isPrimitiveType($type)) {
                 throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid primitive type, but got ":type".', array(':type' => $type));
             }
             $value = Core\Convert::changeType($value, $type);
         }
         if (is_string($value)) {
             $attributes = $node->attributes('xml', true);
             if (isset($attributes['space'])) {
                 $space = Spring\Data\XML::valueOf($attributes['space']);
                 if (!Spring\Data\XML\Syntax::isSpacePreserved($space)) {
                     throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid "space" token, but got ":token".', array(':token' => $space));
                 }
             } else {
                 $value = trim($value);
             }
         }
         return $value;
     }
 }