<commit_before><?php
/**
 * Created by PhpStorm.
 * User: exodus4d
 * Date: 14.02.15
 * Time: 21:28
 */

namespace Data\Mapper;

class CcpSystemsMapper extends \RecursiveArrayIterator {

    static $map = [
        'system_id' => 'systemId',
        'system_name' => 'name',
        'system_security' => 'trueSec',
        'connstallation_id' => ['constellation' => 'id'],
        'constallation_name' => ['constellation' => 'name'],
        'region_id' => ['region' => 'id'],
        'region_name' => ['region' => 'name']
    ];

    function __construct($data){

        parent::__construct($data, \RecursiveIteratorIterator::SELF_FIRST);
    }

    /**
     * get formatted data
     * @return array
     */
    public function getData(){

        // format functions
        self::$map['effect'] = function($iterator){

            $effect = $iterator['effect'];

            switch($iterator['effect']){
                case 'magnetar':
                    $effect = 'magnetar';
                    break;
                case 'red giant':
                    $effect = 'redGiant';
                    break;
                case 'pulsar':
                    $effect = 'pulsar';
                    break;
                case 'wolf-rayet star':
                    $effect = 'wolfRayet';
                    break;
                case 'cataclysmic variable':
                    $effect = 'cataclysmic';
                    break;
                case 'black hole':
                    $effect = 'blackHole';
                    break;
            }

            return $effect;
        };

        // format functions
        self::$map['security'] = function($iterator){

            if(
                $iterator['security'] == 7 ||
                $iterator['security'] == 8 ||
                $iterator['security'] == 9
            ){
                $trueSec = round($iterator['trueSec'], 3);

                if($trueSec <= 0){
                    $security = '0.0';
                }elseif($trueSec < 0.5){
                    $security = 'L';
                }else{
                    $security = 'H';
                }
            }else{
                $security = 'C' . $iterator['security'];
            }

            return $security;
        };

        // format functions
        self::$map['type'] = function($iterator){

            // TODO refactor
            $type = 'w-space';
            $typeId = 1;
            if(
                $iterator['security'] == 7 ||
                $iterator['security'] == 8 ||
                $iterator['security'] == 9
            ){
                $type = 'k-space';
                $typeId = 2;

            }

            return [
                'id' => $typeId,
                'name' => $type
            ];
        };

        iterator_apply($this, 'self::recursiveIterator', [$this]);


        return iterator_to_array($this, false);
    }

    /**
     * recursive iterator function called on every node
     * @param $iterator
     * @return mixed
     */
    static function recursiveIterator($iterator){

        while ( $iterator -> valid() ) {
            if ( $iterator->hasChildren() ) {
                $iterator->offsetSet($iterator->key(), self::recursiveIterator( $iterator->getChildren() )->getArrayCopy() );
            }else {

                while( $iterator -> valid() ){

                    // check for mapping key
                    if(array_key_exists($iterator->key(), self::$map)){

                        if(is_array(self::$map[$iterator->key()])){
                            // a -> array mapping

                            $parentKey = array_keys( self::$map[$iterator->key()] )[0];
                            $entryKey = array_values( self::$map[$iterator->key()] )[0];

                            // check if key already exists
                            if($iterator->offsetExists($parentKey)){
                                $currentValue = $iterator->offsetGet($parentKey);
                                // add new array entry
                                $currentValue[$entryKey] = $iterator->current();

                                $iterator->offsetSet( $parentKey, $currentValue  );
                            }else{
                                $iterator->offsetSet( $parentKey, [$entryKey => $iterator->current() ]  );
                            }

                            $removeOldEntry = true;
                        }elseif(is_object(self::$map[$iterator->key()])){
                            // a -> a (format by function)

                            $formatFunction = self::$map[$iterator->key()];

                            $iterator->offsetSet( $iterator->key(), call_user_func($formatFunction, $iterator)  );

                            // just value change no key change
                            $removeOldEntry = false;
                            $iterator -> next();
                        }else{
                            // a -> b mapping
                            $iterator->offsetSet( self::$map[$iterator->key()], $iterator->current() );

                            $removeOldEntry = true;
                        }

                        // remove "old" entry
                        if($removeOldEntry){
                            $iterator->offsetUnset($iterator->key());
                        }

                    }else{
                        // continue with next entry
                        $iterator -> next();
                    }

                }
            }

            $iterator -> next();
        }

        return $iterator;
    }

} <commit_msg>#65 fixed fixed trueSec rounding for low-sec/high-sec distinction<commit_after><?php
/**
 * Created by PhpStorm.
 * User: exodus4d
 * Date: 14.02.15
 * Time: 21:28
 */

namespace Data\Mapper;

