AbstractDocument.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace Elastica\Bulk\Action;
  3. use Elastica\AbstractUpdateAction;
  4. use Elastica\Bulk\Action;
  5. use Elastica\Document;
  6. use Elastica\Script\AbstractScript;
  7. abstract class AbstractDocument extends Action
  8. {
  9. /**
  10. * @var Document|AbstractScript
  11. */
  12. protected $_data;
  13. /**
  14. * @param Document|AbstractScript $document
  15. */
  16. public function __construct($document)
  17. {
  18. $this->setData($document);
  19. }
  20. /**
  21. * @param Document $document
  22. *
  23. * @return $this
  24. */
  25. public function setDocument(Document $document)
  26. {
  27. $this->_data = $document;
  28. $metadata = $this->_getMetadata($document);
  29. $this->setMetadata($metadata);
  30. return $this;
  31. }
  32. /**
  33. * @param AbstractScript $script
  34. *
  35. * @return $this
  36. */
  37. public function setScript(AbstractScript $script)
  38. {
  39. if (!($this instanceof UpdateDocument)) {
  40. throw new \BadMethodCallException('setScript() can only be used for UpdateDocument');
  41. }
  42. $this->_data = $script;
  43. $metadata = $this->_getMetadata($script);
  44. $this->setMetadata($metadata);
  45. return $this;
  46. }
  47. /**
  48. * @param AbstractScript|Document $data
  49. *
  50. * @throws \InvalidArgumentException
  51. *
  52. * @return $this
  53. */
  54. public function setData($data)
  55. {
  56. if ($data instanceof AbstractScript) {
  57. $this->setScript($data);
  58. } elseif ($data instanceof Document) {
  59. $this->setDocument($data);
  60. } else {
  61. throw new \InvalidArgumentException('Data should be a Document or a Script.');
  62. }
  63. return $this;
  64. }
  65. /**
  66. * Note: This is for backwards compatibility.
  67. *
  68. * @return Document|null
  69. */
  70. public function getDocument()
  71. {
  72. if ($this->_data instanceof Document) {
  73. return $this->_data;
  74. }
  75. return;
  76. }
  77. /**
  78. * Note: This is for backwards compatibility.
  79. *
  80. * @return AbstractScript|null
  81. */
  82. public function getScript()
  83. {
  84. if ($this->_data instanceof AbstractScript) {
  85. return $this->_data;
  86. }
  87. return;
  88. }
  89. /**
  90. * @return Document|AbstractScript
  91. */
  92. public function getData()
  93. {
  94. return $this->_data;
  95. }
  96. /**
  97. * @param \Elastica\AbstractUpdateAction $source
  98. *
  99. * @return array
  100. */
  101. abstract protected function _getMetadata(AbstractUpdateAction $source);
  102. /**
  103. * Creates a bulk action for a document or a script.
  104. *
  105. * The action can be index, update, create or delete based on the $opType param (by default index).
  106. *
  107. * @param Document|AbstractScript $data
  108. * @param string $opType
  109. *
  110. * @return static
  111. */
  112. public static function create($data, $opType = null)
  113. {
  114. //Check type
  115. if (!($data instanceof Document) && !($data instanceof AbstractScript)) {
  116. throw new \InvalidArgumentException('The data needs to be a Document or a Script.');
  117. }
  118. if (null === $opType && $data->hasOpType()) {
  119. $opType = $data->getOpType();
  120. }
  121. //Check that scripts can only be used for updates
  122. if ($data instanceof AbstractScript) {
  123. if (null === $opType) {
  124. $opType = self::OP_TYPE_UPDATE;
  125. } elseif (self::OP_TYPE_UPDATE != $opType) {
  126. throw new \InvalidArgumentException('Scripts can only be used with the update operation type.');
  127. }
  128. }
  129. switch ($opType) {
  130. case self::OP_TYPE_DELETE:
  131. $action = new DeleteDocument($data);
  132. break;
  133. case self::OP_TYPE_CREATE:
  134. $action = new CreateDocument($data);
  135. break;
  136. case self::OP_TYPE_UPDATE:
  137. $action = new UpdateDocument($data);
  138. break;
  139. case self::OP_TYPE_INDEX:
  140. default:
  141. $action = new IndexDocument($data);
  142. break;
  143. }
  144. return $action;
  145. }
  146. }