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