Completed
Push — master ( b08ae0...b7efbe )
by P.R.
04:19
created

TableColumnsMetadata::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
//----------------------------------------------------------------------------------------------------------------------
3
namespace SetBased\Audit\MySql\Metadata;
4
5
//----------------------------------------------------------------------------------------------------------------------
6
use SetBased\Exception\FallenException;
7
8
/**
9
 * Metadata of a set of table columns.
10
 */
11
class TableColumnsMetadata
12
{
13
  //--------------------------------------------------------------------------------------------------------------------
14
  /**
15
   * The metadata of the columns.
16
   *
17
   * @var ColumnMetadata[]
18
   */
19
  private $columns = [];
20
21
  //--------------------------------------------------------------------------------------------------------------------
22
  /**
23
   * Object constructor.
24
   *
25
   * @param \array[] $columns The metadata of the columns as returned by AuditDataLayer::getTableColumns().
26
   * @param string   $type    The class for columns metadata.
27
   */
28 15
  public function __construct($columns = [], $type = 'ColumnMetadata')
29
  {
30 15
    foreach ($columns as $columnName => $column)
31
    {
32 14
      $this->columns[$column['column_name']] = self::columnFactory($type, $column);
33 15
    }
34 15
  }
35
36
  //--------------------------------------------------------------------------------------------------------------------
37
  /**
38
   * Combines the metadata of two sets of metadata of table columns.
39
   *
40
   * @param TableColumnsMetadata $columns1 The first set of metadata of table columns.
41
   * @param TableColumnsMetadata $columns2 The second set of metadata of table columns.
42
   *
43
   * @return TableColumnsMetadata
44
   */
45 12
  public static function combine($columns1, $columns2)
46
  {
47 12
    $columns = new TableColumnsMetadata();
48
49 12
    foreach ($columns1->columns as $column)
50
    {
51 9
      $columns->appendTableColumn($column);
52 12
    }
53
54 12
    foreach ($columns2->columns as $column)
55
    {
56 12
      $columns->appendTableColumn($column);
57 12
    }
58
59 12
    return $columns;
60
  }
61
62
  //--------------------------------------------------------------------------------------------------------------------
63
  /**
64
   * Compares two sets of metadata of table columns and returns a set of metadata of table columns the are in both sets
65
   * but have different column type.
66
   *
67
   * @param TableColumnsMetadata $columns1 The first sets of metadata of table columns.
68
   * @param TableColumnsMetadata $columns2 The second sets of metadata of table columns.
69
   * @param string[]             $ignore   The properties to be ignored.
70
   *
71
   * @return TableColumnsMetadata
72
   */
73 1
  public static function differentColumnTypes($columns1, $columns2, $ignore = [])
74
  {
75 1
    $diff = new TableColumnsMetadata();
76 1
    foreach ($columns1->columns as $column_name => $column1)
77
    {
78 1
      if (isset($columns2->columns[$column_name]))
79 1
      {
80 1
        if (!ColumnMetadata::compare($column1, $columns2->columns[$column_name], $ignore))
81 1
        {
82
          $diff->appendTableColumn($column1);
83
        }
84 1
      }
85 1
    }
86
87 1
    return $diff;
88
  }
89
90
  //--------------------------------------------------------------------------------------------------------------------
91
  /**
92
   * Compares two sets of metadata of table columns and returns a set of metadata of table columns that are in the first
93
   * sets of metadata of table columns but not in the second sets of metadata of table columns.
94
   *
95
   * @param TableColumnsMetadata $columns1 The first sets of metadata of table columns.
96
   * @param TableColumnsMetadata $columns2 The second sets of metadata of table columns.
97
   *
98
   * @return TableColumnsMetadata
99
   */
100 12
  public static function notInOtherSet($columns1, $columns2)
101
  {
102 12
    $diff = new TableColumnsMetadata();
103 12
    foreach ($columns1->columns as $column_name => $column1)
104
    {
105 12
      if (!isset($columns2->columns[$column_name]))
106 12
      {
107 2
        $diff->appendTableColumn($column1);
108 2
      }
109 12
    }
110
111 12
    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 14
  private static function columnFactory($type, $column)
124
  {
125
    switch ($type)
126
    {
127 14
      case 'ColumnMetadata':
128 14
        return new ColumnMetadata($column);
129
130 9
      case 'AlterColumnMetadata':
131
        return new AlterColumnMetadata($column);
132
133 9
      case 'AuditColumnMetadata':
134 9
        return new AuditColumnMetadata($column);
135
136
      default:
137
        throw new FallenException('type', $type);
138
    }
139
  }
140
141
  //--------------------------------------------------------------------------------------------------------------------
142
  /**
143
   * Appends a table column to the table columns.
144
   *
145
   * @param ColumnMetadata $column The metadata of the table columns.
146
   */
147 12
  public function appendTableColumn($column)
148
  {
149 12
    $this->columns[$column->getProperty('column_name')] = $column;
150 12
  }
151
152
  //--------------------------------------------------------------------------------------------------------------------
153
  /**
154
   * Returns the underlying array with metadata of the columns.
155
   *
156
   * @return ColumnMetadata[]
157
   */
158 14
  public function getColumns()
159
  {
160 14
    return $this->columns;
161
  }
162
163
  //--------------------------------------------------------------------------------------------------------------------
164
  /**
165
   * Returns the number of columns.
166
   *
167
   * @return int
168
   */
169 12
  public function getNumberOfColumns()
170
  {
171 12
    return count($this->columns);
172
  }
173
174
  //--------------------------------------------------------------------------------------------------------------------
175
  /**
176
   * Returns previous column of a columns. Returns null if the column name is not found in this TableColumnsMetadata.
177
   *
178
   * @param string $columnName The column name.
179
   *
180
   * @return int|null
181
   */
182 1
  public function getPreviousColumn($columnName)
183
  {
184 1
    $columns = array_keys($this->columns);
185 1
    $key     = array_search($columnName, $columns);
186
187 1
    if ($key>=1)
188 1
    {
189 1
      return $columns[$key - 1];
190
    }
191
192
    return null;
193
  }
194
195
  //--------------------------------------------------------------------------------------------------------------------
196
  /**
197
   * Makes all columns nullable.
198
   */
199 9
  public function makeNullable()
200
  {
201 9
    foreach ($this->columns as $column)
202
    {
203 9
      $column->makeNullable();
204 9
    }
205 9
  }
206
207
  //--------------------------------------------------------------------------------------------------------------------
208
}
209
210
//----------------------------------------------------------------------------------------------------------------------
211