Prepares the model, class, and callable for the instance.

@param Model to query for
@param mixed Class/Callable to customize query/data/logic (optional)

@return array Model, class, and callable
320923
private static function prepareProperties($model, $classOrCallable)
    {
        $callable = null;
        $class = $model;

        if (is_object($classOrCallable) && $classOrCallable instanceof \Closure) {
            $callable = $classOrCallable;
        } elseif (is_string($classOrCallable)) {
            $class = $classOrCallable;
        }

        return [$model, $class, $callable];
    }
359676
public function getModelArray(Model $model)
    {
        $result = [];
        foreach ($model->getAttributes() as $key => $value) {
            $result[] = ['type' => 'text', 'name' => $key, 'key' => $key.' :', 'value' => $value];
        }
        unset($result[0]);

        return $result;
    }
196291
public static function prepareI18nModels($model)
    {//echo __METHOD__;var_dump($model->attributes);
        //$languages = LangHelper::activeLanguages();//var_dump($languages);
        //$config = Module::getModuleConfigByClassname(Module::className());//var_dump($config);
        //$langHelper = $config['params']['langHelper'];

        $moduleClass = static::moduleClass();//var_dump($module);exit;
        if (!$moduleClass) $moduleClass = Module::className(); //?? or better throw new \Exception("Can't found module for " . static::className());
        $module = UniModule::getModuleByClassname($moduleClass);

        $langHelper = $module->langHelper;
        //$languages = $langHelper::activeLanguages();//var_dump($languages);
        $editAllLanguages = empty($module->params['editAllLanguages']) ? false : $module->params['editAllLanguages'];
        $languages = $langHelper::activeLanguages($editAllLanguages);//var_dump($languages);exit;

        $mI18n = $model->i18n;//var_dump($mI18n);exit;

        //$conf = Module::getModuleConfigByClassname(Module::className());//var_dump($conf);
        //$contentHelperClass = $conf['params']['contentHelper'];
        //$module = Module::getModuleByClassname(Module::className());
        //$contentHelperClass = $module->contentHelper;
        //$contentHelper = new $contentHelperClass;

        $modelsI18n = [];
        foreach ($mI18n as $modelI18n) {//var_dump($modelI18n->attributes);
            //$modelI18n->body = $contentHelper::afterSelectBody($modelI18n->body); //!! move to self::correctI18nBodies()
            $modelsI18n[$modelI18n->lang_code] = $modelI18n;
        }
        $modelsI18n = static::correctI18nBodies($modelsI18n);//var_dump($modelsI18n);exit;

        foreach ($languages as $langCode => $lang) {
            if (empty($modelsI18n[$langCode])) {
                //$modelsI18n[$langCode] = (new NewsI18n())->loadDefaultValues();
                $newNewsI18n = $module::model('NewsI18n');//var_dump($newNewsI18n);exit;
                $modelsI18n[$langCode] = $newNewsI18n->loadDefaultValues();
                $modelsI18n[$langCode]->lang_code = $langCode;
            }
        }//var_dump($modelsI18n);exit;
        return $modelsI18n;
    }
1233746
protected function getRelations($model)
    {
        $methods = get_class_methods($model);
        if (!$methods) {
            return [];
        }
        foreach ($methods as $method) {
            if (Str::startsWith($method, 'get') && Str::endsWith($method, 'Attribute') && $method !== 'getAttribute') {
                $name = Str::snake(substr($method, 3, -9));
                if (!empty($name)) {
                    $this->setProperty($name, null, true, null);
                }
            } elseif (Str::startsWith($method, 'set') && Str::endsWith($method, 'Attribute') && $method !== 'setAttribute') {
                $name = Str::snake(substr($method, 3, -9));
                if (!empty($name)) {
                    $this->setProperty($name, null, null, true);
                }
            } elseif (Str::startsWith($method, 'scope') && $method !== 'scopeQuery') {
                $name = Str::camel(substr($method, 5));
                if (!empty($name)) {
                    $reflection = new \ReflectionMethod($model, $method);
                    $args       = $this->getParameters($reflection);
                    array_shift($args);
                    $this->setMethod($name, '\Illuminate\Database\Query\Builder|\\' . $reflection->class, $args);
                }
            } elseif (!method_exists('Illuminate\Database\Eloquent\Model', $method) && !Str::startsWith($method, 'get')) {
                $reflection = new ReflectionMethod($model, $method);
                $file       = new SplFileObject($reflection->getFileName());
                $file->seek($reflection->getStartLine() - 1);
                $code       = '';
                while ($file->key() < $reflection->getEndLine()) {
                    $code .= $file->current();
                    $file->next();
                }
                $code  = trim(preg_replace('/\s\s+/', '', $code));
                $begin = strpos($code, 'function(');
                $code  = substr($code, $begin, strrpos($code, '}') - $begin + 1);

                foreach (array('hasOne', 'belongsTo') as $relation) {
                    $search = '$this->' . $relation . '(';
                    if ($pos    = stripos($code, $search)) {
                        $relationObj = $model->$method();
                        if ($relationObj instanceof Relation) {
                            $relatedModel = '\\' . get_class($relationObj->getRelated());

                            $relations = ['hasManyThrough', 'belongsToMany', 'hasMany', 'morphMany', 'morphToMany'];
                            if (in_array($relation, $relations)) {
                                $this->setProperty($method, $this->getCollectionClass($relatedModel) . '|' . $relatedModel . '[]', true, null);
                            } elseif ($relation === "morphTo") {
                                $this->setProperty($method, '\Illuminate\Database\Eloquent\Model|\Eloquent', true, null);
                            } else {
                                $this->setProperty($method, $relatedModel, true, null);
                            }
                        }
                    }
                }
            }
        }
        return $this->properties;
    }
