Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 70 additions & 2 deletions src/Element/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class Collection extends Fieldset
*/
protected $count = 1;

/**
* Initial key names array (optional parameter)
*
* @var array
*/
protected $keyNames = [];

/**
* Are new elements allowed to be added dynamically ?
*
Expand Down Expand Up @@ -139,6 +146,14 @@ public function setOptions($options)
$this->setCreateNewObjects($options['create_new_objects']);
}

if (isset($options['key_names']) && is_array($options['key_names'])) {
$this->setKeyNames(array_values($options['key_names']));
}

if (isset($options['labels']) && is_array($options['labels'])) {
$this->setLabels(array_values($options['labels']));
}

return $this;
}

Expand Down Expand Up @@ -195,6 +210,11 @@ public function populateValues($data)
));
}

// Can't do anything with empty data
if (empty($data)) {
return;
}

if (!$this->allowRemove && count($data) < $this->count) {
throw new Exception\DomainException(sprintf(
'There are fewer elements than specified in the collection (%s). Either set the allow_remove option '
Expand Down Expand Up @@ -301,6 +321,51 @@ public function getCount()
return $this->count;
}

/**
* Set the labels for each item
*
* @param $labels
* @return Collection
*/
public function setLabels($labels)
{
$this->labels = $labels;
return $this;
}

/**
* Get the labels
*
* @return int
*/
public function getLabels()
{
return $this->labels;
}

/**
* Set the array key names
*
* @param $names
* @return Collection
*/
public function setKeyNames($names)
{
$this->keyNames = $names;
$this->setCount(count($this->keyNames));
return $this;
}

/**
* Get the array key names
*
* @return int
*/
public function getKeyNames()
{
return $this->keyNames;
}

/**
* Set the target element
*
Expand Down Expand Up @@ -472,8 +537,10 @@ public function prepareElement(FormInterface $form)
{
if (true === $this->shouldCreateChildrenOnPrepareElement) {
if ($this->targetElement !== null && $this->count > 0) {
$applyNames = count($this->keyNames) > 0 && count($this->keyNames) === $this->count ? 1 : 0;
while ($this->count > $this->lastChildIndex + 1) {
$this->addNewTargetElementInstance(++$this->lastChildIndex);
$label = isSet($this->labels[$this->lastChildIndex + 1]) ? $this->labels[$this->lastChildIndex + 1] : null;
$this->addNewTargetElementInstance($applyNames ? $this->keyNames[++$this->lastChildIndex] : ++$this->lastChildIndex, $label);
}
}
}
Expand Down Expand Up @@ -564,12 +631,13 @@ protected function createNewTargetElementInstance()
* @return ElementInterface
* @throws Exception\DomainException
*/
protected function addNewTargetElementInstance($name)
protected function addNewTargetElementInstance($name, $label = null)
{
$this->shouldCreateChildrenOnPrepareElement = false;

$elementOrFieldset = $this->createNewTargetElementInstance();
$elementOrFieldset->setName($name);
if($label) $elementOrFieldset->setLabel($label);

$this->add($elementOrFieldset);

Expand Down