Skip to content

Commit 2b4eb2c

Browse files
committed
Merge pull request #1028 from mortenhauberg/fix-script-file
Fix script file issues and move script classes into namespace
2 parents e82649c + e1e244d commit 2b4eb2c

37 files changed

+518
-411
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file based on the
1414
### Improvements
1515

1616
### Deprecated
17+
- Elastica\AbstractScript|Script|ScriptFile|ScriptFields deprecated in favor of Elastica\Script|AbstractScript|Script|ScriptFile|ScriptFields [#1028](https://github.com/ruflin/Elastica/pull/1028)
1718

1819

1920
## [3.0.1](https://github.com/ruflin/Elastica/compare/3.0.0...3.0.1)

lib/Elastica/AbstractScript.php

+4-19
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
11
<?php
22
namespace Elastica;
33

4+
use Elastica\Script\AbstractScript as BaseAbstractScript;
45
/**
5-
* Base class for Script object.
6+
* Kept for BC reasons.
67
*
7-
* @author Nicolas Assing <[email protected]>
8-
*
9-
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html
8+
* @deprecated Use \Elastica\Script\AbstractScript instead.
109
*/
11-
abstract class AbstractScript extends AbstractUpdateAction
10+
abstract class AbstractScript extends BaseAbstractScript
1211
{
13-
/**
14-
* @param array|null $params
15-
* @param string $id
16-
*/
17-
public function __construct(array $params = null, $id = null)
18-
{
19-
if ($params) {
20-
$this->setParams($params);
21-
}
22-
23-
if ($id) {
24-
$this->setId($id);
25-
}
26-
}
2712
}

lib/Elastica/Aggregation/AbstractSimpleAggregation.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22
namespace Elastica\Aggregation;
33

4-
use Elastica\Script;
5-
64
abstract class AbstractSimpleAggregation extends AbstractAggregation
75
{
86
/**
@@ -20,7 +18,7 @@ public function setField($field)
2018
/**
2119
* Set a script for this aggregation.
2220
*
23-
* @param string|Script $script
21+
* @param string|\Elastica\Script\AbstractScript $script
2422
*
2523
* @return $this
2624
*/

lib/Elastica/Aggregation/TopHits.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22
namespace Elastica\Aggregation;
33

4-
use Elastica\Script;
5-
use Elastica\ScriptFields;
4+
use Elastica\Script\AbstractScript;
5+
use Elastica\Script\ScriptFields;
66

77
/**
88
* Class TopHits.
@@ -102,7 +102,7 @@ public function setExplain($explain)
102102
/**
103103
* Set script fields.
104104
*
105-
* @param array|\Elastica\ScriptFields $scriptFields
105+
* @param array|\Elastica\Script\ScriptFields $scriptFields
106106
*
107107
* @return $this
108108
*/
@@ -118,12 +118,12 @@ public function setScriptFields($scriptFields)
118118
/**
119119
* Adds a Script to the aggregation.
120120
*
121-
* @param string $name
122-
* @param \Elastica\Script $script
121+
* @param string $name
122+
* @param \Elastica\Script\AbstractScript $script
123123
*
124124
* @return $this
125125
*/
126-
public function addScriptField($name, Script $script)
126+
public function addScriptField($name, AbstractScript $script)
127127
{
128128
if (!isset($this->_params['script_fields'])) {
129129
$this->_params['script_fields'] = new ScriptFields();

lib/Elastica/Bulk.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Elastica\Bulk\ResponseSet;
88
use Elastica\Exception\Bulk\ResponseException as BulkResponseException;
99
use Elastica\Exception\InvalidException;
10+
use Elastica\Script\AbstractScript;
1011

1112
class Bulk
1213
{
@@ -190,12 +191,12 @@ public function addDocuments(array $documents, $opType = null)
190191
}
191192

192193
/**
193-
* @param \Elastica\Script $script
194-
* @param string $opType
194+
* @param \Elastica\Script\AbstractScript $script
195+
* @param string $opType
195196
*
196197
* @return $this
197198
*/
198-
public function addScript(Script $script, $opType = null)
199+
public function addScript(AbstractScript $script, $opType = null)
199200
{
200201
$action = AbstractDocumentAction::create($script, $opType);
201202

@@ -218,8 +219,8 @@ public function addScripts(array $scripts, $opType = null)
218219
}
219220

220221
/**
221-
* @param \Elastica\Script|\Elastica\Document|array $data
222-
* @param string $opType
222+
* @param \Elastica\Script\AbstractScript|\Elastica\Document|array $data
223+
* @param string $opType
223224
*
224225
* @return $this
225226
*/
@@ -230,7 +231,7 @@ public function addData($data, $opType = null)
230231
}
231232

232233
foreach ($data as $actionData) {
233-
if ($actionData instanceof Script) {
234+
if ($actionData instanceof AbstractScript) {
234235
$this->addScript($actionData, $opType);
235236
} elseif ($actionData instanceof Document) {
236237
$this->addDocument($actionData, $opType);

lib/Elastica/Bulk/Action/AbstractDocument.php

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<?php
22
namespace Elastica\Bulk\Action;
33

4+
use Elastica\Script\AbstractScript;
45
use Elastica\AbstractUpdateAction;
56
use Elastica\Bulk\Action;
67
use Elastica\Document;
7-
use Elastica\Script;
88

99
abstract class AbstractDocument extends Action
1010
{
1111
/**
12-
* @var \Elastica\Document|\Elastica\Script
12+
* @var \Elastica\Document|\Elastica\Script\AbstractScript
1313
*/
1414
protected $_data;
1515

1616
/**
17-
* @param \Elastica\Document|\Elastica\Script $document
17+
* @param \Elastica\Document|\Elastica\Script\AbstractScript $document
1818
*/
1919
public function __construct($document)
2020
{
@@ -38,11 +38,11 @@ public function setDocument(Document $document)
3838
}
3939

4040
/**
41-
* @param \Elastica\Script $script
41+
* @param \Elastica\Script\AbstractScript $script
4242
*
4343
* @return $this
4444
*/
45-
public function setScript(Script $script)
45+
public function setScript(AbstractScript $script)
4646
{
4747
if (!($this instanceof UpdateDocument)) {
4848
throw new \BadMethodCallException('setScript() can only be used for UpdateDocument');
@@ -57,15 +57,15 @@ public function setScript(Script $script)
5757
}
5858

5959
/**
60-
* @param \Elastica\Script|\Elastica\Document $data
60+
* @param \Elastica\Script\AbstractScript|\Elastica\Document $data
6161
*
6262
* @throws \InvalidArgumentException
6363
*
6464
* @return $this
6565
*/
6666
public function setData($data)
6767
{
68-
if ($data instanceof Script) {
68+
if ($data instanceof AbstractScript) {
6969
$this->setScript($data);
7070
} elseif ($data instanceof Document) {
7171
$this->setDocument($data);
@@ -93,19 +93,19 @@ public function getDocument()
9393
/**
9494
* Note: This is for backwards compatibility.
9595
*
96-
* @return \Elastica\Script|null
96+
* @return \Elastica\Script\AbstractScript|null
9797
*/
9898
public function getScript()
9999
{
100-
if ($this->_data instanceof Script) {
100+
if ($this->_data instanceof AbstractScript) {
101101
return $this->_data;
102102
}
103103

104104
return;
105105
}
106106

107107
/**
108-
* @return \Elastica\Document|\Elastica\Script
108+
* @return \Elastica\Document|\Elastica\Script\AbstractScript
109109
*/
110110
public function getData()
111111
{
@@ -120,15 +120,15 @@ public function getData()
120120
abstract protected function _getMetadata(AbstractUpdateAction $source);
121121

122122
/**
123-
* @param \Elastica\Document|\Elastica\Script $data
124-
* @param string $opType
123+
* @param \Elastica\Document|\Elastica\Script\AbstractScript $data
124+
* @param string $opType
125125
*
126126
* @return static
127127
*/
128128
public static function create($data, $opType = null)
129129
{
130130
//Check type
131-
if (!($data instanceof Document) && !($data instanceof Script)) {
131+
if (!($data instanceof Document) && !($data instanceof AbstractScript)) {
132132
throw new \InvalidArgumentException('The data needs to be a Document or a Script.');
133133
}
134134

@@ -137,7 +137,7 @@ public static function create($data, $opType = null)
137137
}
138138

139139
//Check that scripts can only be used for updates
140-
if ($data instanceof Script) {
140+
if ($data instanceof AbstractScript) {
141141
if ($opType === null) {
142142
$opType = self::OP_TYPE_UPDATE;
143143
} elseif ($opType != self::OP_TYPE_UPDATE) {

lib/Elastica/Bulk/Action/UpdateDocument.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22
namespace Elastica\Bulk\Action;
33

4+
use Elastica\Script\AbstractScript;
45
use Elastica\Document;
5-
use Elastica\Script;
66

77
class UpdateDocument extends IndexDocument
88
{
@@ -40,11 +40,11 @@ public function setDocument(Document $document)
4040
}
4141

4242
/**
43-
* @param \Elastica\Script $script
43+
* @param \Elastica\Script\AbstractScript $script
4444
*
4545
* @return $this
4646
*/
47-
public function setScript(Script $script)
47+
public function setScript(AbstractScript $script)
4848
{
4949
parent::setScript($script);
5050

lib/Elastica/Client.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Elastica\Exception\ConnectionException;
66
use Elastica\Exception\InvalidException;
77
use Elastica\Exception\RuntimeException;
8+
use Elastica\Script\AbstractScript;
89
use Psr\Log\LoggerInterface;
910

1011
/**
@@ -319,11 +320,11 @@ public function addDocuments(array $docs)
319320
/**
320321
* Update document, using update script. Requires elasticsearch >= 0.19.0.
321322
*
322-
* @param int $id document id
323-
* @param array|\Elastica\Script|\Elastica\Document $data raw data for request body
324-
* @param string $index index to update
325-
* @param string $type type of index to update
326-
* @param array $options array of query params to use for query. For possible options check es api
323+
* @param int $id document id
324+
* @param array|\Elastica\Script\AbstractScript|\Elastica\Document $data raw data for request body
325+
* @param string $index index to update
326+
* @param string $type type of index to update
327+
* @param array $options array of query params to use for query. For possible options check es api
327328
*
328329
* @return \Elastica\Response
329330
*
@@ -333,7 +334,7 @@ public function updateDocument($id, $data, $index, $type, array $options = array
333334
{
334335
$path = $index.'/'.$type.'/'.$id.'/_update';
335336

336-
if ($data instanceof Script) {
337+
if ($data instanceof AbstractScript) {
337338
$requestData = $data->toArray();
338339
} elseif ($data instanceof Document) {
339340
$requestData = array('doc' => $data->getData());
@@ -370,7 +371,7 @@ public function updateDocument($id, $data, $index, $type, array $options = array
370371
}
371372

372373
//If an upsert document exists
373-
if ($data instanceof Script || $data instanceof Document) {
374+
if ($data instanceof AbstractScript || $data instanceof Document) {
374375
if ($data->hasUpsert()) {
375376
$requestData['upsert'] = $data->getUpsert()->getData();
376377
}

lib/Elastica/Document.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public function getData()
257257
/**
258258
* @deprecated setScript() is no longer available as of 0.90.2. See http://elastica.io/migration/0.90.2/upsert.html to migrate. This method will be removed in further Elastica releases
259259
*
260-
* @param \Elastica\Script $data
260+
* @param \Elastica\Script\Script $data
261261
*
262262
* @throws NotImplementedException
263263
*/

lib/Elastica/Filter/Script.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Script extends AbstractFilter
2222
/**
2323
* Construct script filter.
2424
*
25-
* @param array|string|\Elastica\Script $script OPTIONAL Script
25+
* @param array|string|\Elastica\Script\AbstractScript $script OPTIONAL Script
2626
*/
2727
public function __construct($script = null)
2828
{
@@ -34,13 +34,13 @@ public function __construct($script = null)
3434
/**
3535
* Sets script object.
3636
*
37-
* @param \Elastica\Script|string|array $script
37+
* @param \Elastica\Script\Script|string|array $script
3838
*
3939
* @return $this
4040
*/
4141
public function setScript($script)
4242
{
43-
return $this->setParam('script', Elastica\Script::create($script));
43+
return $this->setParam('script', Elastica\Script\Script::create($script));
4444
}
4545

4646
/**

lib/Elastica/Query.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Elastica\Query\AbstractQuery;
99
use Elastica\Query\MatchAll;
1010
use Elastica\Query\QueryString;
11+
use Elastica\Script\AbstractScript;
12+
use Elastica\Script\ScriptFields;
1113
use Elastica\Suggest\AbstractSuggest;
1214

1315
/**
@@ -303,7 +305,7 @@ public function setFieldDataFields(array $fieldDataFields)
303305
/**
304306
* Set script fields.
305307
*
306-
* @param array|\Elastica\ScriptFields $scriptFields Script fields
308+
* @param array|\Elastica\Script\ScriptFields $scriptFields Script fields
307309
*
308310
* @return $this
309311
*
@@ -321,8 +323,8 @@ public function setScriptFields($scriptFields)
321323
/**
322324
* Adds a Script to the query.
323325
*
324-
* @param string $name
325-
* @param \Elastica\AbstractScript $script Script object
326+
* @param string $name
327+
* @param \Elastica\Script\AbstractScript $script Script object
326328
*
327329
* @return $this
328330
*/

0 commit comments

Comments
 (0)