PHP code example of burgov / key-value-form-bundle

1. Go to this page and download the library: Download burgov/key-value-form-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

burgov / key-value-form-bundle example snippets



// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Burgov\Bundle\KeyValueFormBundle\BurgovKeyValueFormBundle(),
        // ...
    );
}

use Burgov\Bundle\KeyValueFormBundle\Form\Type\KeyValueType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

$builder->add('parameters', KeyValueType::class, array('value_type' => TextType::class));

// or

$formFactory->create(KeyValueType::class, $data, array('value_type' => TextType::class));

class Model
{
    public function addOption($key, $value)
    {
        $this->options[$key] = $value;
    }
}

/** @var $builder Symfony\Component\Form\FormBuilderInterface */
$builder->add('options', 'burgov_key_value', array(
    '

class Model
{
    /**
     * Set the options.
     *
     * @param array|KeyValueContainer|\Traversable $data Something that can be converted to an array.
     */
    public function setOptions($options)
    {
        $this->options = $this->convertToArray($options);
    }

    /**
     * Extract an array out of $data or throw an exception if not possible.
     *
     * @param array|KeyValueContainer|\Traversable $data Something that can be converted to an array.
     *
     * @return array Native array representation of $data
     *
     * @throws InvalidArgumentException If $data can not be converted to an array.
     */
    private function convertToArray($data)
    {
        if (is_array($data)) {
            return $data;
        }

        if ($data instanceof KeyValueContainer) {
            return $data->toArray();
        }

        if ($data instanceof \Traversable) {
            return iterator_to_array($data);
        }

        throw new InvalidArgumentException(sprintf('Expected array, Traversable or KeyValueContainer, got "%s"', is_object($data) ? getclass($data) : get_type($data)));
    }
}