1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Exporter based on persistent.
|
|
|
19 |
*
|
|
|
20 |
* @package core
|
|
|
21 |
* @copyright 2015 Damyon Wiese
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
namespace core\external;
|
|
|
25 |
|
|
|
26 |
use coding_exception;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Abstract exporter based on the persistent model.
|
|
|
30 |
*
|
|
|
31 |
* This automatically fills in the properties of the exporter from those of the persistent.
|
|
|
32 |
*
|
|
|
33 |
* @copyright 2015 Damyon Wiese
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
abstract class persistent_exporter extends exporter {
|
|
|
37 |
|
|
|
38 |
/** @var \core\persistent The persistent object we will export. */
|
|
|
39 |
protected $persistent = null;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Constructor - saves the persistent object, and the related objects.
|
|
|
43 |
*
|
|
|
44 |
* @param \core\persistent $persistent The persistent object to export.
|
|
|
45 |
* @param array $related - An optional list of pre-loaded objects related to this persistent.
|
|
|
46 |
*/
|
|
|
47 |
public function __construct(\core\persistent $persistent, $related = array()) {
|
|
|
48 |
$classname = static::define_class();
|
|
|
49 |
if (!$persistent instanceof $classname) {
|
|
|
50 |
throw new coding_exception('Invalid type for persistent. ' .
|
|
|
51 |
'Expected: ' . $classname . ' got: ' . get_class($persistent));
|
|
|
52 |
}
|
|
|
53 |
$this->persistent = $persistent;
|
|
|
54 |
|
|
|
55 |
if (method_exists($this->persistent, 'get_context') && !isset($this->related['context'])) {
|
|
|
56 |
$this->related['context'] = $this->persistent->get_context();
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
$data = $persistent->to_record();
|
|
|
60 |
parent::__construct($data, $related);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Persistent exporters get their standard properties from the persistent class.
|
|
|
65 |
*
|
|
|
66 |
* @return array Keys are the property names, and value their definition.
|
|
|
67 |
*/
|
|
|
68 |
final protected static function define_properties() {
|
|
|
69 |
$classname = static::define_class();
|
|
|
70 |
return $classname::properties_definition();
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Returns the specific class the persistent should be an instance of.
|
|
|
75 |
*
|
|
|
76 |
* @return string
|
|
|
77 |
*/
|
|
|
78 |
protected static function define_class() {
|
|
|
79 |
throw new coding_exception('define_class() must be overidden.');
|
|
|
80 |
}
|
|
|
81 |
}
|