aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/RangeFactory.php
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+github@gmail.com>2021-07-22 16:07:19 +0200
committerJan-Hendrik Willms <tleilax+github@gmail.com>2021-07-22 16:19:12 +0200
commita3da1483a9e689846179159355badfec8073dbec (patch)
tree770dcca6bdf5f6f2a11b0e7fcbbeda6919a3fc52 /lib/classes/RangeFactory.php
current code from svn, revision 62608
Diffstat (limited to 'lib/classes/RangeFactory.php')
-rw-r--r--lib/classes/RangeFactory.php54
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/classes/RangeFactory.php b/lib/classes/RangeFactory.php
new file mode 100644
index 0000000..73cf11e
--- /dev/null
+++ b/lib/classes/RangeFactory.php
@@ -0,0 +1,54 @@
+<?php
+/**
+ * Factory for ranges.
+ *
+ * @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
+ * @license GPL2 or any later version
+ * @since Stud.IP 4.1
+ */
+final class RangeFactory
+{
+ const TYPE_MAPPING = [
+ 'sem' => 'course',
+ 'user' => 'user',
+ 'inst' => 'institute',
+ 'fak' => 'institute',
+ ];
+
+ public static function find($id)
+ {
+ $type = get_object_type($id, ['sem', 'user', 'inst', 'fak']);
+ if ($type === false) {
+ return false;
+ }
+
+ return self::createRange(self::TYPE_MAPPING[$type], $id);
+ }
+
+ /**
+ * Create a range by given type and id.
+ *
+ * @param string $type Range type
+ * @param mixed $id Range id
+ * @return mixed any of the supported range types
+ * @throws Exception when an invalid range type was given
+ *
+ * @todo Should this be more dynamic in case any more ranges are added?
+ */
+ public static function createRange($type, $id)
+ {
+ if ($type === 'user') {
+ return new User($id);
+ }
+
+ if ($type === 'course') {
+ return new Course($id);
+ }
+
+ if ($type === 'institute' || $type === 'fak') {
+ return new Institute($id);
+ }
+
+ throw new Exception('Unknown type');
+ }
+}