|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00030 abstract class base_atom { 00031 00033 private $name; 00034 00036 private $value; 00037 00039 private $is_set; 00040 00047 public function __construct($name) { 00048 00049 $this->validate_name($name); // Check name 00050 00051 $this->name = $name; 00052 $this->value = null; 00053 $this->is_set= false; 00054 } 00055 00056 protected function validate_name($name) { 00057 // Validate various name constraints, throwing exception if needed 00058 if (empty($name)) { 00059 throw new base_atom_struct_exception('backupatomemptyname', $name); 00060 } 00061 if (preg_replace('/\s/', '', $name) != $name) { 00062 throw new base_atom_struct_exception('backupatomwhitespacename', $name); 00063 } 00064 if (preg_replace('/[^\x30-\x39\x41-\x5a\x5f\x61-\x7a]/', '', $name) != $name) { 00065 throw new base_atom_struct_exception('backupatomnotasciiname', $name); 00066 } 00067 } 00068 00070 00071 public function get_name() { 00072 return $this->name; 00073 } 00074 00075 public function get_value() { 00076 return $this->value; 00077 } 00078 00079 public function set_value($value) { 00080 if ($this->is_set) { 00081 throw new base_atom_content_exception('backupatomalreadysetvalue', $value); 00082 } 00083 $this->value = $value; 00084 $this->is_set= true; 00085 } 00086 00087 public function clean_value() { 00088 $this->value = null; 00089 $this->is_set= false; 00090 } 00091 00092 public function is_set() { 00093 return $this->is_set; 00094 } 00095 00096 public function to_string($showvalue = false) { 00097 $output = $this->name; 00098 if ($showvalue) { 00099 $value = $this->is_set ? $this->value : 'not set'; 00100 $output .= ' => ' . $value; 00101 } 00102 return $output; 00103 } 00104 } 00105 00112 abstract class base_atom_exception extends moodle_exception { 00113 00121 public function __construct($errorcode, $a = null, $debuginfo = null) { 00122 parent::__construct($errorcode, '', '', $a, $debuginfo); 00123 } 00124 } 00125 00133 class base_atom_struct_exception extends base_atom_exception { 00134 00142 public function __construct($errorcode, $a = null, $debuginfo = null) { 00143 parent::__construct($errorcode, $a, $debuginfo); 00144 } 00145 } 00146 00154 class base_atom_content_exception extends base_atom_exception { 00155 00163 public function __construct($errorcode, $a = null, $debuginfo = null) { 00164 parent::__construct($errorcode, $a, $debuginfo); 00165 } 00166 }