예제 #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 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);
                 }
             }
         }
     });
 }