<commit_before><?php

/*
 * This file is part of the 'org.octris.core' package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace org\octris\core\db\device\mongodb {
    /**
     * MongoDB data object
     *
     * @octdoc      c:mongodb/dataobject
     * @copyright   copyright (c) 2012 by <NAME>
     * @author      <NAME> <<EMAIL>>
     */
    class dataobject extends \org\octris\core\db\type\dataobject
    /**/
    {
        /**
         * Constructor.
         *
         * @octdoc  m:dataobject/__construct
         * @param   \org\octris\core\db\device\mongodb      $device         Device the connection belongs to.
         * @param   string                                  $collection     Name of collection the dataobject belongs to.
         * @param   array                                   $data           Data to initialize dataobject with,
         */
        public function __construct(\org\octris\core\db\device\mongodb $device, $collection, array $data = array())
        /**/
        {
            parent::__construct($device, $collection, $data);
        }

        /** Type casting **/
        
        /**
         * Cast a PHP type to DB internal type.
         *
         * @octdoc  m:dataobject/castPhpToDb
         * @param   mixed               $value              Value to cast.
         * @param   string              $name               Name of the value in the data structure.
         * @return  mixed                                   Casted value.
         */
        protected function castPhpToDb($value, $name)
        /**/
        {
            if ($name == '_id') {
                // _id -> MongoId
                $return = new \MongoId($value);
            } elseif (is_object($value)) {
                if ($value instanceof \org\octris\core\type\number) {
                    // number -> float -or- MongoInt64
                    $return = ($value->isDecimal()
                                ? (float)(string)$value
                                : new \MongoInt64((string)$value));
                } elseif ($value instanceof \org\octris\core\type\money) {
                    // money -> float
                   $return = (float)(string)$value;
                } elseif ($value instanceof \DateTime) {
                    // datetime -> MongoDate
                    $tmp = explode('.', $value->format('U.u'));

                    $return = new \MongoDate($tmp[0], $tmp[1]);
                } elseif ($value instanceof \org\octris\core\db\type\dbref) {
                    // dbref -> \MongoDBRef
                    $return = \MongoDBRef::create($value->collection, $value->key);
                } else {
                    $return = (string)$value;
                }
            } else {
                $return = $value;
            }
        }
        
        /**
         * Cast a DB internal type to PHP type.
         *
         * @octdoc  m:dataobject/castDbToPhp
         * @param   mixed               $value              Value to cast.
         * @param   string              $name               Name of the value in the data structure.
         * @return  mixed                                   Casted value.
         */
        protected function castDbToPhp($value, $name)
        /**/
        {
            if (is_object($value)) {
                if ($value instanceof \MongoDate) {
                    $return = new \org\octris\core\type\datetime((float)($value->sec . '.' . $value->usec));
                } elseif ($value instanceof \MongoId) {
                    $return = (string)$value;
                } elseif ($value instanceof \MongoInt32) {
                    $return = new \org\octris\core\type\number((string)$value);
                } elseif ($value instanceof \MongoInt64) {
                    $return = new \org\octris\core\type\number((string)$value);
                } else {
                    $return = $value;
                }
            } else {
                $return = $value;
            }

            return $return;
        }
    }
}
<commit_msg>use own import method to detect database references stored as array<commit_after><?php

/*
 * This file is part of the 'org.octris.core' package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace org\octris\core\db\device\mongodb {
    /**
     * MongoDB data object
     *
     * @octdoc      c:mongodb/dataobject
     * @copyright   copyright (c) 2012 by <NAME>
     * @author      <NAME> <<EMAIL>>
     */
    class dataobject extends \org\octris\core\db\type\dataobject
    /**/
    {
        /**
         * Constructor.
         *
         * @octdoc  m:dataobject/__construct
         * @param   \org\octris\core\db\device\mongodb      $device         Device the connection belongs to.
         * @param   string                                  $collection     Name of collection the dataobject belongs to.
         * @param   array                                   $data           Data to initialize dataobject with,
         */
        public function __construct(\org\octris\core\db\device\mongodb $device, $collection, array $data = array())
        /**/
        {
            parent::__construct($device, $collection, $data);
        }

        /** Type casting **/
        
        /**
         * Cast a PHP type to DB internal type.
         *
         * @octdoc  m:dataobject/castPhpToDb
         * @param   mixed               $value              Value to cast.
         * @param   string              $name               Name of the value in the data structure.
         * @return  mixed                                   Casted value.
         */
        protected function castPhpToDb($value, $name)
        /**/
        {
            if ($name == '_id') {
                // _id -> MongoId
                $return = new \MongoId($value);
            } elseif (is_object($value)) {
                if ($value instanceof \org\octris\core\type\number) {
                    // number -> float -or- MongoInt64
                    $return = ($value->isDecimal()
                                ? (float)(string)$value
                                : new \MongoInt64((string)$value));
                } elseif ($value instanceof \org\octris\core\type\money) {
                    // money -> float
                   $return = (float)(string)$value;
                } elseif ($value instanceof \DateTime) {
                    // datetime -> MongoDate
                    $tmp = explode('.', $value->format('U.u'));

                    $return = new \MongoDate($tmp[0], $tmp[1]);
                } elseif ($value instanceof \org\octris\core\db\type\dbref) {
                    // dbref -> \MongoDBRef
                    $return = \MongoDBRef::create($value->collection, $value->key);
                } else {
                    $return = (string)$value;
                }
            } else {
                $return = $value;
            }
        }
        
        /**
         * Cast a DB internal type to PHP type.
         *
         * @octdoc  m:dataobject/castDbToPhp
         * @param   mixed               $value              Value to cast.
         * @param   string              $name               Name of the value in the data structure.
         * @return  mixed                                   Casted value.
         */
        protected function castDbToPhp($value, $name)
        /**/
        {
            if (is_object($value)) {
                if ($value instanceof \MongoDate) {
                    $return = new \org\octris\core\type\datetime((float)($value->sec . '.' . $value->usec));
                } elseif ($value instanceof \MongoId) {
                    $return = (string)$value;
                } elseif ($value instanceof \MongoInt32) {
                    $return = new \org\octris\core\type\number((string)$value);
                } elseif ($value instanceof \MongoInt64) {
                    $return = new \org\octris\core\type\number((string)$value);
                } else {
                    $return = $value;
                }
            } else {
                $return = $value;
            }

            return $return;
        }
        
        /**
         * Recursive data iteration and casting for preparing data for import into dataobject.
         *
         * @octdoc  m:dataobject/import
         * @param   array               $data               Data to process.
         */
        protected function import(array &$data)
        /**/
        {
            $process = function(&$data) use (&$process) {
                array_walk($data, function(&$value, $name) {
                    if (is_array($value)) {
                        if (\MongoDBRef::isRef($value)) {
                            $value = new \org\octris\core\db\type\dbref(
                                $value['$ref'], $value['$id']
                            );
                        } else {
                            $process($a);
                        }
                    } else {
                        $value = $this->castDbToPhp($value, $name);
                    }
                });
            };
        }
    }
}

{'fraction_non_alphanumeric': 0.11534774736393652, 'fraction_numerical': 0.0029822132282458194, 'mean_word_length': 1.1273221567739011, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3737769', 'n_tokens_mistral': 2469, 'n_tokens_neox': 2359, 'n_words': 576}