UnaryExpression.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database\Expression;
  16. use Cake\Database\ExpressionInterface;
  17. use Cake\Database\ValueBinder;
  18. /**
  19. * An expression object that represents an expression with only a single operand.
  20. */
  21. class UnaryExpression implements ExpressionInterface
  22. {
  23. /**
  24. * Indicates that the operation is in pre-order
  25. *
  26. * @var int
  27. */
  28. const PREFIX = 0;
  29. /**
  30. * Indicates that the operation is in post-order
  31. *
  32. * @var int
  33. */
  34. const POSTFIX = 1;
  35. /**
  36. * The operator this unary expression represents
  37. *
  38. * @var string
  39. */
  40. protected $_operator;
  41. /**
  42. * Holds the value which the unary expression operates
  43. *
  44. * @var mixed
  45. */
  46. protected $_value;
  47. /**
  48. * Where to place the operator
  49. *
  50. * @var int
  51. */
  52. protected $_mode;
  53. /**
  54. * Constructor
  55. *
  56. * @param string $operator The operator to used for the expression
  57. * @param mixed $value the value to use as the operand for the expression
  58. * @param int $mode either UnaryExpression::PREFIX or UnaryExpression::POSTFIX
  59. */
  60. public function __construct($operator, $value, $mode = self::PREFIX)
  61. {
  62. $this->_operator = $operator;
  63. $this->_value = $value;
  64. $this->_mode = $mode;
  65. }
  66. /**
  67. * Converts the expression to its string representation
  68. *
  69. * @param \Cake\Database\ValueBinder $generator Placeholder generator object
  70. * @return string
  71. */
  72. public function sql(ValueBinder $generator)
  73. {
  74. $operand = $this->_value;
  75. if ($operand instanceof ExpressionInterface) {
  76. $operand = $operand->sql($generator);
  77. }
  78. if ($this->_mode === self::POSTFIX) {
  79. return '(' . $operand . ') ' . $this->_operator;
  80. }
  81. return $this->_operator . ' (' . $operand . ')';
  82. }
  83. /**
  84. * {@inheritDoc}
  85. */
  86. public function traverse(callable $visitor)
  87. {
  88. if ($this->_value instanceof ExpressionInterface) {
  89. $visitor($this->_value);
  90. $this->_value->traverse($visitor);
  91. }
  92. }
  93. /**
  94. * Perform a deep clone of the inner expression.
  95. *
  96. * @return void
  97. */
  98. public function __clone()
  99. {
  100. if ($this->_value instanceof ExpressionInterface) {
  101. $this->_value = clone $this->_value;
  102. }
  103. }
  104. }