Prepares data

Converts given data into a format that can be stored into a file.
You might edit this method to your requirements.

@param mixed $data incoming report data

@return string
435224
protected function prepareData($data) 
    {

        $format = '"%s",%s,%d,"%s","%s","%s","%s"';

        $attackedParameters = '';
        foreach ($data as $event) {
            $attackedParameters .= $event->getName() . '=' .
                rawurlencode($event->getValue()) . ' ';
        }

        $dataString = sprintf($format,
            urlencode($this->ip),
            date('c'),
            $data->getImpact(),
            join(' ', $data->getTags()),
            urlencode(trim($attackedParameters)),
            urlencode($_SERVER['REQUEST_URI']),
            $_SERVER['SERVER_ADDR']
        );

        return $dataString;
    }
553740
public function serialize($data)
    {
        if (is_string($data) === true) {
            return $data;
        } else {
            $data = json_encode($data);
            if ($data === '[]') {
                return '{}';
            } else {
                return $data;
            }
        }


    }
1186899
private function parseDataToString($data)
    {
        $format = $this->allowedFormats[$this->format];
        $className = "\\Ginger\\Response\\Format\\".$format['class'];
        return $className::Parse($data);

    }
734693
private function _format($data) {
        $output_callback_function = '';
        
        // If data is in array format then set content-type
        // to application/json
        if (is_array($data) || is_object($data)) {
            $this->header('Content-type: application/json');
            // Convert to json
            $output_callback_function = json_encode($data);
        } else {
            // Default
            $output_callback_function = $data;
        }
        
        $output = $output_callback_function;
        
        // GZIP output compression
        if ($this->_compress) {
            // Set Content coding a gzip
            $this->header('Content-Encoding: gzip');
            
            // Set headers for gzip
            $output = "\x1f\x8b\x08\x00\x00\x00\x00\x00";
            $output .= gzcompress($output_callback_function);
        }
        
        return $output;
    }
79105
protected function prepareData(array $data)
    {
        // If no aggregator is present then set the default
        if (!$this->aggregator) {
            $this->setAggregator(null);
        }

        $temp = array();
        foreach ($data as $key => $value) {
            if ($value === false || $value === null) {
                // False and null will not include the "=". Use an empty string to include the "=".
                $temp[$this->encodeValue($key)] = $value;
            } elseif (is_array($value)) {
                $temp = array_merge($temp, $this->aggregator->aggregate($key, $value, $this));
            } else {
                $temp[$this->encodeValue($key)] = $this->encodeValue($value);
            }
        }

        return $temp;
    }
861523
public function upload($data)
    {
        $payload = cradle()->makePayload();
        $payload['request']->setStage('data', $data);

        cradle()->trigger(
            'utility-file-upload',
            $payload['request'],
            $payload['response']
        );

        if(!$payload['response']->hasResults('data')) {
            return $data;
        }

        return $payload['response']->getResults('data');
    }
1006081
public function write($data)
    {
        $content = $this->file->data();
        $content = substr($content, 0, $this->position());
        $content .= $data;
        $this->file->setData($content);
        $written = strlen($data);
        $this->offsetPosition($written);
        $this->file->setModificationTime(time());
        $this->file->setChangeTime(time());

        return $written;
    }
806880
private function prepareResultPage($data)
    {
        /* convert service data object into stdClass/array */
        $type = $this->getOutDataType();
        $stdObj = $this->outputProcessor->convertValue($data, $type);
        /* prepare result page with JSON data */
        $result = $this->resultFactory->create(AResultFactory::TYPE_JSON);
        $result->setData($stdObj);
        return $result;
    }
1035636
protected function prepSaveData(&$data)
    {
        if (!is_array($data)) {
            $data = [];
        }

        ksort($data);

        $data['containers'] = [];
        $data['pageContainer'] = [];

        if (empty($data['plugins'])) {
            throw new InvalidArgumentException(
                'Save Data missing plugins .
                Please make sure the data you\'re attempting to save is correctly formatted.
            '
            );
        }

        foreach ($data['plugins'] as &$plugin) {
            $this->cleanSaveData($plugin['saveData']);

            /* Patch for a Json Bug */
            if (!empty($plugin['isSitewide'])
                && $plugin['isSitewide'] != 'false'
                && $plugin['isSitewide'] != '0'
            ) {
                $plugin['isSitewide'] = 1;
            } else {
                $plugin['isSitewide'] = 0;
            }

            if (empty($plugin['sitewideName'])) {
                $plugin['sitewideName'] = null;
            }

            $plugin['rank'] = (int)$plugin['rank'];
            $plugin['rowNumber'] = (int)$plugin['rowNumber'];
            $plugin['columnClass'] = (string)$plugin['columnClass'];

            $plugin['containerName'] = $plugin['containerId'];

            if ($plugin['containerType'] == 'layout') {
                $data['containers'][$plugin['containerId']][] = &$plugin;
            } else {
                $data['pageContainer'][] = &$plugin;
            }
        }
    }
194375
protected function prepareData($data)
    {
        $data->collector = $data->item['collector'];

        $data->eventExecuted  = $data->collector['event.executed'];
        $data->eventListeners = $data->collector['event.listeners'];

        // Executed Events
        $events = [];

        foreach ((array) $data->eventExecuted as $event) {
            $eventName = $event['event'];

            if (!isset($events[$eventName])) {
                $events[$eventName] = [];
            }

            foreach ($event['listeners'] as $listener) {
                $id = implode('::', (array) $listener);

                if (!isset($events[$id])) {
                    $events[$eventName][$id] = [
                        'name' => $eventName,
                        'times' => 0,
                        'listener' => implode('::', (array) $listener),
                    ];
                }

                $events[$eventName][$id]['times']++;
            }
        }

        $data->executed = $events;

        // Event No executed
        $events = [];

        foreach ((array) $data->eventListeners as $eventName => $listeners) {
            foreach ($listeners as $listener) {
                if (!empty($data->executed[$eventName])) {
                    continue;
                }

                if (!isset($events[$eventName])) {
                    $events[$eventName] = [];
                }

                $events[$eventName][] = [
                    'name' => $eventName,
                    'times' => 0,
                    'listener' => implode('::', (array) $listener),
                ];
            }
        }

        $data->noExecuted = $events;
    }
1180361
protected function prepare($data = [])
    {
        if (($response = $this->callSubmitted()) instanceof Response) {
            return $response;
        }

        $this->inputs = array_merge($this->removeIgnoredFields($data), $this->inputs);

        if (($response = $this->callSaving()) instanceof Response) {
            return $response;
        }

        $this->relations = $this->getRelationInputs($this->inputs);

        $this->updates = Arr::except($this->inputs, array_keys($this->relations));
    }
