Optimizing configuration for models

@param $data
@internal param $item
@internal param $modelData
@return stdClass
195079
public function optimize (stdClass $data)
    {
        $data->modelDirectory = str_replace ('/http/controllers', '/models', $data->controllerDestination);
        $data->modelNamespace = str_replace ('\\http\\controllers', '\\models', $data->controllerNamespace);

        foreach ($data->database as $dbItem) {
            $dbItem->columns       = $this->getTableColumns ($dbItem->tableName);
            $dbItem->modelLocation = $data->modelDirectory . '/' . $dbItem->modelName . '.php';

            if (isset($dbItem->default) && $dbItem->default)
                if (isset($dbItem->multiLanguage)) {
                    $dbItem->multiLanguage->columns       = $this->getTableColumns ($dbItem->multiLanguage->tableName);
                    $dbItem->multiLanguage->modelName     = $dbItem->modelName . 'Translations';
                    $dbItem->multiLanguage->modelLocation = $data->modelDirectory . '/' . $dbItem->modelName . 'Translations.php';
                }
        }

        //TODO get default model, there can be only one
        $data->mainModel = $data->database[0];

        return $data;
    }
882921
public static function createFromModelData(array $modelData, string $table, string $field = null)
    {
        $dc = new static($table);

        $dc->strTable = $table;
        $dc->activeRecord = null;

        if (isset($modelData['id']) && $modelData['id'] > 0) {
            $dc->activeRecord = System::getContainer()->get('huh.utils.model')->findModelInstanceByPk($table, $modelData['id']);
            $dc->intId = $modelData['id'];
        }

        if ($field) {
            $dc->strField = $field;
        }

        return $dc;
    }
39502
public function item($data, $transformer = null, $resourceKey = null)
    {
        if (!$transformer) {
            $transformer = $this->getTransformer();
        }

        return $this->getService()->item($data, $transformer, $resourceKey);
    }
1298344
public function deserializeQuestion(array $itemData, AbstractItem $item = null, array $options = [])
    {
        return $this->getItemSerializer()->deserialize($itemData, $item, $options);
    }
423648
protected function setUpParams($name = '', $requestId = null, $data = null, $listData = '', $itemData = '')
	{
		$params = [];
		if ( ! is_null($this->locale))
		{
			$params['locale'] = $this->locale;
		}
		if ( ! is_null($this->DDversion))
		{
			$params['version'] = $this->DDversion;
		}
		if ( ! $this->appendId($requestId) && $this->dataById($name))
		{
			$params['dataById'] = 'true';
		}
		if ( ! is_null($data))
		{
			if ($this->appendId($requestId))
			{
				if (is_array($data) && ! empty($data))
				{
					$data = implode(",", $data);
				}

				$params[$itemData] = $data;
			}
			else
			{
				if (is_array($data) && ! empty($data))
				{
					$data = implode(",", $data);
				}

				$params[$listData] = $data;
			}
		}

		return $params;
	}
1145718
public function item($item, $transformer, $model_name = '')
    {
        $resource = new Item($item, $transformer, $model_name);
        return $this->manager->createData($resource)->toArray();
    }
1320861
protected function getMorphableModelsForFieldData(ModelFormFieldDataInterface $data)
    {
        $modelsOption = array_get($data->options(), 'models', []);

        // Users may have set the string value with the model class, instead of a class => array value pair
        $modelClasses = array_map(
            function ($key, $value) {
                if (is_string($value)) {
                    return $value;
                }

                return $key;
            },
            array_keys($modelsOption),
            array_values($modelsOption)
        );

        return array_unique($modelClasses);
    }
337460
public function store($data)
    {
        $modelName = $this->modelFullName;
        if($this->validate($data, $modelName::$rules))
        {
            $model = $modelName::create($data);
            return $model;
        }
    }
920299
public function setItem($key, $item) {
		if (is_array($key)) {
			$key = $this->arrayHashFunction($key);
		}
		
		if (! isset($this->_data[$key])) {
			$this->_map[] = $key;
		}
		
		if (is_object($item)) {
			$modelClass = $this->getModel();
			if (! $item instanceof $modelClass) {
				throw new Exception('Model must be an instance of "' . $modelClass . '"');
			}
			
			$item->setCollection($this);
			
			$this->_data[$key]   = &$item->getDataLinked();
			$this->_models[$key] = $item;
		}
		elseif (is_array($item)) {
			$this->_data[$key] = $item;
		}
		else {
			throw new Exception('Unknown type of item: "' . gettype($item) . '"');
		}
		
		return $this;
	}
