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

Commit

Permalink
get table for mapped class
Browse files Browse the repository at this point in the history
  • Loading branch information
Neal Brooks committed Aug 1, 2019
1 parent 0d9cd26 commit 3d11c8e
Show file tree
Hide file tree
Showing 20 changed files with 191 additions and 20 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 getDynamoDbType(): string
public function dynamoDbType(): 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 getDynamoDbType(): string
public function dynamoDbType(): 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->getDynamoDbType()];
$rawDynamoDbValue = $item[$fieldName][$this->dynamoDbType()];

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

abstract protected function getDynamoDbType(): string;
abstract protected function dynamoDbType(): 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 getDynamoDbType(): string
public function dynamoDbType(): 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 getDynamoDbType(): string
public function dynamoDbType(): 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 getDynamoDbType(): string
public function dynamoDbType(): string
{
return 'S';
}
Expand Down
33 changes: 28 additions & 5 deletions src/Mapping/ClassMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Dynamap\Mapping;

use Dynamap\Mapping\Field\DynamoDBField;
use Dynamap\Mapping\Exception\CannotMapNonExistentFieldException;
use Dynamap\Mapping\Exception\ClassNameInvalidException;
use Dynamap\Mapping\Exception\MappingNotFoundException;
use Dynamap\Mapping\Exception\NoFieldsMappedForClassException;

class ClassMapping
{
Expand Down Expand Up @@ -33,8 +36,28 @@ public static function fromArray(string $className, array $config): ClassMapping
return new static($className, $config);
}

public function getClassName(): string
{
return $this->className;
}

public function getMappedProperty(string $propertyName): DynamoDBField
{
if (true === empty($this->mapping['fields'])) { // todo: add a test for this
throw new NoFieldsMappedForClassException('You have tried to access mapping for a class which has no mapped properties');
}

if (false === \array_key_exists($propertyName, $this->mapping['fields'])) {
throw new MappingNotFoundException('Mapping for ' . $propertyName . ' could not be found');
}

return $this->mapping['fields'][$propertyName];
}

/**
* @param string $className
* @param array $fields
* @return array
* @throws CannotMapNonExistentFieldException
* @throws \ReflectionException
*/
Expand All @@ -48,16 +71,16 @@ private static function mapProperties(string $className, array $fields): array
}, []);

$mappedFields = [];
$factory = new FieldMappingFactory();

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

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



return $fields;
return $mappedFields;
}
}
7 changes: 7 additions & 0 deletions src/Mapping/Exception/MappingNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Dynamap\Mapping\Exception;

class MappingNotFoundException extends \Exception
{
}
7 changes: 7 additions & 0 deletions src/Mapping/Exception/NoFieldsMappedForClassException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Dynamap\Mapping\Exception;

class NoFieldsMappedForClassException extends \Exception
{
}
11 changes: 11 additions & 0 deletions src/Mapping/Field/BooleanField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Dynamap\Mapping\Field;

class BooleanField implements DynamoDBField
{
public function getDynamoDBFieldType(): string
{
return 'BOOL';
}
}
11 changes: 11 additions & 0 deletions src/Mapping/Field/DateTimeField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Dynamap\Mapping\Field;

class DateTimeField implements DynamoDBField
{
public function getDynamoDBFieldType(): string
{
return 'S';
}
}
8 changes: 8 additions & 0 deletions src/Mapping/Field/DynamoDBField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Dynamap\Mapping\Field;

interface DynamoDBField
{
public function getDynamoDBFieldType(): string;
}
11 changes: 11 additions & 0 deletions src/Mapping/Field/NumberField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Dynamap\Mapping\Field;

class NumberField implements DynamoDBField
{
public function getDynamoDBFieldType(): string
{
return 'N';
}
}
11 changes: 11 additions & 0 deletions src/Mapping/Field/StringField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Dynamap\Mapping\Field;

class StringField implements DynamoDBField
{
public function getDynamoDBFieldType(): string
{
return 'S';
}
}
31 changes: 31 additions & 0 deletions src/Mapping/FieldMappingFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Dynamap\Mapping;

