Subversion Repositories php-qbpwcf

Rev

Rev 190 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php

/*

        QBPWCF, Quick Build PHP website Component base on Fedora Linux.
    Copyright (C) 2014~2025 MIN ZHI, CHEN

    This file is part of QBPWCF.

    QBPWCF is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QBPWCF is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.

*/
namespace qbpwcf;

/*
類別說明:
control structures 的應用.
備註:
無.
*/
class controlStructures{

        /*
        #函式說明:
        #當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
        #回傳結果:
        #$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
        #$reuslt["error"],執行不正常結束的錯訊息陣列.
        #$result["function"],當前執行的函式名稱.
        #必填參數:
        #$method,物件,為物件實體或類別名稱,會自動置入該參數.
        #$arguments,陣列,為呼叫方法時所用的參數.
        #參考資料:
        #__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
        */
        public function __call($method,$arguments){

                #取得當前執行的函式
                $result["function"]=__FUNCTION__;

                #設置執行不正常
                $result["status"]="false";

                #設置執行錯誤
                $result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";

                #設置所丟入的參數
                $result["error"][]=$arguments;

                #回傳結果
                return $result;

                }#function __call end

        /*
        #函式說明:
        #當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
        #回傳結果:
        #$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
        #$reuslt["error"],執行不正常結束的錯訊息陣列.
        #$result["function"],當前執行的函式名稱.
        #必填參數:
        #$method,物件,為物件實體或類別名稱,會自動置入該參數.
        #$arguments,陣列,為呼叫方法時所用的參數.
        #參考資料:
        #__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
        */
        public static function __callStatic($method,$arguments){

                #取得當前執行的函式
                $result["function"]=__FUNCTION__;

                #設置執行不正常
                $result["status"]="false";

                #設置執行錯誤
                $result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";

                #設置所丟入的參數
                $result["error"][]=$arguments;

                #回傳結果
                return $result;

                }#function __callStatic end

