<commit_before><?php

class acCouchdbJson extends acCouchdbJsonFields implements IteratorAggregate, ArrayAccess, Countable {

    /**
     *
     * @var string
     */
    private $_filter = null;
    private $_filter_persisent = false;
    private $_parent = null;

    /**
     *
     * @param acCouchdbJsonDefinition $definition
     * @param acCouchdbDocument $document
     * @param string $hash 
     */
    public function __construct(acCouchdbJsonDefinition $definition, acCouchdbDocument $document, $hash) {
        parent::__construct($definition, $document, $hash);
    }

    /**
     *
     * @param string $key_or_hash
     * @return mixed 
     */
    public function get($key_or_hash) {
        $obj_hash = new acCouchdbHash($key_or_hash);
        if ($obj_hash->isAlone()) {
            if (!$this->isArray() && $this->hasAccessor($obj_hash->getFirst())) {
                $method = $this->getAccessor($obj_hash->getFirst());
                return $this->$method();
            }
            return $this->_get($obj_hash->getFirst());
        } else {
            return $this->get($obj_hash->getFirst())->get($obj_hash->getAllWithoutFirst());
        }
    }

    /**
     *
     * @param string $key
     * @return mixed 
     */
    public function __get($key) {
        return $this->get($key);
    }

    /**
     *
     * @param string $key_or_hash
     * @return mixed 
     */
    public function getOrAdd($key_or_hash) {
        $obj_hash = new acCouchdbHash($key_or_hash);
        if ($obj_hash->isAlone()) {
            return $this->add($obj_hash->getFirst());
        }
        return $this->add($obj_hash->getFirst())->getOrAdd($obj_hash->getAllWithoutFirst());
    }

    public function set($key_or_hash, $value) {
        $obj_hash = new acCouchdbHash($key_or_hash);
        if ($obj_hash->isAlone()) {
            if (!$this->isArray() && $this->hasMutator($obj_hash->getFirst())) {
                $method = $this->getMutator($obj_hash->getFirst());
                return $this->$method($value);
            }
            return $this->_set($obj_hash->getFirst(), $value);
        } else {
            return $this->get($obj_hash->getFirst())->set($obj_hash->getAllWithoutFirst(), $value);
        }
    }

    public function __set($key, $value) {
        return $this->set($key, $value);
    }
    
    public function move($key_or_hash, $new_key_or_hash) {
        $object = $this->get($key_or_hash);
        if ($key_or_hash != $new_key_or_hash) {
            if ($this->exist($new_key_or_hash)) {
                throw new acCouchdbException($new_key_or_hash." : new key already exist");
            }
            $clone = clone $object;
            $this->remove($key_or_hash);
            $new = $this->getOrAdd($new_key_or_hash);
            return $new->getParent()->set($new->getKey(), $clone);
        }
        return $object;
    }
    
    public function moveAndClean($key_or_hash, $new_key_or_hash) {
    	$node = $this->get($key_or_hash)->getParent()->getHash();
        $object = $this->move($key_or_hash, $new_key_or_hash);
        $this->clean($node);
        return $object;
    }
    
    public function clean($key_or_hash) {
    	$object = $this->getDocument()->get($key_or_hash);
    	if ($object->count() == 0) {
    		$object->delete();
    	}
    }
    
    public function delete() {
    	return $this->getParent()->remove($this->getKey());
    }

    public function remove($key_or_hash) {
        return $this->_remove($key_or_hash);
    }

    public function add($key = null, $item = null) {
        return $this->_add($key, $item);
    }

    public function exist($key_or_hash) {
    	$obj_hash = new acCouchdbHash($key_or_hash);
        if ($obj_hash->isAlone()) {
        	return $this->_exist($obj_hash->getFirst());
        } else {
        	$exist = true;
        	$object = $this;
        	foreach ($obj_hash->toArray() as $key) {
        		if ($object->_exist($key)) {
        			$object = $object->get($key);
        		} else {
        			$exist = false;
        			break;
        		}
        	}
        	return $exist;
        }
    }

    public function __call($method, $arguments) {
        if (in_array($verb = substr($method, 0, 3), array('set', 'get'))) {
            $name = substr($method, 3);
            if ($this->exist($name)) {
                return call_user_func_array(
                        array($this, $verb), array_merge(array($name), $arguments)
                );
            } else {
                throw new acCouchdbException(sprintf('Method undefined : %s', $method));
            }
        } else {
            throw new acCouchdbException(sprintf('Method undefined : %s', $method));
        }
    }

    public function toJson() {
        return $this->getData();
    }

    public function fromArray(array $values) {
        foreach ($values as $key => $value) {
            if ($this->getDefinition()->exist($key)) {
	        	if ($this->fieldIsCollection($key) && !$value) {
	        		$value = array();
	        	}
                        
	        	if($this->fieldIsCollection($key)) {
                                $item = $this->add($key);
                                if($item->isArray()) {
                                   $item->clear();
                                }
	        		$item->fromArray($value);
	        	} else {
                            $this->add($key, $value);  <commit_msg>Fonction fromArray, si une des valeurs est null elle n'est pas setter<commit_after><?php

class acCouchdbJson extends acCouchdbJsonFields implements IteratorAggregate, ArrayAccess, Countable {

    /**
     *
     * @var string
     */
    private $_filter = null;
    private $_filter_persisent = false;
    private $_parent = null;

    /**
     *
     * @param acCouchdbJsonDefinition $definition
     * @param acCouchdbDocument $document
     * @param string $hash 
     */
    public function __construct(acCouchdbJsonDefinition $definition, acCouchdbDocument $document, $hash) {
        parent::__construct($definition, $document, $hash);
    }

    /**
     *
     * @param string $key_or_hash
     * @return mixed 
     */
    public function get($key_or_hash) {
        $obj_hash = new acCouchdbHash($key_or_hash);
        if ($obj_hash->isAlone()) {
            if (!$this->isArray() && $this->hasAccessor($obj_hash->getFirst())) {
                $method = $this->getAccessor($obj_hash->getFirst());
                return $this->$method();
            }
            return $this->_get($obj_hash->getFirst());
        } else {
            return $this->get($obj_hash->getFirst())->get($obj_hash->getAllWithoutFirst());
        }
    }

    /**
     *
     * @param string $key
     * @return mixed 
     */
    public function __get($key) {
        return $this->get($key);
    }

    /**
     *
     * @param string $key_or_hash
     * @return mixed 
     */
    public function getOrAdd($key_or_hash) {
        $obj_hash = new acCouchdbHash($key_or_hash);
        if ($obj_hash->isAlone()) {
            return $this->add($obj_hash->getFirst());
        }
        return $this->add($obj_hash->getFirst())->getOrAdd($obj_hash->getAllWithoutFirst());
    }

    public function set($key_or_hash, $value) {
        $obj_hash = new acCouchdbHash($key_or_hash);
        if ($obj_hash->isAlone()) {
            if (!$this->isArray() && $this->hasMutator($obj_hash->getFirst())) {
                $method = $this->getMutator($obj_hash->getFirst());
                return $this->$method($value);
            }
            return $this->_set($obj_hash->getFirst(), $value);
        } else {
            return $this->get($obj_hash->getFirst())->set($obj_hash->getAllWithoutFirst(), $value);
        }
    }

    public function __set($key, $value) {
        return $this->set($key, $value);
    }
    
    public function move($key_or_hash, $new_key_or_hash) {
        $object = $this->get($key_or_hash);
        if ($key_or_hash != $new_key_or_hash) {
            if ($this->exist($new_key_or_hash)) {
                throw new acCouchdbException($new_key_or_hash." : new key already exist");
            }
            $clone = clone $object;
            $this->remove($key_or_hash);
            $new = $this->getOrAdd($new_key_or_hash);
            return $new->getParent()->set($new->getKey(), $clone);
        }
        return $object;
    }
    
    public function moveAndClean($key_or_hash, $new_key_or_hash) {
    	$node = $this->get($key_or_hash)->getParent()->getHash();
        $object = $this->move($key_or_hash, $new_key_or_hash);
        $this->clean($node);
        return $object;
    }
    
    public function clean($key_or_hash) {
    	$object = $this->getDocument()->get($key_or_hash);
    	if ($object->count() == 0) {
    		$object->delete();
    	}
    }
    
    public function delete() {
    	return $this->getParent()->remove($this->getKey());
    }

    public function remove($key_or_hash) {
        return $this->_remove($key_or_hash);
    }

    public function add($key = null, $item = null) {
        return $this->_add($key, $item);
    }

    public function exist($key_or_hash) {
    	$obj_hash = new acCouchdbHash($key_or_hash);
        if ($obj_hash->isAlone()) {
        	return $this->_exist($obj_hash->getFirst());
        } else {
        	$exist = true;
        	$object = $this;
        	foreach ($obj_hash->toArray() as $key) {
        		if ($object->_exist($key)) {
        			$object = $object->get($key);
        		} else {
        			$exist = false;
        			break;
        		}
        	}
        	return $exist;
        }
    }

    public function __call($method, $arguments) {
        if (in_array($verb = substr($method, 0, 3), array('set', 'get'))) {
            $name = substr($method, 3);
            if ($this->exist($name)) {
                return call_user_func_array(
                        array($this, $verb), array_merge(array($name), $arguments)
                );
            } else {
                throw new acCouchdbException(sprintf('Method undefined : %s', $method));
            }
        } else {
            throw new acCouchdbException(sprintf('Method undefined : %s', $method));
        }
    }

    public function toJson() {
        return $this->getData();
    }

    public function fromArray(array $values) {
        foreach ($values as $key => $value) {
            if ($this->getDefinition()->exist($key)) {
	        	if ($this->fieldIsCollection($key) && !$value) {
	        		$value = array();
	        	}
                        
	        	if($this->fieldIsCollection($key)) {
                                $item = $this->add($key);
                                if($item->isArray()) {
                                   $item->clear();
                                }
	        		$item->fromArray($value);
	        	} else {
                            $this->add($key);
                            $this->set($key, $value);  
{'fraction_non_alphanumeric': 0.15531409788166545, 'fraction_numerical': 0.0007304601899196494, 'mean_word_length': 1.9083908656399362, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 152, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4916569', 'n_tokens_mistral': 3390, 'n_tokens_neox': 3325, 'n_words': 582}