Passed
Pull Request — master (#7)
by
unknown
02:04
created

BasicExpression::getValue()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PHPSu\ShellCommandBuilder\Conditional;
6
7
use PHPSu\ShellCommandBuilder\Definition\ConditionalOperator;
8
use PHPSu\ShellCommandBuilder\ShellInterface;
9
10
abstract class BasicExpression implements ShellInterface
11
{
12
    /**
13
     * This is not POSIX-compatible (only eg. Korn and Bash), beware before using it
14
     * @var bool
15
     */
16
    protected $bashEnhancedBrackets;
17
    /** @var bool this is always double quoted */
18
    protected $escapedValue = false;
19
    /** @var bool */
20
    private $negateExpression;
21
    /** @var string|ShellInterface */
22
    protected $compare = '';
23
    /** @var string|ShellInterface */
24
    protected $compareWith = '';
25
    /** @var string */
26
    protected $operator = '';
27
28
    public function __construct(bool $useBashBrackets, bool $negateExpression)
29
    {
30
        $this->negateExpression = $negateExpression;
31
        $this->bashEnhancedBrackets = $useBashBrackets;
32
    }
33
34
    public function escapeValue(bool $enable): BasicExpression
35
    {
36
        $this->escapedValue = $enable;
37
        return $this;
38
    }
39
40
    /**
41
     * @todo with min. support of php 7.4 this can be fully implemented here
42
     * @param bool $useBashBrackets
43
     * @param bool $negateExpression
44
     * @return mixed
45
     */
46
    abstract public static function create(bool $useBashBrackets = false, bool $negateExpression = false);
47
48
49
    /**
50
     * @param string|ShellInterface $value
51
     * @return string|array<mixed>
52
     */
53
    private function getValueDebug($value)
54
    {
55
        if ($value instanceof ShellInterface) {
56
            return $value->__toArray();
57
        }
58
        return $value;
59
    }
60
61
    /**
62
     * @param string|ShellInterface $value
63
     * @return string
64
     */
65
    private function getValue($value): string
66
    {
67
        $return = $value;
68
        if ($value instanceof ShellInterface) {
69
            $return = $value->__toString();
70
        }
71
        if ($this->escapedValue) {
72
            /** @psalm-suppress ImplicitToStringCast */
73
            $return = sprintf('"%s"', $return);
74
        }
75
        return $return;
76
    }
77
78
    public function __toString(): string
79
    {
80
        return sprintf(
81
            '%s %s%s%s%s %s',
82
            $this->bashEnhancedBrackets ? ConditionalOperator::BRACKET_LEFT_BASH : ConditionalOperator::BRACKET_LEFT,
83
            $this->negateExpression ? '! ' : '',
84
            $this->compare ? $this->getValue($this->compare) . ' ' : '',
85
            $this->operator,
86
            $this->compareWith ? ' ' . $this->getValue($this->compareWith) : '',
87
            $this->bashEnhancedBrackets ? ConditionalOperator::BRACKET_RIGHT_BASH : ConditionalOperator::BRACKET_RIGHT
88
        );
89
    }
90
91
    /**
92
     * @return array<string,mixed>
93
     */
94
    public function __toArray(): array
95
    {
96
        return [
97
            'bashBrackets' => $this->bashEnhancedBrackets,
98
            'negate' => $this->negateExpression,
99
            'compare' => $this->getValueDebug($this->compare),
100
            'operator' => $this->operator,
101
            'compareWith' => $this->getValueDebug($this->compareWith),
102
        ];
103
    }
104
}
105