-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
181 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Elbformat\FieldHelperBundle\FieldHelper; | ||
|
||
use Elbformat\FieldHelperBundle\Exception\FieldNotFoundException; | ||
use Elbformat\FieldHelperBundle\Exception\InvalidFieldTypeException; | ||
use eZ\Publish\API\Repository\Values\Content\Content; | ||
use eZ\Publish\Core\FieldType\Selection\Value as SelectionValue; | ||
|
||
class SelectionFieldHelper extends AbstractFieldHelper | ||
{ | ||
public function getValue(Content $content, string $fieldName): ?int | ||
{ | ||
$selections = $this->getMultipleValues($content, $fieldName); | ||
|
||
$firstOption = reset($selections); | ||
if (false === $firstOption) { | ||
return null; | ||
} | ||
|
||
return $firstOption; | ||
} | ||
|
||
/** | ||
* @return int[] | ||
*/ | ||
public function getMultipleValues(Content $content, string $fieldName): array | ||
{ | ||
$field = $this->getField($content, $fieldName); | ||
|
||
if (!($field->value instanceof SelectionValue)) { | ||
throw InvalidFieldTypeException::fromActualAndExpected($field->value, [SelectionValue::class]); | ||
} | ||
|
||
$selections = $field->value->selection; | ||
|
||
if (empty($selections)) { | ||
return []; | ||
} | ||
|
||
return $selections; | ||
} | ||
|
||
public function getSelectionName(Content $content, string $fieldName): ?string | ||
{ | ||
$index = $this->getValue($content, $fieldName); | ||
if (null === $index) { | ||
return null; | ||
} | ||
$fieldDef = $content->getContentType()->getFieldDefinition($fieldName); | ||
/** @var array{options:string[]} $settings */ | ||
$settings = $fieldDef?->getFieldSettings() ?? []; | ||
|
||
return $settings['options'][$index] ?? null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Elbformat\FieldHelperBundle\Tests\FieldHelper; | ||
|
||
use Elbformat\FieldHelperBundle\Exception\InvalidFieldTypeException; | ||
use Elbformat\FieldHelperBundle\FieldHelper\FileFieldHelper; | ||
use Elbformat\FieldHelperBundle\FieldHelper\SelectionFieldHelper; | ||
use eZ\Publish\API\Repository\Values\Content\Field; | ||
use eZ\Publish\API\Repository\Values\ContentType\ContentType; | ||
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition; | ||
use eZ\Publish\Core\FieldType\Float\Value as FloatValue; | ||
use eZ\Publish\Core\FieldType\Selection\Value; | ||
use eZ\Publish\Core\Repository\Values\Content\Content; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SelectionFieldHelperTest extends TestCase | ||
{ | ||
public function testGetValue(): void | ||
{ | ||
$content = $this->createContentFromValue(['lorem', 'ipsum'], [1]); | ||
|
||
$fh = new SelectionFieldHelper(); | ||
$this->assertSame(1, $fh->getValue($content, 'select_field')); | ||
} | ||
|
||
public function testGetValueFirst(): void | ||
{ | ||
$content = $this->createContentFromValue(['lorem', 'ipsum'], [0,1]); | ||
|
||
$fh = new SelectionFieldHelper(); | ||
$this->assertSame(0, $fh->getValue($content, 'select_field')); | ||
} | ||
|
||
public function testGetValueNull(): void | ||
{ | ||
$content = $this->createContentFromValue(['lorem', 'ipsum'], []); | ||
|
||
$fh = new SelectionFieldHelper(); | ||
$this->assertNull($fh->getValue($content, 'select_field')); | ||
} | ||
|
||
public function testGetMultipleValues(): void | ||
{ | ||
$content = $this->createContentFromValue(['lorem', 'ipsum'], [0,1]); | ||
|
||
$fh = new SelectionFieldHelper(); | ||
$this->assertSame([0,1], $fh->getMultipleValues($content, 'select_field')); | ||
} | ||
|
||
public function testGetMultipleValuesEmpty(): void | ||
{ | ||
$content = $this->createContentFromValue(['lorem', 'ipsum'], []); | ||
|
||
$fh = new SelectionFieldHelper(); | ||
$this->assertSame([], $fh->getMultipleValues($content, 'select_field')); | ||
} | ||
|
||
public function testSelectionName(): void | ||
{ | ||
$content = $this->createContentFromValue(['lorem', 'ipsum'], [1]); | ||
|
||
$fh = new SelectionFieldHelper(); | ||
$this->assertSame('ipsum', $fh->getSelectionName($content, 'select_field')); | ||
} | ||
|
||
public function testSelectionNameEmpty(): void | ||
{ | ||
$content = $this->createContentFromValue(['lorem', 'ipsum'], []); | ||
|
||
$fh = new SelectionFieldHelper(); | ||
$this->assertNull($fh->getSelectionName($content, 'select_field')); | ||
} | ||
|
||
public function testInvalidFieldType(): void | ||
{ | ||
$this->expectException(InvalidFieldTypeException::class); | ||
$this->expectExceptionMessageMatches('/Expected field type .*Selection\\\\Value/'); | ||
$this->expectExceptionMessageMatches('/but got .*Float\\\\Value/'); | ||
$fh = new SelectionFieldHelper(); | ||
$field = new Field(['value' => new FloatValue(1.0)]); | ||
$content = $this->createMock(Content::class); | ||
$content->method('getField')->with('select_field')->willReturn($field); | ||
$fh->getSelectionName($content, 'select_field'); | ||
} | ||
|
||
protected function createContentFromValue(array $options, ?array $selected): Content | ||
{ | ||
$field = new Field(['value' => new Value($selected)]); | ||
|
||
$content = $this->createMock(Content::class); | ||
$content->method('getField')->with('select_field')->willReturn($field); | ||
$contentType = $this->createMock(ContentType::class); | ||
$fieldDef = $this->createMock(FieldDefinition::class); | ||
$fieldSettings = ['options' => $options]; | ||
$fieldDef->method('getFieldSettings')->willReturn($fieldSettings); | ||
$contentType->method('getFieldDefinition')->willReturn($fieldDef); | ||
$content->method('getContentType')->willReturn($contentType); | ||
|
||
return $content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters