PHP code example of danilovl / entity-traits-bundle

1. Go to this page and download the library: Download danilovl/entity-traits-bundle 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/ */

    

danilovl / entity-traits-bundle example snippets


return [
    // ...
    Danilovl\EntityTraitsBundle\EntityTraitsBundle::class => ['all' => true],
];

use Danilovl\EntityTraitsBundle\Trait\Required\Content\NameTrait;     // string $name
use Danilovl\EntityTraitsBundle\Trait\Optional\Content\TitleTrait;    // ?string $title = null

namespace App\Entity;

use Danilovl\EntityTraitsBundle\Contract\Optional\Audit\BlameableInterface;
use Danilovl\EntityTraitsBundle\Contract\Optional\Timestampable\TimestampableInterface;
use Danilovl\EntityTraitsBundle\Trait\Optional\Audit\BlameableTrait;
use Danilovl\EntityTraitsBundle\Trait\Optional\Content\{ContentTrait, TitleTrait};
use Danilovl\EntityTraitsBundle\Trait\Optional\Counter\ViewsCountTrait;
use Danilovl\EntityTraitsBundle\Trait\Required\Identity\UuidTrait;
use Danilovl\EntityTraitsBundle\Trait\Optional\Seo\{MetaTrait, SlugTrait};
use Danilovl\EntityTraitsBundle\Trait\Optional\Timestampable\{PublishedAtTrait, TimestampableTrait};
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: 'articles')]
class Article implements TimestampableInterface, BlameableInterface
{
    use UuidTrait;
    use TitleTrait;
    use SlugTrait;
    use ContentTrait;
    use MetaTrait;
    use PublishedAtTrait;
    use TimestampableTrait;
    use BlameableTrait;
    use ViewsCountTrait;
}

use Danilovl\EntityTraitsBundle\Trait\Optional\Geographic\LocationTrait;
use Danilovl\EntityTraitsBundle\Validator\{
    Barcode,
    Country,
    Currency,
    HexColor,
    Iban,
    Isbn,
    LatitudeRange,
    Locale,
    LongitudeRange,
    Timezone,
    VatNumber
};

class Product
{
    use LocationTrait;

    #[LatitudeRange]
    protected ?float $latitude = null;

    #[LongitudeRange]
    protected ?float $longitude = null;

    #[HexColor]
    protected ?string $brandColor = null;

    #[Iban]
    protected ?string $iban = null;

    #[VatNumber]
    protected ?string $vatNumber = null;

    #[Isbn]
    protected ?string $isbn = null;

    #[Barcode]   // EAN-8/EAN-13/UPC-A/GTIN-14 with checksum
    protected ?string $barcode = null;

    #[Currency]  // ISO 4217
    protected ?string $currency = null;

    #[Country]   // ISO 3166-1 alpha-2
    protected ?string $country = null;

    #[Locale]    // BCP 47
    protected ?string $locale = null;

    #[Timezone]  // IANA TZDB
    protected ?string $timezone = null;
}

$em->getFilters()->enable('published');

use Danilovl\EntityTraitsBundle\DependencyInjection\Config\EntityTraitsConfig;

final readonly class Audit
{
    public function __construct(private EntityTraitsConfig $config) {}
}

use Danilovl\EntityTraitsBundle\Attribute\AutoSlug;

#[ORM\Entity]
#[AutoSlug(from: 'title')]
class Article { /* ... */ }

use Danilovl\EntityTraitsBundle\Attribute\AutoIdentifier;

#[AutoIdentifier(property: 'externalId')]
class Order
{
    private ?object $externalId = null;
}

use Danilovl\EntityTraitsBundle\Contract\Optional\Timestampable\SoftDeletableInterface;
use Danilovl\EntityTraitsBundle\Trait\Required\Identity\UuidTrait;
use Danilovl\EntityTraitsBundle\Trait\Optional\Timestampable\SoftDeletableTrait;

class User implements SoftDeletableInterface
{
    use UuidTrait;
    use SoftDeletableTrait;
    // ...
}

// Anywhere in your app:
$em->remove($user);   // -> issues UPDATE users SET deleted_at = NOW()
$em->flush();

$em->getFilters()->disable('soft_delete');

