1+ <?php 
2+ 
3+ namespace  Mesour \ArrayManage \Searcher ;
4+ 
5+ use  Mesour \ManagerException ,
6+     Mesour \ArrayManage \Translator ;
7+ 
8+ /** 
9+  * @author mesour <[email protected] > 10+  * @package Mesour ArrayManager 
11+  */ 
12+ class  Condition {
13+ 
14+ 	const  EQUAL  = 'Equal ' ,
15+ 	    NOT_EQUAL  = 'NotEqual ' ,
16+ 	    SMALLER  = 'Smaller ' ,
17+ 	    NOT_SMALLER  = 'NotSmaller ' ,
18+ 	    BIGGER  = 'Bigger ' ,
19+ 	    NOT_BIGGER  = 'NotBigger ' ,
20+ 	    STARTS_WITH  = 'StartsWith ' ,
21+ 	    NOT_STARTS_WITH  = 'NotStartsWith ' ,
22+ 	    ENDS_WITH  = 'EndsWith ' ,
23+ 	    NOT_ENDS_WITH  = 'NotEndsWith ' ,
24+ 	    CONTAINS  = 'Contains ' ,
25+ 	    NOT_CONTAINS  = 'NotContains ' ;
26+ 
27+ 	static  private  $ case_sensitiveFALSE ;
28+ 
29+ 	private  $ allowedarray (
30+ 	    self ::EQUAL , self ::NOT_EQUAL ,
31+ 	    self ::SMALLER , self ::NOT_SMALLER ,
32+ 	    self ::BIGGER , self ::NOT_BIGGER ,
33+ 	    self ::STARTS_WITH , self ::NOT_STARTS_WITH ,
34+ 	    self ::ENDS_WITH , self ::NOT_ENDS_WITH ,
35+ 	    self ::CONTAINS , self ::NOT_CONTAINS ,
36+ 	);
37+ 
38+ 	private  $ matcher
39+ 
40+ 	public  function  __construct ($ matcher
41+ 		if  (!in_array ($ matcher$ this allowed ) && !is_callable ($ matcher
42+ 			throw  new  ManagerException ('First value must be callable or some constant of this class. ' );
43+ 		}
44+ 		$ this matcher  = $ matcher
45+ 	}
46+ 
47+ 	static  public  function  getInstance ($ matcher
48+ 		return  new  self ($ matcher
49+ 	}
50+ 
51+ 	static  public  function  setKeysSensitive ($ sensitiveTRUE ) {
52+ 		self ::$ case_sensitive$ sensitive
53+ 	}
54+ 
55+ 	public  function  match ($ value$ searched_value$ row_data
56+ 		if  (is_string ($ this matcher )) {
57+ 			return  call_user_func (array ($ this 'match '  . $ this matcher ), $ value$ searched_value$ row_data
58+ 		} else  {
59+ 			return  call_user_func ($ this matcher , $ value$ searched_value$ row_data
60+ 		}
61+ 	}
62+ 
63+ 	public  function  translate () {
64+ 		return  (new  Translator (Translator::CONDITION , $ this matcher ))->translate ();
65+ 	}
66+ 
67+ 	private  function  matchEqual ($ value$ searched_value
68+ 		if (!self ::$ case_sensitive
69+ 			$ searched_valuestrtolower ($ searched_value
70+ 			$ valuestrtolower ($ value
71+ 		}
72+ 		return  (is_null ($ searched_valueis_null ($ valuestring )$ searched_valuestring )$ value
73+ 	}
74+ 
75+ 	private  function  matchNotEqual ($ value$ searched_value
76+ 		return  !$ this matchEqual ($ value$ searched_value
77+ 	}
78+ 
79+ 	private  function  matchSmaller ($ value$ searched_value
80+ 		if (!self ::$ case_sensitive
81+ 			$ searched_valuestrtolower ($ searched_value
82+ 			$ valuestrtolower ($ value
83+ 		}
84+ 		return  ($ value$ searched_value
85+ 	}
86+ 
87+ 	private  function  matchNotSmaller ($ value$ searched_value
88+ 		return  !$ this matchSmaller ($ value$ searched_value
89+ 	}
90+ 
91+ 	private  function  matchBigger ($ value$ searched_value
92+ 		if (!self ::$ case_sensitive
93+ 			$ searched_valuestrtolower ($ searched_value
94+ 			$ valuestrtolower ($ value
95+ 		}
96+ 		return  ($ value$ searched_value
97+ 	}
98+ 
99+ 	private  function  matchNotBigger ($ value$ searched_value
100+ 		return  !$ this matchBigger ($ value$ searched_value
101+ 	}
102+ 
103+ 	private  function  matchStartsWith ($ value$ searched_value
104+ 		if (!self ::$ case_sensitive
105+ 			$ searched_valuestrtolower ($ searched_value
106+ 			$ valuestrtolower ($ value
107+ 		}
108+ 		return  strncmp ($ value$ searched_valuestrlen ($ searched_value0 ;
109+ 	}
110+ 
111+ 	private  function  matchNotStartsWith ($ value$ searched_value
112+ 		return  !$ this matchStartsWith ($ value$ searched_value
113+ 	}
114+ 
115+ 	private  function  matchEndsWith ($ value$ searched_value
116+ 		if (!self ::$ case_sensitive
117+ 			$ searched_valuestrtolower ($ searched_value
118+ 			$ valuestrtolower ($ value
119+ 		}
120+ 		return  strlen ($ value0  || substr ($ valuestrlen ($ searched_value$ searched_value
121+ 	}
122+ 
123+ 	private  function  matchNotEndsWith ($ value$ searched_value
124+ 		return  !$ this matchEndsWith ($ value$ searched_value
125+ 	}
126+ 
127+ 	private  function  matchContains ($ value$ searched_value
128+ 		if (!self ::$ case_sensitive
129+ 			$ searched_valuestrtolower ($ searched_value
130+ 			$ valuestrtolower ($ value
131+ 		}
132+ 		return  strpos ($ value$ searched_valueFALSE ;
133+ 	}
134+ 
135+ 	private  function  matchNotContains ($ value$ searched_value
136+ 		return  !$ this matchContains ($ value$ searched_value
137+ 	}
138+ }
0 commit comments