moodle /lib/classes/external/persistent_exporter.php

Language PHP Lines 86
MD5 Hash 0339c602b7da332d7b1821a88f1813a0 Estimated Cost $609 (why?)
Repository https://bitbucket.org/moodle/moodle.git View Raw File View Project SPDX
 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Exporter based on persistent.
 *
 * @package    core
 * @copyright  2015 Damyon Wiese
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
namespace core\external;
defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir . '/externallib.php');

use coding_exception;

/**
 * Abstract exporter based on the persistent model.
 *
 * This automatically fills in the properties of the exporter from those of the persistent.
 *
 * @copyright  2015 Damyon Wiese
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
abstract class persistent_exporter extends exporter {

    /** @var \core\persistent The persistent object we will export. */
    protected $persistent = null;

    /**
     * Constructor - saves the persistent object, and the related objects.
     *
     * @param \core\persistent $persistent The persistent object to export.
     * @param array $related - An optional list of pre-loaded objects related to this persistent.
     */
    public function __construct(\core\persistent $persistent, $related = array()) {
        $classname = static::define_class();
        if (!$persistent instanceof $classname) {
            throw new coding_exception('Invalid type for persistent. ' .
                                       'Expected: ' . $classname . ' got: ' . get_class($persistent));
        }
        $this->persistent = $persistent;

        if (method_exists($this->persistent, 'get_context') && !isset($this->related['context'])) {
            $this->related['context'] = $this->persistent->get_context();
        }

        $data = $persistent->to_record();
        parent::__construct($data, $related);
    }

    /**
     * Persistent exporters get their standard properties from the persistent class.
     *
     * @return array Keys are the property names, and value their definition.
     */
    protected final static function define_properties() {
        $classname = static::define_class();
        return $classname::properties_definition();
    }

    /**
     * Returns the specific class the persistent should be an instance of.
     *
     * @return string
     */
    protected static function define_class() {
        throw new coding_exception('define_class() must be overidden.');
    }

}
Back to Top