Exemple #1
0
 /**
  * Creates a new Row object.
  *
  * @param  array $valueArray Array of values to assign to the row.
  * @return \Khill\Lavacharts\DataTables\Rows\Row
  * @throws \Khill\Lavacharts\Exceptions\FailedCarbonParsing
  * @throws \Khill\Lavacharts\Exceptions\InvalidCellCount
  * @throws \Khill\Lavacharts\Exceptions\InvalidDateTimeString
  * @throws \Khill\Lavacharts\Exceptions\InvalidRowDefinition
  */
 public function create($valueArray)
 {
     if ($valueArray !== null && is_array($valueArray) === false) {
         throw new InvalidRowDefinition($valueArray);
     }
     if ($valueArray === null || is_array($valueArray) === true && empty($valueArray) === true) {
         return new NullRow($this->datatable->getColumnCount());
     }
     $cellCount = count($valueArray);
     $columnCount = $this->datatable->getColumnCount();
     if ($cellCount > $columnCount) {
         throw new InvalidCellCount($cellCount, $columnCount);
     }
     $columnTypes = $this->datatable->getColumnTypes();
     $dateTimeFormat = $this->datatable->getDateTimeFormat();
     $rowData = [];
     foreach ($valueArray as $index => $cell) {
         if ((bool) preg_match('/date|datetime|timeofday/', $columnTypes[$index]) === true) {
             if ($cell instanceof Carbon) {
                 $rowData[] = new DateCell($cell);
             } else {
                 if (isset($dateTimeFormat)) {
                     $rowData[] = DateCell::parseString($cell, $dateTimeFormat);
                 } else {
                     $rowData[] = DateCell::parseString($cell);
                 }
             }
         } else {
             $rowData[] = $cell;
         }
     }
     return new Row($rowData);
 }
Exemple #2
0
 /**
  * Parses a string of JSON data into a DataTable.
  *
  * Most google examples can be passed to this method with some tweaks.
  * PHP requires that only double quotes are used on identifiers.
  * For example:
  *  - {label: 'Team'} would be invalid
  *  - {"label": "Team"} would be accepted.
  *
  * @access public
  * @since  3.0.0
  * @param  string $jsonString JSON string to decode
  * @return \Khill\Lavacharts\DataTables\DataTable
  * @throws \Khill\Lavacharts\Exceptions\InvalidJson
  */
 public static function createFromJson($jsonString)
 {
     $decodedJson = json_decode($jsonString, true);
     if ($decodedJson === null) {
         throw new InvalidJson();
     }
     $datatable = new DataTable();
     foreach ($decodedJson['cols'] as $column) {
         $datatable->addColumn($column['type'], $column['label']);
     }
     foreach ($decodedJson['rows'] as $row) {
         $rowData = [];
         foreach ($row['c'] as $cell) {
             $rowData[] = $cell['v'];
         }
         $datatable->addRow($rowData);
     }
     return $datatable;
 }