ArgumentToOperandConverter::toValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Happyr Doctrine Specification package.
5
 *
6
 * (c) Tobias Nyholm <[email protected]>
7
 *     Kacper Gunia <[email protected]>
8
 *     Peter Gribanov <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Happyr\DoctrineSpecification\Operand;
15
16
/**
17
 * This service is intended for backward compatibility and may be removed in the future.
18
 */
19
class ArgumentToOperandConverter
20
{
21
    /**
22
     * Convert the argument into the field operand if it is not an operand.
23
     *
24
     * @param Operand|string $argument
25
     *
26
     * @return Operand
27
     */
28
    public static function toField($argument)
29
    {
30
        if ($argument instanceof Operand) {
31
            return $argument;
32
        }
33
34
        return new Field($argument);
35
    }
36
37
    /**
38
     * Convert the argument into the value operand if it is not an operand.
39
     *
40
     * @param Operand|string $argument
41
     *
42
     * @return Operand
43
     */
44
    public static function toValue($argument)
45
    {
46
        if ($argument instanceof Operand) {
47
            return $argument;
48
        }
49
50
        return new Value($argument);
51
    }
52
53
    /**
54
     * Are all arguments is a operands?
55
     *
56
     * @param array $arguments
57
     *
58
     * @return bool
59
     */
60
    public static function isAllOperands(array $arguments)
61
    {
62
        foreach ($arguments as $argument) {
63
            if (!($argument instanceof Operand)) {
64
                return false;
65
            }
66
        }
67
68
        return true;
69
    }
70
71
    /**
72
     * Convert all arguments to operands.
73
     *
74
     * @param Operand[]|string[] $arguments
75
     *
76
     * @return Operand[]
77
     */
78
    public static function convert(array $arguments)
79
    {
80
        $result = [];
81
        foreach (array_values($arguments) as $i => $argument) {
82
            // always try convert the first argument to the field operand
83
            if (0 === $i) {
84
                $argument = self::toField($argument);
85
            } else {
86
                $argument = self::toValue($argument);
87
            }
88
89
            $result[] = $argument;
90
        }
91
92
        return $result;
93
    }
94
}
95