Field   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 15
c 2
b 0
f 1
lcom 3
cbo 0
dl 0
loc 131
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A prepareAttributes() 0 17 4
A table() 0 8 2
A name() 0 4 1
A type() 0 4 1
A mapping() 0 4 1
A mappedName() 0 4 2
A attribute() 0 8 2
A attributes() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Storage package
5
 *
6
 * (c) Michal Wachowski <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Moss\Storage\Model\Definition;
13
14
/**
15
 * String field
16
 *
17
 * @author  Michal Wachowski <[email protected]>
18
 * @package Moss\Storage
19
 */
20
class Field implements FieldInterface
21
{
22
    protected $table;
23
    protected $name;
24
    protected $type;
25
    protected $mapping;
26
    protected $attributes;
27
28
    /**
29
     * Constructor
30
     *
31
     * @param string      $field
32
     * @param string      $type
33
     * @param array       $attributes
34
     * @param null|string $mapping
35
     */
36
    public function __construct($field, $type = 'string', $attributes = [], $mapping = null)
37
    {
38
        $this->name = $field;
39
        $this->type = $type;
40
        $this->mapping = $mapping;
41
        $this->attributes = $this->prepareAttributes($attributes);
42
    }
43
44
    /**
45
     * Prepares attributes, changes them into key value pairs
46
     *
47
     * @param array $attributes
48
     *
49
     * @return array
50
     */
51
    protected function prepareAttributes(array $attributes)
52
    {
53
        foreach ($attributes as $key => $value) {
54
            if (is_numeric($key)) {
55
                unset($attributes[$key]);
56
                $attributes[$value] = true;
57
                continue;
58
            }
59
        }
60
61
        if (isset($attributes['null'])) {
62
            unset($attributes['null']);
63
            $attributes['notnull'] = false;
64
        }
65
66
        return $attributes;
67
    }
68
69
    /**
70
     * Returns table owning the field
71
     *
72
     * @param null|string $table
73
     *
74
     * @return string
75
     */
76
    public function table($table = null)
77
    {
78
        if ($table !== null) {
79
            $this->table = $table;
80
        }
81
82
        return $this->table;
83
    }
84
85
    /**
86
     * Returns relation name in entity
87
     *
88
     * @return string
89
     */
90
    public function name()
91
    {
92
        return $this->name;
93
    }
94
95
    /**
96
     * Returns relation type
97
     *
98
     * @return string
99
     */
100
    public function type()
101
    {
102
        return $this->type;
103
    }
104
105
    /**
106
     * Returns mapped table cell or null when no mapping
107
     *
108
     * @return null|string
109
     */
110
    public function mapping()
111
    {
112
        return $this->mapping;
113
    }
114
115
    /**
116
     * Returns fields mapped name or if not mapped returns property name
117
     *
118
     * @return string
119
     */
120
    public function mappedName()
121
    {
122
        return $this->mapping ?: $this->name;
123
    }
124
125
    /**
126
     * Returns attribute value or null if not set
127
     *
128
     * @param string $attribute
129
     *
130
     * @return mixed
131
     */
132
    public function attribute($attribute)
133
    {
134
        if (!array_key_exists($attribute, $this->attributes)) {
135
            return null;
136
        }
137
138
        return $this->attributes[$attribute];
139
    }
140
141
    /**
142
     * Returns array containing field attributes
143
     *
144
     * @return array
145
     */
146
    public function attributes()
147
    {
148
        return $this->attributes;
149
    }
150
}
151