Completed
Push — master ( fa00f5...226160 )
by P.R.
03:39
created

ColumnMetadata::makeNullable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
//----------------------------------------------------------------------------------------------------------------------
3
namespace SetBased\Audit\MySql\Metadata;
4
5
//----------------------------------------------------------------------------------------------------------------------
6
/**
7
 * Metadata of table columns.
8
 */
9
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<string,string>
27
   */
28
  protected $properties = [];
29
30
  //--------------------------------------------------------------------------------------------------------------------
31
  /**
32
   * Object constructor.
33
   *
34
   * @param array[] $properties The metadata of the column.
35
   */
36 14
  public function __construct($properties)
37
  {
38 14
    foreach (static::$fields as $field)
39
    {
40 14
      if (isset($properties[$field]))
41 14
      {
42 14
        $this->properties[$field] = $properties[$field];
43 14
      }
44 14
    }
45 14
  }
46
47
  //--------------------------------------------------------------------------------------------------------------------
48
  /**
49
   * Compares two the metadata of the 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 1
  public static function compare($column1, $column2, $ignore = [])
58
  {
59 1
    $equal = true;
60
61 1
    foreach (self::$fields as $field)
62
    {
63 1
      if (!in_array($field, $ignore))
64 1
      {
65 1
        if ($column1->getProperty($field)!=$column2->getProperty($field))
66 1
        {
67
          $equal = false;
68
        }
69 1
      }
70 1
    }
71
72 1
    return $equal;
73
  }
74
75
  //--------------------------------------------------------------------------------------------------------------------
76
  /**
77
   * Returns the properties of this table column as an array.
78
   *
79
   * @return array[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,string>?

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...
80
   */
81 11
  public function getProperties()
82
  {
83 11
    return $this->properties;
84
  }
85
86
  //--------------------------------------------------------------------------------------------------------------------
87
  /**
88
   * Returns a property of this table column.
89
   *
90
   * @param string $name The name of the property.
91
   *
92
   * @return string|null
93
   */
94 14
  public function getProperty($name)
95
  {
96 14
    if (isset($this->properties[$name]))
97 14
    {
98 14
      return $this->properties[$name];
99
    }
100
101 9
    return null;
102
  }
103
  //--------------------------------------------------------------------------------------------------------------------
104
  /**
105
   * Make this column nullable.
106
   */
107 9
  public function makeNullable()
108
  {
109 9
    $this->properties['is_nullable'] = 'YES';
110 9
  }
111
112
  //--------------------------------------------------------------------------------------------------------------------
113
}
114
115
//----------------------------------------------------------------------------------------------------------------------
116