Passed
Push — master ( 0b2501...8535c6 )
by P.R.
05:00
created

TableColumnsMetadata::appendTableColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SetBased\Audit\Metadata;
5
6
use SetBased\Audit\MySql\Metadata\AlterColumnMetadata;
7
use SetBased\Audit\MySql\Metadata\AuditColumnMetadata;
8
use SetBased\Audit\MySql\Metadata\ColumnMetadata;
9
use SetBased\Exception\FallenException;
10
11
/**
12
 * Metadata of a list of table columns.
13
 */
14
class TableColumnsMetadata
15
{
16
  //--------------------------------------------------------------------------------------------------------------------
17
  /**
18
   * The metadata of the columns.
19
   *
20
   * @var ColumnMetadata[]
21
   */
22
  private $columns = [];
23
24
  //--------------------------------------------------------------------------------------------------------------------
25
  /**
26
   * Object constructor.
27
   *
28
   * @param array[] $columns The metadata of the columns as returned by AuditDataLayer::getTableColumns().
29
   * @param string  $type    The class for columns metadata.
30
   */
31 31
  public function __construct(array $columns = [], string $type = 'ColumnMetadata')
32
  {
33 31
    foreach ($columns as $columnName => $column)
34
    {
35 30
      $this->columns[$column['column_name']] = static::columnFactory($type, $column);
36
    }
37 31
  }
38
39
  //--------------------------------------------------------------------------------------------------------------------
40
  /**
41
   * Combines the metadata of two lists of table columns.
42
   *
43
   * @param TableColumnsMetadata $columns1 The first metadata of a list of table columns.
44
   * @param TableColumnsMetadata $columns2 The second metadata of a list of table columns.
45
   *
46
   * @return TableColumnsMetadata
47
   */
48 28
  public static function combine(TableColumnsMetadata $columns1, TableColumnsMetadata $columns2): TableColumnsMetadata
49
  {
50 28
    $columns = new TableColumnsMetadata();
51
52 28
    $columns->appendTableColumns($columns1);
53 28
    $columns->appendTableColumns($columns2);
54
55 28
    return $columns;
56
  }
57
58
  //--------------------------------------------------------------------------------------------------------------------
59
  /**
60
   * Compares two lists of table columns and returns a list of  table columns the are in both lists but have different
61
   * metadata
62
   *
63
   * @param TableColumnsMetadata $columns1 The first list of table columns.
64
   * @param TableColumnsMetadata $columns2 The second list of table columns.
65
   * @param string[]             $ignore   The properties to be ignored.
66
   *
67
   * @return TableColumnsMetadata
68
   */
69 28
  public static function differentColumnTypes(TableColumnsMetadata $columns1,
70
                                              TableColumnsMetadata $columns2,
71
                                              array $ignore = []): TableColumnsMetadata
72
  {
73 28
    $diff = new TableColumnsMetadata();
74 28
    foreach ($columns1->columns as $column_name => $column1)
75
    {
76 28
      if (isset($columns2->columns[$column_name]))
77
      {
78 28
        if (!ColumnMetadata::compare($column1, $columns2->columns[$column_name], $ignore))
79
        {
80 28
          $diff->appendTableColumn($column1);
81
        }
82
      }
83
    }
84
85 28
    return $diff;
86
  }
87
88
  //--------------------------------------------------------------------------------------------------------------------
89
  /**
90
   * Compares two lists of table columns and returns a list of table columns that are in the first list of table columns
91
   * but not in the second list of table columns.
92
   *
93
   * @param TableColumnsMetadata $columns1 The first list of table columns.
94
   * @param TableColumnsMetadata $columns2 The second list of table columns.
95
   *
96
   * @return TableColumnsMetadata
97
   */
98 28
  public static function notInOtherSet(TableColumnsMetadata $columns1,
99
                                       TableColumnsMetadata $columns2): TableColumnsMetadata
100
  {
101 28
    $diff = new TableColumnsMetadata();
102 28
    foreach ($columns1->columns as $column_name => $column1)
103
    {
104 28
      if (!isset($columns2->columns[$column_name]))
105
      {
106 28
        $diff->appendTableColumn($column1);
107
      }
108
    }
109
110 28
    return $diff;
111
  }
112
113
  //--------------------------------------------------------------------------------------------------------------------
114
  /**
115
   * A factory for table column metadata.
116
   *
117
   * @param string $type   The type of the metadata.
118
   * @param array  $column The metadata of the column
119
   *
120
   * @return AlterColumnMetadata|AuditColumnMetadata|ColumnMetadata
121
   */
122 30
  private static function columnFactory(string $type, array $column): ColumnMetadata
123
  {
124 30
    switch ($type)
125
    {
126 30
      case 'ColumnMetadata':
127 30
        return new ColumnMetadata($column);
128
129 25
      case 'AlterColumnMetadata':
130
        return new AlterColumnMetadata($column);
131
132 25
      case 'AuditColumnMetadata':
133 25
        return new AuditColumnMetadata($column);
134
135
      default:
136
        throw new FallenException('type', $type);
137
    }
138
  }
139
140
  //--------------------------------------------------------------------------------------------------------------------
141
  /**
142
   * Appends a table column to this list of table columns.
143
   *
144
   * @param ColumnMetadata $column The metadata of the table column.
145
   */
146 28
  public function appendTableColumn(ColumnMetadata $column): void
147
  {
148 28
    $this->columns[$column->getName()] = $column;
149 28
  }
150
151
  //--------------------------------------------------------------------------------------------------------------------
152
  /**
153
   * Appends table columns to this list of table columns.
154
   *
155
   * @param TableColumnsMetadata $columns The metadata of the table columns.
156
   */
157 28
  public function appendTableColumns(TableColumnsMetadata $columns): void
158
  {
159 28
    foreach ($columns->columns as $column)
160
    {
161 28
      $this->appendTableColumn($column);
162
    }
163 28
  }
164
165
  //--------------------------------------------------------------------------------------------------------------------
166
  /**
167
   * Returns a column given the column name.
168
   *
169
   * @param string $columnName The name of the column.
170
   *
171
   * @return ColumnMetadata
172
   */
173 6
  public function getColumn(string $columnName): ColumnMetadata
174
  {
175 6
    return $this->columns[$columnName];
176
  }
177
178
  //--------------------------------------------------------------------------------------------------------------------
179
  /**
180
   * Returns the columns names.
181
   *
182
   * @return string[]
183
   */
184 6
  public function getColumnNames(): array
185
  {
186 6
    return array_keys($this->columns);
187
  }
188
189
  //--------------------------------------------------------------------------------------------------------------------
190
  /**
191
   * Returns the underlying array with metadata of this list of table columns.
192
   *
193
   * @return ColumnMetadata[]
194
   */
195 30
  public function getColumns(): array
196
  {
197 30
    return $this->columns;
198
  }
199
200
  //--------------------------------------------------------------------------------------------------------------------
201
  /**
202
   * Returns the length of the longest column name.
203
   *
204
   * @return int
205
   */
206 25
  public function getLongestColumnNameLength(): int
207
  {
208 25
    $max = 0;
209 25
    foreach ($this->columns as $column)
210
    {
211 25
      $max = max($max, mb_strlen($column->getName()));
212
    }
213
214 25
    return $max;
215
  }
216
217
  //--------------------------------------------------------------------------------------------------------------------
218
  /**
219
   * Returns the number of columns.
220
   *
221
   * @return int
222
   */
223 28
  public function getNumberOfColumns(): int
224
  {
225 28
    return count($this->columns);
226
  }
227
228
  //--------------------------------------------------------------------------------------------------------------------
229
  /**
230
   * Returns previous column of a columns. Returns null if the column name is not found in this TableColumnsMetadata.
231
   *
232
   * @param string $columnName The column name.
233
   *
234
   * @return string|null
235
   */
236 2
  public function getPreviousColumn(string $columnName): ?string
237
  {
238 2
    $columns = array_keys($this->columns);
239 2
    $key     = array_search($columnName, $columns);
240
241 2
    if ($key>=1)
242
    {
243 2
      return $columns[$key - 1];
244
    }
245
246
    return null;
247
  }
248
249
  //--------------------------------------------------------------------------------------------------------------------
250
  /**
251
   * Makes all columns nullable.
252
   */
253 25
  public function makeNullable(): void
254
  {
255 25
    foreach ($this->columns as $column)
256
    {
257 25
      $column->makeNullable();
258
    }
259 25
  }
260
261
  //--------------------------------------------------------------------------------------------------------------------
262
  /**
263
   * Prepends table columns to this list of table columns.
264
   *
265
   * @param TableColumnsMetadata $columns The metadata of the table columns.
266
   */
267 8
  public function prependTableColumns(TableColumnsMetadata $columns): void
268
  {
269 8
    $this->columns = array_merge($columns->columns, $this->columns);
270 8
  }
271
272
  //--------------------------------------------------------------------------------------------------------------------
273
  /**
274
   * Removes a table column.
275
   *
276
   * @param string $columnName The table column name.
277
   */
278
  public function removeColumn(string $columnName): void
279
  {
280
    unset($this->columns[$columnName]);
281
  }
282
283
  //--------------------------------------------------------------------------------------------------------------------
284
  /**
285
   * Removes the default values from all columns.
286
   */
287 6
  public function unsetDefaults(): void
288
  {
289 6
    foreach ($this->columns as $column)
290
    {
291 6
      $column->unsetDefault();
292
    }
293 6
  }
294
295
  //--------------------------------------------------------------------------------------------------------------------
296
}
297
298
//----------------------------------------------------------------------------------------------------------------------
299