1. Go to this page and download the library: Download wappcode/pdss-utilities library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wappcode / pdss-utilities example snippets
use PDSSUtilities\AbstractEntityModel;
#[ORM\Entity]
class Product extends AbstractEntityModel
{
#[ORM\Column(type: 'string')]
private string $name;
// Los campos id, created y updated están heredados
}
use PDSSUtilities\AbstractEntityModelUlid;
#[ORM\Entity]
class User extends AbstractEntityModelUlid
{
#[ORM\Column(type: 'string')]
private string $email;
}
use PDSSUtilities\AbstractEntityModelKsuid;
#[ORM\Entity]
class Order extends AbstractEntityModelKsuid
{
#[ORM\Column(type: 'decimal')]
private float $total;
}
use PDSSUtilities\AbstractEntityModelUuidV4;
#[ORM\Entity]
class Invoice extends AbstractEntityModelUuidV4
{
#[ORM\Column(type: 'string')]
private string $number;
}
public function getId(): ?string|?int;
public function getCreated(): DateTimeImmutable;
public function getUpdated(): DateTimeImmutable;
public function __toString(): string;
protected function setUpdated(): self; // Para actualización manual
use PDSSUtilities\AbstractEntityModelUlid;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
#[ORM\AttributeOverride(
name: 'id',
column: new ORM\Column(name: 'product_id', type: 'string', length: 26)
)]
class Product extends AbstractEntityModelUlid
{
// La columna 'id' ahora se llama 'product_id' en la base de datos
}
#[ORM\Entity]
#[ORM\AttributeOverride(
name: 'id',
column: new ORM\Column(name: 'id', type: 'string', length: 50)
)]
class CustomEntity extends AbstractEntityModelUlid
{
// El ID ahora permite hasta 50 caracteres
}
#[ORM\Entity]
#[ORM\AttributeOverrides([
new ORM\AttributeOverride(
name: 'created',
column: new ORM\Column(name: 'created_at', type: 'datetimetz_immutable')
),
new ORM\AttributeOverride(
name: 'updated',
column: new ORM\Column(name: 'updated_at', type: 'datetimetz_immutable')
)
])]
class Article extends AbstractEntityModelUlid
{
// Las columnas ahora se llaman 'created_at' y 'updated_at'
}
#[ORM\Entity]
#[ORM\AttributeOverride(
name: 'id',
column: new ORM\Column(name: 'id', type: 'bigint')
)]
class LargeTable extends AbstractEntityModel
{
// El ID ahora es BIGINT en lugar de INTEGER
}
// Doctrine convierte esto:
$qb->where('u.name = :name')->setParameter('name', $userInput);
// En un prepared statement:
// SELECT * FROM users WHERE name = ?
// Binding: ['John']
class QueryValidator
{
private const ALLOWED_PROPERTIES = [
'User' => ['id', 'name', 'email', 'created', 'updated'],
'Profile' => ['id', 'bio', 'avatar'],
'Order' => ['id', 'total', 'status', 'created']
];
public static function validateProperty(string $entity, string $property): bool
{
return in_array($property, self::ALLOWED_PROPERTIES[$entity] ?? [], true);
}
public static function validateFilters(array $filters, string $entity): void
{
foreach ($filters as $group) {
foreach ($group['conditions'] ?? [] as $condition) {
if (!self::validateProperty($entity, $condition['property'])) {
throw new \InvalidArgumentException("Propiedad no permitida: {$condition['property']}");
}
}
}
}
}
// Uso:
try {
QueryValidator::validateFilters($filters, 'User');
$qb = QueryFilter::addFilters($qb, $filters);
} catch (\InvalidArgumentException $e) {
// Manejar error de validación
}
class QueryValidator
{
private const ALLOWED_JOINS = [
'User' => ['profile', 'orders', 'roles'],
'Order' => ['items', 'user'],
];
public static function validateJoins(array $joins, string $entity): void
{
foreach ($joins as $join) {
$property = $join['property'];
if (!in_array($property, self::ALLOWED_JOINS[$entity] ?? [], true)) {
throw new \InvalidArgumentException("Join no permitido: {$property}");
}
}
}
}