Skip to content

Commit

Permalink
Add a Table attribute to specify filename
Browse files Browse the repository at this point in the history
  • Loading branch information
micoli committed Jul 14, 2023
1 parent 50adb89 commit 5c0f1d4
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
A flat database manager.

[![Build Status](https://github.com/micoli/elql/workflows/Tests/badge.svg)](https://github.com/micoli/elql/actions)
[![Coverage Status](https://coveralls.io/repos/github/micoli/Elql/badge.svg?branch=main)](https://coveralls.io/github/micoli/elql?branch=main)
[![Coverage Status](https://coveralls.io/repos/github/micoli/Elql/badge.svg?branch=main)](https://coveralls.io/github/micoli/Elql?branch=main)
[![Latest Stable Version](http://poser.pugx.org/micoli/elql/v)](https://packagist.org/packages/micoli/elql)
[![Total Downloads](http://poser.pugx.org/micoli/elql/downloads)](https://packagist.org/packages/micoli/elql)
[![Latest Unstable Version](http://poser.pugx.org/micoli/elql/v/unstable)](https://packagist.org/packages/micoli/elql) [![License](http://poser.pugx.org/micoli/elql/license)](https://packagist.org/packages/micoli/elql)
Expand Down
38 changes: 37 additions & 1 deletion src/Metadata/MetadataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,51 @@

namespace Micoli\Elql\Metadata;

use ReflectionClass;

class MetadataManager implements MetadataManagerInterface
{
public function __construct(
/**
* @var array<class-string, string>
*/
private array $tableNames = [],
) {
}

/**
* @param class-string $model
*/
public function tableNameExtractor(string $model): string
{
if (isset($this->tableNames[$model])) {
return $this->tableNames[$model];
}
$reflectedClass = new ReflectionClass($model);
$attributes = $reflectedClass->getAttributes();

foreach ($attributes as $reflectedAttribute) {
$attribute = $reflectedAttribute->newInstance();
// @codeCoverageIgnoreStart
if (!$attribute instanceof Table) {
continue;
}
// @codeCoverageIgnoreStart

// @codeCoverageIgnoreEnd
if ($attribute->name === null) {
continue;
}
// @codeCoverageIgnoreEnd
$this->tableNames[$model] = $attribute->name;

return $this->tableNames[$model];
}

$parts = explode('\\', $model);

return $parts[count($parts) - 1];
$this->tableNames[$model] = $parts[count($parts) - 1];

return $this->tableNames[$model];
}
}
15 changes: 15 additions & 0 deletions src/Metadata/Table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Micoli\Elql\Metadata;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class Table
{
public function __construct(public readonly ?string $name = null)
{
}
}
4 changes: 2 additions & 2 deletions tests/ElqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ public function testACompleteScenarioPass(): void
lastName: c
BAZ
),
trim(file_get_contents($this->databaseDir . '/Baz.yaml')),
trim(file_get_contents($this->databaseDir . '/b_a_z.yaml')),
);
self::assertcount(2, $this->database->persister->getDatabase());
}

public function testItShouldReadFromFiles(): void
{
file_put_contents(
$this->databaseDir . '/Baz.yaml',
$this->databaseDir . '/b_a_z.yaml',
<<<BAZ
-
id: 2
Expand Down
17 changes: 17 additions & 0 deletions tests/Fixtures/Bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Micoli\Elql\Tests\Fixtures;

use Micoli\Elql\Metadata\Table;

#[Table]
class Bar
{
public function __construct(
public readonly int $id,
public string $name,
) {
}
}
3 changes: 3 additions & 0 deletions tests/Fixtures/Baz.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Micoli\Elql\Tests\Fixtures;

use Micoli\Elql\Metadata\Table;

#[Table('b_a_z')]
class Baz
{
public function __construct(
Expand Down
32 changes: 32 additions & 0 deletions tests/Metadata/MetadataManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Micoli\Elql\Tests\Metadata;

use Attribute;
use Micoli\Elql\Metadata\MetadataManager;
use Micoli\Elql\Tests\Fixtures\Bar;
use Micoli\Elql\Tests\Fixtures\Foo;
use PHPUnit\Framework\TestCase;

/**
* @internal
*
* @psalm-suppress PropertyNotSetInConstructor
*/
class MetadataManagerTest extends TestCase
{
public function testItShouldGetTableNameFromAttributes(): void
{
$metadataManager = new MetadataManager([Foo::class => 'aaa']);
self::assertSame('Bar', $metadataManager->tableNameExtractor(Bar::class));
self::assertSame('aaa', $metadataManager->tableNameExtractor(Foo::class));
self::assertSame('FooBar', $metadataManager->tableNameExtractor(FooBar::class));
}
}

#[Attribute]
class FooBar
{
}

0 comments on commit 5c0f1d4

Please sign in to comment.