AbstractScript.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Elastica\Script;
  3. use Elastica\AbstractUpdateAction;
  4. use Elastica\Exception\InvalidException;
  5. /**
  6. * Base class for Script object.
  7. *
  8. * Wherever scripting is supported in the Elasticsearch API, scripts can be referenced as "inline", "id" or "file".
  9. *
  10. * @author Nicolas Assing <nicolas.assing@gmail.com>
  11. * @author Tobias Schultze <http://tobion.de>
  12. * @author Martin Janser <martin.janser@liip.ch>
  13. *
  14. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html
  15. */
  16. abstract class AbstractScript extends AbstractUpdateAction
  17. {
  18. const LANG_MOUSTACHE = 'moustache';
  19. const LANG_EXPRESSION = 'expression';
  20. const LANG_PAINLESS = 'painless';
  21. /**
  22. * @var string
  23. */
  24. private $_lang;
  25. /**
  26. * Factory to create a script object from data structure (reverse toArray).
  27. *
  28. * @param string|array|AbstractScript $data
  29. *
  30. * @throws InvalidException
  31. *
  32. * @return Script|ScriptId
  33. */
  34. public static function create($data)
  35. {
  36. if ($data instanceof self) {
  37. return $data;
  38. }
  39. if (is_array($data)) {
  40. return self::_createFromArray($data);
  41. }
  42. if (is_string($data)) {
  43. $class = self::class === get_called_class() ? Script::class : get_called_class();
  44. return new $class($data);
  45. }
  46. throw new InvalidException('Failed to create script. Invalid data passed.');
  47. }
  48. private static function _createFromArray(array $data)
  49. {
  50. $params = $data['script']['params'] ?? [];
  51. $lang = $data['script']['lang'] ?? null;
  52. if (!is_array($params)) {
  53. throw new InvalidException('Script params must be an array');
  54. }
  55. if (isset($data['script']['source'])) {
  56. return new Script(
  57. $data['script']['source'],
  58. $params,
  59. $lang
  60. );
  61. }
  62. if (isset($data['script']['id'])) {
  63. return new ScriptId(
  64. $data['script']['id'],
  65. $params,
  66. $lang
  67. );
  68. }
  69. throw new InvalidException('Failed to create script. Invalid data passed.');
  70. }
  71. /**
  72. * @param array|null $params
  73. * @param string|null $lang Script language, see constants
  74. * @param string|null $documentId Document ID the script action should be performed on (only relevant in update context)
  75. */
  76. public function __construct(array $params = null, $lang = null, $documentId = null)
  77. {
  78. if ($params) {
  79. $this->setParams($params);
  80. }
  81. if (null !== $lang) {
  82. $this->setLang($lang);
  83. }
  84. if (null !== $documentId) {
  85. $this->setId($documentId);
  86. }
  87. }
  88. /**
  89. * @param string $lang
  90. *
  91. * @return $this
  92. */
  93. public function setLang($lang)
  94. {
  95. $this->_lang = $lang;
  96. return $this;
  97. }
  98. /**
  99. * @return string|null
  100. */
  101. public function getLang()
  102. {
  103. return $this->_lang;
  104. }
  105. /**
  106. * Returns an array with the script type as key and the script content as value.
  107. *
  108. * @return array
  109. */
  110. abstract protected function getScriptTypeArray();
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function toArray()
  115. {
  116. $array = $this->getScriptTypeArray();
  117. if (!empty($this->_params)) {
  118. $array['params'] = $this->_convertArrayable($this->_params);
  119. }
  120. if (null !== $this->_lang) {
  121. $array['lang'] = $this->_lang;
  122. }
  123. return ['script' => $array];
  124. }
  125. }