PHP code example of mikamatto / entity-targeting-bundle

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

    

mikamatto / entity-targeting-bundle example snippets


return [
    // Other bundles...
    Mikamatto\EntityTargetingBundle\EntityTargetingBundle::class => ['all' => true],
];

use App\Entity\Base\Announcement;
use App\Repository\Base\AnnouncementRepository;
use Mikamatto\EntityTargetingBundle\EntityTargetingManager;
use Symfony\Bundle\SecurityBundle\Security;

class ExampleService
{
    public function __construct(
        private EntityTargetingManager $entityTargetingManager, 
        private AnnouncementRepository $announcementRepository,
        private Security $security, 
        )
    {
        // Don't forget to set the repository for the targeted entity
        $this->entityTargetingManager->setRepository(Announcement::class);
    }

    public function example(array $params = []): string
    {
        $user = $this->security->getUser();
        $announcements = $this->entityTargetingManager->getTargetedEntities($user, $params);

        // Here we have all the entities which apply to $user. Job done
   

namespace Mikamatto\EntityTargetingBundle\Entity;

interface CriteriaAwareInterface
{
    public function getCriterion(): ?string;
    public function setCriterion(?string $criterion): self;
    public function getCriterionParams(): ?array;
    public function setCriterionParams(?string $params): self;
}

namespace Mikamatto\EntityTargetingBundle\Repository;

interface CriteriaRepositoryInterface
{
    public function getEntities(array $params = []): array;  // Method to fetch active entities
}

interface TargetCriteriaInterface
{
    public function setParameters(array $parameters): void;
    
    public function isEligible(?UserInterface $user, CriteriaAwareInterface $entity): bool;

    public function supports(string $targetAudience): bool;

    public function getCriterionName(): string;

    public function getCriterionDescription(): ?string;
}

namespace App\TargetingCriteria;

use Mikamatto\EntityTargetingBundle\TargetCriteriaInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Mikamatto\EntityTargetingBundle\CriteriaAwareInterface;

class CustomCriterion implements TargetCriteriaInterface
{
    private array $parameters = [];

    /**
     * Sets the parameters for the current criterion.
     *
     * @param array $parameters
     */
    public function setParameters(array $parameters): void
    {
        $this->parameters = $parameters;
    }

    /**
     * Determines if the current entity is eligible based on custom logic.
     *
     * @param UserInterface|null $user
     * @param CriteriaAwareInterface $entity
     * @return bool
     */
    public function isEligible(?UserInterface $user, CriteriaAwareInterface $entity): bool
    {
        // Custom eligibility logic using $user, $entity, and $this->parameters
        if (!$user) {
            return false;
        }

        // Example logic: allow if user has a custom role or attribute
        return in_array('ROLE_SPECIAL', $user->getRoles(), true);
    }

    /**
     * Checks if the criterion supports the given target audience.
     *
     * @param string $targetAudience
     * @return bool
     */
    public function supports(string $targetAudience): bool
    {
        // Specify if this criterion is meant to support a specific target audience.
        // Example: only applicable to 'special_users' audience
        return $targetAudience === 'special_users';
    }

    /**
     * Retrieves the name of this criterion.
     *
     * @return string
     */
    public function getCriterionName(): string
    {
        return 'custom_criterion';
    }
}

// Example usage
$criteriaProvider = $container->get(TargetingCriteriaProvider::class);
$criteriaList = $criteriaProvider->listCriteria();

foreach ($criteriaList as $criterion) {
    echo "Name: " . $criterion['name'] . "\n";
    echo "Class: " . $criterion['class'] . "\n";
    echo "Description: " . $criterion['description'] . "\n\n";
}

namespace App\Entity;

use Mikamatto\EntityTargetingBundle\CriteriaAwareInterface;

class Notification implements CriteriaAwareInterface
{
    private string $criterion;
    private array $criterionParams;

    public function __construct(string $criterion, array $criterionParams = [])
    {
        $this->criterion = $criterion;
        $this->criterionParams = $criterionParams;
    }

    public function getCriterion(): string
    {
        return $this->criterion;
    }

    public function getCriterionParams(): array
    {
        return $this->criterionParams;
    }
}

$notification = new Notification('guests_only');

$notification = new Notification(
    'user_roles',
    [
        'roles' => ['ROLE_ADMIN', 'ROLE_MANAGER'],
        'mode' => 'ANY'
    ]
);