PHP code example of jul6art / core-bundle

1. Go to this page and download the library: Download jul6art/core-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/ */

    

jul6art / core-bundle example snippets


#[ORM\Column(type: 'encrypted_string', nullable: true)]
private ?string $iban = null;

// rendering the form
return $this->render('security/register.html.twig', [
    'captchaQuestion' => $this->captcha->generate(),   // "3 + 5"
]);

// handling the submission
if (!$this->captcha->validate($request->request->getString('captcha'))) {
    // refuse, and call generate() again for the next attempt
}

use Jul6Art\CoreBundle\Attribute\Purgeable;

#[Purgeable(field: 'createdAt', interval: '-3 months')]
#[Purgeable(field: 'deletedAt', interval: '-1 week', condition: 'entity.isDeleted()')]
class AuditLog { … }

#[Purgeable(field: 'createdAt', interval: '%app.audit_retention%')]

#[Purgeable(field: 'createdAt', interval: '%app.audit_retention%', bulk: true)]

#[AsEventListener(event: EntityPurgedEvent::NAME)]
public function onEntityPurged(EntityPurgedEvent $event): void
{
    $this->auditLogger->log('entity.purged', $event->getOrganizationId(), null,
        $event->getEntityShortName(), $event->getEntityId());
}

final class JsTranslationTest extends AbstractJsTranslationTestCase
{
    protected static function javaScriptDirectories(): array
    {
        return [
            self::projectDir().'/assets',
            // ⚠️ The bundles too: their controllers read keys THIS catalogue must hold.
            self::projectDir().'/vendor/jul6art/datatable-bundle/assets',
        ];
    }

    // Keys no scanner can see — an enum read as `t(`datatable.work_order_status.${value}`)`.
    protected static function declaredKeys(): array
    {
        return array_map(static fn (WorkOrderStatus $c): string => $c->translationKey(), WorkOrderStatus::cases());
    }

    // A weaker promise: alive, but not 

$this->getCurrentUserOrNull();      // ?UserInterface
$this->getCurrentUserIdOrNull();    // ?int — the account the request runs as
$this->getOriginalUserIdOrNull();   // ?int — the account that started an impersonation

#[ORM\Entity]
#[ORM\HasLifecycleCallbacks]
class Page
{
    use IdTrait;
    use SoftDeletableTrait;
    use TimestampableTrait;
}

$user->setEmail(Strings::markDeleted($user->getEmail()));   // [email protected]_DELETED_1755…
$user->softDelete();
// …later
$user->setEmail((string) Strings::restoreDeleted($user->getEmail()));   // [email protected]
$user->restore();

final class UserController extends AbstractController
{
    public function edit(Request $request, User $user): Response
    {
        // …
        return $this->redirectBackWithSuccess($request, 'user.updated', 'user_index');
    }
}

final class ProfileController extends AbstractController
{
    protected function translationDomain(): string
    {
        return 'profile';   // flashes, form errors and trans() alike
    }

    public function edit(Request $request): Response
    {
        // …
        return $this->redirectWithSuccess('profile_show', 'edit.success');   // → domain 'profile'
    }
}

throw $this->createNotFoundException($this->trans('user.not_found'));

#[Route('/bulk-publish', methods: ['POST'])]
#[IsGranted(PermissionCodes::CMS_PAGE_PUBLISH)]
public function bulkPublish(Request $request, BulkActionRunner $runner): Response
{
    $count = $runner->run($request, Page::class, PageVoter::PUBLISH, function (Page $page): void {
        if ($page->isPublished()) {
            throw new \DomainException('Already published.');   // this row only
        }

        $this->pageService->publish($page);
    });

    return $this->redirectWithSuccess('cms_page_index', 'cms.page.bulk_published', ['%count%' => $count]);
}

use Jul6Art\CoreBundle\Security\Voter\AbstractVoter;
use Symfony\Component\Security\Core\User\UserInterface;

final class GalleryVoter extends AbstractVoter
{
    public const string VIEW = 'GALLERY_VIEW';
    public const string EDIT = 'GALLERY_EDIT';

    protected function attributes(): array
    {
        return [self::VIEW, self::EDIT];
    }

    protected function subjects(): array
    {
        return [Gallery::class];
    }

    protected function decide(string $attribute, mixed $subject, UserInterface $user): bool
    {
        if (!$subject instanceof Gallery) {
            return false;
        }

        return match ($attribute) {
            self::VIEW => $subject->isPublished() || $this->owns($subject, $user),
            self::EDIT => $this->owns($subject, $user) || $this->hasRole('ROLE_ADMIN'),
            default => false,
        };
    }
}

public function __construct(private readonly NumberFormatter $formatter) {}
// …
$this->formatter->formatMoney($invoice->getTotal(), 'EUR');

$builder->get('siret')->addModelTransformer(new StripWhitespaceTransformer(digitsOnly: true));
$builder->get('iban')->addModelTransformer(new StripWhitespaceTransformer());