Skip to content

Commit

Permalink
- change value caster method
Browse files Browse the repository at this point in the history
  • Loading branch information
eLFuvo committed Nov 25, 2021
1 parent d0e752f commit 98b0043
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
25 changes: 13 additions & 12 deletions src/models/MapAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,18 @@ protected function castToDateTime($value): ?string
*/
public function setValue(Model $model, $value, string $label = null)
{
$model->setAttributes([$this->attribute => $this->typecastValue($value, $this->castTo, $label)]);
if (!in_array($this->castTo, array_keys(self::AUTO_CASTING_VALIDATORS))
&& class_exists($this->castTo)) {
/** @var ValueCasterInterface $caster */
$caster = Instance::ensure($this->castTo, ValueCasterInterface::class);
if ($label) {
$caster->setHeaderLabel($label);
}

$caster->cast($model, $this->attribute, $value);
} else {
$model->setAttributes([$this->attribute => $this->typecastValue($value, $this->castTo, $label)]);
}
}

/**
Expand Down Expand Up @@ -208,7 +219,7 @@ public static function detectCasting(Model $model, $attribute): ?string
* @param mixed $value value to be type-casted.
* @param string|callable $type type name or typecast callable.
* @return bool|float|int|string|null typecast result.
* @throws \yii\base\InvalidConfigException
* @throws InvalidArgumentException
*/
protected function typecastValue($value, $type, string $label = null)
{
Expand All @@ -235,16 +246,6 @@ protected function typecastValue($value, $type, string $label = null)
case self::TYPE_DATETIME:
return $this->castToDateTime($value);
default:
if (class_exists($type)) {
/** @var ValueCasterInterface $caster */
$caster = Instance::ensure($type, ValueCasterInterface::class);
if ($label) {
$caster->setHeaderLabel($label);
}

return $caster->cast($this->attribute, $value);
}

throw new InvalidArgumentException('Unsupported type "' . $type . '"');
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/services/AbstractValueCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace elfuvo\import\services;

use ReflectionClass;
use yii\base\Model;

/**
*
Expand All @@ -31,7 +32,7 @@ public function setHeaderLabel(string $label)
/**
* @inheritDoc
*/
abstract public function cast(string $attribute, $value);
abstract public function cast(Model $model, string $attribute, $value);

/**
* @return string
Expand Down
11 changes: 5 additions & 6 deletions src/services/BracketValueCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,23 @@
namespace elfuvo\import\services;

use Yii;
use yii\base\Model;

/**
*
*/
class BracketValueCaster extends AbstractValueCaster
{
/**
* @param string $attribute
* @param bool|int|string $value
* @return bool|int|string|null
* @inheritDoc
*/
public function cast(string $attribute, $value)
public function cast(Model $model, string $attribute, $value)
{
if (is_string($value) && preg_match('#\[(.+)\]#', $value, $matches)) {
return $matches[1];
$value = $matches[1];
}

return $value;
$model->setAttributes([$attribute => $value]);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/services/ValueCasterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace elfuvo\import\services;

use yii\base\Model;

/**
*
*/
Expand All @@ -22,11 +24,12 @@ interface ValueCasterInterface
public function setHeaderLabel(string $label);

/**
* @param \yii\base\Model $model
* @param string $attribute
* @param $value
* @return string|int|bool|null
* @return void
*/
public function cast(string $attribute, $value);
public function cast(Model $model, string $attribute, $value);

/**
* @return string
Expand Down

0 comments on commit 98b0043

Please sign in to comment.