From ca4413f301b45c0c17923f6b61d72592f08f9e1a Mon Sep 17 00:00:00 2001 From: mayankamencherla Date: Tue, 7 Mar 2017 17:43:30 +0530 Subject: [PATCH 1/3] [notes typecase] Hardcoding notes to be an array --- src/Entity.php | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/src/Entity.php b/src/Entity.php index 6172dada..a5c0934c 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -67,34 +67,51 @@ protected function request($method, $relativeUrl, $data = null) } else { - return static::buildEntity($response); + return static::buildEntity(null, $response); } } - protected static function buildEntity($data) + protected static function buildEntity($key, $data) { $entities = static::getDefinedEntitiesArray(); - if (isset($data['entity'])) + if (self::isNotes($key) === true) { - if (in_array($data['entity'], $entities)) + $entity = $data; + } + else + { + if (isset($data['entity'])) { - $class = static::getEntityClass($data['entity']); - $entity = new $class; + if (in_array($data['entity'], $entities)) + { + $class = static::getEntityClass($data['entity']); + $entity = new $class; + } + else + { + $entity = new self; + } } else { - $entity = new static; + $entity = new self; } + + $entity->fill($data); } - else + + return $entity; + } + + protected static function isNotes($key) + { + if ($key === 'notes') { - $entity = new static; + return true; } - $entity->fill($data); - - return $entity; + return false; } protected static function getDefinedEntitiesArray() @@ -138,7 +155,7 @@ public function fill($data) { if (is_array($v)) { - $entity = static::buildEntity($v); + $entity = static::buildEntity(null, $v); array_push($collection, $entity); } else @@ -151,7 +168,7 @@ public function fill($data) } else { - $value = static::buildEntity($value); + $value = static::buildEntity($key, $value); } } From bf4b0f8868698270b9e3c9153480c6e62c303df6 Mon Sep 17 00:00:00 2001 From: mayankamencherla Date: Tue, 7 Mar 2017 17:52:27 +0530 Subject: [PATCH 2/3] [notes typecast] Minor improvement --- src/Entity.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Entity.php b/src/Entity.php index a5c0934c..e7c84a9e 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -155,7 +155,7 @@ public function fill($data) { if (is_array($v)) { - $entity = static::buildEntity(null, $v); + $entity = static::buildEntity($key, $v); array_push($collection, $entity); } else From 709d8258ea87682a81907784ed0e4935bb394e61 Mon Sep 17 00:00:00 2001 From: mayankamencherla Date: Wed, 8 Mar 2017 23:52:56 +0530 Subject: [PATCH 3/3] [notes typecast] Minor improvement --- src/Entity.php | 94 ++++++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 42 deletions(-) diff --git a/src/Entity.php b/src/Entity.php index e7c84a9e..06c15bf5 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -75,23 +75,18 @@ protected static function buildEntity($key, $data) { $entities = static::getDefinedEntitiesArray(); - if (self::isNotes($key) === true) + $arrayableAttributes = static::getArrayableAttributes(); + + if (in_array($key, $arrayableAttributes)) { $entity = $data; } - else + else if (isset($data['entity'])) { - if (isset($data['entity'])) + if (in_array($data['entity'], $entities)) { - if (in_array($data['entity'], $entities)) - { - $class = static::getEntityClass($data['entity']); - $entity = new $class; - } - else - { - $entity = new self; - } + $class = static::getEntityClass($data['entity']); + $entity = new $class; } else { @@ -100,18 +95,21 @@ protected static function buildEntity($key, $data) $entity->fill($data); } + else + { + $entity = new self; + + $entity->fill($data); + } return $entity; } - protected static function isNotes($key) + protected static function getArrayableAttributes() { - if ($key === 'notes') - { - return true; - } - - return false; + return array( + 'notes' + ); } protected static function getDefinedEntitiesArray() @@ -147,29 +145,7 @@ public function fill($data) { if (is_array($value)) { - if (static::isAssocArray($value) === false) - { - $collection = array(); - - foreach ($value as $v) - { - if (is_array($v)) - { - $entity = static::buildEntity($key, $v); - array_push($collection, $entity); - } - else - { - array_push($collection, $v); - } - } - - $value = $collection; - } - else - { - $value = static::buildEntity($key, $value); - } + $value = self::getArrayValue($key, $value); } $attributes[$key] = $value; @@ -178,6 +154,40 @@ public function fill($data) $this->attributes = $attributes; } + protected static function getArrayValue($key, $value) + { + if (static::isAssocArray($value) === false) + { + $value = self::getAssocArrayValue($key, $value); + } + else + { + $value = static::buildEntity($key, $value); + } + + return $value; + } + + protected static function getAssocArrayValue($key, $value) + { + $collection = array(); + + foreach ($value as $v) + { + if (is_array($v)) + { + $entity = static::buildEntity($key, $v); + array_push($collection, $entity); + } + else + { + array_push($collection, $v); + } + } + + return $collection; + } + public static function isAssocArray($arr) { return array_keys($arr) !== range(0, count($arr) - 1);