format DB data into standard format

@param array $data
@param array $options

@throws NormalisationException
@return array
162154
public function denormalise(array $data, array $options = [])
    {
        if (empty($data)) {
            return $data;
        }

        if (!isset($options["entityClass"])) {
            throw new NormalisationException("Cannot denormalise data without knowing the main entity class");
        }
        if (!isset($options["metadataProvider"])) {
            throw new NormalisationException("Cannot denormalise data without a metadata provider");
        }

        /** @var EntityMetadataProviderInterface $metadataProvider */
        $this->metadataProvider = $options["metadataProvider"];
        $metadata = $this->metadataProvider->getEntityMetadata($options["entityClass"]);

        // split fields based on prefix
        $prefixedFields = [];
        foreach ($data[0] as $field => $value) {
            $fieldParts = explode("__", $field);
            // if this field has no prefix, set it to an empty string
            if (count($fieldParts) == 1) {
                array_unshift($fieldParts, "");
            }
            // add the field to the prefix's array
            $prefix = $fieldParts[0];
            if (empty($prefixedFields[$prefix])) {
                $prefixedFields[$prefix] = [];
            }
            $prefixedFields[$prefix][$fieldParts[1]] = $field;
        }

        // handle case where only 1 prefix is present
        if (count($prefixedFields) == 1) {
            $fields = array_pop($prefixedFields);
            $primaryKey = $metadata->getPrimaryKey();
            if (isset($fields[$primaryKey])) {
                $this->primaryKeyFields = [$fields[$primaryKey] => true];
            }
            return $this->denormaliseData($data, $fields);
        }

        // complex result set
        if (!isset($options["entityMap"])) {
            throw new NormalisationException("Cannot denormalise data without an entity map");
        }

        $entityMap = $options["entityMap"];

        $collection = $metadata->getCollection();

        if (empty($prefixedFields[$collection])) {
            throw new NormalisationException("The collection '$collection' was not found in the fields array for this record set: '" . implode("', '", array_keys($prefixedFields)) . "'");
        }

        // reset primary key fields
        $this->primaryKeyFields = [];
        $this->treedEntities = [];
        $this->childRowAliases = [];

        $this->createFieldTree($prefixedFields, $entityMap, $collection, $options["entityClass"]);

        reset($data);
        $result = $this->denormaliseData($data, $prefixedFields[$collection], $collection);

        return  $result;
    }
392169
protected function cleanData(array $data): array
    {
        $options = $this->getOptions();

        $disallowedDataKeys = array(
            $options->getIdColumnName(),
            $options->getLeftColumnName(),
            $options->getRightColumnName(),
            $options->getLevelColumnName(),
            $options->getParentIdColumnName(),
        );

        if (null !== $options->getScopeColumnName()) {
            $disallowedDataKeys[] = $options->getScopeColumnName();
        }

        return array_diff_key($data, array_flip($disallowedDataKeys));
    }
482339
protected function createFromArrayWithContext($data, $options)
    {
        $_documentClass = $this->_documentClass;

        return $_documentClass::createFromArray($data, $options);
    }
1103784
public function transform($data = [], $options = [])
    {
        if (empty($data)) {
            $data = $this->all();
        }

        // Table header
        $head = [
            'columns' => [
                "",
                //trans('back-project::users.active'),
                trans('back-project::users.username'),
                trans('back-project::users.email'),
                trans('back-project::users.roles'),
                trans('back-project::crud.actions'),
            ]
        ];

        $body = [];

        foreach ($data as $row):
            $body[] = [
                'columns' => [
                    ['content' => '<img src="'.\Avatar::create(strtoupper($row->username))->toBase64().'" alt="'.$row->username.'" width="40px" height="40px" /> '],
                    //['content' => false, 'action' => false, 'icon' => ($row->isConfirmed() ? "check" : 'times')],
                    ['content' => $row->username],
                    ['content' => $row->email],
                    ['content' => implode(',', $row->roles()->pluck('name')->toArray())],
                    ['content' => false, 'actions' => [
                        'edit' => ['url' => route('bp.users.edit', [$row['id']])], //url('/admin/users/edit', [$row['id']])],
                        'delete' => ['url' => route('bp.users.delete', [$row['id']])]
                    ]
                    ],
                ]
            ];
        endforeach;

        return [
            'head' => $head,
            'body' => $body
        ];
    }
