Esempio n. 1
0
 /**
  * Decode the given data from the csv format
  *
  * @param string $data
  *            data sent from client to
  *            the api in the given format.
  *
  * @return array associative array of the parsed data
  */
 public function decode($data)
 {
     $decoded = array();
     if (empty($data)) {
         return $decoded;
     }
     $lines = array_filter(explode(PHP_EOL, $data));
     $keys = false;
     $row = static::getRow(array_shift($lines));
     if (is_null(static::$haveHeaders)) {
         //try to guess with the given data
         static::$haveHeaders = !count(array_filter($row, 'is_numeric'));
     }
     static::$haveHeaders ? $keys = $row : ($decoded[] = $row);
     while (($row = static::getRow(array_shift($lines), $keys)) !== FALSE) {
         $decoded[] = $row;
     }
     $char = Object::$separatorChar;
     Object::$separatorChar = false;
     $decoded = Object::toArray($decoded);
     Object::$separatorChar = $char;
     return $decoded;
 }
Esempio n. 2
0
 /**
  * Decode the given data stream
  *
  * @param string $stream A stream resource with data
  *                       sent from client to the api
  *                       in the given format.
  *
  * @return array associative array of the parsed data
  */
 public function decodeStream($stream)
 {
     $decoded = array();
     $keys = false;
     $row = static::getRow(stream_get_line($stream, 0, PHP_EOL));
     if (is_null(static::$haveHeaders)) {
         //try to guess with the given data
         static::$haveHeaders = !count(array_filter($row, 'is_numeric'));
     }
     static::$haveHeaders ? $keys = $row : ($decoded[] = $row);
     while (($row = static::getRow(stream_get_line($stream, 0, PHP_EOL), $keys)) !== FALSE) {
         $decoded[] = $row;
     }
     $char = Object::$separatorChar;
     Object::$separatorChar = false;
     $decoded = Object::toArray($decoded);
     Object::$separatorChar = $char;
     return $decoded;
 }