Skip to content

Commit

Permalink
feat: TableName attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
roydejong committed Jun 24, 2024
1 parent c176b83 commit 0e4fc7b
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .idea/dataSources.local.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions lib/Attributes/TableName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace SoftwarePunt\Instarecord\Attributes;

use Attribute;

/**
* Specifies a custom name for a model's backing table.
*/
#[Attribute(Attribute::TARGET_CLASS)]
class TableName
{
public function __construct(public string $name)
{
}
}
23 changes: 15 additions & 8 deletions lib/Database/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ public function getColumnByName(string $columnName): ?Column
return null;
}

public function getTableName(): string
{
if ($this->reflectionModel->nameOverride) {
return $this->reflectionModel->nameOverride;
} else {
return Column::getDefaultColumnName($this->modelClassNameNoNamespace);
}
}

// -----------------------------------------------------------------------------------------------------------------
// Static utils

/**
* @var Table[]
*/
Expand All @@ -170,17 +182,12 @@ public static function getTableInfo(string $modelClassName): Table
}

/**
* Gets the "default" table name.
*
* @param string $className
* @return mixed|string
* Gets the default table name, derived from the model class name.
*/
public static function getDefaultTableName(string $className)
public static function getDefaultTableName(string $className): string
{
$tableName = TextTransforms::removeNamespaceFromClassName($className);
$tableName = TextTransforms::pluralize($tableName);
$tableName = Column::getDefaultColumnName($tableName);

return $tableName;
return Column::getDefaultColumnName($tableName);
}
}
2 changes: 1 addition & 1 deletion lib/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(?array $initialValues = [], bool $loadRelationships
*/
public function getTableName(): string
{
return Table::getDefaultTableName(get_class($this));
return $this->getTableInfo()->getTableName();
}

/**
Expand Down
14 changes: 14 additions & 0 deletions lib/Reflection/ReflectionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ReflectionClass;
use ReflectionException;
use ReflectionProperty;
use SoftwarePunt\Instarecord\Attributes\TableName;
use SoftwarePunt\Instarecord\Config\ConfigException;
use SoftwarePunt\Instarecord\Model;

Expand Down Expand Up @@ -35,6 +36,11 @@ class ReflectionModel
*/
protected array $defaultValues;

/**
* @var string|null
*/
public readonly ?string $nameOverride;

/**
* ReflectionModel constructor.
*
Expand All @@ -61,6 +67,14 @@ public function __construct(Model $model)
foreach ($this->rfClass->getDefaultProperties() as $name => $value) {
$this->defaultValues[$name] = $value;
}

// Get class attributes
$attributes = $this->rfClass->getAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getName() === TableName::class) {
$this->nameOverride = $attribute->getArguments()[0];
}
}
}

// -----------------------------------------------------------------------------------------------------------------
Expand Down
9 changes: 8 additions & 1 deletion tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use SoftwarePunt\Instarecord\Instarecord;
use SoftwarePunt\Instarecord\Tests\Samples\TestDummySerializableType;
use Softwarepunt\Instarecord\Tests\Samples\TestEnum;
use SoftwarePunt\Instarecord\Tests\Samples\TestUserAuto;
use SoftwarePunt\Instarecord\Tests\Samples\TestUserWithSerialized;
use SoftwarePunt\Instarecord\Tests\Samples\TestUser;
use SoftwarePunt\Instarecord\Tests\Testing\TestDatabaseConfig;
Expand Down Expand Up @@ -160,7 +161,13 @@ public function testGetPropertyNameForColumnName()

public function testGetTableName()
{
$sampleUserModel = new TestUser();
$sampleUserModel = new TestUser(); // has getTableName() implemented
$this->assertEquals('users', $sampleUserModel->getTableName());
}

public function testGetTableNameFromAttribute()
{
$sampleUserModel = new TestUserAuto(); // has #[TableName] attribute
$this->assertEquals('users', $sampleUserModel->getTableName());
}

Expand Down
3 changes: 0 additions & 3 deletions tests/Samples/TestReadOnlyUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
use SoftwarePunt\Instarecord\Model;
use SoftwarePunt\Instarecord\Models\IReadOnlyModel;

/**
* @table users
*/
class TestReadOnlyUser extends Model implements IReadOnlyModel
{
private int $secretNotWritable;
Expand Down
10 changes: 2 additions & 8 deletions tests/Samples/TestUserAuto.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace SoftwarePunt\Instarecord\Tests\Samples;

use SoftwarePunt\Instarecord\Attributes\TableName;
use SoftwarePunt\Instarecord\Model;

/**
* @table users
*/
#[TableName("users")]
class TestUserAuto extends Model
{
private int $secretNotWritable;
Expand All @@ -20,9 +19,4 @@ class TestUserAuto extends Model
public \DateTime $modifiedAt;

public \DateTime $createdAt;

public function getTableName(): string
{
return "users";
}
}

0 comments on commit 0e4fc7b

Please sign in to comment.