getMappedKey example

$mappingAsdf = $mappingCollection->get('asdf');
        static::assertInstanceOf(Mapping::class$mappingAsdf);
        static::assertSame('asdf', $mappingAsdf->getKey());

        $idMapping = $mappingCollection->get('id');
        static::assertInstanceOf(Mapping::class$idMapping);
        static::assertSame('id', $idMapping->getKey());
        static::assertNull($idMapping->getDefaultValue());

        $idMappedKey = $mappingCollection->getMapped('id');
        static::assertInstanceOf(Mapping::class$idMappedKey);
        static::assertSame('id', $idMappedKey->getMappedKey());
    }

    public function testInvalidMappingThrows(): void
    {
        $mappingFoo = ['mappedKey' => 'bar'];

        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('key is required in mapping');

        /** @phpstan-ignore-next-line intentionally wrong array provided */
        MappingCollection::fromIterable([$mappingFoo]);
    }
return;
        }

        $testCase = 'test case data: ' . var_export($data, true);
        static::assertSame(is_countable($data['expectedMappings']) ? \count($data['expectedMappings']) : 0, $guessedMapping->count()$testCase);

        foreach ($data['expectedMappings'] as $mappedKey => $key) {
            $mapping = $guessedMapping->getMapped($mappedKey);
            static::assertNotNull($mapping);

            static::assertSame($mappedKey$mapping->getMappedKey()$testCase);
            static::assertSame($key$mapping->getKey()$testCase);
        }

        if (file_exists($filePath)) {
            unlink($filePath);
        }
    }

    public function testSortingWorksAsExpected(): void
    {
        /** @var string $filePath */
        
$mappings = $profile->getMapping();
        if (empty($mappings)) {
            throw new \RuntimeException('ImportExportProfile "' . $profileId . '" has no mappings');
        }

        $config = new Config($mappings[][]);
        $headers = [];
        $mappings = MappingCollection::fromIterable($mappings)->sortByPosition();

        /** @var Mapping $mapping */
        foreach ($mappings as $mapping) {
            $headers[$mapping->getMappedKey()] = '';
        }

        // create the file         $expireDate = new \DateTimeImmutable();
        $expireDate = $expireDate->modify('+1 hour');
        $fileEntity = $this->fileService->storeFile(
            $context,
            $expireDate,
            null,
            $profile->getSourceEntity() . ':' . $profile->getName() . '.csv',
            ImportExportLogEntity::ACTIVITY_TEMPLATE
        );
public function set($key$mapping): void
    {
        $this->validateType($mapping);
        $mappingKey = $mapping->getKey();
        if (empty($mappingKey)) {
            // prevent collision with multiple not mapped mappings (key = '').             // there is no direct lookup needed for these, but they should be stored and not overridden!             $mappingKey = Uuid::randomHex();
        }

        parent::set($mappingKey$mapping);
        $this->reverseIndex[$mapping->getMappedKey()] = $mappingKey;
    }

    public function getMapped(string $readKey): ?Mapping
    {
        if (!\array_key_exists($readKey$this->reverseIndex)) {
            return null;
        }

        $writeKey = $this->reverseIndex[$readKey];

        return $this->get($writeKey);
    }

    }

    private function resolvePrimaryKey(Config $config, EntityDefinition $definition, iterable $record): iterable
    {
        $updatedBy = $config->getUpdateBy()->get($definition->getEntityName());

        if (!$updatedBy) {
            return $record;
        }

        $updateByField = $updatedBy->getMappedKey();

        if (empty($updateByField) || $definition->getField($updateByField) instanceof IdField) {
            return $record;
        }

        $idFields = $definition->getPrimaryKeys()->filter(fn (Field $field) => $field instanceof IdField);
        $idField = $idFields->first();

        if ($idFields->count() !== 1 || !$idField) {
            return $record;
        }

        
return $data;
    }

    /** * @param array<string, mixed> $row */
    private function addUserDefaults(array &$row, Config $config): void
    {
        $mappings = $config->getMapping()->getElements();

        foreach ($mappings as $mapping) {
            $csvKey = $mapping->getMappedKey();

            if (!$mapping->isUseDefaultValue()) {
                continue;
            }

            if (!\array_key_exists($csvKey$row) || empty($row[$csvKey])) {
                $row[$csvKey] = $mapping->getDefaultValue();
            }
        }
    }

    
$flat = ArrayNormalizer::flatten($record);

        $mapped = [];
        foreach ($flat as $key => $value) {
            $key = str_replace('extensions.', '', $key);
            $mapping = $this->mapping->get($key);
            if ($mapping === null) {
                continue;
            }

            $newKey = $mapping->getMappedKey();

            $mapped[$newKey] = $value;
        }

        foreach ($this->mapping as $m) {
            $sorted[$m->getMappedKey()] = $mapped[$m->getMappedKey()] ?? '';
        }

        if (empty($sorted)) {
            return;
        }

        
Home | Imprint | This part of the site doesn't use cookies.