Skip to content

Commit 9f8ca57

Browse files
authored
Merge pull request #7 from phpsu/feature/add-conditional-expression
Validation for options and conditional expressions
2 parents 7bf58da + d833430 commit 9f8ca57

18 files changed

+1303
-2
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
final class ArithmeticExpression extends BasicExpression
11+
{
12+
public static function create(bool $useBashBrackets = true, bool $negateExpression = false): ArithmeticExpression
13+
{
14+
return new self($useBashBrackets, $negateExpression);
15+
}
16+
17+
/**
18+
* @param string|ShellInterface $arg1
19+
* @param string|ShellInterface $arg2
20+
* @return $this
21+
*/
22+
public function equal($arg1, $arg2): self
23+
{
24+
$this->operator = ConditionalOperator::ARTITH_EQUAL;
25+
$this->compare = $arg1;
26+
$this->compareWith = $arg2;
27+
return $this;
28+
}
29+
30+
/**
31+
* @param string|ShellInterface $arg1
32+
* @param string|ShellInterface $arg2
33+
* @return $this
34+
*/
35+
public function notEqual($arg1, $arg2): self
36+
{
37+
$this->operator = ConditionalOperator::ARTITH_NOT_EQUAL;
38+
$this->compare = $arg1;
39+
$this->compareWith = $arg2;
40+
return $this;
41+
}
42+
43+
/**
44+
* @param string|ShellInterface $arg1
45+
* @param string|ShellInterface $arg2
46+
* @return $this
47+
*/
48+
public function less($arg1, $arg2): self
49+
{
50+
$this->operator = ConditionalOperator::ARTITH_LESS_THAN;
51+
$this->compare = $arg1;
52+
$this->compareWith = $arg2;
53+
return $this;
54+
}
55+
56+
/**
57+
* @param string|ShellInterface $arg1
58+
* @param string|ShellInterface $arg2
59+
* @return $this
60+
*/
61+
public function greater($arg1, $arg2): self
62+
{
63+
$this->operator = ConditionalOperator::ARTITH_GREATER_THAN;
64+
$this->compare = $arg1;
65+
$this->compareWith = $arg2;
66+
return $this;
67+
}
68+
69+
/**
70+
* @param string|ShellInterface $arg1
71+
* @param string|ShellInterface $arg2
72+
* @return $this
73+
*/
74+
public function lessEqual($arg1, $arg2): self
75+
{
76+
$this->operator = ConditionalOperator::ARTITH_LESS_EQUAL;
77+
$this->compare = $arg1;
78+
$this->compareWith = $arg2;
79+
return $this;
80+
}
81+
82+
/**
83+
* @param string|ShellInterface $arg1
84+
* @param string|ShellInterface $arg2
85+
* @return $this
86+
*/
87+
public function greaterEqual($arg1, $arg2): self
88+
{
89+
$this->operator = ConditionalOperator::ARTITH_GREATER_EQUAL;
90+
$this->compare = $arg1;
91+
$this->compareWith = $arg2;
92+
return $this;
93+
}
94+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}

0 commit comments

Comments
 (0)