<commit_before><?php
/**
 * Boostack: BaseClass.Class.php
 * ========================================================================
 * Copyright 2014-2017 <NAME>
 * Licensed under MIT (https://github.com/offmania9/Boostack/blob/master/LICENSE)
 * ========================================================================
 * @author <NAME> <<EMAIL>>
 * @version 3.0
 */
abstract class BaseClass implements JsonSerializable {

    /**
     * @var
     */
    protected $id;
    /**
     * @var
     */
    protected $pdo; // TODO: static? Find a way to automatically inject db instance into this field?
    /**
     * @var array
     */
    protected $default_values = [];
    /**
     * @var array
     */
    protected $system_excluded = ['id','default_values','system_excluded','custom_excluded','pdo'];
    /**
     * @var array
     */
    protected $custom_excluded = [];
    /**
     *
     */
    const TABLENAME = "";


    /**
     * Init method: creates the PDO object.
     * Call this in your __construct() function with parent::init()
     */
    protected function init($id = NULL) {
        $this->pdo = Database_PDO::getInstance();
        if($id !== NULL) {
            $this->load($id);
        } else {
            $this->prepare();
        }
    }

    /**
     * Fill the object
     *
     * @param $array
     * @return bool
     * @throws Exception
     */
    public function fill($array) {
        $this->prepare($array);
        return true;
    }

    /**
     * @param $array
     * @return bool
     */
    public function clearAndFill($array){
        $defaultValuesKeys = array_keys($this->default_values);
        $inputKeys = array_keys($array);
        $fieldsNotPresent = array_diff($inputKeys,$defaultValuesKeys,$this->system_excluded,$this->custom_excluded);
        if(count($fieldsNotPresent) > 0) {
            foreach ($fieldsNotPresent as $value)
                unset($array[$value]);
        }
        return $this->fill($array);
    }

    /**
     * Load the object from the database
     *
     * @param $id
     * @return bool
     * @throws Exception
     */
    public function load($id) {
        try {
            $sql = "SELECT * FROM " . static::TABLENAME . " WHERE id = :id";
            $q = $this->pdo->prepare($sql);
            $q->bindValue(':id', $id);
            $q->execute();
            $result = $q->fetch(PDO::FETCH_ASSOC);
            if(empty($result)) {
                throw new Exception("No result found in table ".static::TABLENAME." with ID ".$id);
            }
            $this->prepare($result);
            $this->id = $id;
            return true;
        } catch(PDOException $pdoEx) {
            FileLogger::getInstance()->log($pdoEx);
            throw new PDOException("Database Exception. Please see log file.");
        }
    }

    /**
     * Check if object with specified id exist
     *
     * @param $id
     * @return bool
     */
    public function exist($id) {
        try {
            $sql = "SELECT id FROM " . static::TABLENAME . " WHERE id = :id";
            $q = $this->pdo->prepare($sql);
            $q->bindValue(':id', $id);
            $q->execute();
            $result = $q->fetch(PDO::FETCH_ASSOC);
            return !empty($result);
        } catch(PDOException $pdoEx) {
            FileLogger::getInstance()->log($pdoEx);
            throw new PDOException("Database Exception. Please see log file.");
        }
    }

    /**
     * Save the object into the database
     *
     * @param $forcedID: if not null, insert the entity with $forceID as id
     *
     * @return bool
     */
    public function save($forcedID = null) {
        try {
            if(empty($this->id)) {<commit_msg>Updated exist function to static.<commit_after><?php
/**
 * Boostack: BaseClass.Class.php
 * ========================================================================
 * Copyright 2014-2017 <NAME>
 * Licensed under MIT (https://github.com/offmania9/Boostack/blob/master/LICENSE)
 * ========================================================================
 * @author <NAME> <<EMAIL>>
 * @version 3.0
 */
abstract class BaseClass implements JsonSerializable {

    /**
     * @var
     */
    protected $id;
    /**
     * @var
     */
    protected $pdo; // TODO: static? Find a way to automatically inject db instance into this field?
    /**
     * @var array
     */
    protected $default_values = [];
    /**
     * @var array
     */
    protected $system_excluded = ['id','default_values','system_excluded','custom_excluded','pdo'];
    /**
     * @var array
     */
    protected $custom_excluded = [];
    /**
     *
     */
    const TABLENAME = "";


    /**
     * Init method: creates the PDO object.
     * Call this in your __construct() function with parent::init()
     */
    protected function init($id = NULL) {
        $this->pdo = Database_PDO::getInstance();
        if($id !== NULL) {
            $this->load($id);
        } else {
            $this->prepare();
        }
    }

    /**
     * Fill the object
     *
     * @param $array
     * @return bool
     * @throws Exception
     */
    public function fill($array) {
        $this->prepare($array);
        return true;
    }

    /**
     * @param $array
     * @return bool
     */
    public function clearAndFill($array){
        $defaultValuesKeys = array_keys($this->default_values);
        $inputKeys = array_keys($array);
        $fieldsNotPresent = array_diff($inputKeys,$defaultValuesKeys,$this->system_excluded,$this->custom_excluded);
        if(count($fieldsNotPresent) > 0) {
            foreach ($fieldsNotPresent as $value)
                unset($array[$value]);
        }
        return $this->fill($array);
    }

    /**
     * Load the object from the database
     *
     * @param $id
     * @return bool
     * @throws Exception
     */
    public function load($id) {
        try {
            $sql = "SELECT * FROM " . static::TABLENAME . " WHERE id = :id";
            $q = $this->pdo->prepare($sql);
            $q->bindValue(':id', $id);
            $q->execute();
            $result = $q->fetch(PDO::FETCH_ASSOC);
            if(empty($result)) {
                throw new Exception("No result found in table ".static::TABLENAME." with ID ".$id);
            }
            $this->prepare($result);
            $this->id = $id;
            return true;
        } catch(PDOException $pdoEx) {
            FileLogger::getInstance()->log($pdoEx);
            throw new PDOException("Database Exception. Please see log file.");
        }
    }

    /**
     * Check if object with specified id exist
     *
     * @param $id
     * @return bool
     */
    public static function exist($id) {
        try {
            $pdo = Database_PDO::getInstance();
            $sql = "SELECT id FROM " . static::TABLENAME . " WHERE id = :id";
            $q = $pdo->prepare($sql);
            $q->bindValue(':id', $id);
            $q->execute();
            $result = $q->fetch(PDO::FETCH_ASSOC);
            return !empty($result);
        } catch(PDOException $pdoEx) {
            FileLogger::getInstance()->log($pdoEx);
            throw new PDOException("Database Exception. Please see log file.");
        }
    }

    /**
     * Save the object into the database
     *
     * @param $forcedID: if not null, insert the entity with $forceID as id
     *
     * @return bool
     */
    public function save($forcedID = null) {
        try {
            if(empty($this->id)) {
{'fraction_non_alphanumeric': 0.18191531988780554, 'fraction_numerical': 0.0032055562975824765, 'mean_word_length': 1.963197467352592, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 58, 'https://': 2, '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': '5809265', 'n_tokens_mistral': 2165, 'n_tokens_neox': 2088, 'n_words': 493}