MongoIndex   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 72
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0
wmc 17
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 40 16
A toArray() 0 4 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Minotaur
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2017 Appertly
19
 * @license   Apache-2.0
20
 */
21
namespace Minotaur\Db;
22
23
/**
24
 * A MongoDB Index definition
25
 */
26
class MongoIndex
27
{
28
    /**
29
     * @var array<string,mixed>
30
     */
31
    private $values;
32
33
    private const BOOL_FIELDS = ['background', 'unique', 'sparse'];
34
    private const FLOAT_FIELDS = ['min', 'max', 'bucketSize'];
35
    private const INT_FIELDS = ['bits', '2dsphereIndexVersion', 'textIndexVersion', 'expireAfterSeconds'];
36
    private const STRING_FIELDS = ['default_language', 'language_override'];
37
    private const DOCUMENT_FIELDS = ['partialFilterExpression', 'storageEngine', 'weights'];
38
39
    /**
40
     * Creates an immutable MongoDB Index creation definition
41
     *
42
     * @param $keys - The key definition
43
     * @param $name - Optional. The index name.
44
     * @param $options - Optional. Any index creation options.
45
     * @see https://docs.mongodb.com/manual/reference/command/createIndexes/
46
     */
47
    public function __construct(iterable $keys, string $name = null, ?iterable $options = null)
48
    {
49
        $values = [
50
            'key' => is_array($keys) ? $keys : iterator_to_array($keys, true)
51
        ];
52
        if ($name !== null) {
53
            $values['name'] = $name;
54
        }
55
        if ($options) {
56
            foreach (self::BOOL_FIELDS as $v) {
57
                if (array_key_exists($v, $options)) {
58
                    $values[$v] = (bool) $options[$v];
59
                }
60
            }
61
            foreach (self::FLOAT_FIELDS as $v) {
62
                if (array_key_exists($v, $options)) {
63
                    $values[$v] = (float) $options[$v];
64
                }
65
            }
66
            foreach (self::INT_FIELDS as $v) {
67
                if (array_key_exists($v, $options)) {
68
                    $values[$v] = (int) $options[$v];
69
                }
70
            }
71
            foreach (self::STRING_FIELDS as $v) {
72
                if (array_key_exists($v, $options)) {
73
                    $values[$v] = (string) $options[$v];
74
                }
75
            }
76
            foreach (self::DOCUMENT_FIELDS as $v) {
77
                if (array_key_exists($v, $options)) {
78
                    $doc = $options[$v];
79
                    if (is_iterable($doc)) {
80
                        $values[$v] = is_array($doc) ? $doc : iterator_to_array($doc, true);
81
                    }
82
                }
83
            }
84
        }
85
        $this->values = $values;
86
    }
87
88
    /**
89
     * Gets the array version of this index.
90
     *
91
     * @return array<string,mixed> The array version
92
     */
93
    public function toArray(): array
94
    {
95
        return $this->values;
96
    }
97
}
98