<commit_before><?php
namespace MarketMeSuite\Phranken\Database\Object;

use MarketMeSuite\Phranken\Database\Interfaces\IDbObject;
use MarketMeSuite\Phranken\Database\Exception\DbObjectException;
use MarketMeSuite\Phranken\Crypt\UniqueIDGenerator;

/**
 * Provides a way to load database objects into a well defined class structure
 */
abstract class DbObject implements IDbObject
{
    /**
     * The default "id" field for records
     * In mongo this is usually '_id' and in mysql it is 'id'
     * Because DbObject is database agnostic this is able to be overriden
     * by subclasses that may be for other database engines.
     * @var string
     */
    public static $ID_FIELD = '_id';

    /**
     * @var array An array of key -> value pairs
     */
    public $map;

    /**
     * loads a mongo document array into this object structure
     * @param  array  $data A document array from a mongo query
     */
    public function fromArray(array $data)
    {
        $map = $this->getMap();

        foreach ($map as $dbKey => $variableName) {

            if (! property_exists($this, $variableName)) {
                throw new DbObjectException('property "' . $variableName . '" was not found in this object');
            }

            // if the key exists then set the property
            if (isset($data[$dbKey])) {
                $this->{$variableName} = $data[$dbKey];
            }
        }
    }

    /**
     * Converts this object into a database-compatable associative array
     * @return array An associative array representing this object
     */
    public function toArray()
    {
        $map = $this->getMap();
        $out = array();

        foreach ($map as $dbKey => $variableName) {

            $value = $this->getProp($variableName);

            // If the property is an instance of
            // IDbObject then execute it's toArray
            // and use the result as the value
            if ($value instanceof IDbObject) {
                $value = $value->toArray();
            }

            $out[$dbKey] = $value;
        }



        return $out;
    }

    /**
     * Converts this object into particular mongo queries
     * @param  string $type The type of query to export
     * @return array        The result of toArray() with particular fields ommitted/added
     *                      depending on the type
     */
    public function toQuery($type)
    {
        $arr = $this->toArray();
        switch($type) {
            case 'set':
                // the _id field can not exist for set queries
                unset( $arr[$this->getIdFieldName()] );
                return $arr;
            break;
            case 'insert':
                // genreate a UUID for the insert query
                $arr[$this->getIdFieldName()] = UniqueIDGenerator::GenerateUUID();
                return $arr;
            break;
            default:
                throw new DbObjectException('query type "' . $type . '" has no actions');
            break;
        }
    }

    /**
     * sets an object property
     * @param string $key   The name of the property
     * @param mixed  $value The value to set $key to
     */
    public function setProp($key, $value)
    {
        $map = $this->getMap();

        // does the key exist in the map?
        if (!in_array($key, $map)) {
            throw new DbObjectException('"' . $key . '" does not exist in map');
        }

        $this->{$key} = $value;
    }

    /**
     * Gets a property
     * @param  string $key The name of the property
     * @return mixed       The value of $key
     */
    public function getProp($key)
    {
        $map = $this->getMap();

        if (!in_array($key, $map)) {
            throw new DbObjectException('"' . $key . '" does not exist in map');
        }

        return $this->{$key};
    }

    /**
     * Runs multiple fromArray() for every item in $arr
     * @param array $arr An array of mongo documents, Can also be a mongo cursor
     * @param string $class A valid class name that implements IDbObject
     * @return array An array of objects
     * @throws DbObjectException When $class does not implement IDbObject
     */
    public static function multiFromArray(array $arr, $class = 'DbObject')
    {

        if (new $class instanceof IDbObject) {
            // nothing
        } else {
            throw new DbObjectException('$class must implement IDbObject');
        }

        $objects = array();

        foreach ($arr as $dbArr) {
            $newObject = new $class;
            $newObject->fromArray($dbArr);
            $objects[] = $newObject;
        }

        return $objects;
    }

    /**
     * Runs multiple toArray() for every IDbObject in $arr
     * @param array $arr An array of IDbObject instances
     * @return array An array of arrays where each sub array is the result of calling
     * toArray on each objecting in $arr
     * @throws DbObjectException When an object in $arr does not implement IDbObject
     */
    public static function multiToArray(array $arr)
    {
        $arrs = array();

        foreach ($arr as $dbobject) {

            if ($dbobject instanceof IDbObject) {
                // nothing
            } else {
                throw new DbObjectException('$dbobject must implement IDbObject');
            }

            $arrs[] = $dbobject->toArray();
        }

        return $arrs;
    }

