Translate::normalizeValues()   A
last analyzed

Complexity

Conditions 6
Paths 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 21
rs 9.2222
cc 6
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Th3Mouk\RethinkDB\Translator;
6
7
use r\Cursor;
8
9
class Translate
10
{
11
    public const NORMALIZER_OPTIONS = ['dateTimeToString' => true];
12
13
    /**
14
     * @param array<string, bool> $options
15
     *
16
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification
17
     */
18
    public static function cursorToAssociativeArray(Cursor $cursor, array $options = []): array
19
    {
20
        return self::arrayOfArrayObjectToAssociativeArray($cursor->toArray(), $options);
21
    }
22
23
    /**
24
     * @param array<\ArrayObject> $arrayOfArrayObject
25
     * @param array<string, bool> $options
26
     *
27
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification
28
     */
29
    public static function arrayOfArrayObjectToAssociativeArray(array $arrayOfArrayObject, array $options = []): array
30
    {
31
        array_walk($arrayOfArrayObject, static function (\ArrayObject &$entry) use ($options): void {
32
            $entry = self::normalizeValues($entry->getArrayCopy(), $options);
33
        });
34
35
        return $arrayOfArrayObject;
36
    }
37
38
    /**
39
     * @param array<string, bool> $options
40
     *
41
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification
42
     */
43
    public static function arrayObjectToAssociativeArray(\ArrayObject $arrayObject, array $options = []): array
44
    {
45
        return self::normalizeValues($arrayObject->getArrayCopy(), $options);
46
    }
47
48
    /**
49
     * @param array<string, bool> $options
50
     *
51
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableParameterTypeHintSpecification
52
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification
53
     */
54
    public static function normalizeValues(array $values, array $options = []): array
55
    {
56
        if (empty($options)) {
57
            $options = self::NORMALIZER_OPTIONS;
58
        }
59
60
        return array_map(static function ($value) use ($options) {
61
            if ($value instanceof \ArrayObject) {
62
                return self::arrayObjectToAssociativeArray($value, $options);
63
            }
64
65
            if (is_array($value)) {
66
                return self::normalizeValues($value, $options);
67
            }
68
69
            if ($options['dateTimeToString'] && $value instanceof \DateTime) {
70
                return $value->format(DATE_ISO8601);
71
            }
72
73
            return $value;
74
        }, $values);
75
    }
76
}
77