[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package s9e\TextFormatter 5 * @copyright Copyright (c) 2010-2022 The s9e authors 6 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 7 */ 8 namespace s9e\TextFormatter\Configurator\Traits; 9 10 use InvalidArgumentException; 11 use RuntimeException; 12 use s9e\TextFormatter\Configurator\Collections\Collection; 13 use s9e\TextFormatter\Configurator\Collections\NormalizedCollection; 14 use Traversable; 15 16 /** 17 * Provides magic __get, __set, __isset and __unset implementations 18 */ 19 trait Configurable 20 { 21 /** 22 * Magic getter 23 * 24 * Will return $this->foo if it exists, then $this->getFoo() or will throw an exception if 25 * neither exists 26 * 27 * @param string $propName 28 * @return mixed 29 */ 30 public function __get($propName) 31 { 32 $methodName = 'get' . ucfirst($propName); 33 34 // Look for a getter, e.g. getDefaultTemplate() 35 if (method_exists($this, $methodName)) 36 { 37 return $this->$methodName(); 38 } 39 40 if (!property_exists($this, $propName)) 41 { 42 throw new RuntimeException("Property '" . $propName . "' does not exist"); 43 } 44 45 return $this->$propName; 46 } 47 48 /** 49 * Magic setter 50 * 51 * Will call $this->setFoo($propValue) if it exists, otherwise it will set $this->foo. 52 * If $this->foo is a NormalizedCollection, we do not replace it, instead we clear() it then 53 * fill it back up. It will not overwrite an object with a different incompatible object (of a 54 * different, non-extending class) and it will throw an exception if the PHP type cannot match 55 * without incurring data loss. 56 * 57 * @param string $propName 58 * @param mixed $propValue 59 * @return void 60 */ 61 public function __set($propName, $propValue) 62 { 63 $methodName = 'set' . ucfirst($propName); 64 65 // Look for a setter, e.g. setDefaultChildRule() 66 if (method_exists($this, $methodName)) 67 { 68 $this->$methodName($propValue); 69 70 return; 71 } 72 73 // If the property isn't already set, we just create/set it 74 if (!isset($this->$propName)) 75 { 76 $this->$propName = $propValue; 77 78 return; 79 } 80 81 // If we're trying to replace a NormalizedCollection, instead we clear it then 82 // iteratively set new values 83 if ($this->$propName instanceof NormalizedCollection) 84 { 85 if (!is_array($propValue) && !($propValue instanceof Traversable)) 86 { 87 throw new InvalidArgumentException("Property '" . $propName . "' expects an array or a traversable object to be passed"); 88 } 89 90 $this->$propName->clear(); 91 foreach ($propValue as $k => $v) 92 { 93 $this->$propName->set($k, $v); 94 } 95 96 return; 97 } 98 99 // If this property is an object, test whether they are compatible. Otherwise, test if PHP 100 // types are compatible 101 if (is_object($this->$propName)) 102 { 103 if (!($propValue instanceof $this->$propName)) 104 { 105 throw new InvalidArgumentException("Cannot replace property '" . $propName . "' of class '" . get_class($this->$propName) . "' with instance of '" . get_class($propValue) . "'"); 106 } 107 } 108 else 109 { 110 // Test whether the PHP types are compatible 111 $oldType = gettype($this->$propName); 112 $newType = gettype($propValue); 113 114 // If the property is a boolean, we'll accept "true" and "false" as strings 115 if ($oldType === 'boolean' && preg_match('(^(?:fals|tru)e$)', $propValue)) 116 { 117 $newType = 'boolean'; 118 $propValue = ($propValue === 'true'); 119 } 120 121 if ($oldType !== $newType) 122 { 123 // Test whether the PHP type roundtrip is lossless 124 $tmp = $propValue; 125 settype($tmp, $oldType); 126 settype($tmp, $newType); 127 128 if ($tmp !== $propValue) 129 { 130 throw new InvalidArgumentException("Cannot replace property '" . $propName . "' of type " . $oldType . ' with value of type ' . $newType); 131 } 132 133 // Finally, set the new value to the correct type 134 settype($propValue, $oldType); 135 } 136 } 137 138 $this->$propName = $propValue; 139 } 140 141 /** 142 * Test whether a property is set 143 * 144 * @param string $propName 145 * @return bool 146 */ 147 public function __isset($propName) 148 { 149 $methodName = 'isset' . ucfirst($propName); 150 if (method_exists($this, $methodName)) 151 { 152 return $this->$methodName(); 153 } 154 155 return isset($this->$propName); 156 } 157 158 /** 159 * Unset a property, if the class supports it 160 * 161 * @param string $propName 162 * @return void 163 */ 164 public function __unset($propName) 165 { 166 $methodName = 'unset' . ucfirst($propName); 167 if (method_exists($this, $methodName)) 168 { 169 $this->$methodName(); 170 } 171 elseif (isset($this->$propName)) 172 { 173 if ($this->$propName instanceof Collection) 174 { 175 $this->$propName->clear(); 176 } 177 else 178 { 179 throw new RuntimeException("Property '" . $propName . "' cannot be unset"); 180 } 181 } 182 } 183 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |