<commit_before><?php

namespace Gotron;

use ActiveRecord;

class Model extends ActiveRecord\Model {

    static $auto_load_associations_for_model = array();
	static $auto_load_associations_for_finder = array();
	static $auto_include_in_dump = array();
	static $auto_exclude_in_dump = array();
	static $finders = array();
    static $finder_default_ttl = 120;
    static $cache_ttl = 3600;
			
	public function __construct(  $attributes=array(), $guard_attributes=true, $instantiating_via_find=false, $new_record=true)
	{
		if (is_string($attributes) || is_int($attributes)){

			parent::__construct (array(), $guard_attributes, $instantiating_via_find, false);
			
            return $this->load_from_static_find_obj(static::find($attributes, array('limit'=>1)));
			
			
		}
		else {
			parent::__construct ($attributes, $guard_attributes, $instantiating_via_find, $new_record);
		}

	}


	private function load_from_static_find_obj($obj){
		$this->set_attributes($obj->attributes());
		$this->reset_dirty();
	}

	public function dump($args = array() ){
        
		$arr = $this->attributes();
        $ret = array();

        foreach(static::$auto_exclude_in_dump as $key){
            unset($arr[$key]);
        }

        $includes = array();
        if (isset($args['include']))
            $includes = $args['include'];

        $keys = array_unique(array_merge(array_keys($arr),static::$auto_include_in_dump,$includes));
                
        foreach($keys as $key){
            $varName = Helper::camelize($key);
            $ret[$varName] = $this->_get_dump_value($key);
        }
		return $ret;
	}

    public function _get_dump_value($k){
        $value = $this->$k;
        if(!is_array($value) && !is_object($value))
        {
            return $value;
        }
        elseif (is_array($value)){
            $varValue = array();    
            foreach ($value as $o){
                if (isset($o) && ($o instanceof Model) ){
                    $varValue[] =$o->dump();
                }
                else {
                    $varValue[] = $o;
                }
            }
            return $varValue;
            
        }
        elseif (is_object($value)){
            if (($value instanceof Model) ) {
                $varValue = $this->$k->dump();
                return $varValue;
            }
        }
        return NULL;

    }
	
	public function to_array(array $options = array()){
		$arr = $this->attributes();
		
		return $arr;
	}
	
	/**
     * Takes an array of objects, filters by a unique attribute and returns an array of a limited number of objects
     * 
     * @param array $objects
     * @param string $limit
     * @param string $attribute 
     * @return array
     */
     
    public static function unique($objects,$attribute,$limit = 10){
        $used_attributes = array();
        $returned_objects = array();
        foreach($objects as $object){
            $value = $object->read_attribute($attribute);
            if(!in_array($value,$used_attributes)){
                $used_attributes[] = $object->read_attribute($attribute);
                $returned_objects[] = $object;
                if(count($returned_objects) == $limit){
                    return $returned_objects;
                }
            }
        }
        return $returned_objects;
    }
    
    /**
     * Loads options from predefined finder and runs a find on the Model
     *
     * - set or override conditions by setting conditions array
     * - set or override limit, offset, order by, etc.. by setting filters array
     * - if an attribute appears multiple times in the conditions string set the attribute    
     *   as an array with each value
     *
     * @param string $name 
     * @param array $additional_options
     * @param array $filters
     * @return array of model objects
     */
    public static function finder($name,$conditions = array(),$filters = array(), $ignore_cache = false){
        if(array_key_exists($name,static::$finders)){
            $cache_id = self::finder_cache_id($name,$conditions,$filters);
            $found_objects = false;
            if (!$ignore_cache) {
                if($finder = \ActiveRecord\Cache::fetch($cache_id))
                {<commit_msg>Updated Model::unique to use id attribute and not limit by default<commit_after><?php

namespace Gotron;

use ActiveRecord;

class Model extends ActiveRecord\Model {

    static $auto_load_associations_for_model = array();
	static $auto_load_associations_for_finder = array();
	static $auto_include_in_dump = array();
	static $auto_exclude_in_dump = array();
	static $finders = array();
    static $finder_default_ttl = 120;
    static $cache_ttl = 3600;
			
	public function __construct(  $attributes=array(), $guard_attributes=true, $instantiating_via_find=false, $new_record=true)
	{
		if (is_string($attributes) || is_int($attributes)){

			parent::__construct (array(), $guard_attributes, $instantiating_via_find, false);
			
            return $this->load_from_static_find_obj(static::find($attributes, array('limit'=>1)));
			
			
		}
		else {
			parent::__construct ($attributes, $guard_attributes, $instantiating_via_find, $new_record);
		}

	}


	private function load_from_static_find_obj($obj){
		$this->set_attributes($obj->attributes());
		$this->reset_dirty();
	}

	public function dump($args = array() ){
        
		$arr = $this->attributes();
        $ret = array();

        foreach(static::$auto_exclude_in_dump as $key){
            unset($arr[$key]);
        }

        $includes = array();
        if (isset($args['include']))
            $includes = $args['include'];

        $keys = array_unique(array_merge(array_keys($arr),static::$auto_include_in_dump,$includes));
                
        foreach($keys as $key){
            $varName = Helper::camelize($key);
            $ret[$varName] = $this->_get_dump_value($key);
        }
		return $ret;
	}

    public function _get_dump_value($k){
        $value = $this->$k;
        if(!is_array($value) && !is_object($value))
        {
            return $value;
        }
        elseif (is_array($value)){
            $varValue = array();    
            foreach ($value as $o){
                if (isset($o) && ($o instanceof Model) ){
                    $varValue[] =$o->dump();
                }
                else {
                    $varValue[] = $o;
                }
            }
            return $varValue;
            
        }
        elseif (is_object($value)){
            if (($value instanceof Model) ) {
                $varValue = $this->$k->dump();
                return $varValue;
            }
        }
        return NULL;

    }
	
	public function to_array(array $options = array()){
		$arr = $this->attributes();
		
		return $arr;
	}
	
	/**
     * Takes an array of objects, filters by a unique attribute and returns an array of a limited number of objects
     * 
     * @param array $objects
     * @param string $limit
     * @param string $attribute 
     * @return array
     */
     
    public static function unique($objects, $attribute = 'id', $limit = null){
        $used_attributes = array();
        $returned_objects = array();
        foreach($objects as $object){
            $value = $object->read_attribute($attribute);
            if(!in_array($value,$used_attributes)){
                $used_attributes[] = $object->read_attribute($attribute);
                $returned_objects[] = $object;
                if(!is_null($limit) && count($returned_objects) == $limit){
                    return $returned_objects;
                }
            }
        }

        return $returned_objects;
    }
    
    /**
     * Loads options from predefined finder and runs a find on the Model
     *
     * - set or override conditions by setting conditions array
     * - set or override limit, offset, order by, etc.. by setting filters array
     * - if an attribute appears multiple times in the conditions string set the attribute    
     *   as an array with each value
     *
     * @param string $name 
     * @param array $additional_options
     * @param array $filters
     * @return array of model objects
     */
    public static function finder($name,$conditions = array(),$filters = array(), $ignore_cache = false){
        if(array_key_exists($name,static::$finders)){
            $cache_id = self::finder_cache_id($name,$conditions,$filters);
            $found_objects = false;
            if (!$ignore_cache) {
                if($finder = \ActiveRecord\Cache::fetch($cache_id))
                {
{'fraction_non_alphanumeric': 0.1285397972264305, 'fraction_numerical': 0.0020976576156625103, 'mean_word_length': 2.238490566037736, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '3265369', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2374, 'n_words': 566}