feat: initial commit of camelia/orm

Attribute-mapped Active Record ORM for MariaDB/MySQL over PDO.

- Attribute-driven metadata: TableName, PrimaryKey, AutoIncrement, Column,
  Guarded, Ignore, Required, Enum, Nullable, Length, Precision, Unique,
  UniqueGroup, Index, MaxLength.
- Relationship mapping: BelongsTo, HasOne, HasMany, BelongsToMany with
  batched eager loading and a 3-level depth limit.
- Orm entry point with fromArray, fromPdo, fromEnv and nested transactions.
- Immutable Query with Cond operators and GroupCond grouping.
- Repository read and write API, pagination, and typed exception hierarchy.
- Declares PHP >=8.3; Mago and PHPStan pin the parser to 8.3.
- MIT license.
This commit is contained in:
dgarcia committed 2026-11-15 10:14:22 -03:00
commit 66e93e3070
97 files changed
+12188

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Camelia\Orm\Tests\Unit\Support;
use Camelia\Orm\Support\NameConverter;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class NameConverterTest extends TestCase
{
#[Test]
public function it_converts_internal_capitals_to_underscores(): void
{
self::assertSame('codigo_empresa', NameConverter::camelToSnake('codigoEmpresa'));
self::assertSame('id_zona', NameConverter::camelToSnake('idZona'));
self::assertSame('normal', NameConverter::camelToSnake('normal'));
self::assertSame('', NameConverter::camelToSnake(''));
}
#[Test]
public function it_never_prefixes_the_first_character(): void
{
self::assertSame('nombre', NameConverter::camelToSnake('Nombre'));
}
#[Test]
public function it_round_trips_simple_names(): void
{
self::assertSame('codigoEmpresa', NameConverter::snakeToCamel('codigo_empresa'));
}
}
+104
View File
@@ -0,0 +1,104 @@
<?php
declare(strict_types=1);
namespace Camelia\Orm\Tests\Unit\Support;
use Camelia\Orm\Support\TypeCaster;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class TypeCasterTest extends TestCase
{
#[Test]
public function it_casts_numeric_values_to_int(): void
{
self::assertSame(42, TypeCaster::cast('42', 'int', false));
self::assertSame(1, TypeCaster::cast(true, 'int', false));
}
#[Test]
public function it_casts_boolean_representations(): void
{
self::assertTrue(TypeCaster::cast('true', 'bool', false));
self::assertFalse(TypeCaster::cast('false', 'bool', false));
self::assertTrue(TypeCaster::cast(1, 'bool', false));
self::assertFalse(TypeCaster::cast('0', 'bool', false));
}
#[Test]
public function it_decodes_json_strings_into_arrays(): void
{
self::assertSame(['a' => 1], TypeCaster::cast('{"a":1}', 'array', false));
}
#[Test]
public function it_casts_dates(): void
{
$value = TypeCaster::cast('2024-01-15', DateTimeImmutable::class, false);
self::assertInstanceOf(DateTimeImmutable::class, $value);
self::assertSame('2024-01-15', $value->format('Y-m-d'));
}
#[Test]
public function it_keeps_null_for_nullable_properties(): void
{
self::assertNull(TypeCaster::cast(null, 'int', true));
}
/**
* @return array<string, array{0: mixed, 1: string}>
*/
public static function refusals(): array
{
return [
'int from text' => ['abc', 'int'],
'float from text' => ['abc', 'float'],
'bool from text' => ['quizas', 'bool'],
'array from text' => ['no json', 'array'],
'date from text' => ['no es fecha', DateTimeImmutable::class],
];
}
#[Test]
#[DataProvider('refusals')]
public function it_refuses_values_it_cannot_cast(mixed $value, string $type): void
{
$this->expectException(\InvalidArgumentException::class);
TypeCaster::cast($value, $type, false);
}
#[Test]
public function it_refuses_null_on_a_non_nullable_type(): void
{
$this->expectException(\InvalidArgumentException::class);
TypeCaster::cast(null, 'int', false);
}
#[Test]
public function it_serializes_dates_as_atom_strings(): void
{
self::assertSame(
'2024-01-15T00:00:00+00:00',
TypeCaster::toSerializable(
new DateTimeImmutable('2024-01-15', new \DateTimeZone('UTC')),
),
);
}
#[Test]
public function it_stores_dates_as_datetime_strings(): void
{
self::assertSame(
'2024-01-15 00:00:00',
TypeCaster::toDatabaseValue(
new DateTimeImmutable('2024-01-15', new \DateTimeZone('UTC')),
),
);
}
}