PHP 8.2.30
Preview: ArrayToPartsTransformer.php Size: 2.26 KB
/opt/cloudlinux/alt-php53/root/usr/share/pear/Symfony/Component/Form/Extension/Core/DataTransformer/ArrayToPartsTransformer.php

<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Form\Extension\Core\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

/**
 * @author Bernhard Schussek <bschussek@gmail.com>
 */
class ArrayToPartsTransformer implements DataTransformerInterface
{
    private $partMapping;

    public function __construct(array $partMapping)
    {
        $this->partMapping = $partMapping;
    }

    public function transform($array)
    {
        if (null === $array) {
            $array = array();
        }

        if (!is_array($array) ) {
            throw new TransformationFailedException('Expected an array.');
        }

        $result = array();

        foreach ($this->partMapping as $partKey => $originalKeys) {
            if (empty($array)) {
                $result[$partKey] = null;
            } else {
                $result[$partKey] = array_intersect_key($array, array_flip($originalKeys));
            }
        }

        return $result;
    }

    public function reverseTransform($array)
    {
        if (!is_array($array) ) {
            throw new TransformationFailedException('Expected an array.');
        }

        $result = array();
        $emptyKeys = array();

        foreach ($this->partMapping as $partKey => $originalKeys) {
            if (!empty($array[$partKey])) {
                foreach ($originalKeys as $originalKey) {
                    if (isset($array[$partKey][$originalKey])) {
                        $result[$originalKey] = $array[$partKey][$originalKey];
                    }
                }
            } else {
                $emptyKeys[] = $partKey;
            }
        }

        if (count($emptyKeys) > 0) {
            if (count($emptyKeys) === count($this->partMapping)) {
                // All parts empty
                return null;
            }

            throw new TransformationFailedException(
                sprintf('The keys "%s" should not be empty', implode('", "', $emptyKeys)
            ));
        }

        return $result;
    }
}

Directory Contents

Dirs: 0 × Files: 18

Name Size Perms Modified Actions
2.26 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download
2.22 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download
1.99 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download
3.55 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download
2.10 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download
3.79 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download
1.67 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download
2.39 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download
6.51 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download
5.53 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download
2.27 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download
8.13 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download
2.65 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download
1.26 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download
2.32 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download
8.89 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download
4.10 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download
2.26 KB lrw-r--r-- 2019-12-18 11:20:41
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).