    /**
     * @see IDbObject::getMap
     */<commit_msg>format docblocks nicely<commit_after><?php
namespace MarketMeSuite\Phranken\Database\Object;

use MarketMeSuite\Phranken\Database\Interfaces\IDbObject;
use MarketMeSuite\Phranken\Database\Exception\DbObjectException;
use MarketMeSuite\Phranken\Crypt\UniqueIDGenerator;

/**
 * Provides a way to load database objects into a well defined class structure
 */
abstract class DbObject implements IDbObject
{
    /**
     * The default "id" field for records
     * In mongo this is usually '_id' and in mysql it is 'id'
     * Because DbObject is database agnostic this is able to be overridden
     * by subclasses that may be for other database engines.
     * @var string
     */
    public static $ID_FIELD = '_id';

    /**
     * @var array An array of key => value pairs
     */
    public $map;

    /**
     * loads a mongo document array into this object structure
     *
     * @param  array $data A document array from a mongo query
     *
     * @throws DbObjectException If a required mapped property does not exist in $data
     */
    public function fromArray(array $data)
    {
        $map = $this->getMap();

        foreach ($map as $dbKey => $variableName) {

            if (! property_exists($this, $variableName)) {
                throw new DbObjectException('property "' . $variableName . '" was not found in this object');
            }

            // if the key exists then set the property
            if (isset($data[$dbKey])) {
                $this->{$variableName} = $data[$dbKey];
            }
        }
    }

    /**
     * Converts this object into a database-compatable associative array
     * @return array An associative array representing this object
     */
    public function toArray()
    {
        $map = $this->getMap();
        $out = array();

        foreach ($map as $dbKey => $variableName) {

            $value = $this->getProp($variableName);

            // If the property is an instance of
            // IDbObject then execute it's toArray
            // and use the result as the value
            if ($value instanceof IDbObject) {
                $value = $value->toArray();
            }

            $out[$dbKey] = $value;
        }

        return $out;
    }

    /**
     * Converts this object into particular mongo queries
     *
     * @param  string $type The type of query to export
     *
     * @return array        The result of toArray() with particular fields ommitted/added
     *                      depending on the type
     * @throws DbObjectException If $type is not a recognised type
     */
    public function toQuery($type)
    {
        $arr = $this->toArray();
        switch($type) {
            case 'set':
                // the _id field can not exist for set queries
                unset( $arr[$this->getIdFieldName()] );
                return $arr;
            break;
            case 'insert':
                // genreate a UUID for the insert query
                $arr[$this->getIdFieldName()] = UniqueIDGenerator::GenerateUUID();
                return $arr;
            break;
            default:
                throw new DbObjectException('query type "' . $type . '" has no actions');
            break;
        }
    }

    /**
     * sets an object property
     *
     * @param string $key   The name of the property
     * @param mixed  $value The value to set $key to
     *
     * @throws DbObjectException If $key does not exist in the configured map
     */
    public function setProp($key, $value)
    {
        $map = $this->getMap();

        // does the key exist in the map?
        if (!in_array($key, $map)) {
            throw new DbObjectException('"' . $key . '" does not exist in map');
        }

        $this->{$key} = $value;
    }

    /**
     * Gets a property
     * @param  string $key The name of the property
     * @return mixed       The value of $key
     *
     * @throws DbObjectException When $key does not exist in map
     */
    public function getProp($key)
    {
        $map = $this->getMap();

        if (!in_array($key, $map)) {
            throw new DbObjectException('"' . $key . '" does not exist in map');
        }

        return $this->{$key};
    }

    /**
     * Runs multiple fromArray() for every item in $arr
     *
     * @param array  $arr   An array of mongo documents, Can also be a mongo cursor
     * @param string $class A valid class name that implements IDbObject
     *
     * @return array An array of objects
     * @throws DbObjectException When $class does not implement IDbObject
     */
    public static function multiFromArray(array $arr, $class = 'DbObject')
    {

        if (new $class instanceof IDbObject) {
            // nothing
        } else {
            throw new DbObjectException('$class must implement IDbObject');
        }

        $objects = array();

        foreach ($arr as $dbArr) {
            $newObject = new $class;
            $newObject->fromArray($dbArr);
            $objects[] = $newObject;
        }

        return $objects;
    }

    /**
     * Runs multiple toArray() for every IDbObject in $arr
     *
     * @param array $arr An array of IDbObject instances
     *
     * @return array An array of arrays where each sub array is the result of calling
     * toArray on each objecting in $arr
     * @throws DbObjectException When an object in $arr does not implement IDbObject
     */
    public static function multiToArray(array $arr)
    {
        $arrs = array();

        foreach ($arr as $dbobject) {

            if ($dbobject instanceof IDbObject) {
                // nothing
            } else {
                throw new DbObjectException('$dbobject must implement IDbObject');
            }

            $arrs[] = $dbobject->toArray();
        }

        return $arrs;
    }

    /**
     * @see IDbObject::getMap
     */
{'fraction_non_alphanumeric': 0.10182813741941181, 'fraction_numerical': 0.0, 'mean_word_length': 1.827465667915106, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '5523419', 'n_tokens_mistral': 3117, 'n_tokens_neox': 2999, 'n_words': 1067}