/**
  * Test related method
  *
  * @param array $values
  * @param array $expectedValues
  *
  * @dataProvider dataProviderSort
  */
 public function testSortByProperty($values, $expectedValues)
 {
     $property = 'label';
     // transform values to objects
     foreach ($values as $key => $value) {
         $obj = new \stdClass();
         $obj->{$property} = $value;
         $values[$key] = $obj;
     }
     $sortedValues = SortHelper::sortByProperty($values, $property);
     foreach ($expectedValues as $key => $expectedValue) {
         $this->assertEquals($expectedValue, $sortedValues[$key]->{$property});
     }
 }
예제 #2
0
 /**
  * {@inheritdoc}
  *
  * Translate the locale codes to labels in the current user locale
  * and sort them alphabetically
  *
  * This part is done here because of the choices query is executed just before
  * so we can't access to these properties from form events
  */
 public function finishView(FormView $view, FormInterface $form, array $options)
 {
     if (!isset($view['locales'])) {
         return;
     }
     /** @var array<ChoiceView> $locales */
     $locales = $view['locales'];
     foreach ($locales->vars['choices'] as $locale) {
         $locale->label = $this->localeHelper->getLocaleLabel($locale->label);
     }
     foreach ($locales->vars['preferred_choices'] as $locale) {
         $locale->label = $this->localeHelper->getLocaleLabel($locale->label);
     }
     $locales->vars['choices'] = SortHelper::sortByProperty($locales->vars['choices'], 'label');
     $locales->vars['preferred_choices'] = SortHelper::sortByProperty($locales->vars['preferred_choices'], 'label');
 }