aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/RangeFactory.php
blob: 73cf11e9b7bd0130acdc775509760dfb7e14a4c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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');
    }
}