summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/sql/src/OrderBy.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 11:30:08 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 11:30:08 +0000
commit4ce65d59ca91871cfd126497158200a818720bce (patch)
treee277def01fc7eba7dbc21c4a4ae5576e8aa2cf1f /vendor/ipl/sql/src/OrderBy.php
parentInitial commit. (diff)
downloadicinga-php-library-4ce65d59ca91871cfd126497158200a818720bce.tar.xz
icinga-php-library-4ce65d59ca91871cfd126497158200a818720bce.zip
Adding upstream version 0.13.1.upstream/0.13.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/ipl/sql/src/OrderBy.php')
-rw-r--r--vendor/ipl/sql/src/OrderBy.php74
1 files changed, 74 insertions, 0 deletions
diff --git a/vendor/ipl/sql/src/OrderBy.php b/vendor/ipl/sql/src/OrderBy.php
new file mode 100644
index 0000000..a19d7c5
--- /dev/null
+++ b/vendor/ipl/sql/src/OrderBy.php
@@ -0,0 +1,74 @@
+<?php
+
+namespace ipl\Sql;
+
+/**
+ * Trait for the ORDER BY part of a query
+ */
+trait OrderBy
+{
+ /** @var ?array ORDER BY part of the query */
+ protected $orderBy;
+
+ public function hasOrderBy()
+ {
+ return $this->orderBy !== null;
+ }
+
+ public function getOrderBy()
+ {
+ return $this->orderBy;
+ }
+
+ public function orderBy($orderBy, $direction = null)
+ {
+ if (! is_array($orderBy)) {
+ $orderBy = [$orderBy];
+ }
+
+ foreach ($orderBy as $column => $dir) {
+ if (is_int($column)) {
+ $column = $dir;
+ $dir = $direction;
+ }
+
+ if (is_array($column) && count($column) === 2) {
+ list($column, $dir) = $column;
+ }
+
+ if ($dir === SORT_ASC) {
+ $dir = 'ASC';
+ } elseif ($dir === SORT_DESC) {
+ $dir = 'DESC';
+ }
+
+ $this->orderBy[] = [$column, $dir];
+ }
+
+ return $this;
+ }
+
+ public function resetOrderBy()
+ {
+ $this->orderBy = null;
+
+ return $this;
+ }
+
+ /**
+ * Clone the properties provided by this trait
+ *
+ * Shall be called by using classes in their __clone()
+ */
+ protected function cloneOrderBy()
+ {
+ if ($this->orderBy !== null) {
+ foreach ($this->orderBy as &$orderBy) {
+ if ($orderBy[0] instanceof ExpressionInterface || $orderBy[0] instanceof Select) {
+ $orderBy[0] = clone $orderBy[0];
+ }
+ }
+ unset($orderBy);
+ }
+ }
+}