/*
filter out fields not match schema fields
@param table, $fldArray
@return filtered field Array ( [name] => some name [biography] => some bio )
187419
public static function filterBySchema($tname, $iArray) {

        try {    
            $fields = self::schema($tname,"_none_");
            // diff in array with schma fields
            $diffs = array_diff_key($iArray, $fields);
            // remove unwanted fields
            foreach ($diffs as $k=>$v) {
                unset($iArray[$k]);
            }
            return $iArray;    
        } catch (Exception $e) {
            echo $e->getTraceAsString();
        }        
                    
    }
245760
protected static function stripNonSQLCharactersArray(array $strings, $nonFilter = null)
    {
        return array_map(function ($string) use ($nonFilter) {
            return Query::stripNonSQLCharacters($string, $nonFilter);
        }, $strings);
    }
527528
private function filterArray($arrData, $arrFilters) {
		// Normalize the filter array (['filter' => FILTER, ...])
		foreach ($arrFilters as $key => &$F) {
			if (is_int($F))
				$F = array('filter' => $F);

			elseif (!isset($F['filter'])) {
				unset($arrFilters[$key]);
				continue;

			} elseif (is_string($F['filter'])) {
				try {
					$intF = @constant($F['filter']);
				} catch (\Exception $e) {
					continue;
				}
				if ( $intF )
					$F['filter'] = $intF;
				else {
					continue;

				}
			} elseif (isset($F['required'])) {
				if (!$F['required'] && (string)$arrData[$key] == '') {
					unset($arrFilters[$key]);
					continue;
				}
			}

			$F['value'] = isset($arrData[$key]) ? $arrData[$key] : null;
			if (!isset($F['field']))
				$F['field'] = $key;
		}

		$arrCallbacks = array();
		$arrValidators = array();

		// Remove callback functions from the filter array and allocate them to the callback array
		foreach ($arrFilters as $key => $filter) {
			if (isset($this->filters[$filter['filter']])) {
				$arrValidators[$key] = $filter + $this->filters[$filter['filter']];
				$arrValidators[$key]['filter'] = $this->filters[$filter['filter']]['filter'];
			} elseif (isset($this->callbacks[$filter['filter']]))
				$arrCallbacks[$key] = $filter;
			else
				$arrValidators[$key] = $filter;
		}

		$validate = array();

		// Validate the callbacks
		foreach ($arrCallbacks as $key => $filter)
			$validate[$key] = call_user_func($this->callbacks[$filter['filter']], $filter);

		// Validate through PHP filter function
		$validate = $validate + filter_var_array($arrData, $arrValidators);

		// Check the results
		$res = array();
		foreach ($arrFilters as $key => $filter) {
			$result = isset($validate[$key]) ? $validate[$key] : null;
			if (is_null($result) || ($filter != FILTER_VALIDATE_BOOLEAN && (bool) $result == false))
				$res[$key] = $filter;
		}

		return $res;
	}
372435
public function fieldTypesToFilter($arrFields) {
		foreach ($arrFields as $key => &$properties) {
			if (isset($properties['filter']) ||
				!isset($properties['type']) ||
				!isset($this->typesToFilter[$properties['type']]) )
				continue;

			$properties = $properties + $this->typesToFilter[$properties['type']];
		}

		return $arrFields;
	}
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);
    }
972573
public function getFieldsWithTableAlias($table_alias = null)
    {
        $vals = [];
        $replace = $table_alias === null ? '' : sprintf("%s.", $table_alias);

        foreach ($this->fields as $name => $definition) {
            $vals[$name] = $this->replaceToken($this->fields[$name], $replace);
        }

        return $vals;
    }
1122924
public function filterDataToExistingInDb($data, $existing = true) {
        $table = static::TABLE;

        $columns = $this->getFields();

        $return = array();

        foreach ($data as $key => &$d) {
            if ( array_key_exists($key, $columns) === $existing) {
                $return[$key] = $d;
            }
        }

        return $return;
    }
408550
protected function filterFields(array $fields)
    {
        if ($requested = $this->getOwnFields()) {
            $fields = array_intersect_key($fields, array_flip($requested));
        }

        return $fields;
    }
1185691
public static function fieldsKey($table, $filter="id") {

        try {
            $row = (array) self::findRow("select * from ".$table, "assoc");
            // remove array element base on filter if _none_ no filter
            if (strtolower($filter)!="_none_") {
                // default is to filter id field
                unset($row[$filter]);
            }
            return array_keys($row);
        } catch (Exception $e) {
            echo $e->getTraceAsString();
        }        
    }
566019
public function setDefaultFieldValues(array &$fieldArray, $uidPid = 0)
    {
        // Default values:
        $newRow = []; // Used to store default values as found here:

        // Default values as set in userTS:
        $TCAdefaultOverride = (array)($this->getBackendUser()->getTSConfig()['TCAdefaults'] ?? []);
        if (is_array($TCAdefaultOverride['tt_content.'])) {
            foreach ($TCAdefaultOverride['tt_content.'] as $field => $value) {
                if (isset($GLOBALS['TCA']['tt_content']['columns'][$field])) {
                    $newRow[$field] = $value;
                }
            }
        }

        if ($uidPid < 0) {
            $record = BackendUtility::getRecord('tt_content', abs($uidPid), 'pid');
            $pageId = $record['pid'];
        } else {
            $pageId = (int)$uidPid;
        }

        $pageTS = BackendUtility::getPagesTSconfig($pageId);

        if (isset($pageTS['TCAdefaults.'])) {
            $TCAPageTSOverride = $pageTS['TCAdefaults.'];
            if (is_array($TCAPageTSOverride['tt_content.'])) {
                foreach ($TCAPageTSOverride['tt_content.'] as $field => $value) {
                    if (isset($GLOBALS['TCA']['tt_content']['columns'][$field])) {
                        $newRow[$field] = $value;
                    }
                }
            }
        }

        // Default values as submitted:
        $this->definitionValues = GeneralUtility::_GP('defVals');
        $this->overrideValues = GeneralUtility::_GP('overrideVals');
        if (!is_array($this->definitionValues) && is_array($this->overrideValues)) {
            $this->definitionValues = $this->overrideValues;
        }
        if (is_array($this->definitionValues['tt_content'])) {
            foreach ($this->definitionValues['tt_content'] as $field => $value) {
                if (isset($GLOBALS['TCA']['tt_content']['columns'][$field])) {
                    $newRow[$field] = $value;
                }
            }
        }

        // Fetch default values if a previous record exists
        if ($uidPid < 0 && !empty($record) && $GLOBALS['TCA']['tt_content']['ctrl']['useColumnsForDefaultValues']) {
            // Gets the list of fields to copy from the previous record.
            $fieldArray = explode(',', $GLOBALS['TCA']['tt_content']['ctrl']['useColumnsForDefaultValues']);
            foreach ($fieldArray as $field) {
                $field = trim($field);
                if ($field === '') {
                    continue;
                }
                if (isset($GLOBALS['TCA']['tt_content']['columns'][$field])) {
                    $newRow[$field] = $record[$field];
                }
            }
        }
        $fieldArray = array_merge($newRow, $fieldArray);
    }
904168
function filter($data = array())
    {
        if (! is_array($data)) {
            return $data;
        }
        // first apply the filters to the ROOT
        if (isset($this->filters[self::SELECTOR_ROOT])) {
            /* @var $rootFilters FilterSet */
            $rootFilters = $this->filters[self::SELECTOR_ROOT];
            $data = $rootFilters->applyFilters($data);
        }
        foreach ($data as $key => $value) {
            $data[$key] = $this->filterItem($data, $key);
        }
        return $data;
    }
