Ejemplo n.º 1
0
 /**
  * Analyze row of input data & create $this->struct
  *
  * @param mixed $row
  * @param string $type
  * @return void
  */
 protected function analyzeRow($row, $type)
 {
     // Current row's structure
     $struct = [];
     $rowType = $this->getType($row);
     // If the row is scalar, make it a {"data" => $value} object
     if (is_scalar($row)) {
         $struct[Parser::DATA_COLUMN] = $this->getType($row);
     } elseif (is_object($row)) {
         // process each property of the object
         foreach ($row as $key => $field) {
             $fieldType = $this->getType($field);
             if ($fieldType == "object") {
                 // Only assign the type if the object isn't empty
                 if (Utils::isEmptyObject($field)) {
                     continue;
                 }
                 $this->analyzeRow($field, $type . "." . $key);
             } elseif ($fieldType == "array") {
                 $arrayType = $this->analyze($field, $type . "." . $key);
                 if (false !== $arrayType) {
                     $fieldType = 'arrayOf' . $arrayType;
                 } else {
                     $fieldType = 'NULL';
                 }
             }
             $struct[$key] = $fieldType;
         }
     } elseif ($this->nestedArrayAsJson && is_array($row)) {
         $this->log->log("WARNING", "Unsupported array nesting in '{$type}'! Converting to JSON string.", ['row' => $row]);
         $rowType = $struct[Parser::DATA_COLUMN] = 'string';
     } elseif (is_null($row)) {
         // do nothing
     } else {
         throw new JsonParserException("Unsupported data row in '{$type}'!", ['row' => $row]);
     }
     $this->getStruct()->add($type, $struct);
     return $rowType;
 }