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
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';
}
}