Skip to content

Commit

Permalink
Merge branch 'staging' into MAUT-11616
Browse files Browse the repository at this point in the history
  • Loading branch information
rahuld-dev committed Jul 5, 2024
2 parents 488e2d8 + 9a9ebd3 commit 367cd2e
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 23 deletions.
33 changes: 33 additions & 0 deletions CustomFieldType/DateOperatorTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace MauticPlugin\CustomObjectsBundle\CustomFieldType;

trait DateOperatorTrait
{
/**
* Remove operators that are supported only by segment filters.
*
* @return string[]
*/
public function getOperatorOptions(): array
{
$options = parent::getOperatorOptions();

unset($options['between'], $options['!between'], $options['inLast'], $options['inNext']);

return $options;
}

/**
* @return mixed[]
*/
public function getOperators(): array
{
$allOperators = parent::getOperators();
$allowedOperators = array_flip(['=', '!=', 'gt', 'gte', 'lt', 'lte', 'empty', '!empty', 'between', '!between', 'inLast', 'inNext']);

return array_intersect_key($allOperators, $allowedOperators);
}
}
13 changes: 2 additions & 11 deletions CustomFieldType/DateTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

class DateTimeType extends AbstractCustomFieldType
{
use DateOperatorTrait;

/**
* @var string
*/
Expand Down Expand Up @@ -70,17 +72,6 @@ public function getEntityClass(): string
return CustomFieldValueDateTime::class;
}

/**
* @return mixed[]
*/
public function getOperators(): array
{
$allOperators = parent::getOperators();
$allowedOperators = array_flip(['=', '!=', 'gt', 'gte', 'lt', 'lte', 'empty', '!empty', 'between', '!between', 'inLast', 'inNext']);

return array_intersect_key($allOperators, $allowedOperators);
}

/**
* {@inheritdoc}
*/
Expand Down
13 changes: 2 additions & 11 deletions CustomFieldType/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

class DateType extends AbstractCustomFieldType
{
use DateOperatorTrait;

/**
* @var string
*/
Expand Down Expand Up @@ -70,17 +72,6 @@ public function getEntityClass(): string
return CustomFieldValueDate::class;
}

/**
* @return mixed[]
*/
public function getOperators(): array
{
$allOperators = parent::getOperators();
$allowedOperators = array_flip(['=', '!=', 'gt', 'gte', 'lt', 'lte', 'empty', '!empty', 'between', '!between', 'inLast', 'inNext']);

return array_intersect_key($allOperators, $allowedOperators);
}

/**
* {@inheritdoc}
*/
Expand Down
14 changes: 14 additions & 0 deletions CustomFieldType/IntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ public function createValueEntity(CustomField $customField, CustomItem $customIt
return new CustomFieldValueInt($customField, $customItem, (int) $value);
}

/**
* Remove operators that are supported only by segment filters.
*
* @return string[]
*/
public function getOperatorOptions(): array
{
$options = parent::getOperatorOptions();

unset($options['between'], $options['!between']);

return $options;
}

/**
* @return mixed[]
*/
Expand Down
2 changes: 1 addition & 1 deletion Entity/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ public function valueToLabel(string $value): string
throw new NotFoundException("Label was not found for value {$value}");
}

return $label;
return (string) $label;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions Tests/Unit/Entity/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,22 @@ public function testValueToLabel(): void
$this->assertSame('Option B', $customField->valueToLabel('option_b'));
}

public function testValueToLabelWithInteger(): void
{
$customField = new CustomField();
$optionB = new CustomFieldOption();
$optionB->setLabel('1');
$optionB->setValue('1');
$customField->addOption($optionB);
$customField->setTypeObject(
new SelectType(
$this->createMock(TranslatorInterface::class),
$this->createMock(FilterOperatorProviderInterface::class)
)
);
$this->assertSame('1', $customField->valueToLabel('1'));
}

public function testValueToLabelIfOptionNotFound(): void
{
$customField = new CustomField();
Expand Down

0 comments on commit 367cd2e

Please sign in to comment.