<commit_before><?php

namespace Face\Traits;

use \Face\Core\EntityFaceElement;
use Face\Core\Navigator;

trait EntityFaceTrait {
    
    /**
     * use the given element and use the right way for getting the value on this instance
     * @param \Face\Core\EntityFaceElement $element the element to get
     * @return mixed
     */
    public function faceGetter($needle){
        
        // look the type of $needle then dispatch
        if( is_string($needle) ){
            /*
             * if it is a string the string can be the name of the element or a chain of elements separated by a dot
             * e.g 
             *  - first form : "elementName"
             *  - secnd form : "elementName1.elementName2.elementName3"
             */
            // TODO catch "this.elementName" case for dont instanciate a Navigator not needed for performances
            if(false!==strpos($needle, "."))
                return (new Navigator($needle))->chainGet($this); // "elementName1.elementName2.elementName3" case
            else
                $element=$this->getEntityFace()->getElement($needle); // "elementName" case
            
        }else if(is_a($needle, "\Face\Core\EntityFaceElement"))
            /*  if is already a face element, dont beed anymore work */
            $element=$needle;
        else
            throw new Exception("Variable of type '".gettype($needle)."' is not a valide type for faceGetter");


        // if has a getter, it can be a custom callable annonymous function, or the name of the the method to call on this object
        if($element->hasGetter()){
            
            $getter = $element->getGetter();
            if(is_string($getter)){ //method of this object
                return $this->$getter();
            }else if(is_callable($getter)){ // custom callable
                return $getter();
            }else{
                throw new Exception('Getter is set but it is not usable : '.var_export($getter,true));
            }
        
        // else we use the property directly
        }else{
            
            $property = $element->getPropertyName();
            return $this->$property;
            
        }
        
        // TODO throw exception on no way to get element
    }
    
    
    
    /**
     * use the given element and use the right way for getting the value on this instance
     * @param \Face\Core\EntityFaceElement $element the element to get
     * @return mixed
     */
    public function faceSetter($path,$value){
        
        // look the type of $needle then dispatch
        if( is_string($path) ){
            /*
             * if it is a string the string can be the name of the element or a chain of elements separated by a dot
             * e.g 
             *  - first form : "elementName"
             *  - secnd form : "elementName1.elementName2.elementName3"
             */
            // TODO catch "this.elementName" case for dont instanciate a Navigator not needed for performances
            
            if(false!==strpos($path, ".")){// "elementName1.elementName2.elementName3" case
                (new Navigator($path))->chainSet($this, $value);
                return $value;
            }else{
                $element=$this->getEntityFace()->getElement($path);
            }
            
        }else if(is_a($path, "\Face\Core\EntityFaceElement")){
            /*  if is already a face element, dont need anymore work */
            $element=$path;
        }else
            throw new Exception("Variable of type '".gettype($path)."' is not a valide type for faceSetter");
        
        /* @var $lastElement \Face\Core\EntityFaceElement */
        
        // if has a getter, it can be a custom callable anonymous function, or the name of the the method to call on this object
        if($element->hasSetter()){
            
            $setter = $element->getSetter();
            if(is_string($setter)){ //method of this object
                return $this->$setter($value);
            }else if(is_callable($setter)){ // custom callable
                return $setter($value);
            }else{
                throw new Exception('Setter is set but it is not usable : '.var_export($setter,true));
            }
        
        // else we use the property directly
        }else{
            
            $property = $element->getPropertyName();
            if(!empty($property))
                $this->$property=$value;
            else
                ; // TODO  exception or something else ?
            
        }
        // TODO chainSet in Navigator instead than in this trait
        // TODO throw exception on "no way to get element"

    }
    
    
    
    /**
     * 
     * @return \Face\Core\EntityFace
     */
    public static function getEntityFace(){
        return \Face\Core\FacePool::getFace(__CLASS__);
    }
    
    public function faceHydrate($data,$map){
        foreach($map as $elmName=>$dataName){
            $this->faceSetter($elmName,$data[$dataName]);
        }
    }
    
    /**
     * Takes the DefaultMap contained in this face to create a default map.
     * The given array (the map) maps the element name (the key) to a string (which most of time represnts the sql column name).
     * Each line of the array has this form : ElementName => Wished name
     * @param array $exclude Elements to exclude. e.g. : if you want to exlucde elements "id" and "name" just use ["id","name"]
     * @param array $include Elements to include or replace. e.g : if "name" has not default it wont be included if you dont specify it in the map. Just use ["name"=>"name"]
     * @return array The map with the form [ElementName => Wished name]
     */
    public static function faceDefaultMap($exclude=[],$include=[]){
        $map=array();
        foreach($this as $elm){
            /* @var $elm EntityFaceElement */
            if(!in_array($elm->getName(), $exclude))
                $map[$elm->getName()]=$elm->getDefaultMap();
        }
        array_replace($map,$include);
        
        return $map;
    }
    
    abstract public static function __getEntityFace();
    
}

?><commit_msg>Clean Version<commit_after><?php

namespace Face\Traits;

use \Face\Core\EntityFaceElement;
use Face\Core\Navigator;

