aboutsummaryrefslogtreecommitdiff
path: root/lib/models/eTask/ConfigureTrait.php
blob: 39b8b714b6580cf0c701e90b0b0b3c76f22a8f0f (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
55
56
57
58
59
60
61
62
<?php

namespace eTask;

/**
 * eTask SORM class relationship configuration trait.
 *
 * eTask models are meant to be subclassed. As the current SORM
 * implementation hardcodes the types of relationships, this trait is
 * used to re-wire the relationships according to the applications
 * needs.
 *
 * In this case your application should add configuration either via
 * the #configure method or via another trait.
 *
 * Example:
 * \code
 *        $config['relationTypes'] = [
 *           'Assignment'      => '\\Example\\Assignment',
 *           'AssignmentRange' => '\\Example\\AssignmentRange',
 *           'Attempt'         => '\\Example\\Attempt',
 *           'Response'        => '\\Example\\Response',
 *           'Task'            => '\\Example\\Task',
 *           'Test'            => '\\Example\\Test',
 *           'TestTask'        => '\\Example\\TestTask'
 *       ];
 * \encode
 */
trait ConfigureTrait
{
    /**
     * @property array relationTypes lookup table of potential
     * subclasses to be used as types of the eTask SORMs
     */
    protected $relationTypes = [];

    // set default relationship types
    private static function configureClassNames($config = [])
    {
        $defaultTypes = [
            'Assignment'      => Assignment::class,
            'AssignmentRange' => AssignmentRange::class,
            'Attempt'         => Attempt::class,
            'Response'        => Response::class,
            'Task'            => Task::class,
            'Test'            => Test::class,
            'TestTask'        => TestTask::class,
        ];

        $types = [];

        if (!isset($config['relationTypes'])) {
            $types = $defaultTypes;
        } else {
            foreach ($defaultTypes as $key => $classname) {
                $types[$key] = $config['relationTypes'][$key] ?: $classname;
            }
        }

        return $types;
    }
}