protected function configure($config = []) { if (!empty($config)) { $autoCamelCase = isset($this->autoCamelCase) ? $this->autoCamelCase : false; Creator::configure($this, $config, $autoCamelCase); } }
public function getFormField($form, $fieldClass = '\\Mindy\\Form\\Fields\\DropDownField', array $extra = []) { $model = $this->getModel(); $choices = function () use($model) { $list = ['' => '']; $qs = $model->objects()->order(['root', 'lft']); $parents = $qs->all(); foreach ($parents as $model) { $level = $model->level ? $model->level - 1 : $model->level; $list[$model->pk] = $level ? str_repeat("..", $level) . ' ' . $model->name : $model->name; } return $list; }; if ($this->primary || $this->editable === false) { return null; } if ($fieldClass === null) { $fieldClass = $this->choices ? \Mindy\Form\Fields\DropDownField::className() : \Mindy\Form\Fields\CharField::className(); } elseif ($fieldClass === false) { return null; } $validators = []; if ($form->hasField($this->name)) { $field = $form->getField($this->name); $validators = $field->validators; } if ($this->null === false && $this->autoFetch === false && $this instanceof BooleanField === false) { $validator = new RequiredValidator(); $validator->setName($this->name); $validator->setModel($this); $validators[] = $validator; } return Creator::createObject(array_merge(['class' => $fieldClass, 'required' => $this->required || !$this->null, 'form' => $form, 'choices' => empty($this->choices) ? $choices : $this->choices, 'name' => $this->name, 'label' => $this->verboseName, 'hint' => $this->helpText, 'value' => $this->getValue(), 'validators' => array_merge($validators, $this->validators), 'disabled' => [$model->pk]], $extra)); }
public function __construct(Model $model, array $config = []) { Creator::configure($this, $config); $this->_model = $model; }
/** * @param int $batchSize * @return \Mindy\Orm\BatchDataIterator */ public function batch($batchSize = 100) { $this->prepareConditions(); return Creator::createObject(['class' => BatchDataIterator::className(), 'qs' => $this, 'batchSize' => $batchSize, 'db' => $this->getDb(), 'each' => false, 'asArray' => $this->asArray]); }
/** * @param array $rules list of access rules. */ public function setRules($rules) { foreach ($rules as $rule) { if (is_array($rule)) { $this->_rules[] = Creator::createObject(array_merge(['class' => Rule::class], $rule)); } } }
public function getDiscounts() { if ($this->_discounts === null) { $this->_discounts = []; foreach ($this->discounts as $className) { $this->_discounts[] = Creator::createObject($className); } } return $this->_discounts; }
/** * @param $name * @throws Exception * @return \Modules\User\PasswordHasher\IPasswordHasher */ public function getPasswordHasher($name) { if (isset($this->passwordHashers[$name])) { if (!isset($this->_passwordHashers[$name])) { $this->_passwordHashers[$name] = Creator::createObject(['class' => $this->passwordHashers[$name]]); } return $this->_passwordHashers[$name]; } else { throw new Exception("Unknown password hasher"); } }
protected function configure($config = []) { if (!empty($config)) { Creator::configure($this, $config); } }
/** * Converts the object into an array. * The default implementation will return all public property values as an array. * @return array the array representation of the object */ public function toArray() { return Creator::getObjectVars($this); }
/** * Attaches a behavior to this component. * This method will create the behavior object based on the given * configuration. After that, the behavior object will be initialized * by calling its {@link IBehavior::attach} method. * @param string $name the behavior's name. It should uniquely identify this behavior. * @param mixed $behavior the behavior configuration. This is passed as the first * parameter to {@link YiiBase::createComponent} to create the behavior object. * You can also pass an already created behavior instance (the new behavior will replace an already created * behavior with the same name, if it exists). * @return IBehavior the behavior object */ public function attachBehavior($name, $behavior) { if (!$behavior instanceof IBehavior) { $behavior = Creator::createObject($behavior); } $behavior->setEnabled(true); $behavior->attach($this); return $this->_m[$name] = $behavior; }
public function getFormField($form, $fieldClass = null, array $extra = []) { if ($this->primary || $this->editable === false) { return null; } if ($fieldClass === null) { $fieldClass = $this->choices ? \Mindy\Form\Fields\DropDownField::className() : \Mindy\Form\Fields\CharField::className(); } elseif ($fieldClass === false) { return null; } $validators = []; if ($form->hasField($this->name)) { $field = $form->getField($this->name); $validators = $field->validators; } if (($this->null === false || $this->required) && $this->autoFetch === false && $this instanceof BooleanField === false) { $validator = new RequiredValidator(); $validator->setName($this->name); $validator->setModel($this); $validators[] = $validator; } if ($this->unique) { $validator = new UniqueValidator(); $validator->setName($this->name); $validator->setModel($this); $validators[] = $validator; } return Creator::createObject(array_merge(['class' => $fieldClass, 'required' => !$this->canBeEmpty(), 'form' => $form, 'choices' => $this->choices, 'name' => $this->name, 'label' => $this->verboseName, 'hint' => $this->helpText, 'validators' => array_merge($validators, $this->validators), 'value' => $this->default ? $this->default : null], $extra)); }
/** * @return array */ public function index() { $modelClass = $this->getModel(); /* @var $model \Mindy\Orm\Model */ $model = new $modelClass(); /* @var $qs \Mindy\Orm\QuerySet */ $qs = $this->getQuerySet($model); $filterForm = null; $filterFormClass = $this->getFilterForm(); if ($filterFormClass) { $filterForm = new $filterFormClass(); $filterForm->populate($_GET); $attrs = $filterForm->getQsFilter(); if (!empty($attrs)) { $qs->filter($attrs); } } $this->initBreadcrumbs($model); $currentOrder = null; if (isset($this->params['order'])) { $column = $this->params['order']; $currentOrder = $column; if (substr($column, 0, 1) === '-') { $column = ltrim($column, '-'); $sort = "-"; } else { $sort = ""; } $qs = $qs->order([$sort . $column]); } elseif ($this->sortingColumn) { $qs->order([$this->sortingColumn]); } if (isset($this->params['search'])) { $qs = $this->search($qs); } $table = Creator::createObject($this->adminTableClassName, $qs, ['admin' => $this, 'showPkColumn' => $this->showPkColumn, 'sortingColumn' => $this->sortingColumn, 'moduleName' => $this->moduleName, 'currentOrder' => $currentOrder, 'columns' => $this->getColumns(), 'linkColumn' => $this->linkColumn, 'actionsTemplate' => $this->actionsTemplate, 'paginationConfig' => ['pageSize' => $this->pageSize]]); return ['table' => $table, 'filterForm' => $filterForm, 'breadcrumbs' => $this->getBreadcrumbs(), 'currentOrder' => $currentOrder, 'sortingColumn' => $this->sortingColumn, 'searchFields' => $this->getSearchFields()]; }
public function initFields(array $fields = [], $extra = false) { $className = $this->modelClassName; if (empty($fields)) { $fields = $className::getFields(); } $needPk = !$extra; $fkFields = []; $m2mFields = []; foreach ($fields as $name => $config) { /* @var $field \Mindy\Orm\Fields\Field */ if (!is_object($config) && !$config instanceof \Mindy\Orm\Fields\Field) { $field = Creator::createObject($config); $field->setName($name); $field->setModelClass($className); } else { $field = $config; } // $field->setModel($model); if (is_a($field, $className::$autoField) || $field->primary) { $needPk = false; $this->primaryKeyField = $field; } if (is_a($field, $className::$fileField)) { $this->localFileFields[$name] = $field; } if (is_a($field, $className::$relatedField)) { /* @var $field \Mindy\Orm\Fields\RelatedField */ if (is_a($field, $className::$manyToManyField)) { /* @var $field \Mindy\Orm\Fields\ManyToManyField */ $this->manyToManyFields[$name] = $field; $m2mFields[$name] = $field; } if (is_a($field, $className::$hasManyField)) { /* @var $field \Mindy\Orm\Fields\HasManyField */ $this->hasManyFields[$name] = $field; } if (is_a($field, $className::$oneToOneField) && $field->reversed) { /* @var $field \Mindy\Orm\Fields\ForeignField */ $this->oneToOneFields[$name] = $field; } elseif (is_a($field, $className::$foreignField)) { /* @var $field \Mindy\Orm\Fields\ForeignField */ $fkFields[$name] = $field; } } else { $this->localFields[$name] = $field; } $this->allFields[$name] = $field; if (!$extra) { $extraFields = $field->getExtraFields(); if (!empty($extraFields)) { $extraFieldsInitialized = $this->initFields($extraFields, true); foreach ($extraFieldsInitialized as $key => $value) { $field->setExtraField($key, $this->allFields[$key]); $this->extFields[$name] = [$key => $this->allFields[$key]]; } } } } if ($needPk) { $pkName = 'id'; /* @var $autoField \Mindy\Orm\Fields\AutoField */ $autoFieldClass = $className::$autoField; $autoField = new $autoFieldClass(); $autoField->setName($pkName); // $autoField->setModel($model); $this->allFields = array_merge([$pkName => $autoField], $this->allFields); $this->localFields = array_merge([$pkName => $autoField], $this->localFields); $this->primaryKeyField = $autoField; } foreach ($fkFields as $name => $field) { // ForeignKey in self model if ($field->modelClass == $className) { $this->foreignFields[$name . '_' . $this->primaryKeyField->getName()] = $name; } else { $this->foreignFields[$name . '_' . $field->getForeignPrimaryKey()] = $name; } } return $fields; }
public function testCreateExtra() { $obj = Creator::createObject(['class' => 'TestCreate'], ['test' => 1]); $obj = Creator::createObject(['class' => 'TestCreate'], ['test' => 1]); }