PHP:『パーフェクトPHP』のフレームワークでシステム開発する ~ 『ケーキ管理』その3 ~

PHP

『ケーキ管理』というケーキの販売管理システムを開発していく

前回までの記事では、メニューの機能まで作成が終わりました。今回はその続きとなります。

この記事のプログラムで完成するもの

工事中…

フレームワークにデフォルトで、入力判定の機能を追加

ValidateInput.phpの作成

cake-manager/coreフォルダの中に、ValidateInput.phpと名前をつけ、新規でファイルを作成します。
ファイルの中に下記のコードを記入し、文字コードUTF-8で保存します。

<?php

/**
 * ValidateInput.
 *
 * @author haichoru(ハイチョル)
 */
class ValidateInput
{
    public function IsRequired($s)
    {
        return (!strlen($s)) ? "入力がありません": "";
    }

    public function IsTooLong($s, $l)
    {
        return (mb_strlen($s) > $l) ? "文字数超過です": "";
    }

    public function IllegalCharactersExist($s)
    {
        return (preg_match('/\s|,|"|\'|`|#|%|>|<|!|\.|\[|\]|\*|\$|;|:|\?|\^|\(|\)|\+|\\\\/', $s)) ? "禁止文字が含まれます": "";
    }

    public function IsInteger($s)
    {
        return (preg_match('/\A-?(0|[1-9]{1}[0-9]{0,11})\z/', $s)) ? "": "入力が不正です";
    }

    public function IsTelephone($s)
    {
        return (preg_match('/\A[0-9\-]{10,13}\z/', $s)) ? "": "入力が不正です";
    }

    public function IsZip($s)
    {
        return (preg_match('/\A[0-9]{3}-?[0-9]{4}\z/', $s)) ? "": "入力が不正です";
    }

    public function IsDate($s)
    {
        if (preg_match('/\A(0|[1-9]{1}[0-9]{0,3})-(0[1-9]{1}|1[0-2]{1})-(0[1-9]{1}|[1-2]{1}[0-9]{1}|3[0-1]{1})\z/', $s)) {

            $date = new DateTimeImmutable(s);

            $Y = $date->format("Y");
            $m = $date->format("m");
            $d = $date->format("d");

            if (!checkdate($m, $d, $Y)) {

                return '日付が存在しません!';
            }

        } else {

            return "入力が不正です!";
        }

        return "";
    }

    public function IsInconsistentDates($start, $end)
    {
        $date_1 = new DateTimeImmutable($start);
        $date_2 = new DateTimeImmutable($end);

        if ($date_2 < $date_1) {

            return "矛盾した時間指定です";
        }

        return "";
    }
}

Application.phpの修正

cake-manager/coreフォルダの中のApplication.phpを書き換え。

<?php

/**
 * Application.
 *
 * @author Katsuhiro Ogawa <fivestar@nequal.jp>
 */
abstract class Application
{
    protected $debug = false;
    protected $request;
    protected $response;
    protected $session;
    protected $db_manager;

    /**
     * コンストラクタ
     *
     * @param boolean $debug
     */
    public function __construct($debug = false)
    {
        $this->setDebugMode($debug);
        $this->initialize();
        $this->configure();
    }

    /**
     * デバッグモードを設定
     * 
     * @param boolean $debug
     */
    protected function setDebugMode($debug)
    {
        if ($debug) {
            $this->debug = true;
            ini_set('display_errors', 1);
            error_reporting(-1);
        } else {
            $this->debug = false;
            ini_set('display_errors', 0);
        }
    }

    /**
     * アプリケーションの初期化
     */
    protected function initialize()
    {
        $this->request    = new Request();
        $this->response   = new Response();
        $this->session    = new Session();
        $this->db_manager = new DbManager();
        $this->router     = new Router($this->registerRoutes());
    }

...

上記の部分を下記に修正します。

<?php

/**
 * Application.
 *
 * @author Katsuhiro Ogawa <fivestar@nequal.jp>
 */
abstract class Application
{
    protected $debug = false;
    protected $request;
    protected $response;
    protected $session;
    protected $db_manager;
    protected $validate_input;

    /**
     * コンストラクタ
     *
     * @param boolean $debug
     */
    public function __construct($debug = false)
    {
        $this->setDebugMode($debug);
        $this->initialize();
        $this->configure();
    }

    /**
     * デバッグモードを設定
     * 
     * @param boolean $debug
     */
    protected function setDebugMode($debug)
    {
        if ($debug) {
            $this->debug = true;
            ini_set('display_errors', 1);
            error_reporting(-1);
        } else {
            $this->debug = false;
            ini_set('display_errors', 0);
        }
    }

    /**
     * アプリケーションの初期化
     */
    protected function initialize()
    {
        $this->request    = new Request();
        $this->response   = new Response();
        $this->session    = new Session();
        $this->db_manager = new DbManager();
        $this->router     = new Router($this->registerRoutes());
        $this->validate_input = new ValidateInput();
    }

    public function getValidateInput()
    {
        return $this->validate_input;
    }

...

Controller.phpの修正

cake-manager/coreフォルダの中のController.phpを書き換え。

<?php

/**
 * Controller.
 *
 * @author Katsuhiro Ogawa <fivestar@nequal.jp>
 */
abstract class Controller
{
    protected $controller_name;
    protected $action_name;
    protected $application;
    protected $request;
    protected $response;
    protected $session;
    protected $db_manager;
    protected $auth_actions = array();

    /**
     * コンストラクタ
     *
     * @param Application $application
     */
    public function __construct($application)
    {
        $this->controller_name = strtolower(substr(get_class($this), 0, -10));

        $this->application = $application;
        $this->request     = $application->getRequest();
        $this->response    = $application->getResponse();
        $this->session     = $application->getSession();
        $this->db_manager  = $application->getDbManager();
    }

...

上記の部分を下記に修正します。

<?php

/**
 * Controller.
 *
 * @author Katsuhiro Ogawa <fivestar@nequal.jp>
 */
abstract class Controller
{
    protected $controller_name;
    protected $action_name;
    protected $application;
    protected $request;
    protected $response;
    protected $session;
    protected $db_manager;
    protected $auth_actions = array();
    protected $validate_input;

    /**
     * コンストラクタ
     *
     * @param Application $application
     */
    public function __construct($application)
    {
        $this->controller_name = strtolower(substr(get_class($this), 0, -10));

        $this->application = $application;
        $this->request     = $application->getRequest();
        $this->response    = $application->getResponse();
        $this->session     = $application->getSession();
        $this->db_manager  = $application->getDbManager();
        $this->validate_input = $application->getValidateInput();
    }

...

なぜフレームワークを修正したのか

お客様情報の入力には多くの種類の入力が発生します。
そのとき、『~ その1 ~』や、『~ その2 ~』で作成した入力チェック機構をご覧いただくとわかると思いますが、if文によるネストが増え、読みやすい(リーダブル)コードではなくなります。
それを予防するため、あらかじめ入力チェックを分離しておきます。

$error = $this->validate_input->IsRequired($this->name);

各コントローラー(Controller.phpを継承したクラス)から、こんな感じで簡単に入力判定できるようになります。

こういうところはLaravelなどのフレームワークより柔軟に開発できるのがよいところです。

つづく…

コメント