Completed
Pull Request — master (#40)
by Dima
06:19
created

ColumnTypeExtended::extendColumnTypes()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 26
ccs 0
cts 18
cp 0
rs 8.439
cc 5
eloc 12
nc 5
nop 2
crap 30
1
<?php
2
//----------------------------------------------------------------------------------------------------------------------
3
namespace SetBased\Audit\MySql\Helper;
4
5
//----------------------------------------------------------------------------------------------------------------------
6
use SetBased\Audit\MySql\Metadata\ColumnMetadata;
7
8
/**
9
 * Class for extended column type.
10
 */
11
class ColumnTypeExtended
12
{
13
  //--------------------------------------------------------------------------------------------------------------------
14
  /**
15
   * The metadata of the column.
16
   *
17
   * @var array[]
18
   */
19
  protected $columnTypes = [];
20
21
  //--------------------------------------------------------------------------------------------------------------------
22
  /**
23
   * Object constructor.
24
   *
25
   * @param array[]|ColumnMetadata $columnTypes The metadata of the column.
26
   * @param null|string            $typePrefix  Prefix for column type name.
27
   */
28
  public function __construct($columnTypes, $typePrefix)
29
  {
30
    $this->extendColumnTypes($columnTypes, $typePrefix);
31
  }
32
33
  //--------------------------------------------------------------------------------------------------------------------
34
  /**
35
   * Create array with all columns types.
36
   *
37
   * @param array[]|ColumnMetadata $columnTypes The metadata of the column.
38
   * @param null|string        $typePrefix  Prefix for column type name.
39
   */
40
  public function extendColumnTypes($columnTypes, $typePrefix)
41
  {
42
    $columnTypes = $columnTypes->getProperties();
43
    foreach ($columnTypes as $typeName => $typeValue)
44
    {
45
      if ($typeName=='column_name')
46
      {
47
        if (!isset($this->columnTypes['column_name']))
48
        {
49
          $this->columnTypes[$typeName] = $typeValue;
50
        }
51
      }
52
      else
53
      {
54
        $format = '%s_%s';
55
        if (isset($typePrefix))
56
        {
57
          $this->columnTypes[sprintf($format, $typePrefix, $typeName)] = $typeValue;
58
        }
59
        else
60
        {
61
          $this->columnTypes[$typeName] = $typeValue;
62
        }
63
      }
64
    }
65
  }
66
67
  //--------------------------------------------------------------------------------------------------------------------
68
  /**
69
   * Get columns type.
70
   *
71
   * @return \array[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array[]?

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...
72
   */
73
  public function getTypes()
74
  {
75
    return $this->columnTypes;
76
  }
77
78
  //--------------------------------------------------------------------------------------------------------------------
79
}
80
81
//----------------------------------------------------------------------------------------------------------------------
82