986628
function _filterByArraySchema(data, schema) {
    return Object
        .keys(data)
        .filter(function (key) {
            return !!~schema.indexOf(key);
        })
        .reduce(function (memo, key) {
            memo[key] = data[key];
            return memo;
        }, {});
}
1162130
protected function filterUndefined($class_name)
    {
        $vars = $this->toArray();
        foreach ($vars as $name => $value) {
            if ($name != "_id" && !self::isDefined($class_name, $name)) {
                unset($this->{$name});
            }
        }
    }
1328323
public function filterData(array $data)
    {
        // Filter the sparse field-set if we have a specific list of properties
        // defined that we want.
        $filterProps = $this->filterProps();
        if (!empty($filterProps)) {
            $filteredData = array_filter($data, function ($key) use ($filterProps) {
                return \in_array($key, $filterProps, true);
            }, ARRAY_FILTER_USE_KEY);

            // We must always have some data after filtering, so if our filtered
            // data is empty, we should just ignore it ...
            if (!empty($filteredData)) {
                $data = $filteredData;
            }
        }

        return $data;
    }
722540
function _filter(data, schema) {
    if (typeof data !== 'object' ||
        data === null ||
        schema === null) {
        return data;
    }
    if (schema instanceof Array) {
        return _filterByArraySchema(data, schema);
    }
    if (typeof schema === 'object') {
        return _filterByObjectSchema(data, schema);
    }
    return data;
}
395297
public function get_sql_filter($data) {
        $field  = $this->field;
        if (is_array($field)) {
            $res = " {$field[0]} = {$field[1]} ";
        } else {
            $res = " {$field} = 0 ";
        }
        return array($res, array());
    }
