Completed
Pull Request — master (#17)
by Andreas
15:38
created

TypeConverter::convertToLegacyType()   C

Complexity

Conditions 9
Paths 9

Size

Total Lines 23
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 11.9991

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
ccs 14
cts 21
cp 0.6667
rs 5.8543
cc 9
eloc 20
nc 9
nop 1
crap 11.9991
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
namespace Alcaeus\MongoDbAdapter;
17
18
/**
19
 * @internal
20
 */
21
class TypeConverter
22
{
23 44
    public static function convertLegacyArrayToObject($array)
24
    {
25
        // TODO: provide actual class once mongodb/mongo-php-library#78 has been merged
26 44
        $result = [];
27
28 44
        foreach ($array as $key => $value) {
29 44
            $result[$key] = (is_array($value)) ? static::convertLegacyArrayToObject($value) : static::convertToBSONType($value);
30 44
        }
31
32 44
        return self::ensureCorrectType($result);
33
    }
34
35 24
    public static function convertObjectToLegacyArray($object)
36
    {
37 24
        $result = [];
38
39 24
        foreach ($object as $key => $value) {
40
            // TODO: use actual class instead of \stdClass once mongodb/mongo-php-library#78 has been merged
41 24
            $result[$key] = ($value instanceof \stdClass || is_array($value)) ? static::convertObjectToLegacyArray($value) : static::convertToLegacyType($value);
42 24
        }
43
44 24
        return $result;
45
    }
46
47 26
    public static function convertToLegacyType($value)
48
    {
49 26
        switch (true) {
50 26
            case $value instanceof \MongoDB\BSON\ObjectID:
1 ignored issue
show
Bug introduced by
The class MongoDB\BSON\ObjectID does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
51 8
                return new \MongoId($value);
52 26
            case $value instanceof \MongoDB\BSON\Binary:
1 ignored issue
show
Bug introduced by
The class MongoDB\BSON\Binary does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
53
                return new \MongoBinData($value);
54 26
            case $value instanceof \MongoDB\BSON\Javascript:
1 ignored issue
show
Bug introduced by
The class MongoDB\BSON\Javascript does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
55
                return new \MongoCode($value);
56 26
            case $value instanceof \MongoDB\BSON\MaxKey:
1 ignored issue
show
Bug introduced by
The class MongoDB\BSON\MaxKey does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
57
                return new \MongoMaxKey();
58 26
            case $value instanceof \MongoDB\BSON\MinKey:
1 ignored issue
show
Bug introduced by
The class MongoDB\BSON\MinKey does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
59
                return new \MongoMinKey();
60 26
            case $value instanceof \MongoDB\BSON\Regex:
1 ignored issue
show
Bug introduced by
The class MongoDB\BSON\Regex does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
61
                return new \MongoRegex($value);
62 26
            case $value instanceof \MongoDB\BSON\Timestamp:
1 ignored issue
show
Bug introduced by
The class MongoDB\BSON\Timestamp does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
63
                return new \MongoTimestamp($value);
64 26
            case $value instanceof \MongoDB\BSON\UTCDatetime:
1 ignored issue
show
Bug introduced by
The class MongoDB\BSON\UTCDatetime does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
65
                return new \MongoDate($value);
66 26
            default:
67 26
                return $value;
68 26
        }
69
    }
70
71 44
    public static function convertToBSONType($value)
72
    {
73 44
        switch (true) {
74 44
            case $value instanceof TypeInterface:
75 9
                return $value->toBSONType();
76
77 44
            default:
78 44
                return $value;
79 44
        }
80
    }
81
82
    /**
83
     * @param array $array
84
     * @return bool
85
     */
86 44
    public static function isNumericArray(array $array)
87
    {
88 44
        return $array === [] || is_numeric(array_keys($array)[0]);
89
    }
90
91
    /**
92
     * Converts all arrays with non-numeric keys to stdClass
93
     *
94
     * @param array $array
95
     * @return array|\stdClass
96
     */
97 44
    private static function ensureCorrectType(array $array)
98
    {
99
        // Empty arrays are left untouched since they may be an empty list or empty document
100 44
        if (static::isNumericArray($array)) {
101 7
            return $array;
102
        }
103
104
        // Can convert array to stdClass
105 44
        $object = new \stdClass();
106 44
        foreach ($array as $key => $value) {
107 44
            $object->$key = $value;
108 44
        }
109
110 44
        return $object;
111
    }
112
}
113