@@ -235,7 +235,7 @@ public function save(EntityInterface $entity)
235
235
236
236
$ commandOptions = array (
237
237
'TableName ' => $ this ->getEntityTable ($ entity ),
238
- 'Item ' => $ this ->formatAttributes ($ entity, $ entity -> getAttributes () ),
238
+ 'Item ' => $ this ->formatEntityAttributes ($ entity ),
239
239
'ReturnConsumedCapacity ' => $ this ->returnConsumedCapacity ,
240
240
);
241
241
@@ -426,30 +426,37 @@ protected function initEntity($entityClass, $primaryKey)
426
426
}
427
427
428
428
/**
429
- * Converts an array into something
429
+ * Formats an entity's attributes to the SDK's native data structure.
430
430
*
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
432
445
* @param array $attributes
433
- * @param string
446
+ * @param string $format
434
447
*
435
448
* @return array
436
449
*
437
450
* @throws \InvalidArgumentException
438
451
*
439
452
* @see \Aws\DynamoDb\DynamoDbClient::formatAttributes()
440
453
*/
441
- protected function formatAttributes ($ entity , array $ attributes , $ format = Attribute::FORMAT_PUT )
454
+ protected function formatAttributes ($ entityClass , array $ attributes , $ format = Attribute::FORMAT_PUT )
442
455
{
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 );
450
457
$ formatted = array ();
451
458
452
- $ mappings = $ entity ::getDataTypeMappings ();
459
+ $ mappings = $ entityClass ::getDataTypeMappings ();
453
460
foreach ($ attributes as $ attribute => $ value ) {
454
461
if (isset ($ mappings [$ attribute ])) {
455
462
$ dataType = $ mappings [$ attribute ];
@@ -482,7 +489,7 @@ protected function formatKeyCondition(EntityInterface $entity)
482
489
$ attributes [$ rangeKeyAttribute ] = $ entity ->getRangeKey ();
483
490
}
484
491
485
- return $ this ->formatAttributes ($ entity , $ attributes );
492
+ return $ this ->formatAttributes (get_class ( $ entity) , $ attributes );
486
493
}
487
494
488
495
/**
@@ -569,7 +576,7 @@ protected function flattenArray(array $item)
569
576
protected function populateEntity (EntityInterface $ entity , array $ item )
570
577
{
571
578
$ attributes = $ this ->flattenArray ($ item );
572
- $ entity ->setAttributes ($ item );
579
+ $ entity ->setAttributes ($ attributes );
573
580
}
574
581
575
582
/**
0 commit comments