Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Commit

Permalink
break a bunch of things
Browse files Browse the repository at this point in the history
  • Loading branch information
nealio82 committed Aug 1, 2019
1 parent 8aa2a2d commit 0d9cd26
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Field/BooleanField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class BooleanField extends Field
{
public function dynamoDbType(): string
public function getDynamoDbType(): string
{
return 'BOOL';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Field/DateTimeField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class DateTimeField extends Field
{
public function dynamoDbType(): string
public function getDynamoDbType(): string
{
// Dates are stored as strings
return 'S';
Expand Down
6 changes: 3 additions & 3 deletions src/Field/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function name(): string
*/
public function readFieldValue(array $item, string $fieldName)
{
$rawDynamoDbValue = $item[$fieldName][$this->dynamoDbType()];
$rawDynamoDbValue = $item[$fieldName][$this->getDynamoDbType()];

return $this->castValueFromDynamoDbFormat($rawDynamoDbValue);
}
Expand All @@ -33,11 +33,11 @@ public function readFieldValue(array $item, string $fieldName)
public function dynamoDbQueryValue($fieldValue): array
{
return [
$this->dynamoDbType() => $this->castValueForDynamoDbFormat($fieldValue),
$this->getDynamoDbType() => $this->castValueForDynamoDbFormat($fieldValue),
];
}

abstract protected function dynamoDbType(): string;
abstract protected function getDynamoDbType(): string;

/**
* @param mixed $value
Expand Down
2 changes: 1 addition & 1 deletion src/Field/FloatField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class FloatField extends Field
{
public function dynamoDbType(): string
public function getDynamoDbType(): string
{
return 'N';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Field/IntegerField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class IntegerField extends Field
{
public function dynamoDbType(): string
public function getDynamoDbType(): string
{
return 'N';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Field/StringField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class StringField extends Field
{
public function dynamoDbType(): string
public function getDynamoDbType(): string
{
return 'S';
}
Expand Down
27 changes: 18 additions & 9 deletions src/Mapping/ClassMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@ class ClassMapping
/** @var string */
private $className;
/** @var array */
private $config;
private $mapping;

private function __construct(string $className, array $config)
{
$this->className = $className;
$this->config = $config;
$this->mapping = $config;
}

public static function fromArray(string $className, array $config): ClassMapping
{
if (\class_exists($className) === false) {
if (false === \class_exists($className)) {
throw new ClassNameInvalidException('Could not map ' . $className . ' as the class was not found');
}

if (empty($config['fields']) === false) {
self::validateMappedProperties($className, $config['fields']);
if (false === empty($config['fields'])) {
$fields = self::mapProperties($className, $config['fields']);

$config['fields'] = $fields;
}

return new static($className, $config);
Expand All @@ -36,7 +38,7 @@ public static function fromArray(string $className, array $config): ClassMapping
* @throws CannotMapNonExistentFieldException
* @throws \ReflectionException
*/
private static function validateMappedProperties(string $className, array $fields): void
private static function mapProperties(string $className, array $fields): array
{
$reflection = new \ReflectionClass($className);

Expand All @@ -45,10 +47,17 @@ private static function validateMappedProperties(string $className, array $field
return $carry;
}, []);

foreach ($fields as $mappedField => $type) {
if (\in_array($mappedField, $classProperties) === false) {
throw new CannotMapNonExistentFieldException('The field ' . $mappedField . ' does not exist in ' . $className);
$mappedFields = [];
foreach ($fields as $classField => $type) {
if (\in_array($classField, $classProperties) === false) {
throw new CannotMapNonExistentFieldException('The field ' . $classField . ' does not exist in ' . $className);
}

$mappedFields[$classField] = $factory->getType($type);
}



return $fields;
}
}
2 changes: 1 addition & 1 deletion src/Mapping/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class Mapping

private function __construct(array $mapping)
{
var_dump($mapping);
// var_dump($mapping);
}

public static function fromConfigArray(array $config)
Expand Down
6 changes: 6 additions & 0 deletions tests/Mapping/ClassMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,11 @@ public function test fields are mapped(): void
];

$classMapping = ClassMapping::fromArray(Article::class, $mapping);

$this->assertSame('S', $classMapping->getMappedProperty('id')->getDynamoDBType());
$this->assertSame('S', $classMapping->getMappedProperty('name')->getDynamoDBType());
$this->assertSame('S', $classMapping->getMappedProperty('createdAt')->getDynamoDBType());
$this->assertSame('N', $classMapping->getMappedProperty('rating')->getDynamoDBType());
$this->assertSame('N', $classMapping->getMappedProperty('numComments')->getDynamoDBType());
}
}
2 changes: 1 addition & 1 deletion tests/Mapping/MappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public function test a table mapping is created(): void
],
]);

$this->assertSame('my_table', $mapping->getTableFor(Article::class));
// $this->assertSame('my_table', $mapping->getTableFor(Article::class));
}
}

0 comments on commit 0d9cd26

Please sign in to comment.