Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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   * Author data mapper.
  19   *
  20   * @package    mod_forum
  21   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_forum\local\data_mappers\legacy;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use mod_forum\local\entities\author as author_entity;
  30  use stdClass;
  31  
  32  /**
  33   * Convert an author entity into an stdClass.
  34   *
  35   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class author {
  39      /**
  40       * Convert a list of author entities into stdClasses.
  41       *
  42       * @param author_entity[] $authors The authors to convert.
  43       * @return stdClass[]
  44       */
  45      public function to_legacy_objects(array $authors) : array {
  46          return array_map(function(author_entity $author) {
  47              return (object) [
  48                  'id' => $author->get_id(),
  49                  'picture' => $author->get_picture_item_id(),
  50                  'firstname' => $author->get_first_name(),
  51                  'lastname' => $author->get_last_name(),
  52                  'fullname' => $author->get_full_name(),
  53                  'email' => $author->get_email(),
  54                  'deleted' => $author->is_deleted(),
  55                  'middlename' => $author->get_middle_name(),
  56                  'firstnamephonetic' => $author->get_first_name_phonetic(),
  57                  'lastnamephonetic' => $author->get_last_name_phonetic(),
  58                  'alternatename' => $author->get_alternate_name(),
  59                  'imagealt' => $author->get_image_alt()
  60              ];
  61          }, $authors);
  62      }
  63  
  64      /**
  65       * Convert an author entity into an stdClass.
  66       *
  67       * @param author_entity $author The author to convert.
  68       * @return stdClass
  69       */
  70      public function to_legacy_object(author_entity $author) : stdClass {
  71          return $this->to_legacy_objects([$author])[0];
  72      }
  73  }