792250
public function setModel($modelClass) {
		$reflection = new ReflectionClass($modelClass);
		if (! $reflection->implementsInterface('ZExt\Model\ModelInterface')) {
			throw new Exception('Class of a model must implements "ModelInterface"');
		}
		
		$this->_modelClass = $modelClass;
		
		if (! empty($this->_models)) {
			$currentModels = $this->_models;
			$this->_models = [];

			foreach ($currentModels as $key => $model) {
				if ($model instanceof $modelClass) {
					$this->_models[$key] = $model;
				} else {
					$model->uninitialize();
				}
			}
		}
				
		return $this;
	}
488963
public static function castModel(Model $model)
    {
        $attributes = array_merge(
            $model->getAttributes(),
            $model->getRelations()
        );

        $visible = array_flip(
            $model->getVisible() ?: array_diff(array_keys($attributes), $model->getHidden())
        );

        $results = [];

        foreach (array_intersect_key($attributes, $visible) as $key => $value) {
            $results[(isset($visible[$key]) ? Caster::PREFIX_VIRTUAL : Caster::PREFIX_PROTECTED).$key] = $value;
        }

        return $results;
    }
226863
protected function applyPostProcessorsToModel(Model $model)
    {
        foreach ($this->postProcessors as $processorClass => $parameters) {

            // if a processor class was added as a value instead of a key, it
            // does not have parameters
            if (is_numeric($processorClass)) {
                $processorClass = $parameters;
                $parameters     = null;
            }

            $processor = $this->makePostProcessor($processorClass, $parameters);

            $model = $processor->process($model);
        }

        return $model;
    }
632414
protected function preprocessNoneSimpleTypes($data, Model $model)
    {
        if (is_object($data) === true) {
            // Object?
            // Remember that we've been here before.
            $this->pool->recursionHandler->addToHive($data);

            // We need to check if this is an object first.
            // When calling is_a('myClass', 'anotherClass') the autoloader is
            // triggered, trying to load 'myClass', although it is just a string.
            if ($data instanceof \Closure) {
                // Closures are handled differently than normal objects
                return $this->processClosure->process($model);
            }

            // Normal object.
            return $this->processObject->process($model);
        }

        // Must be an array.
        return $this->processArray->process($model);
    }
391074
protected function prepareForCreation($class, $attributes)
    {
        // We need to get the attributes of each $value from $values into
        // an instance of the related model so that we make sure that it goes
        // through the $fillable filter pipeline.

        // This adds support for having model instances mixed with values, so whenever
        // we encounter a Model we take it as our instance
        if ($attributes instanceof Model)
        {
            $instance = $attributes;
        }
        // Reaching here means the dev entered raw attributes (similar to insert())
        // so we'll need to pass the attributes through the model to make sure
        // the fillables are respected as expected by the dev.
        else
        {
            $instance = new $class($attributes);
        }
        // Update timestamps on the instance, this will only affect newly
        // created models by adding timestamps to them, otherwise it has no effect
        // on existing models.
        if ($instance->usesTimestamps()) $instance->addTimestamps();
        return $instance->toArray();
    }