1002196
public static function findDataProvider( $config = [] ) {

		$modelClass	= static::$modelClass;
		$modelTable = $modelClass::tableName();

		// query generation
		$query			= $config[ 'query' ] ?? $modelClass::find();
		$limit			= $config[ 'limit' ] ?? self::$pageLimit; // Page Limit from config
		$page			= $config[ 'page' ] ?? 0; // Current Page from config
		$page			= Yii::$app->request->get( 'page' ) != null ? Yii::$app->request->get( 'page' ) - 1 : $page; // Current Page from params
		$pageLimit		= Yii::$app->request->get( 'per-page' ); // Page Limit from params
		$conditions		= $config[ 'conditions' ] ?? null;
		$filters		= $config[ 'filters' ] ?? null;
		$random			= $config[ 'random' ] ?? false; // Be careful in using random at database level for tables having high row count
		$softDelete		= $config[ 'softDelete' ] ?? false;

		// search and sort
		$searchParam	= $config[ 'search-param' ] ?? 'keywords';
		$searchCol		= $config[ 'search-col' ] ?? [];
		$sort			= $config[ 'sort' ] ?? false;

		// report
		$reportCol = $config[ 'report-col' ] ?? [];

		// url generation
		$route = $config[ 'route' ] ?? null;

		// pagination
		$pagination = [];

		// Conditions ----------

		if( isset( $conditions ) ) {

			foreach ( $conditions as $ckey => $condition ) {

				if( is_numeric( $ckey ) ) {

					$query->andWhere( $condition );

					unset( $conditions[ $ckey ] );
				}
			}

			$query->andWhere( $conditions );
		}

		$interfaces = class_implements( static::class );

		if( $softDelete || isset( $interfaces[ 'cmsgears\core\common\services\interfaces\base\IApproval' ] ) ) {

			$softDelete = IApproval::STATUS_DELETED;

			$query->andWhere( "$modelTable.status!=$softDelete" );
		}

		// Random -------------

		if( $random ) {

			$query	= $query->orderBy( new Expression( 'rand()' ) );
		}

		// Searching ----------

		$searchTerms	= Yii::$app->request->getQueryParam( $searchParam );

		if( isset( $searchTerms ) && strlen( $searchTerms ) > 0 && count( $searchCol ) > 0 ) {

			$searchTerms	= HtmlPurifier::process( $searchTerms );
			$searchQuery	= static::generateSearchQuery( $searchCol, $searchTerms );
			$query			= $query->andWhere( $searchQuery );
		}

		// Filters -------------

		if( isset( $filters ) ) {

			foreach ( $filters as $filter ) {

				$query	= $query->andFilterWhere( $filter );
			}
		}

		// Reporting -----------

		$report	= Yii::$app->request->getQueryParam( 'report' );

		if( isset( $report ) && $report ) {

			$reportColumns = [];

			foreach( $reportCol as $key => $column ) {

				if( !is_string( $key ) ) {

					$key = $column;
				}

				$find	= Yii::$app->request->getQueryParam( "$key-find" );
				$flag	= Yii::$app->request->getQueryParam( "$key-flag" );
				$match	= Yii::$app->request->getQueryParam( "$key-match" );
				$start	= Yii::$app->request->getQueryParam( "$key-start" );
				$end	= Yii::$app->request->getQueryParam( "$key-end" );

				// String search
				if( isset( $find ) ) {

					$reportColumns[ $column ][ 'find' ] = $find;
				}

				// Flag
				if( isset( $flag ) ) {

					$reportColumns[ $column ][ 'flag' ] = $flag;
				}

				// Numeric
				if( isset( $match ) ) {

					$reportColumns[ $column ][ 'match' ] = $match;
				}

				// Numeric - Range - Start
				if( isset( $start ) ) {

					$reportColumns[ $column ][ 'start' ] = $start;
				}

				// Numeric - Range - End
				if( isset( $end ) ) {

					$reportColumns[ $column ][ 'end' ] = $end;
				}
			}

			foreach( $reportColumns as $key => $column ) {

				$find	= isset( $column[ 'find' ] ) ? $column[ 'find' ] : null;
				$flag	= isset( $column[ 'flag' ] ) ? $column[ 'flag' ] : null;
				$match	= isset( $column[ 'match' ] ) ? $column[ 'match' ] : null;
				$start	= isset( $column[ 'start' ] ) ? $column[ 'start' ] : null;
				$end	= isset( $column[ 'end' ] ) ? $column[ 'end' ] : null;

				// String search
				if( isset( $find ) ) {

					$query->andFilterWhere( [ 'like', $key, $find ] );
				}

				// Flag
				if( isset( $flag ) ) {

					$query->andWhere( "{$key}=:flag", [ ':flag' => $flag ] );
				}

				// Numeric
				if( isset( $match ) ) {

					$query->andWhere( "$key=:match", [ ':match' => $match ] );
				}

				// Numeric - Range - Start & End
				if( isset( $start ) && isset( $end ) ) {

					$query->andWhere( "$key BETWEEN :start AND :end", [ ':start' => $start, ':end' => $end ] );
				}
				// Numeric - Range - Start
				else if( isset( $start ) ) {

					$query->andWhere( "$key >= '$start'" );
				}
				// Numeric - Range - End
				else if( isset( $end ) ) {

					$query->andWhere( "$key <= '$end'" );
				}
			}
		}

		// Print to Debug -------

		/** Use only in case actual generate query is required. It can also be obtained from Yii's awesome
		 * debug bar in debug mode.
		 */
		if( isset( $config[ 'pquery' ] ) && $config[ 'pquery' ] ) {

			$command = $query->createCommand();

			var_dump( $command );
		}

		// Data Provider --------

		$pagination	= [ 'pageSize' => $limit ];

		if( isset( $page ) ) {

			$pagination[ 'page' ] = $page;
		}

		if( isset( $pageLimit ) && $pageLimit <= static::$maxPageLimit ) {

			$pagination[ 'pageSize' ] = $pageLimit;
		}

		if( isset( $route ) ) {

			$pagination[ 'route' ] = $route;
		}

		$dataProvider	= new ActiveDataProvider([
			'query' => $query,
			'sort' => $sort,
			'pagination' => $pagination
		]);

		return $dataProvider;
	}
