Completed
Push — master ( 660564...17f31f )
by Andreas
18s queued 10s
created

MongoRegex   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A __toString() 0 4 1
A toBSONType() 0 4 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 */
15
16
if (class_exists('MongoRegex', false)) {
17
    return;
18
}
19
20
use Alcaeus\MongoDbAdapter\TypeInterface;
21
use MongoDB\BSON\Regex;
22
23
class MongoRegex implements TypeInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    public $regex;
29
30
    /**
31
     * @var string
32
     */
33
    public $flags;
34
35
    /**
36
     * Creates a new regular expression.
37
     *
38
     * @link http://php.net/manual/en/mongoregex.construct.php
39
     * @param string|Regex $regex Regular expression string of the form /expr/flags
40
     */
41
    public function __construct($regex)
42
    {
43
        if ($regex instanceof Regex) {
44
            $this->regex = $regex->getPattern();
45
            $this->flags = $regex->getFlags();
46
            return;
47
        }
48
49
        if (! preg_match('#^/(.*)/([imxslu]*)$#', $regex, $matches)) {
50
            throw new MongoException('invalid regex', 9);
51
        }
52
53
        $this->regex = $matches[1];
54
        $this->flags = $matches[2];
55
    }
56
57
    /**
58
     * Returns a string representation of this regular expression.
59
     * @return string This regular expression in the form "/expr/flags".
60
     */
61
    public function __toString()
62
    {
63
        return '/' . $this->regex . '/' . $this->flags;
64
    }
65
66
    /**
67
     * Converts this MongoRegex to the new BSON Regex type
68
     *
69
     * @return Regex
70
     * @internal This method is not part of the ext-mongo API
71
     */
72
    public function toBSONType()
73
    {
74
        return new Regex($this->regex, $this->flags);
75
    }
76
}
77