Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing notes typecast issue #34

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 57 additions & 30 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,21 @@ protected function request($method, $relativeUrl, $data = null)
}
else
{
return static::buildEntity($response);
return static::buildEntity(null, $response);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@captn3m0 not sure if it's a good idea to pass null. Was thinking if we can make key as the second argument. What do you think?

}
}

protected static function buildEntity($data)
protected static function buildEntity($key, $data)
{
$entities = static::getDefinedEntitiesArray();

if (isset($data['entity']))
$arrayableAttributes = static::getArrayableAttributes();

if (in_array($key, $arrayableAttributes))
{
$entity = $data;
}
else if (isset($data['entity']))
{
if (in_array($data['entity'], $entities))
{
Expand All @@ -84,19 +90,28 @@ protected static function buildEntity($data)
}
else
{
$entity = new static;
$entity = new self;
}

$entity->fill($data);
}
else
{
$entity = new static;
}
$entity = new self;

$entity->fill($data);
$entity->fill($data);
}

return $entity;
}

protected static function getArrayableAttributes()
{
return array(
'notes'
);
}

protected static function getDefinedEntitiesArray()
{
return array(
Expand Down Expand Up @@ -130,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($v);
array_push($collection, $entity);
}
else
{
array_push($collection, $v);
}
}

$value = $collection;
}
else
{
$value = static::buildEntity($value);
}
$value = self::getArrayValue($key, $value);
}

$attributes[$key] = $value;
Expand All @@ -161,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);
Expand Down