- /*
- Identity object Pattern
- The main function of this mode is to create the Wehre condition string in the SQL statement, and look directly at the code and comments below:
- */
- namespace Woo\mapper;
- Field Object
- Class Field {
- 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
- function Addtest ($operator, $value) {
- $this->comps[] = Array (' name ' = = $this->name, ' operator ' = $operator, ' value ' = $value);
- }
- Gets the array that holds the condition
- function Getcomps () {
- return $this->comps;
- }
- function Isincomplete () {
- return empty ($this->comps);
- }
- }
- Identifying objects
- Class Identityobject {
- 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
- function Getobjectfields () {
- return $this->enforce;
- }
- The main function is to set the object that is currently required to operate
- 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 coherent syntax
- }
- Whether the field collection is empty
- function Isvoid () {
- return empty ($this->fields);
- }
- Check if the field is legal
- 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 a Where condition to a Field object
- function eq ($value) {
- return $this->operator ("=", $value);
- }
- Function lt ($value) {
- return $this->operator ("<", $value);
- }
- function GT ($value) {
- return $this->operator (">", $value);
- }
- Add a Where condition to a Field object
- Private function operator ($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
- function Getcomps () {
- $ret = Array ();
- 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 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