Skip to content

Commit 9355b5f

Browse files
author
Chris Pliakas
committed
Fixed array flattening bug.
1 parent 0e3314e commit 9355b5f

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/DocumentManager.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public function save(EntityInterface $entity)
235235

236236
$commandOptions = array(
237237
'TableName' => $this->getEntityTable($entity),
238-
'Item' => $this->formatAttributes($entity, $entity->getAttributes()),
238+
'Item' => $this->formatEntityAttributes($entity),
239239
'ReturnConsumedCapacity' => $this->returnConsumedCapacity,
240240
);
241241

@@ -426,30 +426,37 @@ protected function initEntity($entityClass, $primaryKey)
426426
}
427427

428428
/**
429-
* Converts an array into something
429+
* Formats an entity's attributes to the SDK's native data structure.
430430
*
431-
* @param \Cpliakas\DynamoDb\ODM\EntityInterface|string $entity
431+
* @param \Cpliakas\DynamoDb\ODM\EntityInterface $entity
432+
* @param string $format
433+
*
434+
* @return array
435+
*/
436+
protected function formatEntityAttributes(EntityInterface $entity, $format = Attribute::FORMAT_PUT)
437+
{
438+
return $this->formatAttributes(get_class($entity), $entity->getAttributes());
439+
}
440+
441+
/**
442+
* Formats an array of attributes to the SDK's native data structure.
443+
*
444+
* @param string $entityClass
432445
* @param array $attributes
433-
* @param string
446+
* @param string $format
434447
*
435448
* @return array
436449
*
437450
* @throws \InvalidArgumentException
438451
*
439452
* @see \Aws\DynamoDb\DynamoDbClient::formatAttributes()
440453
*/
441-
protected function formatAttributes($entity, array $attributes, $format = Attribute::FORMAT_PUT)
454+
protected function formatAttributes($entityClass, array $attributes, $format = Attribute::FORMAT_PUT)
442455
{
443-
// Ensure entity is a class or a fully qualified class name.
444-
if (is_string($entity)) {
445-
$entity = $this->getEntityClass($entity);
446-
} elseif (!$entity instanceof EntityInterface) {
447-
throw new \InvalidArgumentException('Entity must be an instance of \Cpliakas\DynamoDb\ODM\EntityInterface or a string');
448-
}
449-
456+
$entityClass = $this->getEntityClass($entityClass);
450457
$formatted = array();
451458

452-
$mappings = $entity::getDataTypeMappings();
459+
$mappings = $entityClass::getDataTypeMappings();
453460
foreach ($attributes as $attribute => $value) {
454461
if (isset($mappings[$attribute])) {
455462
$dataType = $mappings[$attribute];
@@ -482,7 +489,7 @@ protected function formatKeyCondition(EntityInterface $entity)
482489
$attributes[$rangeKeyAttribute] = $entity->getRangeKey();
483490
}
484491

485-
return $this->formatAttributes($entity, $attributes);
492+
return $this->formatAttributes(get_class($entity), $attributes);
486493
}
487494

488495
/**
@@ -569,7 +576,7 @@ protected function flattenArray(array $item)
569576
protected function populateEntity(EntityInterface $entity, array $item)
570577
{
571578
$attributes = $this->flattenArray($item);
572-
$entity->setAttributes($item);
579+
$entity->setAttributes($attributes);
573580
}
574581

575582
/**

0 commit comments

Comments
 (0)