PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/competency/classes/external/persistent_exporter.php

http://github.com/moodle/moodle
PHP | 83 lines | 28 code | 11 blank | 44 comment | 4 complexity | 4457b86482024dcef9c93dc2d3d21b07 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * Abstract class for core_competency objects saved to the DB.
  18. *
  19. * @package core_competency
  20. * @copyright 2015 Damyon Wiese
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. namespace core_competency\external;
  24. defined('MOODLE_INTERNAL') || die();
  25. require_once($CFG->libdir . '/externallib.php');
  26. use coding_exception;
  27. /**
  28. * An extended version of the persistent class with a default implementation of export
  29. *
  30. * @copyright 2015 Damyon Wiese
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. abstract class persistent_exporter extends exporter {
  34. /** @var \core_competency\persistent The persistent object we will export. */
  35. protected $persistent = null;
  36. /**
  37. * Constructor - saves the persistent object, and the related objects.
  38. *
  39. * @param \core_competency\persistent $persistent The persistent object to export.
  40. * @param array $related - An optional list of pre-loaded objects related to this persistent.
  41. */
  42. public final function __construct(\core_competency\persistent $persistent, $related = array()) {
  43. $classname = static::define_class();
  44. if (!$persistent instanceof $classname) {
  45. throw new coding_exception('Invalid type for persistent. ' .
  46. 'Expected: ' . $classname . ' got: ' . get_class($persistent));
  47. }
  48. $this->persistent = $persistent;
  49. if (method_exists($this->persistent, 'get_context') && !isset($this->related['context'])) {
  50. $this->related['context'] = $this->persistent->get_context();
  51. }
  52. $data = $persistent->to_record();
  53. parent::__construct($data, $related);
  54. }
  55. /**
  56. * Persistent exporters get their standard properties from the persistent class.
  57. *
  58. * @return array Keys are the property names, and value their definition.
  59. */
  60. protected final static function define_properties() {
  61. $classname = static::define_class();
  62. return $classname::properties_definition();
  63. }
  64. /**
  65. * Returns the specific class the persistent should be an instance of.
  66. *
  67. * @return string
  68. */
  69. protected static function define_class() {
  70. throw new coding_exception('define_class() must be overidden.');
  71. }
  72. }