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

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