<?php
namespace Engine\Serializer\MagicMethods; abstract class CustomAbstract { public function _getSerializedRepresentation(&$serializedObjects) { return $this->_getSerializedRepresentation_object($this, $serializedObjects); } protected function serializeUnknownObject($ret, $object) { return null; } public function _setSerializedRepresentation($data, &$serializedObjects) { $this->_setSerializedRepresentation_object($this, $data, $serializedObjects); } protected function unserializeUnknownObject($object, $attributes) { return null; } protected function _getSerializedRepresentation_scalar($data) { return $data; } protected function _getSerializedRepresentation_array($data, &$serializedObjects) { $ret = [ '$%t*' => '$%a', '$%m*' => [], ]; foreach ($data as $k=>$v) { if (is_object($v)) { $ret['$%m*'][$k] = $this->_getSerializedRepresentation_object($v, $serializedObjects); } elseif (is_array($v)) { $ret['$%m*'][$k] = $this->_getSerializedRepresentation_array($v, $serializedObjects); } else { $ret['$%m*'][$k] = $this->_getSerializedRepresentation_scalar($v); } } return $ret; } protected function _getSerializedRepresentation_object($obj, &$serializedObjects) { $ret = [ '$%t*' => '$%o', '$%c*' => get_class($obj), ]; $isReference = false; $referenceIndex = null; foreach ($serializedObjects as $idx=>$so) { if($so===$obj) { $isReference = true; $referenceIndex = $idx; break; } } if ($isReference) { $ret = [ '$%t*' => '$%o', '$%c*' => get_class($obj), '$%r*' => $referenceIndex, ]; return $ret; } else { if ($obj instanceof \Engine\Serializer\MagicMethods\CustomAbstract) { $ret['$%a*'] = []; foreach ($obj as $k=>$v) { if (is_object($v)) { $ret['$%a*'][$k] = $this->_getSerializedRepresentation_object($v, $serializedObjects); } elseif (is_array($v)) { $ret['$%a*'][$k] = $this->_getSerializedRepresentation_array($v, $serializedObjects); } else { $ret['$%a*'][$k] = $this->_getSerializedRepresentation_scalar($v); } } } else { $ret = $this->serializeUnknownObject($ret, $obj); if ($ret===null) { throw new \Exception(sprintf("[%s]Class %s needs to extend %s. Parent class: %s", __METHOD__, get_class($obj), __CLASS__, get_class($this))); } if ($ret['skip']) { return null; } } $serializedObjects[] = $obj; } return $ret; } protected function _setSerializedRepresentation_scalar($scalar) { return $scalar; } protected function _setSerializedRepresentation_array($data, &$serializedObjects) { $ret = []; $o = null; foreach($data['$%m*'] as $k=>$v) { if (is_array($v) && array_key_exists('$%t*', $v) && $v['$%t*']=='$%o' && array_key_exists('$%c*', $v)) { if (array_key_exists('$%a*', $v)) { $o = new $v['$%c*'](); if ($o instanceof \Engine\Serializer\MagicMethods\CustomAbstract) { $o->_setSerializedRepresentation($v, $serializedObjects); } else { throw new \Exception(sprintf("[%s] Class needs to extend %s", __METHOD__, __CLASS__)); } $serializedObjects[] = $o; } elseif (array_key_exists('$%r*', $v)) { $o = $serializedObjects[$v['$%r*']]; } else { $o = $this->unserializeUnknownObject($o, $v); if ($o===null) { throw new \Exception("Invalid object declaration found"); } } $v = $o; } if (is_array($v) && array_key_exists('$%t*', $v) && $v['$%t*']=='$%a' && array_key_exists('$%m*', $v)) { $arr = $this->_setSerializedRepresentation_array($v, $serializedObjects); $v = $arr; } elseif (is_array($v)) { throw new \Exception("this is an array, but we dont know how it got encoded like this"); } else { $v = $this->_setSerializedRepresentation_scalar($v); } $ret[$k] = $v; } return $ret; } protected function _setSerializedRepresentation_object($obj, $data, &$serializedObjects) { $o = null; foreach ($data['$%a*'] as $k=>$x) { if (array_key_exists($k, $data['$%a*'])) { $v = $data['$%a*'][$k]; if (is_array($v) && array_key_exists('$%t*', $v) && $v['$%t*']=='$%o' && array_key_exists('$%c*', $v)) { if (array_key_exists('$%a*', $v)) { $o = new $v['$%c*'](); if ($o instanceof \Engine\Serializer\MagicMethods\CustomAbstract) { $o->_setSerializedRepresentation($v, $serializedObjects); } else { throw new \Exception(sprintf("[%s] Class %s needs to extend %s", __METHOD__, get_class($o), __CLASS__)); } $serializedObjects[] = $o; } elseif (array_key_exists('$%r*', $v)) { $o = $serializedObjects[$v['$%r*']]; } else { $o = $this->unserializeUnknownObject($o, $v); if ($o===null) { throw new \Exception(sprintf("[%s] Class %s needs to extend %s, or %s needs to implement unserializeUnknownObject()", __METHOD__, $v['$%c*'], __CLASS__, get_class($this))); } $serializedObjects[] = $o; } $v = $o; } elseif (is_array($v) && array_key_exists('$%t*', $v) && $v['$%t*']=='$%a' && array_key_exists('$%m*', $v)) { $v = $this->_setSerializedRepresentation_array($v, $serializedObjects); } elseif (is_array($v)) { throw new \Exception("this is an array, but we dont know how it got encoded like this"); } else { $v = $this->_setSerializedRepresentation_scalar($v); } if (property_exists($obj, $k)) { $obj->{$k} = $v; } } } return $obj; } } 