8000 [Form] Allow choices with duplicated content by ewgRa · Pull Request #16436 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Allow choices with duplicated content #16436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
phpdocs and interfaces
  • Loading branch information
ewgRa committed Nov 3, 2015
commit 63509d126307e29d16a3da6e3fba28eec1382240
63 changes: 48 additions & 15 deletions src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,26 @@ class ArrayChoiceList implements ChoiceListInterface
*/
protected $valueCallback;

/**
* The raw choice of the choices array.
*
* @var array
*/
protected $rawChoices;

protected $rawKeys;
/**
* The raw choice values of the choices array.
*
* @var array
*/
protected $rawChoiceValues;

protected $rawLabels;
/**
* The raw keys of the choices array.
*
* @var int[]|string[]
*/
protected $rawKeys;

/**
* Creates a list with the given choices and values.
Expand All @@ -69,6 +84,8 @@ class ArrayChoiceList implements ChoiceListInterface
* for a choice. If `null` is passed,
* incrementing integers are used as
* values
*
* @throws UnexpectedTypeException
*/
public function __construct($choices, $value = null)
{
Expand All @@ -94,14 +111,14 @@ public function __construct($choices, $value = null)
// If the choices are given as recursive array (i.e. with explicit
// choice groups), flatten the array. The grouping information is needed
// in the view only.
$this->flatten($choices, $value, $choicesByValues, $rawChoices, $keysByValues, $rawKeys, $rawLabels, $structuredValues);
$this->flatten($choices, $value, $choicesByValues, $keysByValues, $structuredValues, $rawChoices, $rawChoiceValues, $rawKeys);

$this->choices = $choicesByValues;
$this->rawChoices = $rawChoices;
$this->originalKeys = $keysByValues;
$this->rawKeys = $rawKeys;
$this->rawLabels = $rawLabels;
$this->structuredValues = $structuredValues;
$this->rawChoices = $rawChoices;
$this->rawChoiceValues = $rawChoiceValues;
$this->rawKeys = $rawKeys;
}

/**
Expand Down Expand Up @@ -136,19 +153,28 @@ public function getOriginalKeys()
return $this->originalKeys;
}

/**
* {@inheritdoc}
*/
public function getRawChoices()
{
return $this->rawChoices;
}

public function getRawKeys()
/**
* {@inheritdoc}
*/
public function getRawChoiceValues()
{
return $this->rawKeys;
return $this->rawChoiceValues;
}

public function getRawLabels()
/**
* {@inheritdoc}
*/
public function getRawKeys()
{
return $this->rawLabels;
return $this->rawKeys;
}

/**
Expand Down Expand Up @@ -208,30 +234,37 @@ public function getValuesForChoices(array $choices)
* @param array $keysByValues The original keys indexed by the
* corresponding values
*
* @param $structuredValues
* @param $rawChoices
* @param $rawChoiceValues
* @param $rawKeys
* @internal Must not be used by user-land code
*/
protected function flatten(array $choices, $value, &$choicesByValues, &$rawChoices, &$keysByValues, &$rawKeys, &$rawLabels, &$structuredValues)
protected function flatten(array $choices, $value, &$choicesByValues, &$keysByValues, &$structuredValues, &$rawChoices, &$rawChoiceValues, &$rawKeys)
{
if (null === $choicesByValues) {
$choicesByValues = array();
$keysByValues = array();
$structuredValues = array();
$rawChoices = array();
$rawChoiceValues = array();
$rawKeys = array();
}

foreach ($choices as $key => $choice) {
if (is_array($choice)) {
$this->flatten($choice, $value, $choicesByValues, $rawChoices[$key], $keysByValues, $rawKeys[$key], $rawLabels[$key], $structuredValues[$key]);
$this->flatten($choice, $value, $choicesByValues, $keysByValues, $structuredValues[$key], $rawChoices[$key], $rawChoiceValues[$key], $rawKeys[$key]);

continue;
}

$choiceValue = (string) call_user_func($value, $choice);
$choicesByValues[$choiceValue] = $choice;
$keysByValues[$choiceValue] = $key;
$rawChoices[$key] = $choice;
$rawKeys[$key] = $choiceValue;
$rawLabels[$key] = $key;
$structuredValues[$key] = $choiceValue;
$rawChoices[$key] = $choice;
$rawChoiceValues[$key] = $choiceValue;
$rawKeys[$key] = $key;
}
}
}
15 changes: 10 additions & 5 deletions src/Symfony/Component/Form/ChoiceList/ArrayKeyChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,32 +160,37 @@ public function getValuesForChoices(array $choices)
* @param array $keysByValues The original keys indexed by the
* corresponding values
*
* @param $structuredValues
* @param $rawChoices
* @param $rawChoiceValues
* @param $rawKeys
* @internal Must not be used by user-land code
*/
protected function flatten(array $choices, $value, &$choicesByValues, &$rawChoices, &$keysByValues, &$rawKeys, &$rawLabels, &$structuredValues)
protected function flatten(array $choices, $value, &$choicesByValues, &$keysByValues, &$structuredValues, &$rawChoices, &$rawChoiceValues, &$rawKeys)
{
if (null === $choicesByValues) {
$choicesByValues = array();
$keysByValues = array();
$structuredValues = array();
$rawChoices = array();
$rawChoiceValues = array();
$rawKeys = array();
}

foreach ($choices as $choice => $key) {
if (is_array($key)) {
$this->flatten($key, $value, $choicesByValues, $rawChoices[$choice], $keysByValues, $rawKeys[$choice], $rawLabels[$choice], $structuredValues[$choice]);
$this->flatten($key, $value, $choicesByValues, $keysByValues, $structuredValues[$choice], $rawChoices[$choice], $rawChoiceValues[$choice], $rawKeys[$choice]);

continue;
}

$choiceValue = (string) call_user_func($value, $choice);
$choicesByValues[$choiceValue] = $choice;
$keysByValues[$choiceValue] = $key;
$rawChoices[$choice] = $choiceValue;
$rawKeys[$choice] = $choiceValue;
$rawLabels[$choice] = $key;
$structuredValues[$key] = $choiceValue;
$rawChoices[$choice] = $choiceValue;
$rawChoiceValues[$choice] = $choiceValue;
$rawKeys[$choice] = $key;
}
}
}
15 changes: 15 additions & 0 deletions src/Symfony/Component/Form/ChoiceList/ChoiceListInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ public function getStructuredValues();
*/
public function getOriginalKeys();