883002
public function makeData(Data $data): ?DataObjectFormat
    {
        if (!$data->isInstanceOf(Data::class)) {
            return null;
        }

        return DataObjectFormat::make($data);
    }
40658
public function getConfigData($data = null) {
        if ($data !== null && isset($this->_config[$data])) {
            return $this->_config[$data];
        } elseif ($data === null) {
            return $this->_config;
        } else {
            throw new ConfigException("Error while requesting config value: " . $data);
        }
    }
182701
public function set(DataItem $item): void
    {
        $command = [
            'command' => 'set',
            'key' => $item->key,
            'body' => $item->value,
            'metadata.*' => ''
        ];
        $metadataAsArray = $item->metadata->toArray();
        foreach ($metadataAsArray as $name => $value) {
            $command['metadata.' . $name] = $value;
        }
        $this->execute([$command]);
    }
298166
public function create(array $data = array())
    {
        if (!isset($data["configPathPrefix"])) {
            $data["configPathPrefix"] = $this->configPathPrefix;
        }
        return $this->_objectManager->create($this->_instanceName, $data);
    }
1203192
protected function prepareItem(array $item, array $data)
    {
        $product = $this->product->getBySku($item['sku'], $item['store_id']);

        if (empty($product['status']) || $data['store_id'] != $product['store_id']) {
            return array();
        }

        $product['price'] = $this->currency->convert($product['price'], $product['currency'], $data['currency']);

        $calculated = $this->product->calculate($product);

        if ($calculated != $product['price']) {
            $item['original_price'] = $product['price'];
        }

        $item['product'] = $product;
        $item['price'] = $calculated;
        $item['total'] = $item['price'] * $item['quantity'];

        return $item;
    }
333084
protected function transformDataForRepository($modelData)
    {
        foreach ($this->columnTransforms as $columnName => $transforms) {
            if ($transforms[1] !== null) {
                $closure = $transforms[1];

                $transformedData = $closure($modelData);

                if (is_array($transformedData)) {
                    // If the original value is to be retained, the transform function should
                    // explicitly return it - in other cases we need to unset it here.
                    unset($modelData[$columnName]);

                    $modelData = array_merge($modelData, $transformedData);
                } else {
                    $modelData[$columnName] = $transformedData;
                }
            }
        }

        return $modelData;
    }
1113931
public static function config_get_configuration_array($data) {
        return array(
            'server' => $data->server,
            'prefix' => $data->prefix,
            'password' => $data->password,
            'serializer' => $data->serializer
        );
    }