<commit_before><commit_msg>Add DataItem object<commit_after><?php

namespace allejo\stakx\Object;

use allejo\stakx\Environment\Filesystem;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Yaml\Yaml;

class DataItem
{
    protected $converters;
    protected $content;
    protected $data;
    protected $name;
    protected $ext;
    protected $fs;

    public function __construct ($filePath)
    {
        $this->fs      = new Filesystem();
        $this->ext     = strtolower($this->fs->getExtension($filePath));
        $this->name    = $this->fs->getFileName($filePath);
        $this->content = $this->readFile($filePath);
        $functionName  = 'from' . ucfirst($this->ext);

        if (method_exists(get_called_class(), $functionName))
        {
            $this->data = $this->$functionName($this->content);
        }
        else
        {
            throw new IOException("There is no function to handle '$this->ext' file format.");
        }
    }

    public function getName ()
    {
        return $this->name;
    }

    private function fromCsv ($content)
    {
        $rows    = array_map("str_getcsv", explode("\n", trim($content)));
        $columns = array_shift($rows);
        $csv     = array();

        foreach ($rows as $row)
        {
            $csv[] = array_combine($columns, $row);
        }

        return $csv;
    }

    private function fromJson ($content)
    {
        return json_decode($content, true);
    }

    private function fromXml ($content)
    {
        return json_decode(json_encode(simplexml_load_string($content)), true);
    }

    private function fromYaml ($content)
    {
        return Yaml::parse($content);
    }

    private function readFile ($filePath)
    {
        if (!$this->fs->exists($filePath))
        {
            throw new IOException("No file could be found at: $filePath");
        }

        return file_get_contents($filePath);
    }
}
{'fraction_non_alphanumeric': 0.13319672131147542, 'fraction_numerical': 0.0, 'mean_word_length': 2.3270868824531514, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, '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': '2451674', 'n_tokens_mistral': 576, 'n_tokens_neox': 565, 'n_words': 121}