830122
public function arr2xmldb_field($xmlarr) {

        $result = true;

        // Debug the table
        // traverse_xmlize($xmlarr);                   //Debug
        // print_object ($GLOBALS['traverse_array']);  //Debug
        // $GLOBALS['traverse_array']="";              //Debug

        // Process table attributes (name, type, length
        // notnull, sequence, decimals, comment, previous, next)
        if (isset($xmlarr['@']['NAME'])) {
            $this->name = trim($xmlarr['@']['NAME']);
        } else {
            $this->errormsg = 'Missing NAME attribute';
            $this->debug($this->errormsg);
            $result = false;
        }

        if (isset($xmlarr['@']['TYPE'])) {
            // Check for valid type
            $type = $this->getXMLDBFieldType(trim($xmlarr['@']['TYPE']));
            if ($type) {
                $this->type = $type;
            } else {
                $this->errormsg = 'Invalid TYPE attribute';
                $this->debug($this->errormsg);
                $result = false;
            }
        } else {
            $this->errormsg = 'Missing TYPE attribute';
            $this->debug($this->errormsg);
            $result = false;
        }

        if (isset($xmlarr['@']['LENGTH'])) {
            $length = trim($xmlarr['@']['LENGTH']);
            // Check for integer values
            if ($this->type == XMLDB_TYPE_INTEGER ||
                $this->type == XMLDB_TYPE_NUMBER ||
                $this->type == XMLDB_TYPE_CHAR) {
                if (!(is_numeric($length)&&(intval($length)==floatval($length)))) {
                    $this->errormsg = 'Incorrect LENGTH attribute for int, number or char fields';
                    $this->debug($this->errormsg);
                    $result = false;
                } else if (!$length) {
                    $this->errormsg = 'Zero LENGTH attribute';
                    $this->debug($this->errormsg);
                    $result = false;
                }
            }
            // Remove length from text and binary
            if ($this->type == XMLDB_TYPE_TEXT ||
                $this->type == XMLDB_TYPE_BINARY) {
                $length = null;
            }
            // Finally, set the length
            $this->length = $length;
        }

        if (isset($xmlarr['@']['NOTNULL'])) {
            $notnull = strtolower(trim($xmlarr['@']['NOTNULL']));
            if ($notnull == 'true') {
                $this->notnull = true;
            } else if ($notnull == 'false') {
                $this->notnull = false;
            } else {
                $this->errormsg = 'Incorrect NOTNULL attribute (true/false allowed)';
                $this->debug($this->errormsg);
                $result = false;
            }
        }

        if (isset($xmlarr['@']['SEQUENCE'])) {
            $sequence = strtolower(trim($xmlarr['@']['SEQUENCE']));
            if ($sequence == 'true') {
                $this->sequence = true;
            } else if ($sequence == 'false') {
                $this->sequence = false;
            } else {
                $this->errormsg = 'Incorrect SEQUENCE attribute (true/false allowed)';
                $this->debug($this->errormsg);
                $result = false;
            }
        }

        if (isset($xmlarr['@']['DEFAULT'])) {
            $this->setDefault(trim($xmlarr['@']['DEFAULT']));
        }

        $decimals = null;
        if (isset($xmlarr['@']['DECIMALS'])) {
            $decimals = trim($xmlarr['@']['DECIMALS']);
            // Check for integer values
            if ($this->type == XMLDB_TYPE_NUMBER ||
                $this->type == XMLDB_TYPE_FLOAT) {
                if (!(is_numeric($decimals)&&(intval($decimals)==floatval($decimals)))) {
                    $this->errormsg = 'Incorrect DECIMALS attribute for number field';
                    $this->debug($this->errormsg);
                    $result = false;
                } else if ($this->length <= $decimals){
                    $this->errormsg = 'Incorrect DECIMALS attribute (bigget than length)';
                    $this->debug($this->errormsg);
                    $result = false;
                }
            } else {
                $this->errormsg = 'Incorrect DECIMALS attribute for non-number field';
                $this->debug($this->errormsg);
                $result = false;
            }
        } else {
            if ($this->type == XMLDB_TYPE_NUMBER) {
                $decimals = 0;
            }
        }
        // Finally, set the decimals
        if ($this->type == XMLDB_TYPE_NUMBER ||
            $this->type == XMLDB_TYPE_FLOAT) {
            $this->decimals = $decimals;
        }

        if (isset($xmlarr['@']['COMMENT'])) {
            $this->comment = trim($xmlarr['@']['COMMENT']);
        }

        // Set some attributes
        if ($result) {
            $this->loaded = true;
        }
        $this->calculateHash();
        return $result;
    }