| @@ 8-82 (lines=75) @@ | ||
| 5 | use Doctrine\ORM\QueryBuilder; |
|
| 6 | use Happyr\DoctrineSpecification\Exception\InvalidArgumentException; |
|
| 7 | ||
| 8 | abstract class Arithmetic implements Operand |
|
| 9 | { |
|
| 10 | const ADD = '+'; |
|
| 11 | ||
| 12 | const SUB = '-'; |
|
| 13 | ||
| 14 | const MUL = '*'; |
|
| 15 | ||
| 16 | const DIV = '/'; |
|
| 17 | ||
| 18 | const MOD = '%'; |
|
| 19 | ||
| 20 | /** |
|
| 21 | * @var string[] |
|
| 22 | */ |
|
| 23 | private static $operations = array( |
|
| 24 | self::ADD, |
|
| 25 | self::SUB, |
|
| 26 | self::MUL, |
|
| 27 | self::DIV, |
|
| 28 | self::MOD, |
|
| 29 | ); |
|
| 30 | ||
| 31 | /** |
|
| 32 | * @var string |
|
| 33 | */ |
|
| 34 | private $operation = ''; |
|
| 35 | ||
| 36 | /** |
|
| 37 | * @var Operand|string |
|
| 38 | */ |
|
| 39 | private $field; |
|
| 40 | ||
| 41 | /** |
|
| 42 | * @var Operand|string |
|
| 43 | */ |
|
| 44 | private $value; |
|
| 45 | ||
| 46 | /** |
|
| 47 | * @param string $operation |
|
| 48 | * @param Operand|string $field |
|
| 49 | * @param Operand|string $value |
|
| 50 | */ |
|
| 51 | public function __construct($operation, $field, $value) |
|
| 52 | { |
|
| 53 | if (!in_array($operation, self::$operations)) { |
|
| 54 | throw new InvalidArgumentException(sprintf( |
|
| 55 | '"%s" is not a valid arithmetic operation. Valid operations are: "%s"', |
|
| 56 | $operation, |
|
| 57 | implode(', ', self::$operations) |
|
| 58 | )); |
|
| 59 | } |
|
| 60 | ||
| 61 | $this->operation = $operation; |
|
| 62 | $this->field = $field; |
|
| 63 | $this->value = $value; |
|
| 64 | } |
|
| 65 | ||
| 66 | /** |
|
| 67 | * @param QueryBuilder $qb |
|
| 68 | * @param string $dqlAlias |
|
| 69 | * |
|
| 70 | * @return string |
|
| 71 | */ |
|
| 72 | public function transform(QueryBuilder $qb, $dqlAlias) |
|
| 73 | { |
|
| 74 | $field = ArgumentToOperandConverter::toField($this->field); |
|
| 75 | $value = ArgumentToOperandConverter::toValue($this->value); |
|
| 76 | ||
| 77 | $field = $field->transform($qb, $dqlAlias); |
|
| 78 | $value = $value->transform($qb, $dqlAlias); |
|
| 79 | ||
| 80 | return sprintf('(%s %s %s)', $field, $this->operation, $value); |
|
| 81 | } |
|
| 82 | } |
|
| 83 | ||
| @@ 8-82 (lines=75) @@ | ||
| 5 | use Doctrine\ORM\QueryBuilder; |
|
| 6 | use Happyr\DoctrineSpecification\Exception\InvalidArgumentException; |
|
| 7 | ||
| 8 | abstract class Bitwise implements Operand |
|
| 9 | { |
|
| 10 | const B_AND = '&'; |
|
| 11 | ||
| 12 | const B_OR = '|'; |
|
| 13 | ||
| 14 | const B_XOR = '^'; |
|
| 15 | ||
| 16 | const B_LS = '<<'; |
|
| 17 | ||
| 18 | const B_RS = '>>'; |
|
| 19 | ||
| 20 | /** |
|
| 21 | * @var string[] |
|
| 22 | */ |
|
| 23 | private static $operations = array( |
|
| 24 | self::B_AND, |
|
| 25 | self::B_OR, |
|
| 26 | self::B_XOR, |
|
| 27 | self::B_LS, |
|
| 28 | self::B_RS, |
|
| 29 | ); |
|
| 30 | ||
| 31 | /** |
|
| 32 | * @var string |
|
| 33 | */ |
|
| 34 | private $operation = ''; |
|
| 35 | ||
| 36 | /** |
|
| 37 | * @var Operand|string |
|
| 38 | */ |
|
| 39 | private $field; |
|
| 40 | ||
| 41 | /** |
|
| 42 | * @var Operand|string |
|
| 43 | */ |
|
| 44 | private $value; |
|
| 45 | ||
| 46 | /** |
|
| 47 | * @param string $operation |
|
| 48 | * @param Operand|string $field |
|
| 49 | * @param Operand|string $value |
|
| 50 | */ |
|
| 51 | public function __construct($operation, $field, $value) |
|
| 52 | { |
|
| 53 | if (!in_array($operation, self::$operations)) { |
|
| 54 | throw new InvalidArgumentException(sprintf( |
|
| 55 | '"%s" is not a valid bitwise operation. Valid operations are: "%s"', |
|
| 56 | $operation, |
|
| 57 | implode(', ', self::$operations) |
|
| 58 | )); |
|
| 59 | } |
|
| 60 | ||
| 61 | $this->operation = $operation; |
|
| 62 | $this->field = $field; |
|
| 63 | $this->value = $value; |
|
| 64 | } |
|
| 65 | ||
| 66 | /** |
|
| 67 | * @param QueryBuilder $qb |
|
| 68 | * @param string $dqlAlias |
|
| 69 | * |
|
| 70 | * @return string |
|
| 71 | */ |
|
| 72 | public function transform(QueryBuilder $qb, $dqlAlias) |
|
| 73 | { |
|
| 74 | $field = ArgumentToOperandConverter::toField($this->field); |
|
| 75 | $value = ArgumentToOperandConverter::toValue($this->value); |
|
| 76 | ||
| 77 | $field = $field->transform($qb, $dqlAlias); |
|
| 78 | $value = $value->transform($qb, $dqlAlias); |
|
| 79 | ||
| 80 | return sprintf('(%s %s %s)', $field, $this->operation, $value); |
|
| 81 | } |
|
| 82 | } |
|
| 83 | ||