ColumnMetadata   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 162
ccs 27
cts 27
cp 1
rs 10
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A compare() 0 16 4
A getProperties() 0 3 1
A unsetDefault() 0 5 2
A getProperty() 0 3 1
A __construct() 0 7 3
A setAfter() 0 3 1
A getName() 0 3 1
A makeNullable() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SetBased\Audit\Metadata;
5
6
/**
7
 * Metadata of table columns.
8
 */
9
abstract class ColumnMetadata
10
{
11
  //--------------------------------------------------------------------------------------------------------------------
12
  /**
13
   * The properties of the column that are stored by this class.
14
   *
15
   * var string[]
16
   */
17
  protected static array $fields = ['column_name',
18
                                    'column_type',
19
                                    'is_nullable',
20
                                    'character_set_name',
21
                                    'collation_name'];
22
23
  /**
24
   * The properties of this table column.
25
   *
26
   * @var array
27
   */
28
  private array $properties = [];
29
30
  //--------------------------------------------------------------------------------------------------------------------
31
  /**
32
   * Object constructor.
33
   *
34
   * @param array $properties The metadata of the column.
35
   */
36
  public function __construct(array $properties)
37 32
  {
38
    foreach (static::$fields as $field)
39 32
    {
40
      if (isset($properties[$field]))
41 32
      {
42
        $this->properties[$field] = $properties[$field];
43 32
      }
44
    }
45
  }
46 32
47
  //--------------------------------------------------------------------------------------------------------------------
48
  /**
49
   * Compares the metadata of two columns.
50
   *
51
   * @param ColumnMetadata $column1 The metadata of the first column.
52
   * @param ColumnMetadata $column2 The metadata of the second column.
53
   * @param string[]       $ignore  The properties to be ignored.
54
   *
55
   * @return bool True if the columns are equal, false otherwise.
56
   */
57
  public static function compare(ColumnMetadata $column1, ColumnMetadata $column2, array $ignore = []): bool
58 30
  {
59
    $equal = true;
60 30
61
    foreach (static::$fields as $field)
62 30
    {
63
      if (!in_array($field, $ignore))
64 30
      {
65
        if ($column1->getProperty($field)!==$column2->getProperty($field))
66 30
        {
67
          $equal = false;
68 5
        }
69
      }
70
    }
71
72
    return $equal;
73 30
  }
74
75
  //--------------------------------------------------------------------------------------------------------------------
76
  /**
77
   * Returns a SQL snippet with the column definition (without column name) of this column to be used in audit tables.
78
   *
79
   * @return string
80
   */
81
  abstract public function getColumnAuditDefinition(): string;
82
83
  //--------------------------------------------------------------------------------------------------------------------
84
  /**
85
   * Returns a SQL snippet with the column definition (without column name) of this column.
86
   *
87
   * @return string
88
   */
89
  abstract public function getColumnDefinition(): string;
90
91
  //--------------------------------------------------------------------------------------------------------------------
92
  /**
93
   * Returns the name of this column.
94
   *
95
   * @return string
96
   */
97
  public function getName(): string
98 32
  {
99
    return $this->properties['column_name'];
100 32
  }
101
102
  //--------------------------------------------------------------------------------------------------------------------
103
  /**
104
   * Returns the properties of this table column as an array.
105
   *
106
   * @return array
107
   */
108
  public function getProperties(): array
109 23
  {
110
    return $this->properties;
111 23
  }
112
113
  //--------------------------------------------------------------------------------------------------------------------
114
  /**
115
   * Returns a property of this table column.
116
   *
117
   * @param string $name The name of the property.
118
   *
119
   * @return string|null
120
   */
121
  public function getProperty(string $name): ?string
122 30
  {
123
    return $this->properties[$name] ?? null;
124 30
  }
125
126
  //--------------------------------------------------------------------------------------------------------------------
127
  /**
128
   * Returns column type info
129
   *
130
   * @return string
131
   */
132
  abstract public function getTypeInfo1(): string;
133
134
  //--------------------------------------------------------------------------------------------------------------------
135
  /**
136
   * Returns additional column type info
137
   *
138
   * @return string|null
139
   */
140
  abstract public function getTypeInfo2(): ?string;
141
142
  //--------------------------------------------------------------------------------------------------------------------
143
  /**
144
   * Make this column nullable.
145
   */
146
  public function makeNullable(): void
147 27
  {
148
    $this->properties['is_nullable'] = 'YES';
149 27
  }
150 27
151
  //--------------------------------------------------------------------------------------------------------------------
152
  /**
153
   * Sets property 'after'.
154
   *
155
   * @param string|null $after
156
   */
157
  public function setAfter(?string $after): void
158 30
  {
159
    $this->properties['after'] = $after;
160 30
  }
161 30
162
  //--------------------------------------------------------------------------------------------------------------------
163
  /**
164
   * Removes the default value.
165
   */
166
  public function unsetDefault(): void
167 6
  {
168
    if (isset($this->properties['column_default']))
169 6
    {
170 6
      $this->properties['column_default'] = 'NULL';
171
    }
172
  }
173
174
  //--------------------------------------------------------------------------------------------------------------------
175
}
176
177
//----------------------------------------------------------------------------------------------------------------------
178