getCustomFieldsValue example



        static::expectException(\RuntimeException::class);
        $collection->setCustomFields([
            'element-1' => ['foo' => 3, 'bar' => 3, 'baz' => 3],
        ]);
    }

    public function testGetCustomFieldsValue(): void
    {
        // no exception should occur         (new EntityCollection())->getCustomFieldsValue('foo');

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

        static::assertEquals(
            [
                'element-1' => 1,
                'element-2' => 2,
            ],
            
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'));

        static::assertNull($entity->getCustomFieldsValue('not-exists'));
    }

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

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

        static::assertNull($entity->getCustomFieldsValue('not-exists'));

        

    public function getCustomFieldsValue(string $field): array
    {
        if (!$this->hasCustomFieldSupport(__METHOD__)) {
            return [];
        }

        $values = [];
        foreach ($this->elements as $element) {
            // @phpstan-ignore-next-line not possible to typehint or docblock the trait             $values[$element->getUniqueIdentifier()] = $element->getCustomFieldsValue($field);
        }

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

    /** * Sets the custom fields for all entities in the collection. * * The passed array must have the entity id as key and an array of custom fields as value. * * Example: * ```php * $collection->setCustomFields([ * 'entity-id-1' => [ * 'my_custom_field' => 'value', * 'my_other_custom_field' => 'value', * ], * 'entity-id-2' => [ * 'my_custom_field' => 'value', * 'my_other_custom_field' => 'value', * ] * ]); * ``` * * @param array<string, array<string, mixed>> $values */
Home | Imprint | This part of the site doesn't use cookies.