Action.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace Elastica\Bulk;
  3. use Elastica\Bulk;
  4. use Elastica\Index;
  5. use Elastica\JSON;
  6. use Elastica\Type;
  7. class Action
  8. {
  9. const OP_TYPE_CREATE = 'create';
  10. const OP_TYPE_INDEX = 'index';
  11. const OP_TYPE_DELETE = 'delete';
  12. const OP_TYPE_UPDATE = 'update';
  13. /**
  14. * @var array
  15. */
  16. public static $opTypes = [
  17. self::OP_TYPE_CREATE,
  18. self::OP_TYPE_INDEX,
  19. self::OP_TYPE_DELETE,
  20. self::OP_TYPE_UPDATE,
  21. ];
  22. /**
  23. * @var string
  24. */
  25. protected $_opType;
  26. /**
  27. * @var array
  28. */
  29. protected $_metadata = [];
  30. /**
  31. * @var array
  32. */
  33. protected $_source = [];
  34. /**
  35. * @param string $opType
  36. * @param array $metadata
  37. * @param array $source
  38. */
  39. public function __construct($opType = self::OP_TYPE_INDEX, array $metadata = [], array $source = [])
  40. {
  41. $this->setOpType($opType);
  42. $this->setMetadata($metadata);
  43. $this->setSource($source);
  44. }
  45. /**
  46. * @param string $type
  47. *
  48. * @return $this
  49. */
  50. public function setOpType($type)
  51. {
  52. $this->_opType = $type;
  53. return $this;
  54. }
  55. /**
  56. * @return string
  57. */
  58. public function getOpType()
  59. {
  60. return $this->_opType;
  61. }
  62. /**
  63. * @param array $metadata
  64. *
  65. * @return $this
  66. */
  67. public function setMetadata(array $metadata)
  68. {
  69. $this->_metadata = $metadata;
  70. return $this;
  71. }
  72. /**
  73. * @return array
  74. */
  75. public function getMetadata()
  76. {
  77. return $this->_metadata;
  78. }
  79. /**
  80. * @return array
  81. */
  82. public function getActionMetadata()
  83. {
  84. return [$this->_opType => $this->getMetadata()];
  85. }
  86. /**
  87. * @param array $source
  88. *
  89. * @return $this
  90. */
  91. public function setSource($source)
  92. {
  93. $this->_source = $source;
  94. return $this;
  95. }
  96. /**
  97. * @return array
  98. */
  99. public function getSource()
  100. {
  101. return $this->_source;
  102. }
  103. /**
  104. * @return bool
  105. */
  106. public function hasSource()
  107. {
  108. return !empty($this->_source);
  109. }
  110. /**
  111. * @param string|\Elastica\Index $index
  112. *
  113. * @return $this
  114. */
  115. public function setIndex($index)
  116. {
  117. if ($index instanceof Index) {
  118. $index = $index->getName();
  119. }
  120. $this->_metadata['_index'] = $index;
  121. return $this;
  122. }
  123. /**
  124. * @param string|\Elastica\Type $type
  125. *
  126. * @return $this
  127. */
  128. public function setType($type)
  129. {
  130. if ($type instanceof Type) {
  131. $this->setIndex($type->getIndex()->getName());
  132. $type = $type->getName();
  133. }
  134. $this->_metadata['_type'] = $type;
  135. return $this;
  136. }
  137. /**
  138. * @param string $id
  139. *
  140. * @return $this
  141. */
  142. public function setId($id)
  143. {
  144. $this->_metadata['_id'] = $id;
  145. return $this;
  146. }
  147. /**
  148. * @param string $routing
  149. *
  150. * @return $this
  151. */
  152. public function setRouting($routing)
  153. {
  154. $this->_metadata['_routing'] = $routing;
  155. return $this;
  156. }
  157. /**
  158. * @return array
  159. */
  160. public function toArray()
  161. {
  162. $data[] = $this->getActionMetadata();
  163. if ($this->hasSource()) {
  164. $data[] = $this->getSource();
  165. }
  166. return $data;
  167. }
  168. /**
  169. * @return string
  170. */
  171. public function toString()
  172. {
  173. $string = JSON::stringify($this->getActionMetadata(), JSON_FORCE_OBJECT).Bulk::DELIMITER;
  174. if ($this->hasSource()) {
  175. $source = $this->getSource();
  176. if (is_string($source)) {
  177. $string .= $source;
  178. } elseif (is_array($source) && array_key_exists('doc', $source) && is_string($source['doc'])) {
  179. if (isset($source['doc_as_upsert'])) {
  180. $docAsUpsert = ', "doc_as_upsert": '.($source['doc_as_upsert'] ? 'true' : 'false');
  181. } else {
  182. $docAsUpsert = '';
  183. }
  184. $string .= '{"doc": '.$source['doc'].$docAsUpsert.'}';
  185. } else {
  186. $string .= JSON::stringify($source, JSON_UNESCAPED_UNICODE);
  187. }
  188. $string .= Bulk::DELIMITER;
  189. }
  190. return $string;
  191. }
  192. /**
  193. * @param string $opType
  194. *
  195. * @return bool
  196. */
  197. public static function isValidOpType($opType)
  198. {
  199. return in_array($opType, self::$opTypes);
  200. }
  201. }