toArray() public method

public toArray ( )
Example #1
0
 /**
  * Creates new common instance (it means persisted)
  *
  * @param \Dibi\Row|\Dibi\Row[] $data
  * @param string $table
  * @param Connection $connection
  * @param IMapper $mapper
  * @return self
  * @throws InvalidArgumentException
  */
 public static function createInstance($data, $table, Connection $connection, IMapper $mapper)
 {
     $dataArray = [];
     $primaryKey = $mapper->getPrimaryKey($table);
     if ($data instanceof DibiRow) {
         $dataArray = [isset($data->{$primaryKey}) ? $data->{$primaryKey} : self::DETACHED_ROW_ID => $data->toArray()];
     } else {
         $e = new InvalidArgumentException('Invalid type of data given, only \\Dibi\\Row, \\Dibi\\Row[], ArrayAccess[] or array of arrays is supported at this moment.');
         if (!is_array($data)) {
             throw $e;
         }
         if (!empty($data)) {
             $record = reset($data);
             if (!$record instanceof DibiRow and !is_array($record) and !$record instanceof ArrayAccess) {
                 throw $e;
             }
         }
         foreach ($data as $record) {
             $record = (array) $record;
             if (isset($record[$primaryKey])) {
                 $dataArray[$record[$primaryKey]] = $record;
             } else {
                 $dataArray[] = $record;
             }
         }
     }
     return new self($dataArray, $table, $connection, $mapper);
 }