Criteria.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace OntraportAPI;
  3. /**
  4. * Class Criteria
  5. *
  6. * @author ONTRAPORT
  7. *
  8. * @package OntraportAPI
  9. */
  10. class Criteria
  11. {
  12. /**
  13. * @var array of condition data
  14. */
  15. protected $_condition = array();
  16. public function __construct($field, $relational_operator, $value)
  17. {
  18. $this->_condition[] = $this->buildCondition($field, $relational_operator, $value);
  19. }
  20. /**
  21. * @brief Add another condition that must be met in addition to existing condition(s)
  22. *
  23. * @param string $field The field subject to the condition.
  24. * @param string $relational_operator The comparison operator. May be "=","<>",">","<",">=","<=","IN","NOT IN", or "IS".
  25. * @param string|int|null $value The value to compare the field against.
  26. */
  27. public function andCondition($field, $relational_operator, $value)
  28. {
  29. if ($this->_condition)
  30. {
  31. $this->_condition[] = "AND";
  32. }
  33. $this->_condition[] = $this->buildCondition($field, $relational_operator, $value);
  34. }
  35. /**
  36. * @brief Add another condition that may be met in addition to existing condition(s)
  37. *
  38. * @param string $field The field subject to the condition.
  39. * @param string $relational_operator The comparison operator. May be "=","<>",">","<",">=","<=","IN","NOT IN", or "IS".
  40. * @param string|int|null $value The value to compare the field against.
  41. */
  42. public function orCondition($field, $relational_operator, $value)
  43. {
  44. if ($this->_condition)
  45. {
  46. $this->_condition[] = "OR";
  47. }
  48. $this->_condition[] = $this->buildCondition($field, $relational_operator, $value);
  49. }
  50. /**
  51. * @brief Return JSON-encoded criteria object from conditions array
  52. *
  53. * @return string JSON
  54. */
  55. public function fromArray()
  56. {
  57. return json_encode($this->_condition);
  58. }
  59. /**
  60. * @brief Assembles a single condition
  61. *
  62. * @param string $field
  63. * @param string $relational_operator
  64. * @param string|int|null $value
  65. *
  66. * @return array
  67. */
  68. private function buildCondition($field, $relational_operator, $value)
  69. {
  70. $this->_validateCondition($relational_operator, $value);
  71. $condition["field"] = array("field" => $field);
  72. $condition["op"] = $relational_operator;
  73. if (is_array($value))
  74. {
  75. $list = array();
  76. foreach ($value as $item)
  77. {
  78. $list[] = array("value" => $item);
  79. }
  80. $condition["value"] = array("list" => $list);
  81. }
  82. else
  83. {
  84. $condition["value"] = array("value" => $value);
  85. }
  86. return $condition;
  87. }
  88. /**
  89. * @brief Validate condition data
  90. *
  91. * @param string $relational_operator
  92. * @param string|int|null $value
  93. *
  94. * @throws Exceptions\ArrayOperatorException
  95. * @throws Exceptions\ConditionOperatorException
  96. */
  97. private function _validateCondition($relational_operator, $value)
  98. {
  99. $relational_operators = array("=","<>",">","<",">=","<=","IN","NOT IN","IS","LIKE");
  100. if (!in_array($relational_operator, $relational_operators))
  101. {
  102. throw new Exceptions\ConditionOperatorException($relational_operator);
  103. }
  104. if (is_array($value))
  105. {
  106. if ($relational_operator !== "IN")
  107. {
  108. throw new Exceptions\ArrayOperatorException();
  109. }
  110. }
  111. }
  112. }