Completed
Push — master ( 96ab8a...466607 )
by Nicolas
03:10
created

AbstractScript::_createFromArray()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 21
nc 20
nop 1
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_MVEL = 'mvel';
21
    const LANG_JS = 'js';
22
    const LANG_GROOVY = 'groovy';
23
    const LANG_PYTHON = 'python';
24
    const LANG_NATIVE = 'native';
25
    const LANG_EXPRESSION = 'expression';
26
    const LANG_PAINLESS = 'painless';
27
28
    /**
29
     * @var string
30
     */
31
    private $_lang;
32
33
    /**
34
     * Factory to create a script object from data structure (reverse toArray).
35
     *
36
     * @param string|array|AbstractScript $data
37
     *
38
     * @throws InvalidException
39
     *
40
     * @return Script|ScriptFile|ScriptId
41
     */
42
    public static function create($data)
43
    {
44
        if ($data instanceof self) {
45
            return $data;
46
        }
47
48
        if (is_array($data)) {
49
            return self::_createFromArray($data);
50
        }
51
52
        if (is_string($data)) {
53
            $class = self::class === get_called_class() ? Script::class : get_called_class();
54
55
            return new $class($data);
56
        }
57
58
        throw new InvalidException('Failed to create script. Invalid data passed.');
59
    }
60
61
    private static function _createFromArray(array $data)
62
    {
63
        $params = isset($data['script']['params']) ? $data['script']['params'] : [];
64
        $lang = isset($data['script']['lang']) ? $data['script']['lang'] : null;
65
66
        if (!is_array($params)) {
67
            throw new InvalidException('Script params must be an array');
68
        }
69
70
        if (isset($data['script']['inline'])) {
71
            return new Script(
72
                $data['script']['inline'],
73
                $params,
74
                $lang
75
            );
76
        }
77
78
        if (isset($data['script']['file'])) {
79
            return new ScriptFile(
80
                $data['script']['file'],
81
                $params,
82
                $lang
83
            );
84
        }
85
86
        if (isset($data['script']['id'])) {
87
            return new ScriptId(
88
                $data['script']['id'],
89
                $params,
90
                $lang
91
            );
92
        }
93
94
        throw new InvalidException('Failed to create script. Invalid data passed.');
95
    }
96
97
    /**
98
     * @param array|null  $params
99
     * @param string|null $lang       Script language, see constants
100
     * @param string|null $documentId Document ID the script action should be performed on (only relevant in update context)
101
     */
102
    public function __construct(array $params = null, $lang = null, $documentId = null)
103
    {
104
        if ($params) {
105
            $this->setParams($params);
106
        }
107
108
        if (null !== $lang) {
109
            $this->setLang($lang);
110
        }
111
112
        if (null !== $documentId) {
113
            $this->setId($documentId);
114
        }
115
    }
116
117
    /**
118
     * @param string $lang
119
     *
120
     * @return $this
121
     */
122
    public function setLang($lang)
123
    {
124
        $this->_lang = $lang;
125
126
        return $this;
127
    }
128
129
    /**
130
     * @return string|null
131
     */
132
    public function getLang()
133
    {
134
        return $this->_lang;
135
    }
136
137
    /**
138
     * Returns an array with the script type as key and the script content as value.
139
     *
140
     * @return array
141
     */
142
    abstract protected function getScriptTypeArray();
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function toArray()
148
    {
149
        $array = $this->getScriptTypeArray();
150
151
        if (!empty($this->_params)) {
152
            $array['params'] = $this->_convertArrayable($this->_params);
153
        }
154
155
        if (null !== $this->_lang) {
156
            $array['lang'] = $this->_lang;
157
        }
158
159
        return ['script' => $array];
160
    }
161
}
162