<commit_before><?php
/**
 * Formats data as xml
 * @author <NAME>
 * @copyright Loft Digital, 2014
 */

namespace Gisleburt\Formatter\Formats;


use Gisleburt\Formatter\Format;

class Xml extends Format {

    protected $contentType = 'application/xml';

    protected $numericArrayPrefix = '_';

    protected $defaultNodeName = 'data';

    public function getHeader() {
        return '<?xml version="1.0" encoding="UTF-8" ?>';
    }

    public function format($data, $nodeName = null) {

        if(!$nodeName) {
            if(is_object($data)) {
                $nodeName = preg_replace('/.*\\\/', '', get_class($data));
                $nodeName = preg_replace('/\W/', '', $nodeName);
            }
            elseif(is_array($data)) {
                $nodeName = 'array';
            }
            else {
                $nodeName = $this->defaultNodeName;
            }
        }

        $xml = "<$nodeName>";
        foreach($data as $property => $value) {
            // Clear non-alphanumeric characters
            $property = preg_replace('/\W/', '', $property);

            // If numeric we'll stick a character in front of it, a bit hack but should be valid
            if(is_numeric($property)) {
                $property = $this->numericArrayPrefix.$property;
            }

            if(!is_scalar($value)) {
                if($value instanceof \JsonSerializable) {
                    $xml .= $this->format($value->jsonSerialize(), $property);
                }
                else {
                    $xml .= $this->format($value, $property);
                }
            }
            else {
                $xml .= "<{$property}>".htmlspecialchars($value)."</{$property}>";
            }
        }
        $xml .= "</$nodeName>";

        return $xml;
    }

} <commit_msg>Overhauled xml formatter as it just wasn't producing expected results.<commit_after><?php
/**
 * Formats data as xml
 * @author <NAME>
 * @copyright Loft Digital, 2014
 */

namespace Gisleburt\Formatter\Formats;


use Gisleburt\Formatter\Format;

class Xml extends Format {

    protected $contentType = 'application/xml';

    protected $numericArrayPrefix = '_';

    protected $defaultNodeName = 'data';

    public function getHeader() {
        return '<?xml version="1.0" encoding="UTF-8" ?>';
    }

    public function format($data, $nodeName = null) {

        if(!$nodeName) {
            if(is_object($data)) {
                $nodeName = preg_replace('/.*\\\/', '', get_class($data));
                $nodeName = preg_replace('/\W/', '', $nodeName);
            }
            elseif(is_array($data)) {
                $nodeName = 'array';
            }
            else {
                $nodeName = $this->defaultNodeName;
            }
        }

        $xml = "<$nodeName>";

        if(is_scalar($data)) {
            if(is_bool($data)) {
                $xml = $data ? 'true' : 'false';
            }
            else{
                $xml .= htmlspecialchars($data);
            }
        }
        else {
            if($data instanceof \JsonSerializable) {
                $xml .= $this->format($data->jsonSerialize());
            }
            else {
                foreach($data as $property => $value) {
                    // Clear non-alphanumeric characters
                    $property = preg_replace('/\W/', '', $property);

                    // If numeric we'll stick a character in front of it, a bit hack but should be valid
                    if(is_numeric($property)) {
                        $property = $this->numericArrayPrefix.$property;
                    }
                    $xml .= $this->format($value, $property);
                }
            }
        }

        $xml .= "</$nodeName>";

        return $xml;
    }

} 
{'fraction_non_alphanumeric': 0.14519968262364455, 'fraction_numerical': 0.0037027241470510446, 'mean_word_length': 1.538255033557047, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 2, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6772437', 'n_tokens_mistral': 1062, 'n_tokens_neox': 1001, 'n_words': 220}