joao-prrvz / php-utils
A small library made for a active record and schemas validation based workflow
0.0.7
2026-04-02 08:17 UTC
Requires
- php: >=8.1
README
A small library made for a active record and schemas validation based workflow
Examples
Model exmaples
use PHPUtils\BaseModel; use PHPUtils\Attributes\DB as DB; class User extends BaseModel { // Block makes it so when generating the insert and update query it will not add the column "id" to the sql query #[DB\Column, DB\Block(DB\Block::INSERT, DB\Block::UPDATE)] public int $id; #[DB\Column] public string $username; #[DB\Column] public string $email; // Hidden makes it so when we do json_encode on this model it will not return the password #[DB\Column, DB\Hidden] public string $password; public static function selectById(int $id): User { return static::selectBy("id", $id); } public function insert(): int { $sql = static::getInsertQuery(); $params = [ $this->username, $this->email, $this->password, ]; static::run($sql, $params); return static::getDB()->lastInsertId(); } public function update(): void { $params = [ $this->username, $this->email, $this->password, ]; parent::updateBy("id", $this->id, $params); } public function delete(): void { parent::deleteBy("id", $this->id); } } // Select all is already created on BaseModel User::selectAll();
Schema examples
use PHPUtils\BaseSchema; use PHPUtils\Attributes\Property; use PHPUtils\Attributes\Validators as VA; class User extends BaseSchema { #[Property, VA\Min(3), VA\Max(10), VA\Filter(FILTER_SANITIZE_FULL_SPECIAL_CHARS)] public string $username; #[Property, VA\Filter(FILTER_VALIDATE_EMAIL)] public string $email; #[Property, VA\Min(8), VA\Max(20)] public string $password; } $data = [ "username" => "My username", "email" => "wrongemail", "password" => "password", ]; $schema = new User($data); $result = $schema->validate();