Skip to content

Commit

Permalink
Fixed a bug when converting to array and it didn't check IDs.
Browse files Browse the repository at this point in the history
  • Loading branch information
GregaMohorko committed Jan 26, 2018
1 parent da8f7cc commit 16ca418
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/Entity/FieldEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@

abstract class FieldEntity extends DatabaseTable implements IFieldEntity
{
const ARRAY_TYPE="Type";
const ARRAY_KEY="BlueDBKey";
const ARRAY_PROPERTIES="Properties";

/**
* Used for key references in creating JSON arrays.
*
Expand Down Expand Up @@ -160,7 +164,7 @@ public static function toJsonList($entities,$fieldsToIgnore=null,$includeHiddenF
public static function fromArray($array)
{
$session=[];
if(isset($array["Key"]) && isset($array["Type"])){
if(self::isEntityArray($array)){
// is a single entity
return self::fromArraySingle($array,$session);
}
Expand All @@ -185,6 +189,17 @@ private static function fromArrayList($array,&$session)
return $list;
}

/**
* Determines whether this array represents an entity.
*
* @param array $array
* @return bool
*/
private static function isEntityArray($array)
{
return isset($array[self::ARRAY_KEY]);
}

/**
* Decodes provided array into an entity.
*
Expand All @@ -194,9 +209,9 @@ private static function fromArrayList($array,&$session)
*/
private static function fromArraySingle($array,&$session)
{
$key=$array["Key"];
$key=$array[self::ARRAY_KEY];

if(!isset($array["Type"])){
if(!isset($array[self::ARRAY_TYPE])){
// is only a key
// should be already present in the lookup table
if(!isset($session[$key])){
Expand All @@ -205,7 +220,7 @@ private static function fromArraySingle($array,&$session)
return $session[$key];
}

$type=$array["Type"];
$type=$array[self::ARRAY_TYPE];
$class=BlueDBProperties::instance()->Namespace_Entities."\\$type";

$classData=self::getClassData($class);
Expand All @@ -218,7 +233,7 @@ private static function fromArraySingle($array,&$session)
// add to lookup table
$session[$key]=$entity;

foreach($array["Properties"] as $propertyName => $propertyValue){
foreach($array[self::ARRAY_PROPERTIES] as $propertyName => $propertyValue){
/*
if(!property_exists($class, $propertyName)){
throw new Exception("Property '$propertyName' does not exist in class '$class'.");
Expand Down Expand Up @@ -292,10 +307,12 @@ private function toArrayInternal($fieldsToIgnore,$includeHiddenFields,&$session)

// check if already present in the session
if(isset($session[$class])){
$id=$this->getID();
foreach($session[$class] as $key => $object){
if($object===$this){
/* @var $object FieldEntity */
if($object===$this || $object->getID()===$id){
$array=[];
$array["Key"]=$key;
$array[self::ARRAY_KEY]=$key;
return $array;
}
}
Expand Down Expand Up @@ -358,9 +375,9 @@ private function toArrayInternal($fieldsToIgnore,$includeHiddenFields,&$session)
}

$array=[];
$array["Type"]=$this->getClassName();
$array["Key"]=$key;
$array["Properties"]=$properties;
$array[self::ARRAY_TYPE]=$this->getClassName();
$array[self::ARRAY_KEY]=$key;
$array[self::ARRAY_PROPERTIES]=$properties;

return $array;
}
Expand Down

0 comments on commit 16ca418

Please sign in to comment.