feat: match modes for room selection condition to detect MBZ*
addresses #869btdcyh
This commit is contained in:
@@ -17,16 +17,22 @@ use App\Form\Service\Contract\FieldConditionInterface;
|
|||||||
*/
|
*/
|
||||||
class RoomSelectionCondition implements FieldConditionInterface
|
class RoomSelectionCondition implements FieldConditionInterface
|
||||||
{
|
{
|
||||||
|
private const MATCH_MODE_EXACT = 'exact';
|
||||||
|
private const MATCH_MODE_PREFIX = 'prefix';
|
||||||
|
|
||||||
private array $requiredRoomCodes;
|
private array $requiredRoomCodes;
|
||||||
|
private string $matchMode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new room selection condition for specified room codes.
|
* Creates a new room selection condition for specified room codes.
|
||||||
*
|
*
|
||||||
* @param array<string> $requiredRoomCodes Room codes that should trigger this condition
|
* @param array<string> $requiredRoomCodes Room codes that should trigger this condition
|
||||||
|
* @param string $matchMode Match mode: 'prefix' (default) or 'exact'
|
||||||
*/
|
*/
|
||||||
public function __construct(array $requiredRoomCodes)
|
public function __construct(array $requiredRoomCodes, string $matchMode = self::MATCH_MODE_PREFIX)
|
||||||
{
|
{
|
||||||
$this->requiredRoomCodes = array_map('strtolower', $requiredRoomCodes);
|
$this->requiredRoomCodes = array_map('strtolower', $requiredRoomCodes);
|
||||||
|
$this->matchMode = $matchMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -48,7 +54,7 @@ class RoomSelectionCondition implements FieldConditionInterface
|
|||||||
$selectedRoomId = $formData['participants'][$participantIndex]['assignedRoomId'];
|
$selectedRoomId = $formData['participants'][$participantIndex]['assignedRoomId'];
|
||||||
if (true === is_numeric($selectedRoomId)) {
|
if (true === is_numeric($selectedRoomId)) {
|
||||||
$room = $this->findRoomById((int) $selectedRoomId, $bookingDto);
|
$room = $this->findRoomById((int) $selectedRoomId, $bookingDto);
|
||||||
if (null !== $room && in_array(strtolower($room->code ?? ''), $this->requiredRoomCodes, true)) {
|
if (null !== $room && $this->matchesRoomCode($room->code ?? '')) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -58,7 +64,7 @@ class RoomSelectionCondition implements FieldConditionInterface
|
|||||||
$participant = $bookingDto->getParticipant($participantIndex);
|
$participant = $bookingDto->getParticipant($participantIndex);
|
||||||
if (null !== $participant && null !== $participant->assignedRoomId) {
|
if (null !== $participant && null !== $participant->assignedRoomId) {
|
||||||
$room = $this->findRoomById($participant->assignedRoomId, $bookingDto);
|
$room = $this->findRoomById($participant->assignedRoomId, $bookingDto);
|
||||||
if (null !== $room && in_array(strtolower($room->code ?? ''), $this->requiredRoomCodes, true)) {
|
if (null !== $room && $this->matchesRoomCode($room->code ?? '')) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -87,8 +93,9 @@ class RoomSelectionCondition implements FieldConditionInterface
|
|||||||
public function getDescription(): string
|
public function getDescription(): string
|
||||||
{
|
{
|
||||||
$codes = implode(', ', $this->requiredRoomCodes);
|
$codes = implode(', ', $this->requiredRoomCodes);
|
||||||
|
$matchType = self::MATCH_MODE_PREFIX === $this->matchMode ? 'starting with' : 'matching';
|
||||||
|
|
||||||
return "Field visible when room with code(s) [{$codes}] is selected";
|
return "Field visible when room with code(s) {$matchType} [{$codes}] is selected";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -109,4 +116,28 @@ class RoomSelectionCondition implements FieldConditionInterface
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a room code matches any of the required codes based on match mode.
|
||||||
|
*
|
||||||
|
* @param string $roomCode The room code to check
|
||||||
|
*
|
||||||
|
* @return bool True if the room code matches, false otherwise
|
||||||
|
*/
|
||||||
|
private function matchesRoomCode(string $roomCode): bool
|
||||||
|
{
|
||||||
|
$roomCode = strtolower($roomCode);
|
||||||
|
|
||||||
|
if (self::MATCH_MODE_PREFIX === $this->matchMode) {
|
||||||
|
foreach ($this->requiredRoomCodes as $code) {
|
||||||
|
if (str_starts_with($roomCode, $code)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return in_array($roomCode, $this->requiredRoomCodes, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user