<?php

namespace OfficeBrain\Bundle\SearchBundle\Form\Transformer;

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

/**
 * Serialize text to solr supported attribute text;
 *
 * @author OfficeBrain 4220 <info@officebrain.com>
 *
 */
class SerializeToAttributeTextArray implements DataTransformerInterface
{
    /**
     * Transforms an object (issue) to a string (number).
     *
     * @param array $attributeData attribute data
     *
     * @return string
     */
    public function transform($attributeData)
    {
        if (null === $attributeData) {
            return null;
        }

        return json_encode($attributeData);
    }

    /**
     * Transforms a string (number) to an object (issue).
     *
     * @param string $serializeString
     *
     * @return array|null
     *
     * @throws TransformationFailedException if object (issue) is not found.
     */
    public function reverseTransform($serializeString)
    {
        if ($serializeString === null) {
            return null;
        }
        $tmpData = json_decode($serializeString);

        if (!$tmpData) {
            throw new TransformationFailedException(sprintf('Please provide a valid serialization string!'));
        }
        $returnArray = array();
//         foreach ($tmpData as $attributeTypeId => $attributeValues) {
//         	$attributeValuesArray = explode('-',$attributeValues);
//             foreach ($attributeValuesArray as $attributeValueId) {
//                 $returnArray[] = $attributeTypeId.'|'.$attributeValueId;
//             }
//         }
        foreach ($tmpData as $attributeTypeId => $attributeValues) {
        	$returnArray[] = str_replace('_','|',$attributeValues);
        }
        return $returnArray;
    }
}