1275242
public function encode($data, $options = 0) {
        $json = null;

        if ($data instanceof IArrayable) {
            $json = @json_encode($data->toArray(), $options);
        } else if ($data instanceof IJsonable) {
            $json = $data->toJson($options);
        } else if (is_array($data) || is_object($data)) {
            $json =  @json_encode($data, $options);
        } else {
            throw new InvalidDataTypeException(
                s('Can not convert data of type "%s" to json.', get_type($data))
            );
        }

        if ($json === false) {
            throw new JsonEncodeException(json_last_error_msg(), $data);
        }

        return $json;
    }
106213
protected function _prepareDataAndOptions($data, $options)
    {
        $options += ['validate' => true];

        if (!isset($options['fields']) && isset($options['fieldList'])) {
            deprecationWarning(
                'The `fieldList` option for marshalling is deprecated. Use the `fields` option instead.'
            );
            $options['fields'] = $options['fieldList'];
            unset($options['fieldList']);
        }

        $tableName = $this->_table->getAlias();
        if (isset($data[$tableName])) {
            $data += $data[$tableName];
            unset($data[$tableName]);
        }

        $data = new ArrayObject($data);
        $options = new ArrayObject($options);
        $this->_table->dispatchEvent('Model.beforeMarshal', compact('data', 'options'));

        return [(array)$data, (array)$options];
    }
731130
protected function normalizeArray(array $data)
    {
        $ret = [];

        foreach ($data as $key => $value) {
            if (!\is_string($key)) {
                continue;
            }

            $ret[$key[0]] = [
                'value' => $value,
                'pattern' => $key,
            ];
        }

        return $ret;
    }
147216
public function read($data, array $options = []) {
		$class = $options['class'];

		$encrypted = $class::read(null, ['strategies' => false]);
		$key = isset($options['key']) ? $options['key'] : null;

		if (!isset($encrypted['__encrypted']) || !$encrypted['__encrypted']) {
			return isset($encrypted[$key]) ? $encrypted[$key] : null;
		}

		$current = $this->_decrypt($encrypted['__encrypted']);

		if ($key) {
			return isset($current[$key]) ? $current[$key] : null;
		} else {
			return $current;
		}
	}
686101
public function processColumns(& $data, $options = []) {
		$save = [];
		// we need to determine columns that have overrides like ;;bytea
		$data_columns = [];
		foreach ($data as $k => $v) {
			if (strpos($k, ';') !== false) {
				$k2 = explode(';', $k)[0];
				if (!empty($this->columns[$k2])) {
					$data_columns[$k2] = $k;
				}
			}
		}
		// go through all columns
		foreach ($this->columns as $k => $v) {
			$original_key = $data_columns[$k] ?? $k;
			if (!empty($options['ignore_not_set_fields']) && !array_key_exists($original_key, $data)) {
				continue;
			}
			if (empty($options['skip_type_validation'])) {
				$temp = \Object\Table\Columns::processSingleColumnType($k, $v, $data[$original_key] ?? null);
				if (array_key_exists($k, $temp)) {
					$save[$original_key] = $temp[$k];
				}
			} else {
				$save[$original_key] = $data[$original_key];
			}
		}
		$data = $save;
	}
