Index::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 2
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\Index;
13
14
use Moss\Storage\Model\Definition\DefinitionException;
15
use Moss\Storage\Model\Definition\IndexInterface;
16
17
/**
18
 * Index definition describing ordinary index
19
 *
20
 * @author  Michal Wachowski <[email protected]>
21
 * @package Moss\Storage
22
 */
23
class Index implements IndexInterface
24
{
25
    protected $table;
26
    protected $name;
27
    protected $type;
28
    protected $fields = [];
29
30
    /**
31
     * Constructor
32
     *
33
     * @param string $name
34
     * @param array $fields
35
     */
36
    public function __construct($name, array $fields)
37
    {
38
        $this->name = $name;
39
        $this->type = 'index';
40
41
        $this->assertFields($fields);
42
43
        $this->fields = $fields;
44
    }
45
46
    /**
47
     * Asserts index fields
48
     *
49
     * @param array $fields
50
     *
51
     * @throws DefinitionException
52
     */
53
    protected function assertFields($fields)
54
    {
55
        if (empty($fields)) {
56
            throw new DefinitionException(sprintf('No fields in index "%s" definition', $this->name));
57
        }
58
    }
59
60
    /**
61
     * Returns name of table
62
     *
63
     * @param string $table
64
     *
65
     * @return string
66
     */
67
    public function table($table = null)
68
    {
69
        if ($table !== null) {
70
            $this->table = $table;
71
        }
72
73
        return $this->table;
74
    }
75
76
    /**
77
     * Returns relation name in entity
78
     *
79
     * @return string
80
     */
81
    public function name()
82
    {
83
        return $this->name;
84
    }
85
86
    /**
87
     * Returns relation type
88
     *
89
     * @return string
90
     */
91
    public function type()
92
    {
93
        return $this->type;
94
    }
95
96
    /**
97
     * Returns array containing field names (unmapped) that are included in index
98
     *
99
     * @return array
100
     */
101
    public function fields()
102
    {
103
        return $this->fields;
104
    }
105
106
    /**
107
     * Checks if index uses field (unmapped)
108
     * Returns true if it does
109
     *
110
     * @param string $field
111
     *
112
     * @return bool
113
     */
114
    public function hasField($field)
115
    {
116
        return in_array($field, $this->fields);
117
    }
118
119
    /**
120
     * Returns true if index is primary index
121
     *
122
     * @return bool
123
     */
124
    public function isPrimary()
125
    {
126
        return $this->type == 'primary';
127
    }
128
129
    /**
130
     * Returns true if index is unique
131
     *
132
     * @return bool
133
     */
134
    public function isUnique()
135
    {
136
        return $this->type == 'unique' || $this->type == 'primary';
137
    }
138
}
139