Removes references where required.

@param stdClass|array $data
@return mixed What ever was put in but without any references.
182557
protected function unref($data) {
        if ($this->definition->uses_simple_data()) {
            return $data;
        }
        // Check if it requires serialisation in order to produce a reference free copy.
        if ($this->requires_serialisation($data)) {
            // Damn, its going to have to be serialise.
            $data = serialize($data);
            // We unserialise immediately so that we don't have to do it every time on get.
            $data = unserialize($data);
        } else if (!is_scalar($data)) {
            // Its safe to clone, lets do it, its going to beat the pants of serialisation.
            $data = $this->deep_clone($data);
        }
        return $data;
    }
21602
protected function withoutBulk(array $data = [])
    {
        if ($this->isArray($data)) {
            return;
        }

        $row = $this->data->addChild('row');

        $row->addAttribute('no', 1);

        foreach ($data['data'] as $key => $value) {
            $child = $row->addChild('FL', $value);

            $child->addAttribute('val', $key);
        }
    }
633381
private function denormalizeData($data)
    {
        if (is_scalar($data) || $data === null) {
            return $data;
        }

        if (isset($data[$this->classAnnotation])) {
            return $this->denormalizeObject($data);
        }

        return array_map([$this, 'denormalizeData'], $data);
    }
828609
final public function sanitize($data)
    {
        if ($this->_walk && (is_array($data) || is_object($data))) {
            $arr = (array)$data;

            foreach ($arr as $key => $value) {
                if (is_array($data)) {
                    $data[$key] = $this->sanitize($value);
                }

                if (is_object($data)) {
                    $data->$key = $this->sanitize($value);
                }
            }
        } else {
            $context = $this->_chain->getContext();
            $context->data = $data;

            $data = $this->_chain->run('sanitize', $context);
        }

        return $data;
    }
1005160
protected static function removeEmptyAttributes($data)
    {
        $records = $data;
        $unwrapped = false;
        if (isset($data[ResourcesWrapper::getWrapper()])) {
            $records = $data[ResourcesWrapper::getWrapper()];
            $unwrapped = true;
        }

        foreach ($records as $i => $record) {
            if (is_numeric($i) && [$record]) {
                foreach ($record as $key => $value) {
                    if (empty($value)) {
                        unset($records[$i][$key]);
                    } elseif (is_string($value)) {
                        if (strtolower($value) === "true") {
                            $records[$i][$key] = true;
                        } elseif (strtolower($value) === "false") {
                            $records[$i][$key] = false;
                        } elseif (strtolower($value) === "null") {
                            $records[$i][$key] = null;
                        }
                    }
                }
            }
        }

        $data = $records;
        if ($unwrapped) {
            $data = ResourcesWrapper::wrapResources($data);
        }

        return $data;
    }
313110
protected function arrayDotKeysFlattern(array $data)
    {
        foreach ($data as $name => $values) {
            if ($this->isIterable($values)) {
                foreach ($values as $key => $key_value) {
                    $data["$name.$key"] = $values[$key];
                }
                unset($data[$name]);
            }
        }
        return $data;
    }
1258794
public function redislizeArray(array $data)
    {
        $result = [];
        foreach ($data as $key => $value) {
            if (!is_array($value)) {
                $result[$key] = $this->redislize($value);
            }
        }

        return $result;
    }
57554
public function remove($q, $data = [])
    {
        $q['table'] = $this->table;
        $q['limit'] = 1;

        return $this->db->remove($q, $data);
    }
515673
public function remove($data)
    {
        $data = $this->fetchRemainingSegment($this->data, $data);
        if (! is_null($data)) {
            $this->set($data);
        }
    }
