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. */
  27. const PREFIX = 0;
  28. /**
  29. * Indicates that the operation is in post-order
  30. *
  31. */
  32. const POSTFIX = 1;
  33. /**
  34. * The operator this unary expression represents
  35. *
  36. * @var string
  37. */
  38. protected $_operator;
  39. /**
  40. * Holds the value which the unary expression operates
  41. *
  42. * @var mixed
  43. */
  44. protected $_value;
  45. /**
  46. * Where to place the operator
  47. *
  48. * @var int
  49. */
  50. protected $_mode;
  51. /**
  52. * Constructor
  53. *
  54. * @param string $operator The operator to used for the expression
  55. * @param mixed $value the value to use as the operand for the expression
  56. * @param int $mode either UnaryExpression::PREFIX or UnaryExpression::POSTFIX
  57. */
  58. public function __construct($operator, $value, $mode = self::PREFIX)
  59. {
  60. $this->_operator = $operator;
  61. $this->_value = $value;
  62. $this->_mode = $mode;
  63. }
  64. /**
  65. * Converts the expression to its string representation
  66. *
  67. * @param \Cake\Database\ValueBinder $generator Placeholder generator object
  68. * @return string
  69. */
  70. public function sql(ValueBinder $generator)
  71. {
  72. $operand = $this->_value;
  73. if ($operand instanceof ExpressionInterface) {
  74. $operand = $operand->sql($generator);
  75. }
  76. if ($this->_mode === self::POSTFIX) {
  77. return '(' . $operand . ') ' . $this->_operator;
  78. }
  79. return $this->_operator . ' (' . $operand . ')';
  80. }
  81. /**
  82. * {@inheritDoc}
  83. *
  84. */
  85. public function traverse(callable $callable)
  86. {
  87. if ($this->_value instanceof ExpressionInterface) {
  88. $callable($this->_value);
  89. $this->_value->traverse($callable);
  90. }
  91. }
  92. /**
  93. * Perform a deep clone of the inner expression.
  94. *
  95. * @return void
  96. */
  97. public function __clone()
  98. {
  99. if ($this->_value instanceof ExpressionInterface) {
  100. $this->_value = clone $this->_value;
  101. }
  102. }
  103. }