B-Teck!

お仕事からゲームまで幅広く

【PHP】標準入出力を行うクラス

<?php
    class inputManager{
        private $index = 0;
        private $input = [];
        //区切り文字設定
        const SPRIT_STR = " ";
        
        /**
         * コンストラクタ
         * 
         * @access public
         * @param  なし
         * @return なし
         */
        public function __construct($testFlg = false, $testInput = null){
            if(!$testFlg){
                    $input_lines = trim(fgets(STDIN));
                    $input_lines = str_replace(array("\r\n","\r","\n"), "", $input_lines);
                    do {
                        $this->input[] = $input_lines;
                        $input_lines = trim(fgets(STDIN));
                        $input_lines = str_replace(array("\r\n","\r","\n"), "", $input_lines);
                    } while ($input_lines !== "");
            }else{
                $this->input = $testInput;
            }
        }
        
        /**
         * value
         * 現在のインデックスの値を取得する
         * 
         * @access public
         * @param  なし
         * @return string|null 現在のインデックスの値
         */
        public function value(){
            if($this->index < $this->length()){
                return $this->input[$this->index];
            }else{
                return null;
            }
        }
        
        /**
         * explodeValue
         * 現在のインデックスの値をexplodeした配列を取得する
         * 
         * @access public
         * @param  なし
         * @return Array|null explode後配列
         */
        public function explodeValue(){
            if($this->index < $this->length()){
                return explode(self::SPRIT_STR, $this->input[$this->index]);
            }else{
                return null;
            }
        }
        
        /**
         * setindex
         * インデックスを設定する
         * 
         * @access public
         * @param  $i_index 設定するインデックス
         * @return $this
         */
        public function setindex(int $i_index){
            $this->index = $i_index;
            return $this;
        }
        
        /**
         * next
         * インデックスを1つすすめる
         * 
         * @access public
         * @param  なし
         * @return $this
         */
        public function next(){
            $this->index++;
            return $this;
        }
        
        /**
         * length
         * 入力の長さを取得
         * 
         * @access public
         * @param  なし
         * @return int $this->input 配列の長さ
         */
        public function length(){
            return count($this->input);
        }
    }
    
    class out{
        /**
         * output
         * 引数を出力
         * 入力がない場合は空行を出力する
         * 
         * @access public
         * @param  string $i_val 出力する文字列
         * @return なし
         */
        public static function output(string $i_val = ""){
            echo $i_val. "\n";
        }
    }
    
    //テスト用入力
    $input[] = "123";
    $input[] = "456";
    $input[] = "789";
    $input[] = "aaa";
    $input[] = "bbb";
    
    $io = new inputManager(true,$input);
    //長さの取得
    out::output($io->length());
    
    //
    out::output();
    
    //インデックス設定の確認
    out::output($io->value());
    $io->next();
    out::output($io->value());    
    $io->setindex(2);
    out::output($io->value());    
    out::output($io->next()->value());   
    out::output($io->next()->value());   
    
    out::output();
    
    //入力を末尾まで出力する
    $io->setindex(0);
    while(!is_null($io->value())){
        out::output($io->value());
        $io->next();
    }
    
    out::output();
    
    //インデックスをセットして末尾まで出力する
    for($i = 0;$i < $io->length(); $i++){
        out::output($io->setindex($i)->value());
    }