900922
public function reference($data)
	{
		// Process the given data.
		$processPayload = $this->process(__FUNCTION__, $data);

		if( ! $processPayload->isStatus(Payload::STATUS_VALID))
		{
			return $processPayload;
		}

		$entityPayload = $this->getOneByUuid($data['uuid']);

		if($entityPayload->getStatus() != 'found')
		{
			return $entityPayload;
		}
		
		// Either get an existing entity with uuid given or create a new one with data given.
		if(array_key_exists('uuid', $processPayload->getData()['reference']))
		{
			// @todo: Make this more consistent - returns a collection.
			$referencePayload = $this->getOneByUuid($processPayload->getData()['reference']['uuid']);

			if($referencePayload->getStatus() != 'found')
			{
				return $referencePayload;
			}

			// @todo: Make this more consistent - extra instanciating.
			$referencePayload = new Payload($referencePayload->getData(), 'found');
		}
		elseif(array_key_exists('reference_type', $processPayload->getData()['reference']))
		{
			$referencePayload = $this->create(['entity_type' => $processPayload->getData()['reference']['reference_type']] + $processPayload->getData()['reference']);

			if($referencePayload->getStatus() != 'created')
			{
				return $referencePayload;
			}
		}
		else
		{
			dd("@todo: Implement proper validation to avoid getting to this point");
		}

		// Finally, create the reference.
		$referenceData = [];
		$referenceData['entity_uuid']    = $entityPayload->getData()->first()->getUuid();
		$referenceData['entity_type']    = $entityPayload->getData()->first()->getType();
		$referenceData['entity_id']      = $entityPayload->getData()->first()->getKey();
		$referenceData['reference_uuid'] = $referencePayload->getData()->first()->getUuid();
		$referenceData['reference_type'] = $referencePayload->getData()->first()->getType();
		$referenceData['reference_id']   = $referencePayload->getData()->first()->getKey();

		$newReferencePayload = $this->createReference($referenceData);

		if($newReferencePayload->getStatus() != 'created')
		{
			return $newReferencePayload;
		}

		// @todo: What do we actually want to return here?
		return new Payload($entityPayload->getData()->first(), 'created');
	}
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));
    }
963990
public function withoutFalseyValues($data)
    {
        for ($i = 0; $i < count($data[ClientResource::FIELD_RESOURCES]); $i++) {
            if (!$data[ClientResource::FIELD_RESOURCES][$i]['value']) {
                unset($data[ClientResource::FIELD_RESOURCES][$i]);
            }
        }

        // Resort array
        $data[ClientResource::FIELD_RESOURCES] = array_values($data[ClientResource::FIELD_RESOURCES]);

        return $data;
    }
717726
private function cleanFeatureData($data)
    {
        $fields = array_merge($this->getFields(), array($this->getUniqueId(), $this->getGeomField()));

        // clean data from feature
        foreach ($data as $fieldName => $value) {
            if (isset($fields[ $fieldName ])) {
                unset($data[ $fieldName ]);
            }
        }
        return $data;
    }
111489
private function convertArrayObjects($data)
    {
        if ($data instanceof \ArrayObject || $data instanceof \stdClass) {
            $data = (array) $data;
        }
        if (\is_array($data)) {
            foreach ($data as $k => $v) {
                $data[$k] = $this->convertArrayObjects($v);
            }
        }

        return $data;
    }
262558
protected static function filterValues(array $data)
    {
        $returnValue = array();
        foreach ($data as $d) {
            if ($d !== null) {
                $returnValue[] = $d->getValue();
            } else {
                $returnValue[] = $d;
            }
        }

        return $returnValue;
    }
683918
protected function removeRedundantParams(array $data): array
    {
        $data = array_filter($data, function ($item) {
            if ($item instanceof ArrayObject) {
                return $item->count() !== 0;
            }
            return !empty($item);
        });

        foreach ($data as $key => $value) {
            if (is_array($value) || $value instanceof ArrayObject) {
                $data[$key] = $this->removeRedundantParams($value);
            }
        }

        return $data;
    }
848286
protected function getClassFromArray($data)
    {
        if (!is_array($data)) {
            return;
        }

        if (isset($data['@type']) and $type = $data['@type'] and isset($this->types[$type])) {
            $class = $this->types[$type];
        }

        if (isset($data['@class'])) {
            $class = $data['@class'];
        }

        if (isset($class) and class_exists($class)) {
            return $class;
        }
    }