use Danilovl\EntityTraitsBundle\Contract\Optional\Audit\{
    CreatedByStringAwareInterface,
    UpdatedByStringAwareInterface
};
use Danilovl\EntityTraitsBundle\Trait\Optional\Audit\{
    CreatedByStringTrait,
    UpdatedByStringTrait
};

class Article implements CreatedByStringAwareInterface, UpdatedByStringAwareInterface
{
    use CreatedByStringTrait;   // string $createdBy column
    use UpdatedByStringTrait;   // string $updatedBy column
}

use Danilovl\EntityTraitsBundle\Attribute\Tree;
use Danilovl\EntityTraitsBundle\Trait\Optional\Sorting\{ParentTrait, TreePathTrait};
use Danilovl\EntityTraitsBundle\Trait\Required\Identity\IdTrait;

#[ORM\Entity]
#[Tree(separator: '/', pathProperty: 'path', depthProperty: 'depth', identifier: 'id')]
class Category
{
    use IdTrait;
    use ParentTrait;
    use TreePathTrait;
}

use Danilovl\EntityTraitsBundle\Trait\Optional\Security\{
    FailedLoginAttemptsTrait,
    LockedUntilTrait,
    TwoFactorEnabledTrait,
    PasswordChangedAtTrait
};

class User
{
    use FailedLoginAttemptsTrait;  // $failedLoginAttempts, $lastFailedLoginAt
    use LockedUntilTrait;          // $lockedUntil + isCurrentlyLocked()
    use TwoFactorEnabledTrait;     // $twoFactorEnabled, $twoFactorSecret
    use PasswordChangedAtTrait;    // $passwordChangedAt
}

$user->incrementFailedLoginAttempts();  // bumps counter + sets lastFailedLoginAt
$user->resetFailedLoginAttempts();      // zeroes counter
$user->isCurrentlyLocked();            // true when lockedUntil > now

use Danilovl\EntityTraitsBundle\Trait\Required\Tenancy\TenantTrait;

class Invoice
{
    use TenantTrait;       // int $tenantId
    use OrganizationTrait; // int $organizationId
}

use Danilovl\EntityTraitsBundle\Trait\Optional\Integration\{
    ExternalIdTrait,
    ExternalSourceTrait,
    ExternalUrlTrait,
    ImportedAtTrait,
    SyncedAtTrait
};

class Product
{
    use ExternalIdTrait;     // $externalId — ID in the foreign system
    use ExternalSourceTrait; // $externalSource — e.g. 'shopify', 'magento'
    use ImportedAtTrait;     // $importedAt
    use SyncedAtTrait;       // $syncedAt
}

use Danilovl\EntityTraitsBundle\Trait\Optional\Pricing\{
    PriceRangeTrait,
    SubscriptionPriceTrait,
    TierPricingTrait
};

class Plan
{
    use SubscriptionPriceTrait; // $monthlyPrice, $annualPrice
    use PriceRangeTrait;        // $priceMin, $priceMax
}

use Danilovl\EntityTraitsBundle\Trait\Optional\Counter\{
    FavoritesCountTrait,
    FollowersCountTrait,
    PointsTrait,
    BalanceTrait
};

class UserProfile
{
    use FavoritesCountTrait;  // incrementFavoritesCount() / decrementFavoritesCount()
    use FollowersCountTrait;
    use PointsTrait;          // loyalty / gamification points
    use BalanceTrait;         // wallet balance (in cents)
}

use Danilovl\EntityTraitsBundle\Trait\Optional\Geolocation\{
    RegionTrait,
    CityTrait,
    PostalCodeTrait,
    AddressLineTrait
};

use Danilovl\EntityTraitsBundle\Trait\Optional\Seo\{
    TwitterCardTrait,   // twitterCard, twitterTitle, twitterDescription, twitterImage
    SitemapTrait,       // sitemapPriority (float), sitemapChangefreq
    HreflangTrait,      // hreflang JSON map — setHreflangLocale('de', '/de/page')
    SchemaOrgTrait,     // schemaOrg JSON (JSON-LD structured data)
    SlugAliasTrait      // slugAlias for redirect from old URLs
};
yaml
danilovl_entity_traits:
    timestampable:
        timezone: 'Europe/Berlin'