Completed
Push — master ( 5834ca...7116ff )
by
unknown
13s queued 11s
created

CollectionTuple::noSpaceAfterJoin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PHPSu\ShellCommandBuilder\Collection;
6
7
use PHPSu\ShellCommandBuilder\Exception\ShellBuilderException;
8
use PHPSu\ShellCommandBuilder\ShellInterface;
9
10
/**
11
 * @internal
12
 * @psalm-internal PHPSu\ShellCommandBuilder
13
 */
14
final class CollectionTuple implements ShellInterface
15
{
16
    /** @var string */
17
    protected $join = '';
18
19
    /** @var string|ShellInterface */
20
    protected $value = '';
21
    /** @var bool */
22
    private $noSpaceBeforeJoin = false;
23
    /** @var bool */
24
    private $noSpaceAfterJoin = false;
25
26
    /**
27
     * @param string|ShellInterface|mixed $value
28
     * @param string $join
29
     * @return static
30
     * @throws ShellBuilderException
31
     */
32
    public static function create($value, string $join = ''): self
33
    {
34
        if (!(is_string($value) || $value instanceof ShellInterface)) {
35
            throw new ShellBuilderException('Value must be of Type string or an instance of ShellInterface');
36
        }
37
        $tuple = new self();
38
        $tuple->value = $value;
39
        $tuple->join = $join;
40
        return $tuple;
41
    }
42
43
    public function noSpaceBeforeJoin(bool $space): self
44
    {
45
        $this->noSpaceBeforeJoin = $space;
46
        return $this;
47
    }
48
49
    public function noSpaceAfterJoin(bool $space): self
50
    {
51
        $this->noSpaceAfterJoin = $space;
52
        return $this;
53
    }
54
55
    /**
56
     * @return array<string|ShellInterface|array<mixed>>
57
     */
58
    public function __toArray(): array
59
    {
60
        $value = $this->value instanceof ShellInterface ? $this->value->__toArray() : $this->value;
61
        return [$this->join, $value];
62
    }
63
64
    public function __toString(): string
65
    {
66
        /** @psalm-suppress ImplicitToStringCast **/
67
        return sprintf('%s%s%s%s', $this->noSpaceBeforeJoin ? '' : ' ', $this->join, ($this->value === '' || $this->noSpaceAfterJoin) ? '' : ' ', $this->value);
68
    }
69
}
70