<commit_before><?php

namespace CodeJetter\core;

use CodeJetter\core\database\QueryMaker;
use CodeJetter\core\io\Input;
use CodeJetter\core\io\Output;
use CodeJetter\core\security\Validator;
use CodeJetter\core\security\ValidatorRule;
use CodeJetter\core\utility\MysqlUtility;
use CodeJetter\core\utility\StringUtility;

/**
 * Class BaseMapper
 * @package CodeJetter\core
 */
abstract class BaseMapper implements ICrud
{
    protected $database;
    protected $table;
    protected $modelName;
    protected $lastQuery;
    protected $component;

    /**
     * BaseMapper constructor.
     *
     * @param null $database
     * @param      $component
     */
    public function __construct($database = null, $component = null)
    {
        // TODO this needs to be in its own function
        $className = (new StringUtility())->getClassNameFromNamespace(get_called_class());
        $mapperTableRelations = Registry::getConfigClass()->get('mapperTableRelations');

        $mapperTable = isset($mapperTableRelations[$className]) ? $mapperTableRelations[$className] : '';

        if (empty($mapperTable)) {
            // mapper table is not specified in the config file, generate the name automatically

            // convert camel case to snake case
            $snakeCaseClassName = strtolower((new StringUtility())->camelCaseToSnakeCase($className));

            // remove mapper from the end of the string
            $snakeCaseClassName = preg_replace('#_mapper$#', '', $snakeCaseClassName);

            // replace 'y' with 'ie' if 'y' is the last character
            if (substr($snakeCaseClassName, -1) === 'y') {
                $snakeCaseClassName = substr_replace($snakeCaseClassName, 'ie', -1);
            }

            $mapperTable = $snakeCaseClassName . 's';
        }

        $this->setTable($mapperTable);
        $this->setDatabase($database);
        $this->setComponent($component);
    }

    /**
     * @param      $id
     * @param bool $excludeArchived
     *
     * @return Output
     * @throws \Exception
     */
    public function getOneById($id, $excludeArchived = true)
    {
        $output = new Output();
        /**
         * start validating
         */
        try {
            $rules = [
                new ValidatorRule('required'),
                new ValidatorRule('id')
            ];

            $idInput = new Input('id', $rules);

            $validatorOutput = (new Validator([$idInput], ['id' => $id]))->validate();

            if ($validatorOutput->getSuccess() !== true) {
                $output->setSuccess(false);
                $output->setMessages($validatorOutput->getMessages());
                return $output;
            }
        } catch (\Exception $e) {
            (new \CodeJetter\core\ErrorHandler())->logError($e);
        }
        /**
         * finish validating
         */

        $criteria = [
            [
                'column' => 'id',
                'value' => $id,
                'type' => \PDO::PARAM_INT
            ]
        ];

        try {
            $output->setSuccess(true);
            $output->setData($this->getOne($criteria, [], $excludeArchived));
            return $output;
        } catch (\PDOException $e) {
            (new \CodeJetter\core\ErrorHandler())->logError($e);
        }
    }

    /**
     * @param array $criteria
     * @param array $fromColumns
     * @param null  $order
     * @param int   $start
     * @param int   $limit
     * @param bool  $returnTotalNo
     * @param bool  $excludeArchived
     *
     * @return mixed
     *
     * @throws \Exception
     */
    public function getAll(
        array $criteria = [],
        array $fromColumns = [],
        $order = null,
        $start = 0,
        $limit = 0,
        $returnTotalNo = false,
        $excludeArchived = true
    ) {
        // By default do not return archived records
        if ($excludeArchived === true) {
            $criteria[] = [
                'column' => 'archivedAt',
                'operator' => 'IS NULL'
            ];

            $criteria[] = [
                'column' => 'live',
                'value' => '1'
            ];
        }

        // generate the query
        $query = (new QueryMaker($this->getTable()))->selectQuery($criteria, $fromColumns, $order, $start, $limit);

        try {
            $connection = Registry::getMySQLDBClass()->getConnection($this->getDatabase());
            $st = $connection->prepare($query);

            // set last query
            $this->setLastQuery($st->queryString);

            // bind values
            $st = (new QueryMaker())->bindValues($st, $criteria, $start, $limit);
            $st->execute();

            $result = $st->fetchAll(\PDO::FETCH_CLASS, $this->getModelName());

            if ($returnTotalNo == true) {
                $total = $this->countByCriteria($criteria);
                //$total = $connection->query("SELECT COUNT(*) FROM {$this->getTable()}")->fetchColumn();
                return ['result' => $result, 'total' => $total];
            } else {
                return $result;
            }
        } catch (\PDOException $e) {
            (new \CodeJetter\core\ErrorHandler())->logError($e);
        }
    }<commit_msg>Add fetch style to getAll<commit_after><?php

namespace CodeJetter\core;

use CodeJetter\core\database\QueryMaker;
use CodeJetter\core\io\Input;
use CodeJetter\core\io\Output;
use CodeJetter\core\security\Validator;
use CodeJetter\core\security\ValidatorRule;
use CodeJetter\core\utility\MysqlUtility;
use CodeJetter\core\utility\StringUtility;

/**
 * Class BaseMapper
 * @package CodeJetter\core
 */
abstract class BaseMapper implements ICrud
{
    protected $database;
    protected $table;
    protected $modelName;
    protected $lastQuery;
    protected $component;