1003335
protected function formatCallback($data)
    {
        if (\count($data) < 2) {
            return $data[0];
        }

        // simple auto or explicit placeholder
        if ($this->mode == self::MODE_INDEXED && $this->hasKey($data[1])) {
            $this->evaluateParam($data[1] || $this->pointer);
            return $this->getParam($data[1]);
        }

        // simple named, explicit placeholder
        elseif ($this->mode == self::MODE_NAMED && \strlen($data[1]) > 0 && $this->hasKey($data[1])) {
            $this->evaluateParam($data[1]);
            return $this->getParam($data[1]);
        }

        // keywords
        elseif (\preg_match('
            /
            ^
                @
                (class|classLong|method|methodLong|function|file|fileLong|dir|dirLong|line)
            $
            /x', $data[1], $match)
        ) {
            switch ($match[1]) {
                case 'classLong':
                    if (!$this->traceClass || !isset($this->traceClass['class'])) {
                        $msg = "Bad usage of @classLong keyword in {$this->traceFile['file']} on " .
                            "line {$this->traceFile['line']} (by m36\\StringFormatter)";
                        \trigger_error($msg, E_USER_WARNING);

                        return '';
                    }

                    return $this->traceClass['class'];
                case 'class':
                    if (!$this->traceClass || !isset($this->traceClass['class'])) {
                        $msg = "Bad usage of @class keyword in {$this->traceFile['file']} on " .
                            "line {$this->traceFile['line']} (by m36\\StringFormatter)";
                        \trigger_error($msg, E_USER_WARNING);

                        return '';
                    }

                    $cls = \explode('\\', $this->traceClass['class']);

                    return end($cls);
                case 'method':
                    if (!$this->traceClass || !isset($this->traceClass['class'])) {
                        $msg = "Bad usage of @method keyword in {$this->traceFile['file']} on " .
                            "line {$this->traceFile['line']} (by m36\\StringFormatter)";
                        \trigger_error($msg, E_USER_WARNING);

                        return '';
                    }

                    $cls = \explode('\\', $this->traceClass['class']);
                    $cls = \end($cls);

                    return $cls . '::' . $this->traceClass['function'];
                case 'methodLong':
                    if (!$this->traceClass || !isset($this->traceClass['class'])) {
                        $msg = "Bad usage of @methodLong keyword in {$this->traceFile['file']} on " .
                            "line {$this->traceFile['line']} (by m36\\StringFormatter)";
                        \trigger_error($msg, E_USER_WARNING);

                        return '';
                    }

                    return $this->traceClass['class'] . '::' . $this->traceClass['function'];
                case 'function':
                    if (!$this->traceClass || !isset($this->traceClass['function'])) {
                        $msg = "Bad usage of @function keyword in {$this->traceFile['file']} on " .
                            "line {$this->traceFile['line']} (by m36\\StringFormatter)";
                        \trigger_error($msg, E_USER_WARNING);

                        return '';
                    }

                    return $this->traceClass['function'];
                case 'file':
                    return \basename($this->traceFile['file']);
                case 'fileLong':
                    return $this->traceFile['file'];
                case 'dir':
                    return \basename(\dirname($this->traceFile['file']));
                case 'dirLong':
                    return \dirname($this->traceFile['file']);
                case 'line':
                    return $this->traceFile['line'];
            }
        }

        // text alignment
        elseif (\preg_match('
            /
            ^
                (' . self::$rxp_keys[$this->mode] . ')  # placeholder
                :                                       # explicit colon
                (.)?                                    # pad character
                ([<>^])                                 # alignment
                (\d+)                                   # pad length
            $
            /x', $data[1], $match) &&
            $this->hasKey($match[1])
        ) {
            $this->evaluateParam($match[1]);
            return \str_pad(
                $this->getParam($match[1]),
                $match[4],
                (\strlen($match[2]) > 0 ? $match[2] : ' '),
                static::$matrix__str_pad[$match[3]]
            );
        }

        // sprintf pattern
        elseif (\preg_match('
            /
            ^
                (' . self::$rxp_keys[$this->mode] . ')  # placeholder
                %                                       # explicit percent
                (.*)                                    # sprintf pattern
            $
            /x', $data[1], $match) &&
            $this->hasKey($match[1])
        ) {
            $this->evaluateParam($match[1]);
            return \vsprintf($match[2], $this->getParam($match[1]));
        }

        // call object method or get object property
        elseif (\preg_match('
            /
            ^
                (' . self::$rxp_keys[$this->mode] . ')  # placeholder
                ->                                      # explicit arrow
                (\w+)                                   # keyword (field or method name)
            $
            /x', $data[1], $match) &&
            $this->hasKey($match[1])
        ) {
            $this->evaluateParam($match[1]);
            $param = $this->getParam($match[1]);
            if (\method_exists($param, $match[2])) {
                return \call_user_func(array($param, $match[2]));
            } elseif (\property_exists($param, $match[2])) {
                return $param->{$match[2]};
            } elseif (\in_array('__call', \get_class_methods($param))) {
                return \call_user_func(array($param, $match[2]));
            } elseif (\in_array('__get', \get_class_methods($param))) {
                return $param->{$match[2]};
            } else {
                return $data[0];
            }
        }

        // converting int to other base
        elseif (\preg_match('
            /
            ^
            (' . self::$rxp_keys[$this->mode] . ')  # placeholder
            [#]                                     # explicit hash
            (?:
                (\d+)                               # source base
                [#]                                 # explicit hash
            )?
            ([dxXob]|\d\d?)                         # destination base
            $
            /x', $data[1], $match) &&
            $this->hasKey($match[1])
        ) {
            $this->evaluateParam($match[1]);
            $ret = \base_convert(
                $this->getParam($match[1]),                     // value to convert
                ($match[2] ? $match[2] : 10),                   // source base (defaults to 10)
                (
                \is_numeric($match[3])                          // destination base is:
                    ? $match[3]                                 // - numeric
                    : self::$matrix__base_convert[$match[3]]    // - or named
                )
            );
            if ($match[3] == 'X') {
                $ret = \strtoupper($ret);
            }

            return $ret;
        }

        // array index
        elseif (\preg_match('
            /
            ^
                (' . self::$rxp_keys[$this->mode] . ')  # placeholder
                \[                                      # opening square bracket
                    (\w+)                               # key
                \]                                      # closing square bracket
            $
            /x', $data[1], $match) &&
            $this->hasKey($match[1]) &&
            \is_array($ret = $this->getParam($match[1])) &&
            isset($ret[$match[2]])
        ) {
            $this->evaluateParam($match[1]);
            return $ret[$match[2]];
        }

        // unknown token
        else {
            $msg = "Unknown token found in format: {$data[0]} in {$this->traceFile['file']} on " .
                "line {$this->traceFile['line']} (by m36\\StringFormatter)";
            \trigger_error($msg, E_USER_WARNING);

            return $data[0];
        }
    }
456479
public function serializeData($data) {
		if(is_array($data)) {
			ksort($data);
			$return = '';
			foreach($data as $key => $value) {
				if(is_numeric($key)) {
					$return .= $this->serializeData($value);
				} else {
					$return .= $key . $this->serializeData($value);
				}
			}
			return $return;
		} else {
			return (string)$data;
		}
	}
638349
protected static function prepareInputBuffer($data)
    {
        if ($data instanceof Buffer) {
            return $data;
        }
        elseif (is_string($data) && ctype_xdigit($data)) {
            return Buffer::fromHex($data);
        }
        elseif (is_string($data)) {
            return new Buffer($data);
        }
        elseif (is_array($data)) {
            // Uint8 provided (serialized data)
            return Buffer::fromUInt8($data);
        }

        return new Buffer((string) $data);
    }
564366
public function setData($data)
    {
        if ($this->getEntryType() === FileType::class) {
            $data = self::normalizeFilesArray($data);
        }

        $this->data = $data;

        return $this;
    }
230717
protected function runFormatters($data)
    {
        $data = self::formatAble($data);

        foreach ($this->formatters as $formatter) {
            $data = $formatter->format($data);
        }

        return $data->getObject();
    }
685776
public function serialize($data)
    {
        $json = '';
        $formattedData = null;

        if ($data instanceof DataObject) {
            $formattedData = $this->formatDataObject($data);
        } elseif ($data instanceof DataList) {
            $formattedData = $this->formatDataList($data);
        }

        if ($formattedData !== null) {
            $json = $this->jsonify($formattedData);
        } else {
            //fallback: convert non array to object then encode
            if (!is_array($data)) {
                $data = (object) $data;
            }
            $json = $this->jsonify($data);
        }

        return $json;
    }