Bolt\Storage\Field\Type\RepeaterType::hydrate PHP Method

hydrate() public method

public hydrate ( $data, $entity )
    public function hydrate($data, $entity)
    {
        $key = $this->mapping['fieldname'];
        if (isset($data[$key]) && $this->isJson($data[$key])) {
            $originalMapping[$key]['fields'] = $this->mapping['fields'];
            $originalMapping[$key]['type'] = 'repeater';
            $mapping = $this->em->getMapper()->getRepeaterMapping($originalMapping);
            $decoded = json_decode($data[$key], true);
            $collection = new RepeatingFieldCollection($this->em, $mapping);
            $collection->setName($key);
            if (isset($decoded) && count($decoded)) {
                foreach ($decoded as $group => $repdata) {
                    $collection->addFromArray($repdata, $group);
                }
            }
            $this->set($entity, $collection);
            return;
        }
        $vals = array_filter(explode(',', $data[$key]));
        $values = [];
        foreach ($vals as $fieldKey) {
            $split = explode('_', $fieldKey);
            $id = array_pop($split);
            $group = array_pop($split);
            $field = join('_', $split);
            $values[$field][$group][] = $id;
        }
        $collection = new RepeatingFieldCollection($this->em, $this->mapping);
        $collection->setName($key);
        if (isset($values[$key]) && count($values[$key])) {
            foreach ($values[$key] as $group => $refs) {
                $collection->addFromReferences($refs, $group);
            }
        }
        $this->set($entity, $collection);
    }