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 2 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
45 changes: 31 additions & 14 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,51 @@ 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']))
if (self::isNotes($key) === true)
Copy link
Member

Choose a reason for hiding this comment

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

make a generic isArrayableAttribute function.
Shift 'notes' to an array in the class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

{
if (in_array($data['entity'], $entities))
$entity = $data;
}
else
Copy link
Member

Choose a reason for hiding this comment

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

else if

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored

{
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()
Expand Down Expand Up @@ -138,7 +155,7 @@ public function fill($data)
{
if (is_array($v))
{
$entity = static::buildEntity($v);
$entity = static::buildEntity($key, $v);
Copy link
Member

Choose a reason for hiding this comment

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

create a separate function for filling a single key => value pair.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored. Added a helper for the associate array case as well.

array_push($collection, $entity);
}
else
Expand All @@ -151,7 +168,7 @@ public function fill($data)
}
else
{
$value = static::buildEntity($value);
$value = static::buildEntity($key, $value);
}
}

Expand Down