1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200:
<?php
/**
* MvcCore
*
* This source file is subject to the BSD 3 License
* For the full copyright and license information, please view
* the LICENSE.md file that are distributed with this source code.
*
* @copyright Copyright (c) 2016 Tom Flidr (https://github.com/mvccore)
* @license https://mvccore.github.io/docs/mvccore/5.0.0/LICENCE.md
*/
namespace MvcCore\Config;
trait MagicMethods {
/**
* Serialize only given properties:
* @return \string[]
*/
public function __sleep () {
/** @var $this \MvcCore\Config */
if (!$this->mergedData) {
$app = self::$app ?: self::$app = \MvcCore\Application::GetInstance();
$envName = $app->GetEnvironment()->GetName();
if (!$envName) {
$cfgClass = $app->GetConfigClass();
$envDetectionData = & $cfgClass::GetEnvironmentDetectionData($this);
$envClass = $app->GetEnvironmentClass();
$envName = $envClass::DetectBySystemConfig((array) $envDetectionData);
}
$cfgClass::SetUpEnvironmentData($this, $envName);
}
return [
'system',
'mergedData',
'fullPath',
'lastChanged',
];
}
/** Classic PHP magic methods for object access ***************************/
/**
* Get not defined property from `$this->currentData` array store,
* if there is nothing, return `NULL`.
* @param string $key
* @return mixed
*/
public function __get ($key) {
/** @var $this \MvcCore\Config */
if (array_key_exists($key, $this->currentData))
return $this->currentData[$key];
return NULL;
}
/**
* Store not defined property inside `$this->currentData` array store.
* @param string $key
* @return mixed
*/
public function __set ($key, $value) {
/** @var $this \MvcCore\Config */
return $this->currentData[$key] = $value;
}
/**
* Magic function triggered by: `isset($cfg->key);`.
* @param string $key
* @return bool
*/
public function __isset ($key) {
/** @var $this \MvcCore\Config */
return isset($this->currentData[$key]);
}
/**
* Magic function triggered by: `unset($cfg->key);`.
* @param string $key
* @return void
*/
public function __unset ($key) {
/** @var $this \MvcCore\Config */
if (isset($this->currentData[$key])) unset($this->currentData[$key]);
}
/** \Iterator interface ***************************************************/
/**
* Return the current element.
* @return mixed
*/
public function current () {
/** @var $this \MvcCore\Config */
return current($this->currentData);
}
/**
* Return the key of the current element.
* @return string|int
*/
public function key () {
/** @var $this \MvcCore\Config */
return key($this->currentData);
}
/**
* Move forward to next element.
* @return void
*/
public function next () {
/** @var $this \MvcCore\Config */
return next($this->currentData);
}
/**
* Rewind the Iterator to the first element.
* @return void
*/
public function rewind () {
/** @var $this \MvcCore\Config */
reset($this->currentData);
}
/**
* Checks if current position is valid.
* @return bool
*/
public function valid () {
/** @var $this \MvcCore\Config */
return key($this->currentData) !== NULL;
}
/** \ArrayAccess interface ************************************************/
/**
* Return whether the requested index exists in the internal store.
* Example: `isset($cfg['any']);`
* @param mixed $offset
* @return bool
*/
public function offsetExists ($offset) {
/** @var $this \MvcCore\Config */
return isset($this->currentData[$offset]);
}
/**
* Get the value at the specified index from the internal store.
* Example: `$thing = $cfg['any'];`
* @param mixed $offset
* @param mixed $value
*/
public function offsetGet ($offset) {
/** @var $this \MvcCore\Config */
return isset($this->currentData[$offset]) ? $this->currentData[$offset] : NULL;
}
/**
* Set the value at the specified index in the internal store.
* Example: `$cfg['any'] = 'thing';`
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet ($offset, $value) {
/** @var $this \MvcCore\Config */
if ($offset === NULL) {
$this->currentData[] = $value;
} else {
$this->currentData[$offset] = $value;
}
}
/**
* Unset the value at the specified index in the internal store.
* Example: `unset($cfg['any']);`
* @param mixed $offset
*/
public function offsetUnset ($offset) {
/** @var $this \MvcCore\Config */
unset($this->currentData[$offset]);
}
/** \Countable interface **************************************************/
/**
* Get how many records is in the config internal store.
* Example: `count($cfg);`
* @return int
*/
public function count () {
/** @var $this \MvcCore\Config */
return count($this->currentData);
}
}