class-column.php
190 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex\Schema; |
| 4 | |
| 5 | /** |
| 6 | * Helper to represent the schema for a column |
| 7 | * |
| 8 | * @phpstan-type ColumnType 'integer'|'string'|'date'|'member'|'keyvalue' |
| 9 | * @phpstan-type ColumnOption array{value: string, label: string} |
| 10 | * @phpstan-type ColumnSchemaJson array{ |
| 11 | * column?: string, |
| 12 | * type?: ColumnType, |
| 13 | * global?: bool, |
| 14 | * join?: string, |
| 15 | * joined_by?: string, |
| 16 | * options?: list<ColumnOption> |
| 17 | * } |
| 18 | */ |
| 19 | class Column { |
| 20 | /** |
| 21 | * Column name |
| 22 | */ |
| 23 | private string $column = ''; |
| 24 | |
| 25 | /** |
| 26 | * Column type |
| 27 | * |
| 28 | * @var ColumnType |
| 29 | */ |
| 30 | private string $type = 'string'; |
| 31 | |
| 32 | /** |
| 33 | * Is this a global column? |
| 34 | */ |
| 35 | private bool $global = false; |
| 36 | |
| 37 | /** |
| 38 | * Join column, if any |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | private string $join = ''; |
| 43 | |
| 44 | /** |
| 45 | * Joined by column |
| 46 | * |
| 47 | * @var string |
| 48 | */ |
| 49 | private string $joined_by = ''; |
| 50 | |
| 51 | /** |
| 52 | * Any options, if this is a member type |
| 53 | * |
| 54 | * @var list<ColumnOption> |
| 55 | */ |
| 56 | private array $options = []; |
| 57 | |
| 58 | /** |
| 59 | * Source name for this column |
| 60 | */ |
| 61 | private Source $source; |
| 62 | |
| 63 | /** |
| 64 | * Constructor |
| 65 | * |
| 66 | * @param ColumnSchemaJson $schema JSON. |
| 67 | * @param Source $source Source. |
| 68 | */ |
| 69 | public function __construct( $schema, Source $source ) { |
| 70 | $this->source = $source; |
| 71 | |
| 72 | if ( isset( $schema['type'] ) ) { |
| 73 | $this->type = $schema['type']; |
| 74 | } |
| 75 | |
| 76 | if ( isset( $schema['column'] ) ) { |
| 77 | $this->column = $schema['column']; |
| 78 | } |
| 79 | |
| 80 | if ( isset( $schema['global'] ) ) { |
| 81 | $this->global = $schema['global']; |
| 82 | } |
| 83 | |
| 84 | if ( isset( $schema['join'] ) ) { |
| 85 | $this->join = $schema['join']; |
| 86 | } |
| 87 | |
| 88 | if ( isset( $schema['joined_by'] ) ) { |
| 89 | $this->joined_by = $schema['joined_by']; |
| 90 | } |
| 91 | |
| 92 | // @phpstan-ignore booleanAnd.rightAlwaysTrue |
| 93 | if ( isset( $schema['options'] ) && is_array( $schema['options'] ) ) { |
| 94 | $this->options = $schema['options']; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get source name |
| 100 | * |
| 101 | * @return string |
| 102 | */ |
| 103 | public function get_source() { |
| 104 | return $this->source->get_type(); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get table name |
| 109 | * |
| 110 | * @return string |
| 111 | */ |
| 112 | public function get_table() { |
| 113 | return $this->source->get_table(); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Get column type |
| 118 | * |
| 119 | * @return ColumnType |
| 120 | */ |
| 121 | public function get_type() { |
| 122 | return $this->type; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get column |
| 127 | * |
| 128 | * @return string |
| 129 | */ |
| 130 | public function get_column() { |
| 131 | return $this->column; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get column join |
| 136 | * |
| 137 | * @return string|null |
| 138 | */ |
| 139 | public function get_join_column() { |
| 140 | return $this->join; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get column joined by |
| 145 | * |
| 146 | * @return string|null |
| 147 | */ |
| 148 | public function get_joined_by() { |
| 149 | return $this->joined_by; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Get column options |
| 154 | * |
| 155 | * @return list<ColumnOption> |
| 156 | */ |
| 157 | public function get_options() { |
| 158 | return $this->options; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get label for a particular column, or false |
| 163 | * |
| 164 | * @param string $value Value name. |
| 165 | * @return string|false |
| 166 | */ |
| 167 | public function get_option_label( $value ) { |
| 168 | $options = $this->get_options(); |
| 169 | |
| 170 | if ( $options ) { |
| 171 | foreach ( $options as $option ) { |
| 172 | if ( $option['value'] === $value ) { |
| 173 | return $option['label']; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Is this a global column? |
| 183 | * |
| 184 | * @return boolean |
| 185 | */ |
| 186 | public function is_global() { |
| 187 | return $this->global; |
| 188 | } |
| 189 | } |
| 190 |