479694
public function make(array $data = null, array $options = null)
    {
        $data = $data ? $data : $this->getData();
        $this->setData($data);

        $options = $options ? $options : $this->getOptions();
        $options = array_replace_recursive($this->getDefaults(), $options);
        $this->setOptions($options);




        if ($this->isShowRowCheckbox()) {
            $this->addRowCheckbox();
        }

        if ($this->isShowRowIndex()) {
            $this->addRowIndex();
        }

        if ($this->isShowColSearch()) {
            $this->addColSearch();
        }

        $options = $this->getOptions();
        $data = $this->getData();

        $attributes = [];

        // Attributes for wrappers
        $attributes['table'] = $this->getAttributes('table', $options);
        $attributes['caption'] = $this->getAttributes('caption', $options);
        $attributes['colgroup'] = $this->getAttributes('colgroup', $options);
        $attributes['thead'] = $this->getAttributes('thead', $options);
        $attributes['tbody'] = $this->getAttributes('tbody', $options);
        $attributes['tfoot'] = $this->getAttributes('tfoot', $options);

        // caption
        $caption = isset($options['caption']['value']) ?
            $options['caption']['value'] : '';

        // colgroup data
        $cols = '';
            if (is_array($options['colgroup']) && $this->isShowColgroup()) {
            $colsData = isset($options['colgroup']['value']) ?
                $options['colgroup']['value'] : array_slice($data, 0, 1);
            $cols = $this->getColgroup($colsData, $options);
        }

        // thead data
        $thead_rows = '';
        if (is_array($options['thead']) && $this->isShowThead()) {
            $theadData = isset($options['thead']['value']) ?
                $options['thead']['value'] : array_slice($data, 0, 1);
            $theadData = is_callable($theadData) ? $theadData($data) : $theadData;
            $thead_rows = $this->getGrid($theadData, 'thead_tr', 'th',
                $options);
        }

        // tfoot data
        $tfoot_rows = '';
        if (is_array($options['tfoot']) && $this->isShowTfoot()) {
            $tfootData = isset($options['tfoot']['value']) ?
                $options['tfoot']['value'] : null;
            $tfootData = is_callable($tfootData) ? $tfootData($data) : $tfootData;
            $tfoot_rows = $this->getGrid($tfootData, 'tfoot_tr', 'tfoot_td',
                $options);
        }

        // tbody data
        $tbody_rows = $this->getGrid($data, 'tr', 'td', $options);

        // Render wrappers
        $caption = empty($caption) ?
            '' : sprintf($options['caption']['tag'], $attributes['caption'],
                $caption);

        $colgroup = empty($cols) ?
            '' : sprintf($options['colgroup']['tag'], $attributes['colgroup'],
                $cols);

        $thead = empty($thead_rows) ?
            '' : sprintf($options['thead']['tag'], $attributes['thead'],
                $thead_rows);

        $tbody = sprintf($options['tbody']['tag'], $attributes['tbody'],
            $tbody_rows);

        $tfoot = empty($tfoot_rows) ?
            '' : sprintf($options['tfoot']['tag'], $attributes['tfoot'],
                $tfoot_rows);

        $table = $caption .$colgroup . $thead . $tbody . $tfoot;

        return sprintf($options['table']['tag'], $attributes['table'], $table);
    }
249733
public function deserialize(array $data, Options $options = null)
    {
        if (empty($options)) {
            $options = $this->optionsRepo->findOneBy(['id' => $data['id']]);
        }
        if (empty($options)) {
            $options = new Options();
        }
        if (isset($data['user'])) {
            $user = isset($data['user']['id']) ? $this->userRepo->findOneBy(['uuid' => $data['user']['id']]) : null;
            $options->setUser($user);
        }
        if (isset($data['data'])) {
            $options->setOptions($data['data']);
        }

        return $options;
    }
176404
private function normalizeData($data, \stdClass $schema)
    {
        if (\is_array($data)) {
            return $this->normalizeArray(
                $data,
                $schema
            );
        }

        if ($data instanceof \stdClass) {
            return $this->normalizeObject(
                $data,
                $schema
            );
        }

        return $data;
    }
1148135
protected function formatData(array $data) : string {
    $data_string = '';

    foreach ($data as $column => $value) {
      $data_string .= "`{$column}` = :{$column}";

      if (next($data) !== false) {
        $data_string .= ', ';
      }
    }

    return $data_string;
  }
1008245
protected function _normalizeData($data)
    {
        $data += [
            'name' => '',
            'empty' => false,
            'disabled' => null,
            'val' => null,
            'year' => [],
            'month' => [],
            'day' => [],
            'hour' => [],
            'minute' => [],
            'second' => [],
            'meridian' => null,
            'templateVars' => [],
        ];

        $timeFormat = isset($data['hour']['format']) ? $data['hour']['format'] : null;
        if ($timeFormat === 12 && !isset($data['meridian'])) {
            $data['meridian'] = [];
        }
        if ($timeFormat === 24) {
            $data['meridian'] = false;
        }

        return $data;
    }
1087463
private function normalizeArrayForNewLines(array $data) {
		foreach($data as $k => $item) {
			if(is_string($item)) {
				$data[$k] = str_replace(["\r\n", "\n\r", "\r"], "\n", $item);
			} elseif(is_array($item)) {
				$data[$k] = $this->normalizeArrayForNewLines($item);
			}
		}
		return $data;
	}
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;
    }
1167998
private function denormalizeMetadata(array $data) {
        $result = [];

        foreach ($data as $key => $value) {
            $keys = explode($this->metadataNamespaceSeparator, $key);
            $tmp = &$result;

            foreach ($keys as $i => $key) {
                if (!isset($tmp[$key])) {
                    $tmp[$key] = null;
                }

                $tmp = &$tmp[$key];
            }

            $tmp = $value;
        }

        return $result;
    }