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:
17: use Cake\Database\ExpressionInterface;
18: use Cake\Database\ValueBinder;
19:
20: /**
21: * An expression object for complex ORDER BY clauses
22: */
23: class OrderClauseExpression implements ExpressionInterface, FieldInterface
24: {
25: use FieldTrait;
26:
27: /**
28: * The direction of sorting.
29: *
30: * @var string
31: */
32: protected $_direction;
33:
34: /**
35: * Constructor
36: *
37: * @param \Cake\Database\ExpressionInterface|string $field The field to order on.
38: * @param string $direction The direction to sort on.
39: */
40: public function __construct($field, $direction)
41: {
42: $this->_field = $field;
43: $this->_direction = strtolower($direction) === 'asc' ? 'ASC' : 'DESC';
44: }
45:
46: /**
47: * {@inheritDoc}
48: */
49: public function sql(ValueBinder $generator)
50: {
51: $field = $this->_field;
52: if ($field instanceof ExpressionInterface) {
53: $field = $field->sql($generator);
54: }
55:
56: return sprintf('%s %s', $field, $this->_direction);
57: }
58:
59: /**
60: * {@inheritDoc}
61: */
62: public function traverse(callable $visitor)
63: {
64: if ($this->_field instanceof ExpressionInterface) {
65: $visitor($this->_field);
66: $this->_field->traverse($visitor);
67: }
68: }
69:
70: /**
71: * Create a deep clone of the order clause.
72: *
73: * @return void
74: */
75: public function __clone()
76: {
77: if ($this->_field instanceof ExpressionInterface) {
78: $this->_field = clone $this->_field;
79: }
80: }
81: }
82: