Returns formatted data

@param *array $data
@param Fieldset $fieldset

@return array
551706
public function formatData(array $data, $fieldset = null)
    {
        $schema = $this->schema;

        if (!is_null($fieldset)) {
            $schema = $fieldset;
        }

        $fields = $schema->getFields();
        $table = $schema->getName();

        foreach ($fields as $field) {
            $name = $table . '_' . $field['name'];
            //if there's no data
            if (!isset($data[$name])) {
                //no need to format
                continue;
            }

            if ($data[$name] === 'NOW()') {
                $data[$name] = date('Y-m-d H:i:s');
            }

            if ($field['field']['type'] === 'latlng' && is_array($data[$name])) {
                if (!is_numeric($data[$name][0])) {
                    $data[$name][0] = 0;
                }

                if (!is_numeric($data[$name][1])) {
                    $data[$name][1] = 0;
                }

                $data[$name][0] = sprintf('%.8F', $data[$name][0]);
                $data[$name][1] = sprintf('%.8F', $data[$name][1]);
            }

            if ($field['field']['type'] === 'fieldset') {
                //case for multiple
                if (isset($field['field']['attributes']['data-multiple'])
                    && !$field['field']['attributes']['data-multiple']
                ) {
                    //format single data
                    $data[$name] = $this->formatData(
                        $data[$name],
                        Fieldset::i($field['field']['parameters']),
                        false
                    );
                } else {
                    //format each data
                    foreach($data[$name] as $index => $row) {

                        $data[$name][$index] = $this->formatData(
                            $row,
                            Fieldset::i($field['field']['parameters']),
                            false
                        );
                    }
                }
            }

            switch ($field['field']['type']) {
                case 'file':
                case 'image':
                    //upload files
                    $data[$name] = $this->upload($data[$name]);
                    break;
                case 'filelist':
                case 'imagelist':
                    //upload files
                    $data[$name] = $this->upload($data[$name]);

                    //we should not encode if fieldset
                    if (is_null($fieldset)) {
                        $data[$name] = json_encode($data[$name]);
                    }
                    break;
                case 'tag':
                case 'textlist':
                case 'textarealist':
                case 'wysiwyglist':
                case 'meta':
                case 'checkboxes':
                case 'multirange':
                case 'multiselect':
                case 'table':
                case 'latlng':
                case 'fieldset':
                    //if it's an array already
                    if((is_array($data[$name]) || is_object($data[$name])) && is_null($fieldset)) {
                        $data[$name] = json_encode($data[$name]);

                        //if it's a json string
                        if(strpos($data[$name], '{') === 0
                            || strpos($data[$name], '[') === 0
                        )
                        {
                            break;
                        }

                        //it can only be comma separated
                        $data[$name] = explode(',', $data[$name]);
                        $data[$name] = json_encode($data[$name]);
                    }

                    break;
                case 'created':
                case 'updated':
                case 'datetime':
                    if (trim($data[$name])) {
                        $data[$name] = date('Y-m-d H:i:s', strtotime($data[$name]));
                    } else {
                        $data[$name] = null;
                    }

                    break;
                case 'date':
                    if (trim($data[$name])) {
                        $data[$name] = date('Y-m-d', strtotime($data[$name]));
                    } else {
                        $data[$name] = null;
                    }

                    break;
                case 'time':
                    if (trim($data[$name])) {
                        $data[$name] = date('H:i:s', strtotime($data[$name]));
                    } else {
                        $data[$name] = null;
                    }

                    break;
                case 'password':
                    $data[$name] = password_hash($data[$name], PASSWORD_DEFAULT);
                    break;
                case 'md5':
                    $data[$name] = md5($data[$name]);
                    break;
                case 'sha1':
                    $data[$name] = sha1($data[$name]);
                    break;
                case 'active':
                case 'checkbox':
                    if ($data[$name]) {
                        $data[$name] = 1;
                    } else {
                        $data[$name] = 0;
                    }
                    break;
                case 'uuid':
                case 'token':
                    $data[$name] = md5(uniqid());
                    break;
                case 'number':
                case 'small':
                    if (!$data[$name]) {
                        $data[$name] = null;
                    }
                    break;
                case 'textarea':
                case 'wysiwyg':
                case 'code':
                    //fix for textarea in textarea
                    $data[$name] = str_replace('<\/textarea>', '</textarea>', $data[$name]);
                    break;
            }
        }

        return $data;
    }
538914
public function withData(array $data, array $fields = [])
    {
        $clone = clone $this;
        $clone->_fields = !empty($fields) ? \array_intersect_key($data, \array_flip($fields)) : $data;
        $clone->_errors = [];

        return $clone;
    }
729481
public function transform($data)
    {
        if ($data instanceof FormBuilder) {
            $response = $data->getRawResponse();

            foreach ($response['fieldsets'] as $fieldset) {
                $this->transformFieldset($fieldset);
            }

            return [
                'form' => [
                    'name'      => array_get($response, 'name'),
                    'rules'     => array_get($response, 'rules', []),
                    'fieldsets' => $this->fieldsets,
                ],
            ];
        }

        return parent::transform($data);
    }
1039359
public function getTemplateData($data = [])
    {
        // generate fields
        $fillableColumns = $this->schemaParser->getFillableFields($data['TABLE_NAME']);
        logger($fillableColumns);

        $faker = Factory::create();

        $fieldStr = [];
        foreach ($fillableColumns as $column) {
            try {
                if ($column['field'] === 'password') {
                    $func = 'bcrypt(str_random(10))';
                } else {
                    $faker->getFormatter($column['field']);

                    $func = '$faker->'.$column['field'];
                }
            } catch (\Exception $e) {
                logger($column['type']);
                switch ($column['type']) {
                    case 'tinyInteger':
                        $func = '$faker->randomNumber(2)';
                        break;
                    case 'smallInteger':
                        $func = '$faker->randomNumber(2)';
                        break;
                    case 'mediumInteger':
                        $func = '$faker->randomNumber(4)';
                        break;
                    case 'integer':
                        $func = '$faker->randomNumber(8)';
                        break;
                    case 'char':
                    case 'string':
                        logger($column['field']);
                        if (isset($column['args']) && !empty($column['args'])) {
                            $max = $column['args'];
                        } else {
                            $max = 100;
                        }
                        $func = "str_random($max)";
                        break;
                    case 'text':
                    case 'boolean':
                        $func = '$faker->'.$column['type'].'()';
                        break;
                    case 'date':
                        $func = '$faker->'.'dateTimeBetween()';
                        break;
                    default:
                        $func = '';
                }
            }

            if (!empty($func)) {
                $fieldStr[] = "'".$column['field']."' => ".$func;
            }
            
        }
        $data['FIELDS'] = implode(",\n\t\t", $fieldStr);

        return $data;
    }
931550
public function data(array $data, array $fields = [])
    {
        // Allows filtering of used input fields against optional second array of field names allowed
        // This is useful for limiting raw $_POST or $_GET data to only known fields
        $this->_fields = $fields !== [] ? \array_intersect_key($data, \array_flip($fields)) : $data;

        return $this;
    }
698318
private function toArrayData(array $data){
		foreach ($data as $key => &$val){

			if (is_object($val)){
				if ($val instanceof self){
					$val = $val->toArray();
				}
				elseif ($val instanceof DateTimeInterface) {
					$val = $val->format(self::DATE_FORMAT);
				}
				else {
					$val = (array) $val;
				}
			}

			if (is_array($val)){
				$val = $this->toArrayData($val);
			}
		}

		return $data;
	}
1204750
protected function filterFieldsets(array $data)
    {
        if (!$this->hasFilterFieldset()) {
            return $data;
        }
        $serializer = $this->manager->getSerializer();
        $requestedFieldset = iterator_to_array($this->getFilterFieldset());
        //Build the array of requested fieldsets with the mandatory serializer fields
        $filterFieldset = array_flip(
            array_merge(
                $serializer->getMandatoryFields(),
                $requestedFieldset
            )
        );
        return array_intersect_key($data, $filterFieldset);
    }
196259
private function getDataFromFields(DataProviderInterface $provider, string $fieldname, array $element, array $data)
    {
        $getMethod = 'get' . $fieldname;
        if (strpos($element['type'], 'DataProviderInterface[]') !== false) {
            $data = $this->getDataProviderCollectionData($provider, $fieldname, $data, $getMethod);
        }
        elseif (
            strpos($element['type'], 'DataProviderInterface') !== false
            && $provider->{$getMethod}() instanceof DataProviderInterface
        ) {
            $anyDataProvider = new AnyDataProvider($provider->{$getMethod}());
            $data[$fieldname] = $anyDataProvider->toArray();
        }
        elseif ($element['is_dataprovider'] && $provider->{$getMethod}() instanceof DataProviderInterface) {
            $data[$fieldname] = $this->convertToArray($provider->{$getMethod}());
        }
        elseif ($element['is_collection']) {
            $data = $this->getCollectionData($provider, $fieldname, $data, $getMethod);
        }
        else {
            $data[$fieldname] = $provider->{$getMethod}();
        }
        return $data;
    }
444731
protected function retrieveParams(array $data)/*# : array */
    {
        $params = [];
        foreach ($data as $i => $val) {
            if (0 === $i % 2) {
                $params[$val] = $data[$i + 1];
            }
        }
        return $params;
    }
589381
public static function completeFields($data) {
		foreach( static::$fields as $fieldname ) {
			if( !isset($data[$fieldname]) ) {
				$data[$fieldname] = '';
			}
		}
		return $data;
	}
738920
protected function renderElements(FieldsetInterface $fieldset)
    {
        $markup = '';
        $elementHelper = $this->getElementHelper();

        $elements = ArrayUtils::iteratorToArray($fieldset, false);
        foreach ($elements as $key => $elementOrFieldset) {
            if ($elementOrFieldset instanceof FieldsetInterface &&
                $fieldset instanceof Collection
            ) {
                $elementOrFieldset->setAttribute('data-counter', $key);
            	$elementOrFieldset->setOption(
            	    'allow_remove',
            	    $key >= $fieldset->getOption('count')
            	       ? $fieldset->allowRemove()
            	       : false
            	);
            }

            $markup .= $elementHelper($elementOrFieldset);
        }

        $this->reset($fieldset);

        return $markup;
    }
1129406
public function getUpdateErrors(array $data, array &$errors = [], $fieldset = null)
    {
        $schema = $this->schema;

        if (!is_null($fieldset)) {
            $schema = $fieldset;
        }

        $fields = $schema->getFields();
        $table = $schema->getName();
        $primary = $schema->getPrimaryFieldName();

        if ((!isset($data[$primary]) || !is_numeric($data[$primary])) && !is_null($primary)) {
            $errors[$primary] = 'Invalid ID';
        }

        foreach ($fields as $name => $field) {
            if ($field['field']['type'] === 'fieldset') {
                if (!isset($data[$name])) {
                    continue;
                }

                if (!isset($errors[$name])) {
                    $errors[$name] = [];
                }

                if (isset($field['field']['attributes']['data-multiple'])
                    && !$field['field']['attributes']['data-multiple']
                ) {
                    $errors[$name] = $this->getUpdateErrors(
                        $data[$name],
                        $errors[$name],
                        Fieldset::i($field['field']['parameters'])
                    );

                    if (empty($errors[$name])) {
                        unset($errors[$name]);
                    }

                    continue;
                }

                foreach($data[$name] as $index => $row) {
                    if (!isset($errors[$name][$index])) {
                        $errors[$name][$index] = [];
                    }

                    $errors[$name][$index] = $this->getUpdateErrors(
                        $row,
                        $errors[$name][$index],
                        Fieldset::i($field['field']['parameters'])
                    );

                    if (empty($errors[$name][$index])) {
                        unset($errors[$name][$index]);
                    }
                }

                if (empty($errors[$name])) {
                    unset($errors[$name]);
                }

                continue;
            }

            if (!isset($field['validation'])) {
                continue;
            }

            foreach ($field['validation'] as $validation) {
                if ($validation['method'] === 'required'
                    && array_key_exists($name, $data)
                    && empty($data[$name])
                ) {
                    $errors[$name] = $validation['message'];
                }
            }
        }

        return $this->getOptionalErrors($data, $errors, $fieldset);
    }
1312033
protected function parseSet($data, $options)
    {
        if (empty($data)) {
            return [];
        }
        unset($options);
        $result = [];
        foreach ($data as $key => $val) {
            $item = $this->parseKey($key);
            if (is_array($val) && isset($val[0]) && in_array($val[0], ['$inc', '$set', '$unset', '$push', '$pushall', '$addtoset', '$pop', '$pull', '$pullall'])) {
                $result[$val[0]][$item] = $this->parseValue($val[1], $key);
            } else {
                $result['$set'][$item] = $this->parseValue($val, $key);
            }
        }
        return $result;
    }
263841
public function field_format_value($fields, $data) 
    {
        if (!$fields || !$data || !is_array($data)) {
            return $data;
        }
        foreach ($fields as $field) {
            $obj = $this->get($field['fieldtype']);
            if (!$obj) {
                continue;
            }
            if ($field['fieldtype'] === 'Group') {
                continue;
            }
            $data = $obj->output_data($data, $field);
        }
        return $data;
    }
440889
private function getCollectionData(
        DataProviderInterface $provider,
        string $fieldname,
        array $data,
        string $getMethod
    ): array {
        $data[$fieldname] = [];
        foreach ($provider->{$getMethod}() as $child) {
            $data[$fieldname][] = $child->toArray();
        }
        return $data;
    }
398052
public function getValues($fieldset = null, $group = null)
    {
        $data = [];

        foreach ($this->getFields($fieldset, $group) as $name => $field) {
            FormHelper::setByPath($data, $name, $field->getRawValue());
        }

        return $data;
    }
706167
private function generateAttachmentFields(array $data): array
    {
        $fields = array();
        foreach ($this->normalizerFormatter->format($data) as $key => $value) {
            $fields[] = $this->generateAttachmentField($key, $value);
        }

        return $fields;
    }