trait EntityFaceTrait {
    
    /**
     * use the given element and use the right way for getting the value on this instance
     * @param \Face\Core\EntityFaceElement $element the element to get
     * @return mixed
     */
    public function faceGetter($needle){
        
        // look the type of $needle then dispatch
        if( is_string($needle) ){
            /*
             * if it is a string the string can be the name of the element or a chain of elements separated by a dot
             * e.g 
             *  - first form : "elementName"
             *  - secnd form : "elementName1.elementName2.elementName3"
             */
            // TODO catch "this.elementName" case for dont instanciate a Navigator not needed for performances
            if(false!==strpos($needle, "."))
                return (new Navigator($needle))->chainGet($this); // "elementName1.elementName2.elementName3" case
            else
                $element=$this->getEntityFace()->getElement($needle); // "elementName" case
            
        }else if(is_a($needle, "\Face\Core\EntityFaceElement"))
            /*  if is already a face element, dont beed anymore work */
            $element=$needle;
        else
            throw new Exception("Variable of type '".gettype($needle)."' is not a valide type for faceGetter");


        // if has a getter, it can be a custom callable annonymous function, or the name of the the method to call on this object
        if($element->hasGetter()){
            
            $getter = $element->getGetter();
            if(is_string($getter)){ //method of this object
                return $this->$getter();
            }else if(is_callable($getter)){ // custom callable
                return $getter();
            }else{
                throw new Exception('Getter is set but it is not usable : '.var_export($getter,true));
            }
        
        // else we use the property directly
        }else{
            
            $property = $element->getPropertyName();
            return $this->$property;
            
        }
        
        // TODO throw exception on no way to get element
    }
    
    
    
    /**
     * use the given element and use the right way for getting the value on this instance
     * @param \Face\Core\EntityFaceElement $element the element to get
     * @return mixed
     */
    public function faceSetter($path,$value){
        
        // look the type of $needle then dispatch
        if( is_string($path) ){
            /*
             * if it is a string the string can be the name of the element or a chain of elements separated by a dot
             * e.g 
             *  - first form : "elementName"
             *  - secnd form : "elementName1.elementName2.elementName3"
             */
            // TODO catch "this.elementName" case for dont instanciate a Navigator not needed for performances
            
            if(false!==strpos($path, ".")){// "elementName1.elementName2.elementName3" case
                (new Navigator($path))->chainSet($this, $value);
                return $value;
            }else{
                $element=$this->getEntityFace()->getElement($path);
            }
            
        }else if(is_a($path, "\Face\Core\EntityFaceElement")){
            /*  if is already a face element, dont need anymore work */
            $element=$path;
        }else
            throw new Exception("Variable of type '".gettype($path)."' is not a valide type for faceSetter");
        
        /* @var $lastElement \Face\Core\EntityFaceElement */
        
        // if has a getter, it can be a custom callable anonymous function, or the name of the the method to call on this object
        if($element->hasSetter()){
            
            $setter = $element->getSetter();
            if(is_string($setter)){ //method of this object
                return $this->$setter($value);
            }else if(is_callable($setter)){ // custom callable
                return $setter($value);
            }else{
                throw new Exception('Setter is set but it is not usable : '.var_export($setter,true));
            }
        
        // else we use the property directly
        }else{
            
            $property = $element->getPropertyName();
            if(!empty($property))
                $this->$property=$value;
            else
                ; // TODO  exception or something else ?
            
        }
        // TODO chainSet in Navigator instead than in this trait
        // TODO throw exception on "no way to get element"

    }
    
    
    
    /**
     * 
     * @return \Face\Core\EntityFace
     */
    public static function getEntityFace(){
        return \Face\Core\FacePool::getFace(__CLASS__);
    }
    
    public function faceHydrate($data,$map){
        foreach($map as $elmName=>$dataName){
            $this->faceSetter($elmName,$data[$dataName]);
        }
    }
    
    /**
     * Takes the DefaultMap contained in this face to create a default map.
     * The given array (the map) maps the element name (the key) to a string (which most of time represnts the sql column name).
     * Each line of the array has this form : ElementName => Wished name
     * @param array $exclude Elements to exclude. e.g. : if you want to exlucde elements "id" and "name" just use ["id","name"]
     * @param array $include Elements to include or replace. e.g : if "name" has not default it wont be included if you dont specify it in the map. Just use ["name"=>"name"]
     * @return array The map with the form [ElementName => Wished name]
     */
    public static function faceDefaultMap($exclude=[],$include=[]){
        $map=array();
        foreach(self::__getEntityFace() as $elm){
            /* @var $elm EntityFaceElement */
            if(!in_array($elm->getName(), $exclude))
                $map[$elm->getName()]=$elm->getDefaultMap();
        }
        array_replace($map,$include);
        
        return $map;
    }
    
    abstract public static function __getEntityFace();
    
}

?>
{'fraction_non_alphanumeric': 0.107414602557876, 'fraction_numerical': 0.001942690626517727, 'mean_word_length': 1.828525641025641, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, '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': '3380305', 'n_tokens_mistral': 3276, 'n_tokens_neox': 3221, 'n_words': 1097}