diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index b272daa..0ac3ada 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -234,27 +234,37 @@ public function __set($key, $value) } if ($this->hasField($key)) { - $field = $this->getField($key); + $this->setOnField($this->getField($key), $value); - if (is_array($value)) { - $field->clear(); - - // This happens when the array is replaced completely by another array - if (count($value)) { - foreach ($value as $k => $v) { - $field->offsetSet($k, $v); - } - } + return; + } - return; - } + throw new NonExistentFieldException("Field '$key' doesn't exist in '" . get_class($this) . "'"); + } + /** + * Set values on a field + * + * @param FieldCollection $field + * @param $value + */ + protected function setOnField(FieldCollection $field, $value) + { + if (!is_array($value)) { $field->offsetSet(0, $value); return; } - throw new NonExistentFieldException("Field '$key' doesn't exist in '" . get_class($this) . "'"); + $field->clear(); + + // This happens when the array is + // replaced completely by another array + if (count($value)) { + foreach ($value as $k => $v) { + $field->offsetSet($k, $v); + } + } } /** diff --git a/src/Entities/FieldCollection.php b/src/Entities/FieldCollection.php index 9965dfa..89a30e2 100644 --- a/src/Entities/FieldCollection.php +++ b/src/Entities/FieldCollection.php @@ -52,10 +52,14 @@ public static function initField($configuration = []) } /** - * {@inheritdoc} + * Validate input of OffsetSet + * + * @param $key + * @param $value + * @throws ItemCountException + * @throws NullValueException */ - public function offsetSet($key, $value) - { + protected function validateSet($key, $value) { if ((is_null($key) || !array_key_exists($key, $this->items)) && $this->count() >= $this->getMaxItems()) { throw new ItemCountException('The maximum number of items has been reached on this field.'); } @@ -63,6 +67,31 @@ public function offsetSet($key, $value) if (is_null($key) && is_null($value)) { throw new NullValueException('You cannot add a null value'); } + } + + /** + * Get a field from a value + * + * @param Field|mixed $value + * @return Field + */ + protected function getFieldInstance($value) { + if ($value instanceof Field) { + return $value; + } + + $container = new $this->type(); + $container->value = $value; + + return $container; + } + + /** + * {@inheritdoc} + */ + public function offsetSet($key, $value) + { + $this->validateSet($key, $value); if (is_null($value)) { $this->offsetUnset($key); @@ -70,21 +99,17 @@ public function offsetSet($key, $value) return; } - if ($value instanceof Field) { - $container = $value; - } else { - $container = new $this->type(); - $container->value = $value; + if (is_null($key)) { + $this->items[] = $this->getFieldInstance($value); + + return; } - if (is_null($key)) { - $this->items[] = $container; - } else { - if ($value instanceof Field && $this->has($key)) { - $this->deleted[] = $this->items[$key]; - } - $this->items[$key] = $container; + if ($value instanceof Field && $this->has($key)) { + $this->deleted[] = $this->items[$key]; } + + $this->items[$key] = $this->getFieldInstance($value); } /**