class CcpSystemsMapper extends \RecursiveArrayIterator {

    static $map = [
        'system_id' => 'systemId',
        'system_name' => 'name',
        'system_security' => 'trueSec',
        'connstallation_id' => ['constellation' => 'id'],
        'constallation_name' => ['constellation' => 'name'],
        'region_id' => ['region' => 'id'],
        'region_name' => ['region' => 'name']
    ];

    function __construct($data){

        parent::__construct($data, \RecursiveIteratorIterator::SELF_FIRST);
    }

    /**
     * get formatted data
     * @return array
     */
    public function getData(){

        // format functions
        self::$map['effect'] = function($iterator){

            $effect = $iterator['effect'];

            switch($iterator['effect']){
                case 'magnetar':
                    $effect = 'magnetar';
                    break;
                case 'red giant':
                    $effect = 'redGiant';
                    break;
                case 'pulsar':
                    $effect = 'pulsar';
                    break;
                case 'wolf-rayet star':
                    $effect = 'wolfRayet';
                    break;
                case 'cataclysmic variable':
                    $effect = 'cataclysmic';
                    break;
                case 'black hole':
                    $effect = 'blackHole';
                    break;
            }

            return $effect;
        };

        // format functions
        self::$map['security'] = function($iterator){

            if(
                $iterator['security'] == 7 ||
                $iterator['security'] == 8 ||
                $iterator['security'] == 9
            ){
                $trueSec = round($iterator['trueSec'], 3);

                if($trueSec <= 0){
                    $security = '0.0';
                }else{
                    // more precise rounding required for "low sec" and "high sec"  distinction
                    $trueSec = round($trueSec, 1);

                    if($trueSec < 0.5){
                        $security = 'L';
                    }else{
                        $security = 'H';
                    }
                }
            }else{
                $security = 'C' . $iterator['security'];
            }

            return $security;
        };

        // format functions
        self::$map['type'] = function($iterator){

            // TODO refactor
            $type = 'w-space';
            $typeId = 1;
            if(
                $iterator['security'] == 7 ||
                $iterator['security'] == 8 ||
                $iterator['security'] == 9
            ){
                $type = 'k-space';
                $typeId = 2;

            }

            return [
                'id' => $typeId,
                'name' => $type
            ];
        };

        iterator_apply($this, 'self::recursiveIterator', [$this]);


        return iterator_to_array($this, false);
    }

    /**
     * recursive iterator function called on every node
     * @param $iterator
     * @return mixed
     */
    static function recursiveIterator($iterator){

        while ( $iterator -> valid() ) {
            if ( $iterator->hasChildren() ) {
                $iterator->offsetSet($iterator->key(), self::recursiveIterator( $iterator->getChildren() )->getArrayCopy() );
            }else {

                while( $iterator -> valid() ){

                    // check for mapping key
                    if(array_key_exists($iterator->key(), self::$map)){

                        if(is_array(self::$map[$iterator->key()])){
                            // a -> array mapping

                            $parentKey = array_keys( self::$map[$iterator->key()] )[0];
                            $entryKey = array_values( self::$map[$iterator->key()] )[0];

                            // check if key already exists
                            if($iterator->offsetExists($parentKey)){
                                $currentValue = $iterator->offsetGet($parentKey);
                                // add new array entry
                                $currentValue[$entryKey] = $iterator->current();

                                $iterator->offsetSet( $parentKey, $currentValue  );
                            }else{
                                $iterator->offsetSet( $parentKey, [$entryKey => $iterator->current() ]  );
                            }

                            $removeOldEntry = true;
                        }elseif(is_object(self::$map[$iterator->key()])){
                            // a -> a (format by function)

                            $formatFunction = self::$map[$iterator->key()];

                            $iterator->offsetSet( $iterator->key(), call_user_func($formatFunction, $iterator)  );

                            // just value change no key change
                            $removeOldEntry = false;
                            $iterator -> next();
                        }else{
                            // a -> b mapping
                            $iterator->offsetSet( self::$map[$iterator->key()], $iterator->current() );

                            $removeOldEntry = true;
                        }

                        // remove "old" entry
                        if($removeOldEntry){
                            $iterator->offsetUnset($iterator->key());
                        }

                    }else{
                        // continue with next entry
                        $iterator -> next();
                    }

                }
            }

            $iterator -> next();
        }

        return $iterator;
    }

} 
{'fraction_non_alphanumeric': 0.13298148468593182, 'fraction_numerical': 0.004931649074234296, 'mean_word_length': 1.1020185488270595, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 95, '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': '1356037', 'n_tokens_mistral': 2927, 'n_tokens_neox': 2699, 'n_words': 562}