53248
public static function with($model)
    {
        $data = static::model()->findAll();

        $idKey = null;
        $whereIn = [];
        foreach ($data as $key => $value) {
            $idKey = $value->primaryKey;
            $whereIn[] = $value->{$value->primaryKey};
        }

        $associatedModel = new $model();
        $associatedData = $associatedModel
            ->where(static::getForeignKey(static::model()->tableName), 'IN', implode(',', $whereIn))
            ->findAll();

        $data = static::buildRelations($data, $associatedModel, $associatedData);

        return $data;
    }
1248954
public function process(Model $model)
    {
        $object = $model->getData();
        // Output data from the class.
        return $this->pool->render->renderExpandableChild(
            $model->setType(static::TYPE_CLASS)
                ->addParameter(static::PARAM_DATA, $object)
                ->addParameter(static::PARAM_NAME, $model->getName())
                ->setNormal('\\' . get_class($object))
                ->setDomid($this->generateDomIdFromObject($object))
                ->injectCallback(
                    $this->pool->createClass('Brainworxx\\Krexx\\Analyse\\Callback\\Analyse\\Objects')
                )
        );
    }
975105
private function getParametersInOrder(callable $callable): array
    {
        if (null === $this->parametersCache) {
            $this->parametersCache = $this->listParameters($callable);
        }

        return $this->parametersCache;
    }
597379
public function fetchAllObj(string $class = null): array
    {
        if ($class == null && $this->modelClass !== null) {
            return parent::fetchAllObj($this->modelClass);
        } else {
            return parent::fetchAllObj($class);
        }
    }
1133400
public function getModelCallbacks(Model $model, $scope = '*') {
        $callbacks = array();
        $models = Configure::read('Admin.modelCallbacks');

        if (isset($models[$model->qualifiedName])) {
            foreach ($models[$model->qualifiedName] as $method => $options) {
                if (is_string($options)) {
                    $options = array('title' => $options, 'access' => 'update', 'scope' => '*');
                } else {
                    $options = $options += array('access' => 'update', 'scope' => '*');
                }

                if ($options['scope'] !== $scope) {
                    continue;
                }

                if ($this->hasAccess($model->qualifiedName, $options['access'])) {
                    $callbacks[$method] = __d('admin', $options['title'], $model->singularName);
                }
            }
        }

        return $callbacks;
    }
1026016
protected function prepareQuery($model)
    {
        // Set the relationships that should be eager loaded
        if (! empty($this->relations)) {
            $model = $model->with($this->relations);
        }

        // Add a basic where clause to the query
        foreach ($this->where as $where) {
            list($attribute, $operator, $value, $boolean) = array_pad($where, 4, null);

            $model = $model->where($attribute, $operator, $value, $boolean);
        }

        // Add a "where in" clause to the query
        foreach ($this->whereIn as $whereIn) {
            list($attribute, $values, $boolean, $not) = array_pad($whereIn, 4, null);

            $model = $model->whereIn($attribute, $values, $boolean, $not);
        }

        // Add a "where not in" clause to the query
        foreach ($this->whereNotIn as $whereNotIn) {
            list($attribute, $values, $boolean) = array_pad($whereNotIn, 3, null);

            $model = $model->whereNotIn($attribute, $values, $boolean);
        }

        // Set the "offset" value of the query
        if ($this->offset > 0) {
            $model = $model->offset($this->offset);
        }

        // Set the "limit" value of the query
        if ($this->limit > 0) {
            $model = $model->limit($this->limit);
        }

        // Add an "order by" clause to the query.
        if (! empty($this->orderBy)) {
            list($attribute, $direction) = $this->orderBy;

            $model = $model->orderBy($attribute, $direction);
        }

        return $model;
    }
202914
public function clearReusableForModel(Model $model) {
        $values = $this->_reusable;
        if(is_null($values)) {
            return;
        }
        $className = get_class($model);
        foreach($values as $key=>$value) {
            if(strpos($key, $className)===0) {
                unset($values[$key]);
            }
        }
        $this->_reusable = $values;
    }
432947
function StartableModel(model,instance) {
    if(!model) {
        throw new Error('model is required')
    }
    if(!instance) {
        throw new Error('instance is required')
    }
    if(!(this instanceof StartableModel)) {
        return new StartableModel(model,instance,index)
    }
    this.model = model
    this.key = model.key
    this.instance= instance
    this.fn = instance[model.startable]
    if(!this.fn) {
        throw new Error('The instance of ' +
                        this.key +
                        ' does not have a method ' +
                        model.startable +
                       ' which is required for starting this service.')
    }
    this.inject = (this.fn.inject || [])
    this.hasDeps = (this.inject.length > 0)
}