Creates a model from an array filled with the class data.

@param string $class
@param string|\Illuminate\Database\Eloquent\Model $data
@return \Illuminate\Database\Eloquent\Model
438571
public static function hidrateModel($class, $data)
    {
        if ($data instanceof Model) {
            return $data;
        }

        if (!isset($data['pivot'])) {
            throw new \Exception("The 'pivot' attribute in the {$class} is hidden");
        }

        $model = new $class;
        $primaryKey = $model->getKeyName();

        $model->setAttribute($primaryKey, $data[$primaryKey])->setAttribute('name', $data['name']);
        $model->setRelation(
            'pivot',
            MorphPivot::fromRawAttributes($model, $data['pivot'], 'pivot_table')
        );

        return $model;
    }
323023
protected function createMigration($class)
    {
        $query = new Query;
        $migrate = $query->select(['migrationPath'])
                ->from($this->migrationTable)
                ->where(['version' => $class])
                ->one();
        $filename = DIRECTORY_SEPARATOR . $class . '.php';
        if ($migrate && file_exists($migrate['migrationPath'] . $filename)) {
            $this->migrationPath = $migrate['migrationPath'];
        }

        $file = $this->migrationPath . $filename;
        require_once($file);

        return new $class(['db' => $this->db]);
    }
102698
static private function _create_model(array $data)
    {
        self::$preventInit = true;
        self::$skipDefaultAttributes = true;
        $model = new static();
        $model->attributes = $data;
        
        # Check for primary_key and set init_attrs.
        if (!static::table()->primaryKey()) {
            $model->storedAttributes = $data;
        }
        $model->isNewRecord = false;
        $model->_register();
        $model->init();
        
        return $model;
    }
653332
public final function toClass(string $class): ?array
    {
        $data = null;
        if (!empty($this->data)) {
            $data = $this->data;
            foreach ($data as &$dat) {
                $datClass = new $class();
                foreach ((array) $dat as $key => $value) {
                    $datClass->{$key} = $value;
                }
                $dat = $datClass;
            }
        }

        return $data;
    }
1180499
public function whenCreating($class)
    {
        if (!isset($this->args[$class])) {
            $this->args[$class] = new ClassMeta($class, $this);
        }

        return $this->args[$class];
    }
617612
private static function createObject($data, $class)
    {
        if (!class_exists($class)) {
            throw new \UnexpectedValueException(sprintf('Class %s does not exist.', $class));
        }

        if (!is_subclass_of($class, AbstractResult::class)) {
            throw new \UnexpectedValueException(
                sprintf('Result class %s must extend %s.', $class, AbstractResult::class)
            );
        }

        foreach ($class::getMap() as $property => $propertyClass) {
            if (isset($data[$property])) {
                $data[$property] = self::create($data[$property], $propertyClass);
            }
        }

        return new $class($data);
    }
487962
public function denormalize($data, $class, $format = null, array $context = array())
    {
        if (!($_class = $this->getClassFromArray($data))) {
            return parent::denormalize($data, $class, $format, $context);
        }

        $class = $_class;

        return parent::denormalize($data, $class, $format, $context);
    }
671268
protected function createMigration($class)
    {
        // since Yii 2.0.12 includeMigrationFile() exists, which replaced the code below
        // remove this construct when composer requirement raises above 2.0.12
        if (method_exists($this, 'includeMigrationFile')) {
            $this->includeMigrationFile($class);
        } else {
            $class = trim($class, '\\');
            if (strpos($class, '\\') === false) {
                $file = $this->migrationPath . DIRECTORY_SEPARATOR . $class . '.php';
                require_once($file);
            }
        }

        return new $class(['db' => $this->db, 'compact' => isset($this->compact) ? $this->compact : false]);
    }
529552
protected function transform(stdClass $data, $className = null)
    {
        $model = null;
        $name = !empty($className) ? $className : $this->getFirstPropertyName($data);
        $data = !empty($className) ? $data : $this->getFirstProperty($data);
        $class = '\\Pokemon\\Models\\' . ucfirst(Inflector::singularize($name));
        if (class_exists($class)) {
            /** @var Model $model */
            $model = new $class;
            $model->fill($data);
        }

        return $model;
    }
784079
public function create(array $data = [])
    {
        $model = $this->factory($data);

        $this->save($model);

        return $model;
    }
218746
private function createDataMapper($entityClass)
    {
        /** @var DataMapperInterface $dataMapper */
        $dataMapperClass = isset($this->config['dataMapper']) ? $this->config['dataMapper'] : DoctrineDataMapper::class;

        $meta = $this->builder->getMeta($entityClass);

        if ($meta->storage['type'] == 'api') {
            $dataMapperClass = $meta->storage['handler'];
        }

        switch ($dataMapperClass) {
            case CsvDataMapper::class:
                $connectionClass = CsvDataGateway::class;

                break;

            default:
                $connectionClass = Connection::class;

                break;
        }

        $dataMapper = new $dataMapperClass(
            $this->getConnection($connectionClass),
            $entityClass,
            $meta->storage['table'],
            $this->entityRegistry
        );

        if ($dataMapper instanceof DispatcherAwareInterface) {
            $dataMapper->setDispatcher($this->getDispatcher());
        }

        return $dataMapper;
    }
369268
public function factory(array $data = [])
    {
        $model = $this->newQuery()->getModel()->newInstance();

        $this->setModelData($model, $data);

        return $model;
    }
1288073
public function getOrCreateObject(string $className, array $data)
    {
        $object = $this->objectIdentityMap->tryGetById($className, $data);

        if ($object !== null) {
            return $object;
        }

        return $this->createObject($className, $data);
    }
17923
protected function makeModelInstance($class)
    {
        if ( ! class_exists($class)) {
            throw new UnexpectedValueException("Class '{$class}' does not exist");
        }

        $instance = new $class;

        if ( ! $instance instanceof Model) {
            throw new UnexpectedValueException("Instance of '{$class}' is not a model");
        }

        $this->modelInstance = $instance;

        $this->modelReflection = new ReflectionClass($class);

        return $this;
    }
589579
protected function fill(Model $model, object $data): Model
    {
        $mapper = new ObjectMapper();

        return $mapper->map($model)->fill($data);
    }
1125366
public static function fromData(array $data)
    {
        $reflection = new \ReflectionClass(static::class);
        $instance = $reflection->newInstanceWithoutConstructor();
        $instance->data = $data;
        $instance->jsonUnserialize($data);
        return $instance;
    }
1053062
public function makeCollection(array $values, $class = null)
    {
        $collection = new Collection($values);
    
        if (!is_null($class) && class_exists($class)) {
            $model = new $class();
        
            if ($model instanceof Model) {
                foreach ($collection as $key => $item) {
                    $collection[$key] = $model->newInstance($item);
                }
            }
        }
    
        return $collection;
    }