Group.php
1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace AC\Helper\Select;
use AC\ArrayIterator;
abstract class Group extends ArrayIterator {
/**
* @var Formatter
*/
private $formatter;
/**
* @param Formatter $formatter
*/
public function __construct( Formatter $formatter ) {
$this->formatter = $formatter;
parent::__construct( $this->get_group() );
}
/**
* @param array $groups
*
* @return array
*/
protected function sort( array $groups ) {
uksort( $groups, 'strnatcmp' );
return $groups;
}
/**
* @return OptionGroup[]
*/
protected function get_group() {
$groups = [];
foreach ( $this->formatter as $option ) {
$label = $this->get_label(
$this->formatter->get_entity( $option->get_value() ),
$option
);
$groups[ $label ][] = $option;
}
return $this->get_option_groups( $this->sort( $groups ) );
}
/**
* @param array $groups
*
* @return OptionGroup[]
*/
protected function get_option_groups( array $groups ) {
$option_groups = [];
foreach ( $groups as $label => $options ) {
$option_groups[] = new OptionGroup( $label, $options );
}
return $option_groups;
}
/**
* @param $entity
* @param Option $option
*
* @return string
*/
protected abstract function get_label( $entity, Option $option );
}