PHP Object-oriented Identity object

Source: Internet
Author: User

  1. /*
  2. Identity object Pattern
  3. 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:
  4. */
  5. namespace Woo\mapper;
  6. Field Object
  7. Class Field {
  8. protected $name = null; Field name
  9. protected $operator = null; Operator
  10. Protected $comps = Array (); Array of storage conditions
  11. protected $incomplete = false; Checks if the condition array has a value
  12. function __construct ($name) {
  13. $this->name= $name;
  14. }
  15. Add a Where condition
  16. function Addtest ($operator, $value) {
  17. $this->comps[] = Array (' name ' = = $this->name, ' operator ' = $operator, ' value ' = $value);
  18. }
  19. Gets the array that holds the condition
  20. function Getcomps () {
  21. return $this->comps;
  22. }
  23. function Isincomplete () {
  24. return empty ($this->comps);
  25. }
  26. }
  27. Identifying objects
  28. Class Identityobject {
  29. protected $currentfield = null; Field object for the current action
  30. Protected $fields = Array (); Field Collection
  31. Private $and = null;
  32. Private $enforce = Array (); Qualified legal fields
  33. function __construct ($field = null, array $enforce = null) {
  34. if (!is_null ($enforce)) {
  35. $this->enforce = $enforce;
  36. }
  37. if (!is_null ($field)) {
  38. $this->field ($field);
  39. }
  40. }
  41. Get qualified legal fields
  42. function Getobjectfields () {
  43. return $this->enforce;
  44. }
  45. The main function is to set the object that is currently required to operate
  46. function field ($fieldname) {
  47. if (! $this->isvoid () && $this->currentfield->isincomplete ()) {
  48. throw new \exception ("incomplete field");
  49. }
  50. $this->enforcefield ($fieldname);
  51. if (Isset ($this->fields[$fieldname]) {
  52. $this->currentfield = $this->fields[$fieldname];
  53. } else {
  54. $this->currentfield = new Field ($fieldname);
  55. $this->fields[$fieldname] = $this->currentfield;
  56. }
  57. return $this; Use coherent syntax
  58. }
  59. Whether the field collection is empty
  60. function Isvoid () {
  61. return empty ($this->fields);
  62. }
  63. Check if the field is legal
  64. function Enforcefield ($fieldname) {
  65. if (!in_array ($fieldname, $this->enforce) &&!empty ($this->enforce)) {
  66. $forcelist = Implode (', ', $this->enforce);
  67. throw new \exception ("{$fieldname} not a legal field {$forcelist}");
  68. }
  69. }
  70. Add a Where condition to a Field object
  71. function eq ($value) {
  72. return $this->operator ("=", $value);
  73. }
  74. Function lt ($value) {
  75. return $this->operator ("<", $value);
  76. }
  77. function GT ($value) {
  78. return $this->operator (">", $value);
  79. }
  80. Add a Where condition to a Field object
  81. Private function operator ($symbol, $value) {
  82. if ($this->isvoid) {
  83. throw new \exception ("No object field defined");
  84. }
  85. $this->currentfield->addtest ($symbol, $value);
  86. return $this; Use coherent syntax
  87. }
  88. Gets an array of where conditions for all the Field object collections in this class
  89. function Getcomps () {
  90. $ret = Array ();
  91. foreach ($this->fields as $key = + $field) {
  92. $ret = Array_merge ($ret, $field->getcomps ());
  93. }
  94. return $ret;
  95. }
  96. }
  97. Client code
  98. $idobj = new Identityobject ();
  99. $idobj->field ("name")->eq ("The Good Show")->field ("Start")->gt (Time ())->lt (Time () + (24*60*60));
  100. $test = $idobj->getcomps ();
  101. Var_dump ($test);
  102. Output similar to the following content
  103. /*
  104. array{
  105. Array (' name ' = = ' name ', ' operator ' = ' = ', ' value ' = ' The Good Show '),
  106. Array (' name ' = = ' start ', ' operator ' = ' > ', ' value ' = ' 123456 '),//123456 represents the time () function output timestamp
  107. Array (' name ' = ' start ', ' operator ' = ' < ', ' value ' = ' 123456 ')
  108. }
  109. */

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.