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

TableMetadata::getColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SetBased\Audit\Metadata;
4
5
/**
6
 * Class for the metadata of a database table.
7
 */
8
class TableMetadata
9
{
10
  //--------------------------------------------------------------------------------------------------------------------
11
  /**
12
   * The properties of the table that are stored by this class.
13
   *
14
   * var string[]
15
   */
16
  protected static $fields = [];
17
18
  /**
19
   * The metadata of the columns of this table.
20
   *
21
   * @var TableColumnsMetadata
22
   */
23
  private $columns;
24
25
  /**
26
   * The the properties of this table column.
27
   *
28
   * @var array
29
   */
30
  private $properties = [];
31
32
  //--------------------------------------------------------------------------------------------------------------------
33
  /**
34
   * Object constructor.
35
   *
36
   * @param array[]              $properties The metadata of the table.
37
   * @param TableColumnsMetadata $columns    The metadata of the columns of this table.
38
   */
39
  public function __construct($properties, $columns)
40
  {
41
    foreach (static::$fields as $field)
42
    {
43
      if (isset($properties[$field]))
44
      {
45
        $this->properties[$field] = $properties[$field];
46
      }
47
    }
48
49
    $this->columns = $columns;
50
  }
51
52
  //--------------------------------------------------------------------------------------------------------------------
53
  /**
54
   * Compares two metadata of two tables. Returns an array with the names of the different properties.
55
   *
56
   * @param TableMetadata $table1 The metadata of the first table.
57
   * @param TableMetadata $table2 The metadata of the second table.
58
   *
59
   * @return string[]
60
   */
61
  public static function compareOptions($table1, $table2)
62
  {
63
    $diff = [];
64
65
    foreach (static::$fields as $field)
66
    {
67
      if (!in_array($field, ['table_schema', 'table_name']))
68
      {
69
        if ($table1->getProperty($field)!=$table2->getProperty($field))
70
        {
71
          $diff[] = $field;
72
        }
73
      }
74
    }
75
76
    return $diff;
77
  }
78
79
  //--------------------------------------------------------------------------------------------------------------------
80
  /**
81
   * Returns table columns.
82
   *
83
   * @return TableColumnsMetadata
84
   */
85
  public function getColumns()
86
  {
87
    return $this->columns;
88
  }
89
90
  //--------------------------------------------------------------------------------------------------------------------
91
  /**
92
   * Returns the options of this table.
93
   *
94
   * @return array[]
95
   */
96
  public function getOptions()
97
  {
98
    $ret = $this->properties;
99
100
    unset($ret['table_name']);
101
    unset($ret['table_schema']);
102
103
    return $ret;
104
  }
105
106
  //--------------------------------------------------------------------------------------------------------------------
107
  /**
108
   * Returns a property of this table.
109
   *
110
   * @param string $name The name of the property.
111
   *
112
   * @return string|null
113
   */
114
  public function getProperty($name)
115
  {
116
    if (isset($this->properties[$name]))
117
    {
118
      return $this->properties[$name];
119
    }
120
121
    return null;
122
  }
123
124
  //--------------------------------------------------------------------------------------------------------------------
125
  /**
126
   * Returns the name of schema.
127
   *
128
   * @return string
129
   */
130
  public function getSchemaName()
131
  {
132
    return $this->properties['table_schema'];
133
  }
134
135
  //--------------------------------------------------------------------------------------------------------------------
136
  /**
137
   * Returns the name of this table.
138
   *
139
   * @return string
140
   */
141
  public function getTableName()
142
  {
143
    return $this->properties['table_name'];
144
  }
145
146
  //--------------------------------------------------------------------------------------------------------------------
147
}
148
149
//----------------------------------------------------------------------------------------------------------------------
150