Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Mar 24, 2023
1 parent ad1463c commit 6ce7915
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 66 deletions.
8 changes: 1 addition & 7 deletions src/Parser.php
Expand Up @@ -56,13 +56,7 @@ private function generator(SplFileObject $file, Schema $schema, bool $ignoreFirs
continue;
}

$data = [];

foreach ($schema->columnDefinitions() as $columnDefinition) {
$columnDefinition->parse($line, $data);
}

yield $data;
yield $schema->apply($line);
}
}
}
25 changes: 2 additions & 23 deletions src/schema/ColumnDefinition.php
Expand Up @@ -55,7 +55,7 @@ private function __construct(int $position, string $name, Type $type)
*
* @throws OutOfBoundsException
*/
public function parse(array $input, array &$output): void
public function apply(array $input, array &$output): void
{
if (!array_key_exists($this->position - 1, $input)) {
throw new OutOfBoundsException(
Expand All @@ -65,27 +65,6 @@ public function parse(array $input, array &$output): void

$value = $input[$this->position - 1];

$output[$this->name] = $this->type->cast($value);
}

/**
* @psalm-return positive-int
*/
public function position(): int
{
return $this->position;
}

/**
* @psalm-return non-empty-string
*/
public function name(): string
{
return $this->name;
}

public function type(): Type
{
return $this->type;
$output[$this->name] = $this->type->apply($value);
}
}
14 changes: 11 additions & 3 deletions src/schema/Schema.php
Expand Up @@ -38,10 +38,18 @@ private function __construct(array $columnDefinitions)
}

/**
* @psalm-return array<int, ColumnDefinition>
* @psalm-param list<string> $line
*
* @psalm-return array<string, bool|float|int|string>
*/
public function columnDefinitions(): array
public function apply(array $line): array
{
return $this->columnDefinitions;
$data = [];

foreach ($this->columnDefinitions as $columnDefinition) {
$columnDefinition->apply($line, $data);
}

return $data;
}
}
2 changes: 1 addition & 1 deletion src/schema/type/BooleanType.php
Expand Up @@ -16,7 +16,7 @@
*/
final class BooleanType extends Type
{
public function cast(string $value): bool
public function apply(string $value): bool
{
return (bool) $value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/schema/type/FloatType.php
Expand Up @@ -16,7 +16,7 @@
*/
final class FloatType extends Type
{
public function cast(string $value): float
public function apply(string $value): float
{
return (float) $value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/schema/type/IntegerType.php
Expand Up @@ -16,7 +16,7 @@
*/
final class IntegerType extends Type
{
public function cast(string $value): int
public function apply(string $value): int
{
return (int) $value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/schema/type/StringType.php
Expand Up @@ -16,7 +16,7 @@
*/
final class StringType extends Type
{
public function cast(string $value): string
public function apply(string $value): string
{
return $value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/schema/type/Type.php
Expand Up @@ -40,5 +40,5 @@ private function __construct()
{
}

abstract public function cast(string $value): mixed;
abstract public function apply(string $value): mixed;
}
19 changes: 2 additions & 17 deletions tests/unit/schema/ColumnDefinitionTest.php
Expand Up @@ -20,27 +20,12 @@
#[Small]
final class ColumnDefinitionTest extends TestCase
{
public function test_Defines_position_of_column_in_CSV_line(): void
{
$this->assertSame(1, $this->column()->position());
}

public function testDefinesNameForArrayElement(): void
{
$this->assertSame('name', $this->column()->name());
}

public function testDefinesTypeForArrayElement(): void
{
$this->assertInstanceOf(IntegerType::class, $this->column()->type());
}

public function test_Parses_column_from_input_array_into_output_array(): void
{
$input = ['1'];
$output = [];

$this->column()->parse($input, $output);
$this->column()->apply($input, $output);

$this->assertSame(['name' => 1], $output);
}
Expand All @@ -53,7 +38,7 @@ public function testCannotParseColumnFromInputArrayThatDoesNotExist(): void
$this->expectException(OutOfBoundsException::class);
$this->expectExceptionMessage('Input array does not have an element at position 1');

$this->column()->parse($input, $output);
$this->column()->apply($input, $output);
}

private function column(): ColumnDefinition
Expand Down
35 changes: 29 additions & 6 deletions tests/unit/schema/SchemaTest.php
Expand Up @@ -17,16 +17,39 @@
#[CoversClass(Schema::class)]
#[UsesClass(ColumnDefinition::class)]
#[UsesClass(Type::class)]
#[UsesClass(BooleanType::class)]
#[UsesClass(IntegerType::class)]
#[UsesClass(FloatType::class)]
#[UsesClass(StringType::class)]
#[Small]
final class SchemaTest extends TestCase
{
public function testHasColumnDefinitions(): void
public function testAppliesColumnDefinitionsToMapInputArrayToOutputArray(): void
{
$column = ColumnDefinition::from(1, 'name', Type::integer());
$schema = Schema::from([1 => $column]);
$schema = Schema::from(
[
ColumnDefinition::from(1, 'a', Type::integer()),
ColumnDefinition::from(2, 'b', Type::float()),
ColumnDefinition::from(3, 'c', Type::string()),
ColumnDefinition::from(4, 'd', Type::boolean()),
]
);

$this->assertCount(1, $schema->columnDefinitions());
$this->assertArrayHasKey(1, $schema->columnDefinitions());
$this->assertContains($column, $schema->columnDefinitions());
$this->assertSame(
[
'a' => 1,
'b' => 1.1,
'c' => '1',
'd' => true,
],
$schema->apply(
[
'1',
'1.1',
'1',
'1',
]
)
);
}
}
10 changes: 5 additions & 5 deletions tests/unit/schema/TypeTest.php
Expand Up @@ -23,22 +23,22 @@ final class TypeTest extends TestCase
{
public function testCanCastStringToBoolean(): void
{
$this->assertSame(true, Type::boolean()->cast('1'));
$this->assertSame(false, Type::boolean()->cast('0'));
$this->assertSame(true, Type::boolean()->apply('1'));
$this->assertSame(false, Type::boolean()->apply('0'));
}

public function testCanCastStringToInteger(): void
{
$this->assertSame(1, Type::integer()->cast('1'));
$this->assertSame(1, Type::integer()->apply('1'));
}

public function testCanCastStringToFloat(): void
{
$this->assertSame(1.0, Type::float()->cast('1.0'));
$this->assertSame(1.0, Type::float()->apply('1.0'));
}

public function testCanCastStringToString(): void
{
$this->assertSame('1', Type::string()->cast('1'));
$this->assertSame('1', Type::string()->apply('1'));
}
}

0 comments on commit 6ce7915

Please sign in to comment.