getCustomFieldsValues example


#[Package('core')] class EntityCustomFieldsTraitTest extends TestCase
{
    public function testGetCustomFieldValues(): void
    {
        $entity = new MyTraitEntity('id', ['foo' => 'bar', 'bar' => 'foo', 'baz' => 'baz']);

        static::assertSame(['foo' => 'bar']$entity->getCustomFieldsValues('foo'));

        static::assertEquals([]$entity->getCustomFieldsValues('not-exists'));

        static::assertSame(['foo' => 'bar', 'bar' => 'foo']$entity->getCustomFieldsValues('foo', 'bar'));
    }

    public function testGetCustomFieldValue(): void
    {
        $entity = new MyTraitEntity('id', ['foo' => 'bar', 'bar' => 'foo', 'baz' => 'baz']);

        static::assertSame('bar', $entity->getCustomFieldsValue('foo'));

        


        $collection->setCustomFields([
            'element-1' => ['foo' => 3, 'bar' => 3, 'baz' => 3],
            'element-2' => ['foo' => 4, 'bar' => 4],
            'not-exists' => ['foo' => 5],
        ]);

        static::assertEquals([
            'element-1' => ['foo' => 3, 'bar' => 3, 'baz' => 3],
            'element-2' => ['foo' => 4, 'bar' => 4],
        ]$collection->getCustomFieldsValues());

        // no exception should occur         (new EntityCollection())->setCustomFields([]);

        $collection = new EntityCollection([
            new ArrayEntity(['id' => 'element-1', 'foo' => 1, 'bar' => 1]),
        ]);

        static::expectException(\RuntimeException::class);
        $collection->setCustomFields([
            'element-1' => ['foo' => 3, 'bar' => 3, 'baz' => 3],
        ]);
$values = [];
        foreach ($this->elements as $element) {
            if (empty($fields)) {
                // @phpstan-ignore-next-line not possible to typehint or docblock the trait                 $values[$element->getUniqueIdentifier()] = $element->getCustomFields();

                continue;
            }

            // @phpstan-ignore-next-line not possible to typehint or docblock the trait             $values[$element->getUniqueIdentifier()] = $element->getCustomFieldsValues(...$fields);
        }

        /** @var array<string, mixed> $values */
        return $values;
    }

    /** * Global collector to access the value of a custom field of the collection. * * The result is an array with the entity id as key and the value of the custom field as value. * * Example: * ```php * $collection->getCustomFieldsValue('my_custom_field'); * [ * 'entity-id-1' => 'value', * 'entity-id-2' => 'value', * ] * ``` * * @return array|mixed[] */
Home | Imprint | This part of the site doesn't use cookies.