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

+92
View File
@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
namespace Camelia\Orm\Pagination;
use Camelia\Orm\Model;
/**
* Immutable paginated result.
*
* @api
*/
final readonly class Page
{
public const MIN_PER_PAGE = 1;
public const MAX_PER_PAGE = 1000;
/**
* @param list<Model> $items
*/
public function __construct(
public array $items,
public int $page,
public int $perPage,
public int $total,
public int $totalPages,
) {}
/**
* Builds a page with boundary-tolerant pagination inputs.
*
* @param list<Model> $items
*/
public static function of(array $items, int $page, int $perPage, int $total): self
{
$safePage = max(1, $page);
$safePerPage = max(self::MIN_PER_PAGE, min(self::MAX_PER_PAGE, $perPage));
$safeTotal = max(0, $total);
$totalPages = $safePerPage === 0 ? 0 : (int) ceil($safeTotal / $safePerPage);
return new self($items, $safePage, $safePerPage, $safeTotal, $totalPages);
}
public function hasNext(): bool
{
return $this->page < $this->totalPages;
}
public function hasPrev(): bool
{
return $this->page > 1;
}
/**
* @return array{
* items: list<array<string, mixed>>,
* page: int,
* per_page: int,
* total: int,
* total_pages: int,
* has_next: bool,
* has_prev: bool
* }
*/
public function toArray(): array
{
$items = [];
foreach ($this->items as $item) {
$items[] = $item->toArray();
}
return [
'items' => $items,
'page' => $this->page,
'per_page' => $this->perPage,
'total' => $this->total,
'total_pages' => $this->totalPages,
'has_next' => $this->hasNext(),
'has_prev' => $this->hasPrev(),
];
}
public function toJson(int $flags = 0): string
{
return (string) json_encode(
$this->toArray(),
$flags | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES,
);
}
}