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: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464:
<?php
namespace Coast;
use ArrayAccess;
use Closure;
use Coast;
use Coast\Model;
use Coast\Model\Metadata;
use Iterable;
use JsonSerializable;
use ReflectionClass;
use ReflectionProperty;
class Model implements ArrayAccess, JsonSerializable
{
const TRAVERSE_SKIP = '__Coast\Model::TRAVERSE_SKIP__';
const TYPE_ONE = 'one';
const TYPE_MANY = 'many';
protected static $_metadataStatic = [];
protected $_metadataSource;
protected $_metadata;
protected static $_initMethods = [
'isInitialized',
'_isInitialized',
'__isInitialized',
];
public static function initMethods($initMethods = null)
{
if (func_num_args() > 0) {
self::$_initMethods = $initMethods;
}
return self::$_initMethods;
}
public static function createDefault()
{
$class = get_called_class();
return new $class();
}
protected static function _metadataStaticBuild()
{
$class = get_called_class();
$metadata = new Metadata($class);
$reflect = new ReflectionClass($class);
$names = array_map(function($v) { return $v->getName(); }, array_diff(
$reflect->getProperties(),
$reflect->getProperties(ReflectionProperty::IS_STATIC)
));
foreach ($names as $name) {
if ($name[0] == '_') {
continue;
}
$metadata->property($name, [
'name' => $name,
]);
}
return static::$_metadataStatic[$class] = $metadata;
}
protected static function _metadataStaticModify()
{
$class = get_called_class();
return static::$_metadataStatic[$class];
}
public static function metadataStatic(Metadata $metadata = null)
{
$class = get_called_class();
if (func_num_args() > 0) {
static::$_metadataStatic[$class] = $metadata;
return $this;
}
if (!isset(static::$_metadataStatic[$class])) {
static::_metadataStaticBuild();
static::_metadataStaticModify();
}
return static::$_metadataStatic[$class];
}
protected function _metadataBuild()
{
return $this->_metadataSource = clone static::metadataStatic();
}
protected function _metadataModify()
{
return $this->_metadata = clone $this->_metadataSource;
}
public function metadata(Metadata $metadata = null)
{
if (func_num_args() > 0) {
$this->_metadataSource = $metadata;
$this->_metadata = null;
return $this;
}
if (!isset($this->_metadataSource)) {
$this->_metadataBuild();
}
if (!isset($this->_metadata)) {
$this->_metadataModify();
}
return $this->_metadata;
}
public function metadataReset($isTraverse = null)
{
$this->traverseModels(function() {
$this->_metadata = null;
}, $isTraverse);
return $this;
}
public function traverse(Closure $func, $isTraverse = null, array &$history = array())
{
array_push($history, $this);
$func = $func->bindTo($this);
$output = [];
foreach ($this->metadata->properties() as $name => $metadata) {
$value = $this->__get($name);
$isDeep = isset($isTraverse)
? $isTraverse
: $metadata['isTraverse'];
if (is_object($value)) {
foreach (self::$_initMethods as $method) {
if (method_exists($value, $method) && !$value->$method()) {
$isDeep = false;
break;
}
}
}
if (!$isDeep) {
$value = $func($name, $value, $metadata, $isDeep);
if ($value !== self::TRAVERSE_SKIP) {
$output[$name] = $value;
}
continue;
}
if (in_array($metadata['type'], [
self::TYPE_ONE,
self::TYPE_MANY,
]) && in_array($value, $history, true)) {
continue;
}
if ($metadata['type'] == self::TYPE_ONE) {
if (isset($value)) {
$value = $value->traverse($func, $isTraverse, $history);
}
} else if ($metadata['type'] == self::TYPE_MANY) {
$items = [];
foreach ($value as $key => $item) {
$items[$key] = $item->traverse($func, $isTraverse, $history);
}
$value = $items;
}
$value = $func($name, $value, $metadata, $isDeep);
if ($value !== self::TRAVERSE_SKIP) {
$output[$name] = $value;
}
}
return $output;
}
public function traverseModels(Closure $func, $isTraverse = null, array &$history = array())
{
array_push($history, $this);
$func = $func->bindTo($this);
foreach ($this->metadata->properties() as $name => $metadata) {
if (!in_array($metadata['type'], [
self::TYPE_ONE,
self::TYPE_MANY,
])) {
continue;
}
$value = $this->__get($name);
$isDeep = isset($isTraverse)
? $isTraverse
: $metadata['isTraverse'];
if (is_object($value)) {
foreach (self::$_initMethods as $method) {
if (method_exists($value, $method) && !$value->$method()) {
$isDeep = false;
break;
}
}
}
if (!$isDeep) {
continue;
}
if (in_array($value, $history, true)) {
continue;
}
if ($metadata['type'] == self::TYPE_ONE) {
if (isset($value)) {
$value->traverseModels($func, $isTraverse, $history);
}
} else if ($metadata['type'] == self::TYPE_MANY) {
foreach ($value as $item) {
$item->traverseModels($func, $isTraverse, $history);
}
}
}
$func();
}
public function fromArray(array $array, $isTraverse = null)
{
foreach ($array as $name => $value) {
$metadata = $this->metadata->property($name);
if (!isset($metadata)) {
$this->__setUnknown($name, $value);
continue;
}
$isDeep = isset($isTraverse)
? $isTraverse
: $metadata['isTraverse'];
if (is_object($value)) {
foreach (self::$_initMethods as $method) {
if (method_exists($value, $method) && !$value->$method()) {
$isDeep = false;
break;
}
}
}
if (!$isDeep) {
$this->__set($name, $value);
continue;
}
if ($metadata['type'] == self::TYPE_ONE) {
$current = $this->__get($name);
if (!isset($value)) {
$this->__unset($name);
continue;
}
if (!isset($current) && $metadata['isConstruct']) {
$this->__set($name, $current = $this->_construct($metadata['construct']));
}
if (isset($current)) {
if ($metadata['isImmutable']) {
$current = clone $current;
$this->__set($name, $current);
}
$current->fromArray($value, $isTraverse);
}
} else if ($metadata['type'] == self::TYPE_MANY) {
$current = $this->__get($name);
if (!is_array($current) && (!$current instanceof Iterable && !$current instanceof ArrayAccess)) {
throw new Model\Exception("Value of MANY property '" . get_class($this) . "->{$name}' must be an array or object that implements Iterable and ArrayAccess");
}
if ($metadata['isImmutable']) {
$current = clone $current;
$this->__set($name, $current);
}
if (!isset($value)) {
foreach ($current as $key => $item) {
unset($current[$key]);
}
continue;
}
foreach ($value as $key => $item) {
if (!isset($item)) {
unset($current[$key]);
continue;
}
if (!isset($current[$key]) && $metadata['isConstruct']) {
$current[$key] = $this->_construct($metadata['construct']);
}
if (isset($current[$key])) {
if ($metadata['isImmutable']) {
$current[$key] = clone $current[$key];
}
$current[$key]->fromArray($item, $isTraverse);
}
}
$keys = [];
foreach ($current as $key => $item) {
if (!isset($value[$key])) {
$keys[] = $key;
}
}
foreach ($keys as $key) {
unset($current[$key]);
}
$this->__set($name, $current);
} else {
$this->__set($name, $value);
}
}
return $this;
}
public function toArray($isTraverse = null)
{
return $this->traverse(function($name, $value, $metadata = null) {
return $value;
}, $isTraverse);
}
public function isValid($isTraverse = null)
{
$isValid = true;
$this->traverse(function($name, $value, $metadata) use (&$isValid) {
if (!$metadata['validator']($this->__get($name), $this)) {
$isValid = false;
}
}, $isTraverse);
return $isValid;
}
public function debug($isTraverse = null)
{
return $this->traverse(function($name, $value, $metadata) {
return [
'value' => $value,
'errors' => $metadata['validator']->errors(),
];
}, $isTraverse);
}
protected function _construct($construct)
{
$construct = (array) $construct;
$className = array_shift($construct);
$reflection = new \ReflectionClass($className);
return $reflection->newInstanceArgs($construct);
}
protected function _set($name, $value)
{
$metadata = $this->metadata->property($name);
$value = $metadata['filter']->filter($value);
$value = $metadata['transformer']->transform($value, $this);
$this->{$name} = $value;
}
protected function _get($name)
{
return $this->{$name};
}
protected function _isset($name)
{
return isset($this->{$name});
}
protected function _unset($name)
{
$this->{$name} = null;
}
public function __set($name, $value)
{
if ($name[0] == '_') {
throw new Model\Exception("Access to '{$name}' is prohibited");
}
if (method_exists($this, $name)) {
$this->{$name}($value);
} else if (property_exists($this, $name)) {
$this->_set($name, $value);
} else {
throw new Model\Exception\NotDefined("Property or method '{$name}' is not defined");
}
}
public function __setUnknown($name, $value)
{
try {
$this->__set($name, $value);
} catch (Model\Exception\NotDefined $e) {
try {
$this->__set(Coast\str_camel($name), $value);
} catch (Model\Exception\NotDefined $e) {}
}
}
public function __get($name)
{
if ($name[0] == '_') {
throw new Model\Exception("Access to '{$name}' is prohibited");
}
if (method_exists($this, $name)) {
return $this->{$name}();
} else if (property_exists($this, $name)) {
return $this->_get($name);
} else {
throw new Model\Exception\NotDefined("Property or method '{$name}' is not defined");
}
}
public function __isset($name)
{
if ($name[0] == '_') {
throw new Model\Exception("Access to '{$name}' is prohibited");
}
if (method_exists($this, $name)) {
return $this->{$name}() !== null;
} else if (property_exists($this, $name)) {
return $this->_isset($name);
} else {
throw new Model\Exception\NotDefined("Property or method '{$name}' is not defined");
}
}
public function __unset($name)
{
if ($name[0] == '_') {
throw new Model\Exception("Access to '{$name}' is prohibited");
}
if (property_exists($this, $name)) {
$this->_unset($name);
} else {
throw new Model\Exception\NotDefined("Property or method '{$name}' is not defined");
}
}
public function __call($name, array $args)
{
if ($name[0] == '_') {
throw new Model\Exception("Access to '{$name}' is prohibited");
}
if (isset($args[0])) {
$this->__set($name, $args[0]);
return $this;
}
return $this->__get($name);
}
public function offsetSet($offset, $value)
{
return $this->__set($offset, $value);
}
public function offsetExists($offset)
{
return $this->__isset($offset);
}
public function offsetUnset($offset)
{
return $this->__unset($offset);
}
public function offsetGet($offset)
{
return $this->__get($offset);
}
public function jsonSerialize()
{
return $this->toArray(false);
}
}