summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/orm/src/Common
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/ipl/orm/src/Common')
-rw-r--r--vendor/ipl/orm/src/Common/PropertiesWithDefaults.php31
-rw-r--r--vendor/ipl/orm/src/Common/SortUtil.php65
2 files changed, 96 insertions, 0 deletions
diff --git a/vendor/ipl/orm/src/Common/PropertiesWithDefaults.php b/vendor/ipl/orm/src/Common/PropertiesWithDefaults.php
new file mode 100644
index 0000000..e8d3a84
--- /dev/null
+++ b/vendor/ipl/orm/src/Common/PropertiesWithDefaults.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace ipl\Orm\Common;
+
+use Closure;
+use Traversable;
+
+trait PropertiesWithDefaults
+{
+ use \ipl\Stdlib\Properties {
+ \ipl\Stdlib\Properties::getProperty as private parentGetProperty;
+ }
+
+ protected function getProperty($key)
+ {
+ if (isset($this->properties[$key]) && $this->properties[$key] instanceof Closure) {
+ $this->setProperty($key, $this->properties[$key]($this, $key));
+ }
+
+ return $this->parentGetProperty($key);
+ }
+
+ public function getIterator(): Traversable
+ {
+ foreach ($this->properties as $key => $value) {
+ if (! $value instanceof Closure) {
+ yield $key => $value;
+ }
+ }
+ }
+}
diff --git a/vendor/ipl/orm/src/Common/SortUtil.php b/vendor/ipl/orm/src/Common/SortUtil.php
new file mode 100644
index 0000000..a14ea2b
--- /dev/null
+++ b/vendor/ipl/orm/src/Common/SortUtil.php
@@ -0,0 +1,65 @@
+<?php
+
+namespace ipl\Orm\Common;
+
+use ipl\Stdlib\Str;
+
+class SortUtil
+{
+ /**
+ * Create the sort column(s) and direction(s) from the given sort spec
+ *
+ * @param array|string $sort
+ *
+ * @return array<int, mixed> Sort column(s) and direction(s) suitable for {@link OrderByInterface::orderBy()}
+ */
+ public static function createOrderBy($sort): array
+ {
+ $columnsAndDirections = static::explodeSortSpec($sort);
+ $orderBy = [];
+
+ foreach ($columnsAndDirections as $columnAndDirection) {
+ list($column, $direction) = static::splitColumnAndDirection($columnAndDirection);
+
+ $orderBy[] = [$column, $direction];
+ }
+
+ return $orderBy;
+ }
+
+ /**
+ * Explode the given sort spec into its sort parts
+ *
+ * @param array|string $sort
+ *
+ * @return array
+ */
+ public static function explodeSortSpec($sort)
+ {
+ return Str::trimSplit(implode(',', (array) $sort));
+ }
+
+ /**
+ * Normalize the given sort spec to a sort string
+ *
+ * @param array|string $sort
+ *
+ * @return string
+ */
+ public static function normalizeSortSpec($sort)
+ {
+ return implode(',', static::explodeSortSpec($sort));
+ }
+
+ /**
+ * Explode the given sort part into its sort column and direction
+ *
+ * @param string $sort
+ *
+ * @return array
+ */
+ public static function splitColumnAndDirection($sort)
+ {
+ return Str::symmetricSplit($sort, ' ', 2);
+ }
+}