[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/competency/classes/external/ -> persistent_exporter.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1