PHP object-oriented identity object and php object-oriented identity

Source: Internet
Author: User

PHP object-oriented identity object and php object-oriented identity

/* Identify the object mode. The main function of this mode is to create the wehre condition string in the SQL statement. The following code and comments are available: */namespace woo \ mapper; // Field object class Field {protected $ name = null; // Field name protected $ operator = null; // operator protected $ comps = array (); // The array that stores the condition protected $ incomplete = false; // check whether the condition array has a value of function _ construct ($ name) {$ this-> name = $ name ;} // Add the where condition function addTest ($ operator, $ value) {$ this-> comps [] = array ('name' => $ this-> name, 'operator' => $ operator Ator, 'value' => $ value);} // obtain the Array function getComps () {return $ this-> comps;} function isIncomplete () {return empty ($ this-> comps) ;}// identifies the class IdentityObject {protected $ currentfield = null; // The Field object protected $ fields = array (); // The field set private $ and = null; private $ enforce = array (); // The specified valid field function _ construct ($ field = null, array $ enforce = null) {if (! Is_null ($ enforce) {$ this-> enforce = $ enforce;} if (! Is_null ($ field) {$ this-> field ($ field) ;}// obtain the qualified legal field function getObjectFields () {return $ this-> enforce ;} // The main function is to set the function field ($ fieldname) {if (! $ This-> isVoid () & $ this-> currentfield-> isIncomplete () {throw new \ Exception ("Incomplete field ");} $ this-> enforceField ($ fieldname); if (isset ($ this-> fields [$ fieldname]) {$ this-> currentfield = $ this-> fields [$ fieldname];} else {$ this-> currentfield = new Field ($ fieldname ); $ this-> fields [$ fieldname] = $ this-> currentfield;} return $ this; // use the coherent syntax} // whether the field set is empty function isVoid () {return empty ($ this-> fi Elds);} // check whether the field is valid function enforceField ($ fieldname) {if (! In_array ($ fieldname, $ this-> enforce )&&! Empty ($ this-> enforce) {$ forcelist = implode (',', $ this-> enforce ); throw new \ Exception ("{$ fieldname} not a legal field {$ forcelist}") ;}// Add the where condition function eq ($ value) to the field object) {return $ this-> operator ("=", $ value);} function lt ($ value) {return $ this-> operator ("<", $ value );} function gt ($ value) {return $ this-> operator (">", $ value);} // Add the where condition private function operator ($ symbol, $ value) {if ($ this-> isVoid) {throw new \ Exception ("no object field defined") ;}$ this-> currentfield-> addTest ($ symbol, $ value); return $ this; // use the coherent syntax} // obtain the where condition array function getComps () {$ ret = array () for all field objects in this class (); foreach ($ this-> fields as $ key => $ field) {$ ret = array_merge ($ ret, $ field-> getComps ();} return $ ret ;}} // client code $ idobj = new IdentityObject (); $ idobj-> field ("name")-> eq ("The Good Show")-> field ("start ") -> gt (time ()-> lt (time () + (24*60*60); $ test = $ idobj-> getComps (); var_dump ($ test); // output content similar to the following/* array {array ('name' => 'name', 'operator' => '= ', 'value' => 'the Good show'), array ('name' => 'start', 'operator' => ', 'value' => '000000'), // 123456 indicates the timestamp array ('name' => 'start ', 'operator' => '<', 'value' => '000000 ')}*/

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.