PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/backup/util/structure/restore_path_element.class.php

https://bitbucket.org/moodle/moodle
PHP | 138 lines | 65 code | 24 blank | 49 comment | 7 complexity | 9db42aa5b6e013864328ef2a545e97fb MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * @package moodlecore
  18. * @subpackage backup-structure
  19. * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. *
  22. * TODO: Finish phpdocs
  23. */
  24. /**
  25. * Class representing one path to be restored from XML file
  26. */
  27. class restore_path_element {
  28. /** @var string name of the element */
  29. private $name;
  30. /** @var string path within the XML file this element will handle */
  31. private $path;
  32. /** @var bool flag to define if this element will get child ones grouped or no */
  33. private $grouped;
  34. /** @var object object instance in charge of processing this element. */
  35. private $pobject;
  36. /** @var mixed last data read for this element or returned data by processing method */
  37. private $data;
  38. /**
  39. * Constructor - instantiates one restore_path_element, specifying its basic info.
  40. *
  41. * @param string $name name of the thing being restored. This determines the name of the process_... method called.
  42. * @param string $path path of the element.
  43. * @param bool $grouped to gather information in grouped mode or no.
  44. */
  45. public function __construct($name, $path, $grouped = false) {
  46. $this->validate_name($name); // Check name
  47. $this->name = $name;
  48. $this->path = $path;
  49. $this->grouped = $grouped;
  50. $this->pobject = null;
  51. $this->data = null;
  52. }
  53. protected function validate_name($name) {
  54. // Validate various name constraints, throwing exception if needed
  55. if (empty($name)) {
  56. throw new restore_path_element_exception('restore_path_element_emptyname', $name);
  57. }
  58. if (preg_replace('/\s/', '', $name) != $name) {
  59. throw new restore_path_element_exception('restore_path_element_whitespace', $name);
  60. }
  61. if (preg_replace('/[^\x30-\x39\x41-\x5a\x5f\x61-\x7a]/', '', $name) != $name) {
  62. throw new restore_path_element_exception('restore_path_element_notasciiname', $name);
  63. }
  64. }
  65. protected function validate_pobject($pobject) {
  66. if (!is_object($pobject)) {
  67. throw new restore_path_element_exception('restore_path_element_noobject', $pobject);
  68. }
  69. if (!method_exists($pobject, $this->get_processing_method())) {
  70. throw new restore_path_element_exception('restore_path_element_missingmethod', $this->get_processing_method());
  71. }
  72. }
  73. /// Public API starts here
  74. public function set_processing_object($pobject) {
  75. $this->validate_pobject($pobject);
  76. $this->pobject = $pobject;
  77. }
  78. public function set_data($data) {
  79. $this->data = $data;
  80. }
  81. public function get_name() {
  82. return $this->name;
  83. }
  84. public function get_path() {
  85. return $this->path;
  86. }
  87. public function is_grouped() {
  88. return $this->grouped;
  89. }
  90. public function get_processing_object() {
  91. return $this->pobject;
  92. }
  93. public function get_processing_method() {
  94. return 'process_' . $this->name;
  95. }
  96. public function get_data() {
  97. return $this->data;
  98. }
  99. }
  100. /**
  101. * restore_path_element exception class
  102. */
  103. class restore_path_element_exception extends moodle_exception {
  104. /**
  105. * Constructor - instantiates one restore_path_element_exception
  106. *
  107. * @param string $errorcode key for the corresponding error string
  108. * @param object $a extra words and phrases that might be required in the error string
  109. * @param string $debuginfo optional debugging information
  110. */
  111. public function __construct($errorcode, $a = null, $debuginfo = null) {
  112. parent::__construct($errorcode, '', '', $a, $debuginfo);
  113. }
  114. }