<commit_before><?php

namespace Stratedge\Wye;

use Exception;
use ReflectionClass;
use Stratedge\Wye\PDO\PDO;
use Stratedge\Wye\Traits\UsesWye;

class Row
{
    use UsesWye;


    /**
     * @var array
     */
    protected $data = [];


    public function __construct(Wye $wye, array $data = null)
    {
        $this->wye($wye);

        if (!is_null($data)) {
            $this->data($data);
        }
    }


    public function format($how, $class_name = "stdClass", $ctor_args = [])
    {
        /**
         * @todo:
         *     - Implement PDO::FETCH_CLASSTYPE flag on PDO::FETCH_CLASS
         *     - Implement PDO::FETCH_PROPS_LATE flag on PDO::FETCH_CLASS
         *     - Implement PDO::FETCH_ASSOC
         *     - Implement PDO::FETCH_BOTH
         *     - Implement PDO::FETCH_BOUND
         *     - Implement PDO::FETCH_INTO
         *     - Implement PDO::FETCH_LAZY
         *     - Implement PDO::FETCH_NAMED
         *     - Implement PDO::FETCH_NUM
         *     - Implement PDO::FETCH_OBJ
         */

        switch ($how) {
            case PDO::FETCH_ASSOC:
                $data = $this->formatAssoc();
                break;
            case PDO::FETCH_CLASS:
                $data = $this->formatClass($class_name, $ctor_args);
                break;
            default:
                throw new Exception("Need to implement formatter for $how");
        }

        return $data;
    }


    protected function formatAssoc()
    {
        return $this->data();
    }


    protected function formatClass($class_name, $ctor_args)
    {
        $r = new ReflectionClass($class_name);
        $data = $r->newInstanceArgs((array) $ctor_args);

        foreach ($this->data() as $key => $value) {
            $data->$key = $value;
        }

        return $data;
    }


    //**************************************************************************
    // DATA
    //**************************************************************************

    public function data(array $data = null)
    {
        if (is_null($data)) {
            return $this->getData();
        } else {
            return $this->setData($data);
        }
    }

    public function getData()
    {
        return $this->data;
    }

    public function setData(array $data)
    {
        $this->data = $data;
    }
}
<commit_msg>Handle formatting a row with PDO::FETCH_BOTH<commit_after><?php

namespace Stratedge\Wye;

use Exception;
use ReflectionClass;
use Stratedge\Wye\PDO\PDO;
use Stratedge\Wye\Traits\UsesWye;

class Row
{
    use UsesWye;


    /**
     * @var array
     */
    protected $data = [];


    public function __construct(Wye $wye, array $data = null)
    {
        $this->wye($wye);

        if (!is_null($data)) {
            $this->data($data);
        }
    }


    public function format($how, $class_name = "stdClass", $ctor_args = [])
    {
        /**
         * @todo:
         *     - Implement PDO::FETCH_CLASSTYPE flag on PDO::FETCH_CLASS
         *     - Implement PDO::FETCH_PROPS_LATE flag on PDO::FETCH_CLASS
         *     - Implement PDO::FETCH_BOUND
         *     - Implement PDO::FETCH_INTO
         *     - Implement PDO::FETCH_LAZY
         *     - Implement PDO::FETCH_NAMED
         *     - Implement PDO::FETCH_NUM
         *     - Implement PDO::FETCH_OBJ
         */

        switch ($how) {
            case PDO::FETCH_ASSOC:
                $data = $this->formatAssoc();
                break;
            case PDO::FETCH_BOTH:
                $data = $this->formatBoth();
                break;
            case PDO::FETCH_CLASS:
                $data = $this->formatClass($class_name, $ctor_args);
                break;
            default:
                throw new Exception("Need to implement formatter for $how");
        }

        return $data;
    }


    protected function formatAssoc()
    {
        return $this->data();
    }


    protected function formatBoth()
    {
        $data = [];
        $index = 0;

        foreach ($this->data() as $key => $value) {
            $data[$key] = $value;
            $data[$index] = $value;
            $index++;
        }

        return $data;
    }


    protected function formatClass($class_name, $ctor_args)
    {
        $r = new ReflectionClass($class_name);
        $data = $r->newInstanceArgs((array) $ctor_args);

        foreach ($this->data() as $key => $value) {
            $data->$key = $value;
        }

        return $data;
    }


    //**************************************************************************
    // DATA
    //**************************************************************************

    public function data(array $data = null)
    {
        if (is_null($data)) {
            return $this->getData();
        } else {
            return $this->setData($data);
        }
    }

    public function getData()
    {
        return $this->data;
    }

    public function setData(array $data)
    {
        $this->data = $data;
    }
}

{'fraction_non_alphanumeric': 0.1811449159327462, 'fraction_numerical': 0.00020016012810248197, 'mean_word_length': 1.7425905598243687, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4457115', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1493, 'n_words': 294}