マイクロフレームワーク Flight で、text,csv,json を読む

次のプログラムを改造して、text,csv,json を読みます。
[マイクロフレームワーク Flight の簡単なルーティング](https://qiita.com/ekzemplaro/items/1c96108d15a0dbef1903)

1) index.php

```php:index.php
<?php
// -----------------------------------------------------------------
require 'flight/Flight.php';

require 'application/views/greeting.php';

Flight::route('/',array('Greeting','hello'));
Flight::route('/text',array('Greeting','text'));
Flight::route('/json',array('Greeting','json'));
Flight::route('/csv',array('Greeting','csv'));


Flight::start();
// -----------------------------------------------------------------
```

2) application/views/greeting.php

```php:application/views/greeting.php
<?php
require_once ("text_manipulate.php");

// -----------------------------------------------------------------
class Greeting {
// -----------------------------------------------------------------
    public static function hello() {
        echo '*** Greeting ***<p />';
        echo '*** 挨拶 ***<p />';
        echo '*** Jul/30/2018 *** AM 10:37<p />';
    }

// -----------------------------------------------------------------
    public static function text() 
{
	$file_in = "/var/tmp/plain_text/cities.txt";

	$str_in = file_get_contents($file_in);
	$dict_aa = Text_manipulate::str_to_dict_proc ($str_in);

	Text_manipulate::dict_display_proc ($dict_aa);

        echo '*** Jul/30/2018 *** AM 10:40<p />';
    }
// -----------------------------------------------------------------
    public static function json()
{

	$file_json="/var/tmp/json/cities.json";
	$json_string = file_get_contents ($file_json);

	$dict_aa = json_decode ($json_string,true);
	Text_manipulate::dict_display_proc ($dict_aa);

        echo '*** Jul/30/2018 *** PM 15:26<p />';
    }
// -----------------------------------------------------------------
    public static function csv()
{
	$file_csv = "/var/tmp/csv/cities.csv";

	$dict_aa = Text_manipulate::csv_read_proc ($file_csv);
	Text_manipulate::dict_display_proc ($dict_aa);

        echo '*** Jul/30/2018 *** AM 10:55<p />';
    }

// -----------------------------------------------------------------
}
// -----------------------------------------------------------------
?>
```

3) application/views/text_manipulate.php

```php:application/views/text_manipulate.php
<?php
// -----------------------------------------------------------------
class Text_manipulate {
// -----------------------------------------------------------------
public static function str_to_dict_proc ($str_in)
{
	$dict_aa = array ();

	$lines = explode ("\n",$str_in);

	$count = count($lines);

	echo "count = $count<p />";

	for ($it = 0; $it < $count; $it++)
		{
		$line = $lines[$it];

		if (3 < strlen ($line))
			{
			$mtx = explode ("\t",rtrim ($line));
			$dict_unit = array ();
			$dict_unit['name'] = $mtx[1];
			$dict_unit['population'] = $mtx[2];
			$dict_unit['date_mod'] = $mtx[3];
			$dict_aa[$mtx[0]]= $dict_unit;
			}
		}

	return	$dict_aa;
}
// -----------------------------------------------------------------
public static function dict_display_proc ($dict_aa)
{
	ksort ($dict_aa);

	echo "<table>";

	foreach ($dict_aa as $key => $value)
		{
		echo "<tr>";

		$name = $value["name"];
		$population = $value['population'];
		$date_mod = $value['date_mod'];

		echo "<td>$key</td>";
		echo "<td>$name</td>";
		echo "<td>$population</td>";
		echo "<td>$date_mod</td>";
		echo "</tr>";
		}

	echo "</table>";
}

// -----------------------------------------------------------------
public static function csv_read_proc ($file_in)
{
	$dict_aa = array ();

	$file = new SplFileObject($file_in); 
	$file->setFlags(SplFileObject::READ_CSV);
	foreach($file as $line)
		{
		if(!is_null($line[0]))
			{
			$dict_unit = array ();
			$dict_unit['name'] = $line[1];
			$dict_unit['population'] = $line[2];
			$dict_unit['date_mod'] = $line[3];
			$dict_aa[$line[0]]= $dict_unit;
			}
		}

	$file = null;

	return	$dict_aa;
}

// -----------------------------------------------------------------
}
// -----------------------------------------------------------------
?>
```

4) データ
>text

```text:/var/tmp/plain_text/cities.txt
t2381	名古屋	32761	2013-5-30
t2382	豊橋	87295	2013-9-10
t2383	岡崎	21674	2013-2-14
t2384	一宮	73612	2013-3-9
t2385	蒲郡	45982100	2018-07-05
t2386	常滑	95187	2013-1-21
t2388	瀬戸	85791	2013-11-26
t2389	犬山	24139	2013-10-15
```

>csv

```text:/var/tmp/csv/cities.csv
t1271,千葉,51769,2002-9-8
t1272,勝浦,63473,2002-8-12
t1273,市原,57182,2002-6-1
t1274,流山,46359,2002-10-29
t1275,八千代,32765,2002-7-14
t1276,我孫子,29134,2002-6-12
t1277,鴨川,82157,2002-4-17
t1278,銚子,91548,2002-2-24
t1279,市川,74256,2002-11-12
```

>json

```json:/var/tmp/json/cities.json
{
  "t0921": {
    "name": "宇都宮",
    "population": 68714,
    "date_mod": "1950-9-24"
  },
  "t0922": {
    "name": "小山",
    "population": 29157,
    "date_mod": "1950-3-15"
  },
  "t0923": {
    "name": "佐野",
    "population": 65741,
    "date_mod": "1950-10-7"
  },
  "t0924": {
    "name": "足利",
    "population": 38164,
    "date_mod": "1950-6-22"
  },
  "t0925": {
    "name": "日光",
    "population": 49675,
    "date_mod": "1950-8-28"
  },
  "t0926": {
    "name": "下野",
    "population": 65813,
    "date_mod": "1950-9-12"
  },
  "t0927": {
    "name": "さくら",
    "population": 37251,
    "date_mod": "1950-3-21"
  },
  "t0928": {
    "name": "矢板",
    "population": 52416,
    "date_mod": "1950-7-26"
  },
  "t0929": {
    "name": "真岡",
    "population": 26857,
    "date_mod": "1950-10-2"
  },
  "t0930": {
    "name": "栃木",
    "population": 48923,
    "date_mod": "1950-12-20"
  },
  "t0931": {
    "name": "大田原",
    "population": 75284,
    "date_mod": "1950-2-7"
  },
  "t0932": {
    "name": "鹿沼",
    "population": 92351,
    "date_mod": "1950-5-17"
  },
  "t0933": {
    "name": "那須塩原",
    "population": 81937,
    "date_mod": "1950-6-19"
  },
  "t0934": {
    "name": "那須烏山",
    "population": 72158,
    "date_mod": "1950-8-14"
  }
}
```

5) ブラウザーからアクセス

text
>http://localhost:9000/text

csv
>http://localhost:9000/csv

json
>http://localhost:9000/json