Entities
4 years ago
Formatter
4 years ago
Group
4 years ago
Options
4 years ago
Value
4 years ago
Entities.php
4 years ago
Formatter.php
4 years ago
Group.php
4 years ago
MetaValuesFactory.php
4 years ago
Option.php
4 years ago
OptionGroup.php
4 years ago
Options.php
4 years ago
Paginated.php
4 years ago
Response.php
4 years ago
Value.php
4 years ago
Formatter.php
118 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Helper\Select; |
| 4 | |
| 5 | use AC\ArrayIterator; |
| 6 | |
| 7 | abstract class Formatter extends ArrayIterator { |
| 8 | |
| 9 | /** |
| 10 | * @var Entities |
| 11 | */ |
| 12 | private $entities; |
| 13 | |
| 14 | /** |
| 15 | * @var Value |
| 16 | */ |
| 17 | protected $value; |
| 18 | |
| 19 | /** |
| 20 | * @param Entities $entities |
| 21 | * @param Value $value |
| 22 | */ |
| 23 | public function __construct( Entities $entities, Value $value = null ) { |
| 24 | $this->entities = $entities; |
| 25 | $this->value = $value; |
| 26 | |
| 27 | parent::__construct( $this->get_labels() ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @return Entities |
| 32 | */ |
| 33 | public function get_entities() { |
| 34 | return $this->entities; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param $value |
| 39 | * |
| 40 | * @return mixed |
| 41 | */ |
| 42 | public function get_entity( $value ) { |
| 43 | if ( ! $this->entities->has_offset( $value ) ) { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | return $this->entities->get_offset( $value ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @return Option[] |
| 52 | */ |
| 53 | protected function get_labels() { |
| 54 | $labels = []; |
| 55 | |
| 56 | foreach ( $this->entities as $value => $entity ) { |
| 57 | $labels[ $value ] = $this->get_label( $entity ); |
| 58 | } |
| 59 | |
| 60 | if ( $this->value ) { |
| 61 | $labels = $this->get_labels_unique( $labels ); |
| 62 | } |
| 63 | |
| 64 | return $this->get_options( $labels ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param $entity |
| 69 | * |
| 70 | * @return string |
| 71 | */ |
| 72 | protected abstract function get_label( $entity ); |
| 73 | |
| 74 | /** |
| 75 | * @param $labels |
| 76 | * |
| 77 | * @return array |
| 78 | */ |
| 79 | protected function get_labels_unique( array $labels ) { |
| 80 | $duplicates = array_diff_assoc( $labels, array_unique( $labels ) ); |
| 81 | |
| 82 | foreach ( $labels as $value => $label ) { |
| 83 | if ( ! in_array( $label, $duplicates ) ) { |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | $labels[ $value ] = $this->get_label_unique( $label, $this->get_entity( $value ) ); |
| 88 | } |
| 89 | |
| 90 | return $labels; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @param string $label |
| 95 | * @param mixed $entity |
| 96 | * |
| 97 | * @return string |
| 98 | */ |
| 99 | protected function get_label_unique( $label, $entity ) { |
| 100 | return $label . sprintf( ' (%s)', $this->value->get_value( $entity ) ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @param array $labels |
| 105 | * |
| 106 | * @return Option[] |
| 107 | */ |
| 108 | private function get_options( array $labels ) { |
| 109 | $options = []; |
| 110 | |
| 111 | foreach ( $labels as $value => $label ) { |
| 112 | $options[] = new Option( $value, $label ); |
| 113 | } |
| 114 | |
| 115 | return $options; |
| 116 | } |
| 117 | |
| 118 | } |