Skip to content

Commit

Permalink
Merge pull request #7 from phpsu/feature/add-conditional-expression
Browse files Browse the repository at this point in the history
Validation for options and conditional expressions
  • Loading branch information
ChrisB9 committed May 19, 2020
2 parents 7bf58da + d833430 commit 9f8ca57
Show file tree
Hide file tree
Showing 18 changed files with 1,303 additions and 2 deletions.
94 changes: 94 additions & 0 deletions src/Conditional/ArithmeticExpression.php
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;
}
}
104 changes: 104 additions & 0 deletions src/Conditional/BasicExpression.php
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),
];
}
}

0 comments on commit 9f8ca57

Please sign in to comment.