Completed
Push — master ( 8e94f8...744b49 )
by P.R.
04:26
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 10
  public function __construct($columns = [], $type = 'ColumnMetadata')
29
  {
30 10
    foreach ($columns as $columnName => $column)
31
    {
32 10
      $this->columns[$column['column_name']] = self::columnFactory($type, $column);
33 10
    }
34 10
  }
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 8
  public static function combine($columns1, $columns2)
46
  {
47 8
    $columns = new TableColumnsMetadata();
48
49 8
    foreach ($columns1->columns as $column)
50
    {
51 8
      $columns->appendTableColumn($column);
52 8
    }
53
54 8
    foreach ($columns2->columns as $column)
55
    {
56 8
      $columns->appendTableColumn($column);
57 8
    }
58
59 8
    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
   *
70
   * @return TableColumnsMetadata
71
   */
72 8
  public static function differentColumnTypes($columns1, $columns2)
73
  {
74 8
    $diff = new TableColumnsMetadata();
75 8
    foreach ($columns1->columns as $column_name => $column1)
76
    {
77 8
      if (isset($columns2->columns[$column_name]))
78 8
      {
79 7
        if ($columns2->columns[$column_name]->getProperty('column_type')!=$column1->getProperty('column_type'))
80 7
        {
81
          $diff->appendTableColumn($column1);
82
        }
83 7
      }
84 8
    }
85
86 8
    return $diff;
87
  }
88
89
  //--------------------------------------------------------------------------------------------------------------------
90
  /**
91
   * Compares two sets of metadata of table columns and returns a set of metadata of table columns that are in the first
92
   * sets of metadata of table columns but not in the second sets of metadata of table columns.
93
   *
94
   * @param TableColumnsMetadata $columns1 The first sets of metadata of table columns.
95
   * @param TableColumnsMetadata $columns2 The second sets of metadata of table columns.
96
   *
97
   * @return TableColumnsMetadata
98
   */
99 8
  public static function notInOtherSet($columns1, $columns2)
100
  {
101 8
    $diff = new TableColumnsMetadata();
102 8
    foreach ($columns1->columns as $column_name => $column1)
103
    {
104 8
      if (!isset($columns2->columns[$column_name]))
105 8
      {
106
        $diff->appendTableColumn($column1);
107
      }
108 8
    }
109
110 8
    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 10
  private static function columnFactory($type, $column)
123
  {
124
    switch ($type)
125
    {
126 10
      case 'ColumnMetadata':
127 10
        return new ColumnMetadata($column);
128
129 8
      case 'AlterColumnMetadata':
130
        return new AlterColumnMetadata($column);
131
132 8
      case 'AuditColumnMetadata':
133 8
        return new AuditColumnMetadata($column);
134
135
      default:
136
        throw new FallenException('type', $type);
137
    }
138
  }
139
140
  //--------------------------------------------------------------------------------------------------------------------
141
  /**
142
   * Appends a table column to the table columns.
143
   *
144
   * @param ColumnMetadata $column The metadata of the table columns.
145
   */
146 8
  public function appendTableColumn($column)
147
  {
148 8
    $this->columns[$column->getProperty('column_name')] = $column;
149 8
  }
150
151
  //--------------------------------------------------------------------------------------------------------------------
152
  /**
153
   * Returns the underlying array with metadata of the columns.
154
   *
155
   * @return ColumnMetadata[]
156
   */
157 10
  public function getColumns()
158
  {
159 10
    return $this->columns;
160
  }
161
162
  //--------------------------------------------------------------------------------------------------------------------
163
  /**
164
   * Returns the number of columns.
165
   *
166
   * @return int
167
   */
168 8
  public function getNumberOfColumns()
169
  {
170 8
    return count($this->columns);
171
  }
172
173
  //--------------------------------------------------------------------------------------------------------------------
174
  /**
175
   * Returns previous column of a columns. Returns null if the column name is not found in this TableColumnsMetadata.
176
   *
177
   * @param string $columnName The column name.
178
   *
179
   * @return null|string
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
180
   */
181
  public function getPreviousColumn($columnName)
182
  {
183
    $columns = array_keys($this->columns);
184
    $key     = array_search($columnName, $columns);
185
186
    if ($key>=1)
187
    {
188
      return $columns[$key - 1];
189
    }
190
191
    return null;
192
  }
193
194
  //--------------------------------------------------------------------------------------------------------------------
195
  /**
196
   * Makes all columns nullable.
197
   */
198 5
  public function makeNullable()
199
  {
200 5
    foreach ($this->columns as $column)
201
    {
202 5
      $column->makeNullable();
203 5
    }
204 5
  }
205
206
  //--------------------------------------------------------------------------------------------------------------------
207
}
208
209
//----------------------------------------------------------------------------------------------------------------------
210