Skip to content

Commit

Permalink
Merge pull request #44 from clementtalleu/feat/do_not_allow_weird_map…
Browse files Browse the repository at this point in the history
…ping

no id = no mapping
  • Loading branch information
clementtalleu authored Jun 21, 2024
2 parents 9e4b9ce + 1d53447 commit 8fd628b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Command/GenerateSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

namespace Talleu\RedisOm\Command;

use Talleu\RedisOm\Client\RedisCommands;
use Talleu\RedisOm\Exception\BadIdentifierConfigurationException;
use Talleu\RedisOm\Om\Mapping\Entity;
use Talleu\RedisOm\Om\Mapping\Id;
use Talleu\RedisOm\Om\Mapping\Property;
use Talleu\RedisOm\Om\RedisFormat;

Expand Down Expand Up @@ -47,13 +48,22 @@ public static function generateSchema(string $dir): void
$entity->redisClient->dropIndex($entity->prefix ?? $fqcn);
$format = $entity->format ?? RedisFormat::HASH->value;

$idExist = false;
$propertiesToIndex = [];
$properties = $reflectionClass->getProperties();
foreach ($properties as $reflectionProperty) {
if (($propertyAttribute = $reflectionProperty->getAttributes(Property::class)) === []) {
continue;
}

if ($reflectionProperty->getAttributes(Id::class) !== []) {
if ($idExist) {
throw new BadIdentifierConfigurationException("Multiple identifiers found for $fqcn, only one is allowed");
}

$idExist = true;
}

/** @var Property $property */
$property = $propertyAttribute[0]->newInstance();
/** @var \ReflectionNamedType|null $propertyReflectionType */
Expand Down Expand Up @@ -95,6 +105,10 @@ public static function generateSchema(string $dir): void
}
}

if (!$idExist) {
throw new BadIdentifierConfigurationException("No identifier found for $fqcn, or identifier is not mapped by RedisOm");
}

$entity->redisClient->createIndex($entity->prefix ?? $fqcn, $format, $propertiesToIndex);
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/Bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class Bar
{
#[RedisOm\Property]
#[RedisOm\Id]
public ?int $id = null;
#[RedisOm\Property]
public ?string $title = null;
Expand Down

0 comments on commit 8fd628b

Please sign in to comment.