        /*
        #函式說明:
        #多層if判斷.
        #回傳結果:
        #$result["status"],字串,"true"代表執行正常;"false"代表執行異常.
        #$result["function"],當前函式的名稱.
        #$result["pass"],字串,"true"代表通過多層if判斷;反之為"false".
        #$result["reason"]["varLeftName"],當$result["pass"]為"false"時,會回傳用於判斷的varLeft名稱資訊.
        #$result["reason"]["varLeftVal"],當$result["pass"]為"false"且對應變數存在時,會回傳用於判斷的varLeft數值資訊.
        #$result["reason"]["condition"],當$result["pass"]為"false"且對應變數存在時,會回傳用於判斷的condition資訊.
        #$result["reason"]["varRightVal"],當$result["pass"]為"false"且對應變數存在時,會回傳用於判斷的varRight數值資訊.
        #必填參數:
        #$conf["get_defined_vars"],陣列,所有已經定義的變數.
        $conf["get_defined_vars"]=get_defined_vars();
        #$conf["varToControlLeft"],字串陣列,依照順序每層要做判斷的變數名稱,例如"$var1"請寫成"var1";若為$array2["key2"]則請寫成"array("array2","key2")"
        $conf["varToControlLeft"]=array();
        #$conf["conditions"],字串陣列,依照順序每層變數判斷的條件,例如">","<",">=","<=","!=","==","<==","!==","===".
        $conf["conditions"]=array();
        #$conf["varToControlRight"],陣列,依照順序每層變數判斷期望的結果.
        $conf["varToControlRight"]=array();
        #可省略參數:
        #無.
        #參考資料:
        #無.
        #備註:
        #無.
        */
        public static function nestedIf(&$conf){

                #初始化要回傳的結果
                $result=array();

                #取得當前執行的函數名稱
                $result["function"]=__FUNCTION__;

                #如果沒有參數
                if(func_num_args()==0){

                        #設置執行失敗
                        $result["status"]="false";

                        #設置執行錯誤訊息
                        $result["error"]="函數".$result["function"]."需要參數";

                        #回傳結果
                        return $result;

                        }#if end

                #取得參數
                $result["argu"]=$conf;

                #如果 $conf 不為陣列
                if(gettype($conf)!=="array"){

                        #設置執行失敗
                        $result["status"]="false";

                        #設置執行錯誤訊息
                        $result["error"][]="\$conf變數須為陣列形態";

                        #如果傳入的參數為 null
                        if(is_null($conf)){

                                #設置執行錯誤訊息
                                $result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";

                                }#if end

                        #回傳結果
                        return $result;

                        }#if end

                #函式說明:
                #檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
                #回傳結果:
                #$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
                #$result["error"],執行不正常結束的錯訊息陣列.
                #$result["simpleError"],簡單表示的錯誤訊息.
                #$result["function"],當前執行的函式名稱.
                #$result["argu"],設置給予的參數.
                #$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
                #$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
                #$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
                #$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
                #$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
                #$result["argu"],字串陣列,目前輸入的參數名稱陣列.
                #$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
                #$result["notNeedVar"],字串陣列,多餘的參數名稱.
                #必填參數:
                #$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
                $conf["variableCheck::checkArguments"]["varInput"]=&$conf;
                #$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
                $conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
                #可省略參數:
                #$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
                $conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("get_defined_vars","varToControlLeft","conditions","varToControlRight");
                #$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null、any代表不指定變數形態.其中 resource也包含"resource (closed)".
                $conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array","array","array","array");
                #$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
                #$conf["canBeEmptyString"]="false";
                #$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
                #$conf["canNotBeEmpty"]=array();
                #$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
                #$conf["canBeEmpty"]=array();
                #$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
                #$conf["skipableVariableCanNotBeEmpty"]=array();
                #$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
                #$conf["skipableVariableName"]=array();
                #$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
                #$conf["skipableVariableType"]=array();
                #$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
                #$conf["skipableVarDefaultValue"]=array("");
                #$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
                #$conf["disallowAllSkipableVarIsEmpty"]="";
                #$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
                #$conf["disallowAllSkipableVarIsEmptyArray"]="";
                #$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
                #$conf["disallowAllSkipableVarNotExist"]="";
                #$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
                $conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array("varToControlLeft","conditions","varToControlRight");
                #參考資料:
                #array_keys=>http://php.net/manual/en/function.array-keys.php
                #備註:
                #無.
                $checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
                unset($conf["variableCheck::checkArguments"]);

                #如果執行異常
                if($checkArguments["status"]==="false"){

                        #設置執行異常
                        $result["status"]="false";

                        #設置執行錯誤
                        $result["error"]=$checkArguments;

                        #回傳結果
                        return $result;

                        }#if end

                #如果檢查不通過
                if($checkArguments["passed"]==="false"){

                        #設置執行異常
                        $result["status"]="false";

                        #設置執行錯誤
                        $result["error"]=$checkArguments;

                        #回傳結果
                        return $result;

                        }#if end

                #預設通過判斷的識別
                $result["pass"]="true";

                #針對每個要判斷的變數
                foreach($conf["varToControlLeft"] as $index => $varLeftName){

                        #判斷為字串或陣列
                        switch(gettype($varLeftName)){

                                #如果是字串
                                case "string":

                                        #如果要判斷的變數不存在
                                        if(!isset($conf["get_defined_vars"][$varLeftName])){

                                                #設置不通過的識別
                                                $result["pass"]="false";

                                                #記錄用於判斷的varLeft變數名稱
                                                $result["reason"]["varLeftName"]=$varLeftName;

                                                #結束 foreach
                                                break 2;

                                                }#if end

                                        #記錄用於判斷的varLeft變數數值
                                        $varLeftVal=&$conf["get_defined_vars"][$varLeftName];

                                        #跳出 switch
                                        break;

                                #如果是array
                                case "array":

                                        #初始化儲存 $varToControlLeft 父元素的變數
                                        $parentVarOfVarLeft=$conf["get_defined_vars"];

                                        #針對每個元素
                                        foreach($varLeftName as $key){

                                                #如果要判斷的變數不存在
                                                if(!isset($parentVarOfVarLeft[$key])){

                                                        #設置不通過的識別
                                                        $result["pass"]="false";

                                                        #記錄用於判斷的varLeft變數名稱
                                                        $result["reason"]["varLeftName"]=$varLeftName;

                                                        #結束 foreach
                                                        break 3;

                                                        }#if end

                                                #執行到這代表存在

                                                #更新 $varToControlLeft 父元素的變數
                                                $parentVarOfVarLeft=$parentVarOfVarLeft[$key];

                                                }#foreach end

                                        #記錄用於判斷的varLeft變數數值
                                        $varLeftVal=&$parentVarOfVarLeft;

                                        #跳出switch
                                        break;

                                #如果是其他結果
                                default:

                                        #設置執行異常
                                        $result["status"]="false";

                                        #設置執行錯誤
                                        $result["error"][]="element of param varToControlLeft should be string or array";

                                        #設置執行錯誤
                                        $result["error"][]=$checkArguments;

                                        #回傳結果
                                        return $result;

                                }#switch end

                        #判斷條件
                        $condition=$conf["conditions"][$index];

                        #記錄用於判斷的varRight變數名稱
                        $varRightVal=&$conf["varToControlRight"][$index];

                        #儲存要給eval運行的內容
                        $evalStr="return \$varLeftVal".$condition."\$varRightVal;";

                        #debug
                        #var_dump(__LINE__,$evalStr);

                        #如果不滿足條件
                        if(!(eval($evalStr))){

                                #設置不通過的識別
                                $result["pass"]="false";

                                #記錄用於判斷的varLeft變數名稱
                                $result["reason"]["varLeftName"]=$varLeftName;

                                #記錄用於判斷的varLeft變數數值
                                $result["reason"]["varLeftVal"]=$varLeftVal;

                                #記錄用於判斷的varRight資訊
                                $result["reason"]["varRightVal"]=$varRightVal;

                                #記錄用於判斷的condition資訊
                                $result["reason"]["condition"]=$condition;

                                #結束 foreach
                                break;

                                }#if end

                        }#foreach end

                #設置執行正常
                $result["status"]="true";

                #回傳結果
                return $result;

                }#function nestedIf end

        }#class