PHP Object-oriented Identity object

Source: Internet
Author: User

/*Identity Object mode The primary function of this pattern is to create a wehre conditional string in an SQL statement, and see the code and comments directly below:*/namespace Woo\mapper;//Field ObjectclassField {protected $name=NULL;//Field name    protected $operator=NULL;//operator    protected $comps=Array();//array of storage conditions    protected $incomplete=false;//checks if the condition array has a value        function__construct ($name){        $this->name=$name; }        //add a Where condition    functionAddtest ($operator,$value){        $this->comps[] =Array(' name ' = =$this->name, ' operator ' =$operator, ' value ' =$value); }        //gets the array that holds the condition    functionGetcomps () {return $this-comps; }        functionIsincomplete () {return Empty($this-comps); }}//Identifying ObjectsclassIdentityobject {protected $currentfield=NULL;//Field object for the current action    protected $fields=Array();//Field Collection    Private $and=NULL; Private $enforce=Array();//qualified legal fields        function__construct ($field=NULL,Array $enforce=NULL){        if(!Is_null($enforce)){            $this->enforce =$enforce; }        if(!Is_null($field)){            $this->field ($field); }    }        //get qualified legal fields    functionGetobjectfields () {return $this-enforce; }        //The main function is to set the object that is currently required to operate    functionField$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 =NewField ($fieldname); $this->fields[$fieldname] =$this-Currentfield; }        return $this;//use coherent syntax    }        //whether the field collection is empty    functionisvoid () {return Empty($this-Fields ); }        //Check if the field is legal    functionEnforcefield ($fieldname){        if(!In_array($fieldname,$this->enforce) &&!Empty($this-enforce)) {            $forcelist=implode(‘,‘,$this-enforce); Throw New\Exception("{$fieldname} Not a legal field {$forcelist}"); }    }            //add a Where condition to a Field object    functioneq$value){        return $this->operator ("=",$value); }        functionLt$value){        return $this->operator ("<",$value); }        functionGt$value){        return $this->operator (">",$value); }        //add a Where condition to a Field object    Private functionoperator$symbol,$value){        if($this-isvoid) {            Throw New\Exception("No object field defined"); }        $this->currentfield->addtest ($symbol,$value); return $this;//use coherent syntax    }        //gets an array of where conditions for all the Field object collections in this class    functionGetcomps () {$ret=Array(); foreach($this->fields as $key=$field){            $ret=Array_merge($ret,$field-getcomps ()); }        return $ret; }}//Client Code$idobj=NewIdentityobject ();$idobj->field ("name")->eq ("The Good Show")->field ("Start")->gt ( Time())->lt ( Time() + (24*60*60));$test=$idobj-getcomps ();Var_dump($test);//output similar to the following content/*array{Array (' name ' = = ' name ', ' operator ' = ' = ', ' value ' = ' ' The Good Show '), Array (' name ' = = ' start ', ' Operator ' = ' > ', ' value ' = ' 123456 '),//123456 represents the time () function output timestamp array (' name ' = = ' start ', ' operator ' = ' = ') < ', ' value ' = ' 123456 ')}*/

PHP Object-oriented Identity object

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.