Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
Small refactoring on setters
Browse files Browse the repository at this point in the history
  • Loading branch information
onigoetz committed Jun 7, 2016
1 parent 24c8263 commit e3a2d50
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 28 deletions.
36 changes: 23 additions & 13 deletions src/Entities/Entity.php
Expand Up @@ -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);
}
}
}

/**
Expand Down
55 changes: 40 additions & 15 deletions src/Entities/FieldCollection.php
Expand Up @@ -52,39 +52,64 @@ 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.');
}

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);

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);
}

/**
Expand Down

0 comments on commit e3a2d50

Please sign in to comment.