Subversion Repositories php-qbpwcf

Rev

Rev 190 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
167 liveuser 1
<?php
2
 
3
/*
4
 
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2014~2025 Min-Jhin,Chen
7
 
8
    This file is part of QBPWCF.
9
 
10
    QBPWCF is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
 
15
    QBPWCF is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
    GNU General Public License for more details.
19
 
20
    You should have received a copy of the GNU General Public License
21
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
22
 
23
*/
24
namespace qbpwcf;
25
 
26
/*
27
類別說明:
28
control structures 的應用.
29
備註:
30
無.
31
*/
32
class controlStructures{
33
 
34
	/*
35
	#函式說明:
36
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
37
	#回傳結果:
38
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
39
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
40
	#$result["function"],當前執行的函式名稱.
41
	#必填參數:
42
	#$method,物件,為物件實體或類別名稱,會自動置入該參數.
43
	#$arguments,陣列,為呼叫方法時所用的參數.
44
	#參考資料:
45
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
46
	*/
47
	public function __call($method,$arguments){
48
 
49
		#取得當前執行的函式
50
		$result["function"]=__FUNCTION__;
51
 
52
		#設置執行不正常
53
		$result["status"]="false";
54
 
55
		#設置執行錯誤
56
		$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
57
 
58
		#設置所丟入的參數
59
		$result["error"][]=$arguments;
60
 
61
		#回傳結果
62
		return $result;
63
 
64
		}#function __call end
65
 
66
	/*
67
	#函式說明:
68
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
69
	#回傳結果:
70
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
71
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
72
	#$result["function"],當前執行的函式名稱.
73
	#必填參數:
74
	#$method,物件,為物件實體或類別名稱,會自動置入該參數.
75
	#$arguments,陣列,為呼叫方法時所用的參數.
76
	#參考資料:
77
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
78
	*/
79
	public static function __callStatic($method,$arguments){
80
 
81
		#取得當前執行的函式
82
		$result["function"]=__FUNCTION__;
83
 
84
		#設置執行不正常
85
		$result["status"]="false";
86
 
87
		#設置執行錯誤
88
		$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
89
 
90
		#設置所丟入的參數
91
		$result["error"][]=$arguments;
92
 
93
		#回傳結果
94
		return $result;
95
 
96
		}#function __callStatic end
97
 
98
	/*
99
	#函式說明:
100
	#多層if判斷.
101
	#回傳結果:
102
	#$result["status"],字串,"true"代表執行正常;"false"代表執行異常.
103
	#$result["function"],當前函式的名稱.
104
	#$result["pass"],字串,"true"代表通過多層if判斷;反之為"false".
105
	#必填參數:
106
	#$conf["get_defined_vars"],陣列,所有已經定義的變數.
107
	$conf["get_defined_vars"]=get_defined_vars();
108
	#$conf["varToControlLeft"],字串陣列,依照順序每層要做判斷的變數名稱,例如"$var1"請寫成"var1".
109
	$conf["varToControlLeft"]=array();
110
	#$conf["conditions"],字串陣列,依照順序每層變數判斷的條件,例如">","<",">=","<=","!=","==","<==","!==","===".
111
	$conf["conditions"]=array();
112
	#$conf["varToControlRight"],陣列,依照順序每層變數判斷期望的結果.
113
	$conf["varToControlRight"]=array();
114
	#可省略參數:
115
	#無.
116
	#參考資料:
117
	#無.
118
	#備註:
119
	#無.
120
	*/
121
	public static function nestedIf(&$conf){
122
 
123
		#初始化要回傳的結果
124
		$result=array();
125
 
126
		#取得當前執行的函數名稱
127
		$result["function"]=__FUNCTION__;
128
 
129
		#如果沒有參數
130
		if(func_num_args()==0){
131
 
132
			#設置執行失敗
133
			$result["status"]="false";
134
 
135
			#設置執行錯誤訊息
136
			$result["error"]="函數".$result["function"]."需要參數";
137
 
138
			#回傳結果
139
			return $result;
140
 
141
			}#if end
142
 
143
		#取得參數
144
		$result["argu"]=$conf;
145
 
146
		#如果 $conf 不為陣列
147
		if(gettype($conf)!=="array"){
148
 
149
			#設置執行失敗
150
			$result["status"]="false";
151
 
152
			#設置執行錯誤訊息
153
			$result["error"][]="\$conf變數須為陣列形態";
154
 
155
			#如果傳入的參數為 null
156
			if(is_null($conf)){
157
 
158
				#設置執行錯誤訊息
159
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
160
 
161
				}#if end
162
 
163
			#回傳結果
164
			return $result;
165
 
166
			}#if end
167
 
168
		#函式說明:
169
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
170
		#回傳結果:
171
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
172
		#$result["error"],執行不正常結束的錯訊息陣列.
173
		#$result["simpleError"],簡單表示的錯誤訊息.
174
		#$result["function"],當前執行的函式名稱.
175
		#$result["argu"],設置給予的參數.
176
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
177
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
178
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
179
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
180
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
181
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
182
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
183
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
184
		#必填參數:
185
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
186
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
187
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
188
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
189
		#可省略參數:
190
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
191
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("get_defined_vars","varToControlLeft","conditions","varToControlRight");
192
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null、any代表不指定變數形態.其中 resource也包含"resource (closed)".
193
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array","array","array","array");
194
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
195
		#$conf["canBeEmptyString"]="false";
196
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
197
		#$conf["canNotBeEmpty"]=array();
198
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
199
		#$conf["canBeEmpty"]=array();
200
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
201
		#$conf["skipableVariableCanNotBeEmpty"]=array();
202
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
203
		#$conf["skipableVariableName"]=array();
204
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
205
		#$conf["skipableVariableType"]=array();
206
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
207
		#$conf["skipableVarDefaultValue"]=array("");
208
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
209
		#$conf["disallowAllSkipableVarIsEmpty"]="";
210
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
211
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
212
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
213
		#$conf["disallowAllSkipableVarNotExist"]="";
214
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
215
		$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array("varToControlLeft","conditions","varToControlRight");
216
		#參考資料:
217
		#array_keys=>http://php.net/manual/en/function.array-keys.php
218
		#備註:
219
		#無.
220
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
221
		unset($conf["variableCheck::checkArguments"]);
222
 
223
		#如果執行異常
224
		if($checkArguments["status"]==="false"){
225
 
226
			#設置執行異常
227
			$result["status"]="false";
228
 
229
			#設置執行錯誤
230
			$result["error"]=$checkArguments;
231
 
232
			#回傳結果
233
			return $result;
234
 
235
			}#if end
236
 
237
		#如果檢查不通過
238
		if($checkArguments["passed"]==="false"){
239
 
240
			#設置執行異常
241
			$result["status"]="false";
242
 
243
			#設置執行錯誤
244
			$result["error"]=$checkArguments;
245
 
246
			#回傳結果
247
			return $result;
248
 
249
			}#if end
250
 
251
		#預設通過判斷的識別
252
		$result["pass"]="true";
253
 
254
		#針對每個要判斷的變數
255
		foreach($conf["varToControlLeft"] as $index => $varLeft){
256
 
257
			#如果要判斷的變數不存在
258
			if(!isset($conf["get_defined_vars"][$varLeft])){
259
 
260
				#設置不通過的識別
261
				$result["pass"]="false";
262
 
263
				#結束 foreach
264
				break;
265
 
266
				}#if end
267
 
268
			#要判斷的變數
269
			$varLeft=&$conf["get_defined_vars"][$varLeft];
270
 
271
			#判斷條件
272
			$condition=$conf["conditions"][$index];
273
 
274
			#判斷預期的結果
275
			$varRight=&$conf["varToControlRight"][$index];
276
 
277
			#如果不滿足條件
278
			if(!(eval("return ".$varLeft.$condition.$varRight.";"))){
279
 
280
				#設置不通過的識別
281
				$result["pass"]="false";
282
 
283
				#結束 foreach
284
				break;
285
 
286
				}#if end
287
 
288
			}#foreach end
289
 
290
		#設置執行正常
291
		$result["status"]="true";
292
 
293
		#回傳結果
294
		return $result;
295
 
296
		}#function nestedIf end
297
 
298
	}#class