/**
* Returns the raw choices.
*/
public function getRawChoices();

/**
* Returns the raw choice values.
*/
public function getRawChoiceValues();

/**
* Returns the raw keys.
*/
public function getRawKeys();

/**
* Returns the choices corresponding to the given values.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,11 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
}
} else {
// Otherwise use the original structure of the choices
self::addStructuredChoiceViews(
self::addChoiceViews(
$list->getRawChoices(),
$list->getRawChoiceValues(),
$label,
$choices,
$list->getRawKeys(),
$list->getRawLabels(),
$index,
$attr,
$preferredChoices,
Expand Down Expand Up @@ -173,9 +172,9 @@ private static function addChoiceView($data, $value, $labelCallback, $label, &$i
}
}

private static function addStructuredChoiceViews($structuredValues, $labelCallback, $choices, $keys, $labels, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
private static function addChoiceViews($dataValues, $values, $labelCallback, $labels, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
{
foreach ($structuredValues as $key => $data) {
foreach ($dataValues as $key => $data) {
if (null === $data) {
continue;
}
Expand All @@ -185,11 +184,10 @@ private static function addStructuredChoiceViews($structuredValues, $labelCallba
$preferredViewsForGroup = array();
$otherViewsForGroup = array();

self::addStructuredChoiceViews(
self::addChoiceViews(
$data,
$values[$key],
$labelCallback,
$choices,
$keys[$key],
$labels[$key],
$index,
$attr,
Expand All @@ -212,7 +210,7 @@ private static function addStructuredChoiceViews($structuredValues, $labelCallba
// Add ungrouped items directly
self::addChoiceView(
$data,
$keys[$key],
$values[$key],
$labelCallback,
$labels[$key],
$index,
Expand All @@ -224,17 +222,17 @@ private static function addStructuredChoiceViews($structuredValues, $labelCallba
}
}

private static function addChoiceViewGroupedBy($groupBy, $choice, $value, $label, $key, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
private static function addChoiceViewGroupedBy($groupBy, $data, $value, $labelCallback, $label, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
{
$groupLabel = call_user_func($groupBy, $choice, $key, $value);
$groupLabel = call_user_func($groupBy, $data, $label, $value);

if (null === $groupLabel) {
// If the callable returns null, don't group the choice
self::addChoiceView(
$choice,
$data,
$value,
$labelCallback,
$label,
$key,
$index,
$attr,
$isPreferred,
Expand All @@ -255,10 +253,10 @@ private static function addChoiceViewGroupedBy($groupBy, $choice, $value, $label
}

self::addChoiceView(
$choice,
$data,
$value,
$labelCallback,
$label,
$key,
$index,
$attr,
$isPreferred,
Expand Down
36 changes: 36 additions & 0 deletions src/Symfony/Component/Form/ChoiceList/LazyChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,42 @@ public function getStructuredValues()
return $this->loadedList->getStructuredValues();
}

/**
* {@inheritdoc}
*/
public function getRawChoices()
{
if (!$this->loadedList) {
$this->loadedList = $this->loader->loadChoiceList($this->value);
}

return $this->loadedList->getRawChoices();
}

/**
* {@inheritdoc}
*/
public function getRawChoiceValues()
{
if (!$this->loadedList) {
$this->loadedList = $this->loader->loadChoiceList($this->value);
}

return $this->loadedList->getRawChoiceValues();
}

/**
* {@inheritdoc}
*/
public function getRawKeys()
{
if (!$this->loadedList) {
$this->loadedList = $this->loader->loadChoiceList($this->value);
}

return $this->loadedList->getRawKeys();
}

/**
* {@inheritdoc}
*/
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/Form/ChoiceList/LegacyChoiceListAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ public function getOriginalKeys()
return array_flip($this->structuredValues);
}

/**
* {@inheritdoc}
*/
public function getRawChoices()
{
throw new \BadMethodCallException('Raw choices not support by LegacyChoiceList');
}

/**
* {@inheritdoc}
*/
public function getRawChoiceValues()
{
throw new \BadMethodCallException('Raw choice values not support by LegacyChoiceList');
}

/**
* {@inheritdoc}
*/
public function getRawKeys()
{
throw new \BadMethodCallException('Raw keys not support by LegacyChoiceList');
}

/**
* {@inheritdoc}
*/
Expand Down
0