public function testArrayConfigConverter() { $model = new Post(self::$dtConverter); $model->attachBehaviors([['class' => ConverterBehavior::className(), 'type' => ConverterBehavior::TYPE_DATE_TIME, 'to' => ConverterBehavior::TO_SAVE, 'attributes' => [Post::EVENT_BEFORE_VALIDATE => ['datetime']], 'converter' => ['class' => Converter::className(), 'patterns' => self::$dtConverter->patterns]], ['class' => ConverterBehavior::className(), 'type' => ConverterBehavior::TYPE_DATE_TIME, 'to' => ConverterBehavior::TO_DISPLAY, 'attributes' => [Post::EVENT_AFTER_VALIDATE => ['datetime']]]]); $model->load(['datetime' => '12.12.2016, 11:25'], ''); if ($model->validate()) { $this->assertEquals('1481531100', $model->getPreparedData()['datetime']); $this->assertEquals('12.12.2016, 11:25', $model->datetime); } else { $this->fail(implode(PHP_EOL, $model->getErrors('datetime'))); } }
/** * @inheritdoc * @throws InvalidConfigException */ public function init() { parent::init(); if (is_string($this->converter)) { $this->converter = Instance::ensure($this->converter, Converter::className()); } elseif (is_array($this->converter)) { $this->converter = Yii::createObject($this->converter); } if (!$this->converter instanceof Converter) { throw new InvalidConfigException('Invalid configuration of `$converter` property.'); } if (empty($this->type) || !in_array($this->type, [self::TYPE_DATE, self::TYPE_TIME, self::TYPE_DATE_TIME])) { throw new InvalidConfigException('Invalid configuration of `$type` property.'); } if (empty($this->to) || !in_array($this->to, [self::TO_SAVE, self::TO_DISPLAY])) { throw new InvalidConfigException('Invalid configuration of `$to` property.'); } $this->_method = 'to'; switch ($this->to) { case self::TO_SAVE: $this->_method .= 'Save'; break; case self::TO_DISPLAY: $this->_method .= 'Display'; } switch ($this->type) { case self::TYPE_TIME: $this->_method .= 'Time'; break; case self::TYPE_DATE: $this->_method .= 'Date'; break; case self::TYPE_DATE_TIME: $this->_method .= 'DateTime'; break; } }