|
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
|
|
|
|