Test Failed
Push — master ( d02081...898276 )
by P.R.
04:01
created

ColumnMetadata::getProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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