-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from phpsu/feature/add-conditional-expression
Validation for options and conditional expressions
- Loading branch information
Showing
18 changed files
with
1,303 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PHPSu\ShellCommandBuilder\Conditional; | ||
|
||
use PHPSu\ShellCommandBuilder\Definition\ConditionalOperator; | ||
use PHPSu\ShellCommandBuilder\ShellInterface; | ||
|
||
final class ArithmeticExpression extends BasicExpression | ||
{ | ||
public static function create(bool $useBashBrackets = true, bool $negateExpression = false): ArithmeticExpression | ||
{ | ||
return new self($useBashBrackets, $negateExpression); | ||
} | ||
|
||
/** | ||
* @param string|ShellInterface $arg1 | ||
* @param string|ShellInterface $arg2 | ||
* @return $this | ||
*/ | ||
public function equal($arg1, $arg2): self | ||
{ | ||
$this->operator = ConditionalOperator::ARTITH_EQUAL; | ||
$this->compare = $arg1; | ||
$this->compareWith = $arg2; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string|ShellInterface $arg1 | ||
* @param string|ShellInterface $arg2 | ||
* @return $this | ||
*/ | ||
public function notEqual($arg1, $arg2): self | ||
{ | ||
$this->operator = ConditionalOperator::ARTITH_NOT_EQUAL; | ||
$this->compare = $arg1; | ||
$this->compareWith = $arg2; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string|ShellInterface $arg1 | ||
* @param string|ShellInterface $arg2 | ||
* @return $this | ||
*/ | ||
public function less($arg1, $arg2): self | ||
{ | ||
$this->operator = ConditionalOperator::ARTITH_LESS_THAN; | ||
$this->compare = $arg1; | ||
$this->compareWith = $arg2; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string|ShellInterface $arg1 | ||
* @param string|ShellInterface $arg2 | ||
* @return $this | ||
*/ | ||
public function greater($arg1, $arg2): self | ||
{ | ||
$this->operator = ConditionalOperator::ARTITH_GREATER_THAN; | ||
$this->compare = $arg1; | ||
$this->compareWith = $arg2; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string|ShellInterface $arg1 | ||
* @param string|ShellInterface $arg2 | ||
* @return $this | ||
*/ | ||
public function lessEqual($arg1, $arg2): self | ||
{ | ||
$this->operator = ConditionalOperator::ARTITH_LESS_EQUAL; | ||
$this->compare = $arg1; | ||
$this->compareWith = $arg2; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string|ShellInterface $arg1 | ||
* @param string|ShellInterface $arg2 | ||
* @return $this | ||
*/ | ||
public function greaterEqual($arg1, $arg2): self | ||
{ | ||
$this->operator = ConditionalOperator::ARTITH_GREATER_EQUAL; | ||
$this->compare = $arg1; | ||
$this->compareWith = $arg2; | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PHPSu\ShellCommandBuilder\Conditional; | ||
|
||
use PHPSu\ShellCommandBuilder\Definition\ConditionalOperator; | ||
use PHPSu\ShellCommandBuilder\ShellInterface; | ||
|
||
abstract class BasicExpression implements ShellInterface | ||
{ | ||
/** | ||
* This is not POSIX-compatible (only eg. Korn and Bash), beware before using it | ||
* @var bool | ||
*/ | ||
protected $bashEnhancedBrackets; | ||
/** @var bool this is always double quoted */ | ||
protected $escapedValue = false; | ||
/** @var bool */ | ||
private $negateExpression; | ||
/** @var string|ShellInterface */ | ||
protected $compare = ''; | ||
/** @var string|ShellInterface */ | ||
protected $compareWith = ''; | ||
/** @var string */ | ||
protected $operator = ''; | ||
|
||
public function __construct(bool $useBashBrackets, bool $negateExpression) | ||
{ | ||
$this->negateExpression = $negateExpression; | ||
$this->bashEnhancedBrackets = $useBashBrackets; | ||
} | ||
|
||
public function escapeValue(bool $enable): BasicExpression | ||
{ | ||
$this->escapedValue = $enable; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @todo with min. support of php 7.4 this can be fully implemented here | ||
* @param bool $useBashBrackets | ||
* @param bool $negateExpression | ||
* @return mixed | ||
*/ | ||
abstract public static function create(bool $useBashBrackets = false, bool $negateExpression = false); | ||
|
||
|
||
/** | ||
* @param string|ShellInterface $value | ||
* @return string|array<mixed> | ||
*/ | ||
private function getValueDebug($value) | ||
{ | ||
if ($value instanceof ShellInterface) { | ||
return $value->__toArray(); | ||
} | ||
return $value; | ||
} | ||
|
||
/** | ||
* @param string|ShellInterface $value | ||
* @return string | ||
*/ | ||
private function getValue($value): string | ||
{ | ||
$return = $value; | ||
if ($value instanceof ShellInterface) { | ||
$return = $value->__toString(); | ||
} | ||
if ($this->escapedValue) { | ||
/** @psalm-suppress ImplicitToStringCast */ | ||
$return = sprintf('"%s"', $return); | ||
} | ||
return $return; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf( | ||
'%s %s%s%s%s %s', | ||
$this->bashEnhancedBrackets ? ConditionalOperator::BRACKET_LEFT_BASH : ConditionalOperator::BRACKET_LEFT, | ||
$this->negateExpression ? '! ' : '', | ||
$this->compare ? $this->getValue($this->compare) . ' ' : '', | ||
$this->operator, | ||
$this->compareWith ? ' ' . $this->getValue($this->compareWith) : '', | ||
$this->bashEnhancedBrackets ? ConditionalOperator::BRACKET_RIGHT_BASH : ConditionalOperator::BRACKET_RIGHT | ||
); | ||
} | ||
|
||
/** | ||
* @return array<string,mixed> | ||
*/ | ||
public function __toArray(): array | ||
{ | ||
return [ | ||
'bashBrackets' => $this->bashEnhancedBrackets, | ||
'negate' => $this->negateExpression, | ||
'compare' => $this->getValueDebug($this->compare), | ||
'operator' => $this->operator, | ||
'compareWith' => $this->getValueDebug($this->compareWith), | ||
]; | ||
} | ||
} |
Oops, something went wrong.