    /**
     * BaseMapper constructor.
     *
     * @param null $database
     * @param      $component
     */
    public function __construct($database = null, $component = null)
    {
        // TODO this needs to be in its own function
        $className = (new StringUtility())->getClassNameFromNamespace(get_called_class());
        $mapperTableRelations = Registry::getConfigClass()->get('mapperTableRelations');

        $mapperTable = isset($mapperTableRelations[$className]) ? $mapperTableRelations[$className] : '';

        if (empty($mapperTable)) {
            // mapper table is not specified in the config file, generate the name automatically

            // convert camel case to snake case
            $snakeCaseClassName = strtolower((new StringUtility())->camelCaseToSnakeCase($className));

            // remove mapper from the end of the string
            $snakeCaseClassName = preg_replace('#_mapper$#', '', $snakeCaseClassName);

            // replace 'y' with 'ie' if 'y' is the last character
            if (substr($snakeCaseClassName, -1) === 'y') {
                $snakeCaseClassName = substr_replace($snakeCaseClassName, 'ie', -1);
            }

            $mapperTable = $snakeCaseClassName . 's';
        }

        $this->setTable($mapperTable);
        $this->setDatabase($database);
        $this->setComponent($component);
    }

    /**
     * @param      $id
     * @param bool $excludeArchived
     *
     * @return Output
     * @throws \Exception
     */
    public function getOneById($id, $excludeArchived = true)
    {
        $output = new Output();
        /**
         * start validating
         */
        try {
            $rules = [
                new ValidatorRule('required'),
                new ValidatorRule('id')
            ];

            $idInput = new Input('id', $rules);

            $validatorOutput = (new Validator([$idInput], ['id' => $id]))->validate();

            if ($validatorOutput->getSuccess() !== true) {
                $output->setSuccess(false);
                $output->setMessages($validatorOutput->getMessages());
                return $output;
            }
        } catch (\Exception $e) {
            (new \CodeJetter\core\ErrorHandler())->logError($e);
        }
        /**
         * finish validating
         */

        $criteria = [
            [
                'column' => 'id',
                'value' => $id,
                'type' => \PDO::PARAM_INT
            ]
        ];

        try {
            $output->setSuccess(true);
            $output->setData($this->getOne($criteria, [], $excludeArchived));
            return $output;
        } catch (\PDOException $e) {
            (new \CodeJetter\core\ErrorHandler())->logError($e);
        }
    }

    /**
     * @param array $criteria
     * @param array $fromColumns
     * @param null  $order
     * @param int   $start
     * @param int   $limit
     * @param bool  $returnTotalNo
     * @param bool  $excludeArchived
     * @param int   $fetchStyle
     *
     * @return array
     * @throws \Exception
     */
    public function getAll(
        array $criteria = [],
        array $fromColumns = [],
        $order = null,
        $start = 0,
        $limit = 0,
        $returnTotalNo = false,
        $excludeArchived = true,
        $fetchStyle = \PDO::FETCH_CLASS
    ) {
        // By default do not return archived records
        if ($excludeArchived === true) {
            $criteria[] = [
                'column' => 'archivedAt',
                'operator' => 'IS NULL'
            ];

            $criteria[] = [
                'column' => 'live',
                'value' => '1'
            ];
        }

        // generate the query
        $query = (new QueryMaker($this->getTable()))->selectQuery($criteria, $fromColumns, $order, $start, $limit);

        try {
            $connection = Registry::getMySQLDBClass()->getConnection($this->getDatabase());
            $st = $connection->prepare($query);

            // set last query
            $this->setLastQuery($st->queryString);

            // bind values
            $st = (new QueryMaker())->bindValues($st, $criteria, $start, $limit);
            $st->execute();

            if ($fetchStyle === \PDO::FETCH_CLASS) {
                $result = $st->fetchAll(\PDO::FETCH_CLASS, $this->getModelName());
            } else {
                $result = $st->fetchAll($fetchStyle);
            }

            if ($returnTotalNo == true) {
                $total = $this->countByCriteria($criteria);
                //$total = $connection->query("SELECT COUNT(*) FROM {$this->getTable()}")->fetchColumn();
                return ['result' => $result, 'total' => $total];
            } else {
                return $result;
            }
        } catch (\PDOException $e) {
            (new \CodeJetter\core\ErrorHandler())->logError($e);
        }
    }
{'fraction_non_alphanumeric': 0.1404295051353875, 'fraction_numerical': 0.0009337068160597573, 'mean_word_length': 1.860844017094017, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 88, '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': '5067997', 'n_tokens_mistral': 3055, 'n_tokens_neox': 2933, 'n_words': 631}