Completed
Push — master ( 2229d2...230b5d )
by P.R.
10:23
created

TableColumnsMetadata::getPreviousColumn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 10
c 0
b 0
f 0

1 Method

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