Ajde_Db_Table::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
class Ajde_Db_Table extends Ajde_Object_Standard
4
{
5
    protected $_connection;
6
    protected $_name;
7
    protected $_fields;
8
9
    public function __construct($name)
10
    {
11
        $this->_name = $name;
12
        $this->_connection = Ajde_Db::getInstance()->getConnection();
13
        $this->initTableStructure();
14
    }
15
16
    /**
17
     * @return PDO
18
     */
19
    public function getConnection()
20
    {
21
        return $this->_connection;
22
    }
23
24
    public function initTableStructure()
25
    {
26
        $structure = Ajde_Db::getInstance()->getAdapter()->getTableStructure($this->_name);
27
        foreach ($structure as $field) {
28
            $fieldName = $field['Field'];
29
            $fieldType = $field['Type'];
30
            $fieldParsedType = Ajde_Db::getInstance()->getAdapter()->getFieldType($fieldType);
31
            $fieldDefault = $field['Default'];
32
            $fieldLabel = !empty($field['Comment']) ? $field['Comment'] : $field['Field'];
33
34
            $fieldIsRequired = strtoupper($field['Null']) === 'NO';
35
            $fieldIsPK = strtoupper($field['Key']) === 'PRI';
36
            $fieldIsAutoIncrement = strtolower($field['Extra']) === 'auto_increment';
37
            $fieldIsAutoUpdate = strtolower($field['Extra']) === 'on update current_timestamp';
38
            $fieldIsUnique = strtoupper($field['Key']) === 'UNI';
39
40
            // Fix for certain MySQL versions
41
            if (strtolower($fieldDefault) === 'current_timestamp') {
42
                $fieldIsAutoUpdate = true;
43
            }
44
45
            $this->_fields[$fieldName] = [
46
                'name'            => $fieldName,
47
                'dbtype'          => $fieldType,
48
                'type'            => $fieldParsedType['type'],
49
                'length'          => $fieldParsedType['length'],
50
                'default'         => $fieldDefault,
51
                'label'           => $fieldLabel,
52
                'isRequired'      => $fieldIsRequired,
53
                'isPK'            => $fieldIsPK,
54
                'isAutoIncrement' => $fieldIsAutoIncrement,
55
                'isAutoUpdate'    => $fieldIsAutoUpdate,
56
                'isUnique'        => $fieldIsUnique,
57
            ];
58
        }
59
    }
60
61
    public function getPK()
62
    {
63
        foreach ($this->_fields as $field) {
64
            if ($field['isPK'] === true) {
65
                return $field['name'];
66
            }
67
        }
68
69
        return false;
70
    }
71
72
    /**
73
     * @param Ajde_Db_Table|string $parent
0 ignored issues
show
Bug introduced by
There is no parameter named $parent. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
74
     *
75
     * @return array
76
     */
77
    public function getFK($column)
78
    {
79
        $fk = Ajde_Db::getInstance()->getAdapter()->getForeignKey((string) $this, (string) $column);
80
81
        return [
82
            'field'        => $fk['COLUMN_NAME'],
83
            'parent_table' => $fk['REFERENCED_TABLE_NAME'],
84
            'parent_field' => $fk['REFERENCED_COLUMN_NAME'],
85
        ];
86
    }
87
88
    public function getParents()
89
    {
90
        $parents = Ajde_Db::getInstance()->getAdapter()->getParents((string) $this);
91
        $parentColumns = [];
92
        foreach ($parents as $parent) {
93
            if (
94
                isset($parent['COLUMN_NAME'])
95
                && isset($parent['REFERENCED_TABLE_NAME'])
96
                && strtoupper($parent['CONSTRAINT_NAME']) != 'PRIMARY'
97
            ) {
98
                $parentColumns[] = $parent['COLUMN_NAME'];
99
            }
100
        }
101
102
        return array_unique($parentColumns);
103
    }
104
105
    public function getFieldProperties($fieldName = null, $property = null)
106
    {
107
        if (isset($fieldName)) {
108
            if (isset($this->_fields[$fieldName])) {
109
                if (isset($property)) {
110
                    if (isset($this->_fields[$fieldName][$property])) {
111
                        return $this->_fields[$fieldName][$property];
112
                    }
113
                } else {
114
                    return $this->_fields[$fieldName];
115
                }
116
            }
117
        } else {
118
            return $this->_fields;
119
        }
120
    }
121
122
    public function getFieldNames()
123
    {
124
        return array_keys($this->_fields);
125
    }
126
127
    public function getFieldLabels()
128
    {
129
        $labels = [];
130
        foreach ($this->_fields as $field) {
131
            $labels[$field['name']] = $field['label'];
132
        }
133
134
        return $labels;
135
    }
136
137
    public function __toString()
138
    {
139
        return $this->_name;
140
    }
141
}
142