PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 4.4.1
Admin Columns v4.4.1
7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Helper / Select / Formatter.php
codepress-admin-columns / classes / Helper / Select Last commit date
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 }