use Dynamap\Mapping\Field\BooleanField;
use Dynamap\Mapping\Field\DynamoDBField;
use Dynamap\Mapping\Field\NumberField;
use Dynamap\Mapping\Field\StringField;

class FieldMappingFactory
{
public function getDynamoDBType(string $propertyType): DynamoDBField
{
switch (strtolower($propertyType)) {
case 'uuid' :
case 'string':
case 'datetime':
return new StringField();
break;
case 'float':
case 'integer':
return new NumberField();
break;
case 'bool':
return new BooleanField();
break;
default: // todo: add a test for this
throw new InvalidMappingTypeException();
}
}
}
20 changes: 19 additions & 1 deletion src/Mapping/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Dynamap\Mapping;

use Dynamap\Mapping\Exception\ClassNameInvalidException;
use Dynamap\Mapping\Exception\NoTableSpeficiedException;

final class Mapping
Expand All @@ -11,7 +12,7 @@ final class Mapping

private function __construct(array $mapping)
{
// var_dump($mapping);
$this->mapping = $mapping;
}

public static function fromConfigArray(array $config)
Expand All @@ -27,4 +28,21 @@ public static function fromConfigArray(array $config)

return new static($mapping);
}

public function getTableFor(string $className): string
{
// todo: add a test for this
if (false === \class_exists($className)) {
throw new ClassNameInvalidException('Get table for ' . $className . ' as the class was not found');
}

foreach ($this->mapping as $mappedTable) {
if ($mappedTable->containsMappingForClass($className)) {
return $mappedTable->getTableName();
}
}

// todo: add a test for this
throw new ClassNotMappedException('The class ' . $className . ' was not found in the mapping configuration');
}
}
12 changes: 12 additions & 0 deletions src/Mapping/TableMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,16 @@ public function getTableName(): string
{
return $this->tableName;
}

// todo: add a test for this method
public function containsMappingForClass(string $className): bool
{
foreach ($this->classMappings as $classMapping) {
if ($className === $classMapping->getClassName()) {
return true;
}
}

return false;
}
}
8 changes: 8 additions & 0 deletions tests/Fixture/Tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Dynamap\Test\Fixture;

class Tag
{

}
12 changes: 7 additions & 5 deletions tests/Mapping/ClassMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ public function test fields are mapped(): void
'createdAt' => 'datetime',
'rating' => 'float',
'numComments' => 'integer',
'published' => 'bool'
],
];

$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());
$this->assertSame('S', $classMapping->getMappedProperty('id')->getDynamoDBFieldType());
$this->assertSame('S', $classMapping->getMappedProperty('name')->getDynamoDBFieldType());
$this->assertSame('S', $classMapping->getMappedProperty('createdAt')->getDynamoDBFieldType());
$this->assertSame('N', $classMapping->getMappedProperty('rating')->getDynamoDBFieldType());
$this->assertSame('N', $classMapping->getMappedProperty('numComments')->getDynamoDBFieldType());
$this->assertSame('BOOL', $classMapping->getMappedProperty('published')->getDynamoDBFieldType());
}
}
13 changes: 12 additions & 1 deletion tests/Mapping/MappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Dynamap\Mapping\Mapping;
use Dynamap\Test\Fixture\Article;
use Dynamap\Test\Fixture\Author;
use Dynamap\Test\Fixture\Tag;
use PHPUnit\Framework\TestCase;

class MappingTest extends TestCase
Expand Down Expand Up @@ -35,9 +36,19 @@ public function test a table mapping is created(): void
Author::class => [],
],
],
[
// if you're thinking about using mulitple tables, go back and read the AWS docs on why you shouldn't.
// then (and only then) come back and think about if you actually _really_ do want to do this.
'name' => 'other_table',
'mappings' => [
Tag::class => []
]
]
],
]);

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

0 comments on commit 3d11c8e

Please sign in to comment.