Completed
Pull Request — master (#358)
by
unknown
01:31
created

Search::setSourceFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchDSL;
13
14
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
15
use ONGR\ElasticsearchDSL\Highlight\Highlight;
16
use ONGR\ElasticsearchDSL\InnerHit\NestedInnerHit;
17
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery;
18
use ONGR\ElasticsearchDSL\SearchEndpoint\AbstractSearchEndpoint;
19
use ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint;
20
use ONGR\ElasticsearchDSL\SearchEndpoint\HighlightEndpoint;
21
use ONGR\ElasticsearchDSL\SearchEndpoint\InnerHitsEndpoint;
22
use ONGR\ElasticsearchDSL\SearchEndpoint\PostFilterEndpoint;
23
use ONGR\ElasticsearchDSL\SearchEndpoint\QueryEndpoint;
24
use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointFactory;
25
use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointInterface;
26
use ONGR\ElasticsearchDSL\SearchEndpoint\SortEndpoint;
27
use ONGR\ElasticsearchDSL\Serializer\Normalizer\CustomReferencedNormalizer;
28
use ONGR\ElasticsearchDSL\Serializer\OrderedSerializer;
29
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
30
use ONGR\ElasticsearchDSL\SearchEndpoint\SuggestEndpoint;
31
32
/**
33
 * Search object that can be executed by a manager.
34
 */
35
class Search
36
{
37
    /**
38
     * If you don’t need to track the total number of hits at all you can improve
39
     * query times by setting this option to false. Defaults to true.
40
     *
41
     * @var bool
42
     */
43
    private $trackTotalHits;
44
45
    /**
46
     * To retrieve hits from a certain offset. Defaults to 0.
47
     *
48
     * @var int
49
     */
50
    private $from;
51
52
    /**
53
     * The number of hits to return. Defaults to 10. If you do not care about getting some
54
     * hits back but only about the number of matches and/or aggregations, setting the value
55
     * to 0 will help performance.
56
     *
57
     * @var int
58
     */
59
    private $size;
60
61
    /**
62
     * Allows to control how the _source field is returned with every hit. By default
63
     * operations return the contents of the _source field unless you have used the
64
     * stored_fields parameter or if the _source field is disabled.
65
     *
66
     * @var bool
67
     */
68
    private $source;
69
70
    /**
71
     * Allows to selectively load specific source fields for each document represented by a search hit.
72
     *
73
     * @var array
74
     */
75
    private $sourceFields;
76
77
    /**
78
     * Allows to selectively load specific stored fields for each document represented by a search hit.
79
     *
80
     * @var array
81
     */
82
    private $storedFields;
83
84
    /**
85
     * Allows to return a script evaluation (based on different fields) for each hit.
86
     * Script fields can work on fields that are not stored, and allow to return custom
87
     * values to be returned (the evaluated value of the script). Script fields can
88
     * also access the actual _source document indexed and extract specific elements
89
     * to be returned from it (can be an "object" type).
90
     *
91
     * @var array
92
     */
93
    private $scriptFields;
94
95
    /**
96
     * Allows to return the doc value representation of a field for each hit. Doc value
97
     * fields can work on fields that are not stored. Note that if the fields parameter
98
     * specifies fields without docvalues it will try to load the value from the fielddata
99
     * cache causing the terms for that field to be loaded to memory (cached), which will
100
     * result in more memory consumption.
101
     *
102
     * @var array
103
     */
104
    private $docValueFields;
105
106
    /**
107
     * Enables explanation for each hit on how its score was computed.
108
     *
109
     * @var bool
110
     */
111
    private $explain;
112
113
    /**
114
     * Returns a version for each search hit.
115
     *
116
     * @var bool
117
     */
118
    private $version;
119
120
    /**
121
     * Allows to configure different boost level per index when searching across more
122
     * than one indices. This is very handy when hits coming from one index matter more
123
     * than hits coming from another index (think social graph where each user has an index).
124
     *
125
     * @var array
126
     */
127
    private $indicesBoost;
128
129
    /**
130
     * Exclude documents which have a _score less than the minimum specified in min_score.
131
     *
132
     * @var int
133
     */
134
    private $minScore;
135
136
    /**
137
     * Pagination of results can be done by using the from and size but the cost becomes
138
     * prohibitive when the deep pagination is reached. The index.max_result_window which
139
     * defaults to 10,000 is a safeguard, search requests take heap memory and time
140
     * proportional to from + size. The Scroll api is recommended for efficient deep
141
     * scrolling but scroll contexts are costly and it is not recommended to use it for
142
     * real time user requests. The search_after parameter circumvents this problem by
143
     * providing a live cursor. The idea is to use the results from the previous page to
144
     * help the retrieval of the next page.
145
     *
146
     * @var array
147
     */
148
    private $searchAfter;
149
150
    /**
151
     * URI parameters alongside Request body search.
152
     *
153
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html
154
     *
155
     * @var array
156
     */
157
    private $uriParams = [];
158
159
    /**
160
     * While a search request returns a single “page” of results, the scroll API can be used to retrieve
161
     * large numbers of results (or even all results) from a single search request, in much the same way
162
     * as you would use a cursor on a traditional database. Scrolling is not intended for real time user
163
     * requests, but rather for processing large amounts of data, e.g. in order to reindex the contents
164
     * of one index into a new index with a different configuration.
165
     *
166
     * @var string
167
     */
168
    private $scroll;
169
170
    /**
171
     * @var OrderedSerializer
172
     */
173
    private static $serializer;
174
175
    /**
176
     * @var SearchEndpointInterface[]
177
     */
178
    private $endpoints = [];
179
180
    /**
181
     * Constructor to initialize static properties
182
     */
183
    public function __construct()
184
    {
185
        $this->initializeSerializer();
186
    }
187
188
    /**
189
     * Wakeup method to initialize static properties
190
     */
191
    public function __wakeup()
192
    {
193
        $this->initializeSerializer();
194
    }
195
196
    /**
197
     * Initializes the serializer
198
     */
199
    private function initializeSerializer()
200
    {
201
        if (static::$serializer === null) {
0 ignored issues
show
Bug introduced by
Since $serializer is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $serializer to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
202
            static::$serializer = new OrderedSerializer(
0 ignored issues
show
Bug introduced by
Since $serializer is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $serializer to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
203
                [
204
                    new CustomReferencedNormalizer(),
205
                    new CustomNormalizer(),
206
                ]
207
            );
208
        }
209
    }
210
211
    /**
212
     * Destroys search endpoint.
213
     *
214
     * @param string $type Endpoint type.
215
     */
216
    public function destroyEndpoint($type)
217
    {
218
        unset($this->endpoints[$type]);
219
    }
220
221
    /**
222
     * Adds query to the search.
223
     *
224
     * @param BuilderInterface $query
225
     * @param string           $boolType
226
     * @param string           $key
227
     *
228
     * @return $this
229
     */
230
    public function addQuery(BuilderInterface $query, $boolType = BoolQuery::MUST, $key = null)
231
    {
232
        $endpoint = $this->getEndpoint(QueryEndpoint::NAME);
233
        $endpoint->addToBool($query, $boolType, $key);
0 ignored issues
show
Documentation introduced by
$boolType is of type string, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
It seems like $key defined by parameter $key on line 230 can also be of type string; however, ONGR\ElasticsearchDSL\Se...tInterface::addToBool() does only seem to accept array|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
234
235
        return $this;
236
    }
237
238
    /**
239
     * Returns endpoint instance.
240
     *
241
     * @param string $type Endpoint type.
242
     *
243
     * @return SearchEndpointInterface
244
     */
245
    private function getEndpoint($type)
246
    {
247
        if (!array_key_exists($type, $this->endpoints)) {
248
            $this->endpoints[$type] = SearchEndpointFactory::get($type);
249
        }
250
251
        return $this->endpoints[$type];
252
    }
253
254
    /**
255
     * Returns queries inside BoolQuery instance.
256
     *
257
     * @return BoolQuery
258
     */
259
    public function getQueries()
260
    {
261
        $endpoint = $this->getEndpoint(QueryEndpoint::NAME);
262
263
        return $endpoint->getBool();
264
    }
265
266
    /**
267
     * Sets query endpoint parameters.
268
     *
269
     * @param array $parameters
270
     *
271
     * @return $this
272
     */
273
    public function setQueryParameters(array $parameters)
274
    {
275
        $this->setEndpointParameters(QueryEndpoint::NAME, $parameters);
276
277
        return $this;
278
    }
279
280
    /**
281
     * Sets parameters to the endpoint.
282
     *
283
     * @param string $endpointName
284
     * @param array  $parameters
285
     *
286
     * @return $this
287
     */
288
    public function setEndpointParameters($endpointName, array $parameters)
289
    {
290
        /** @var AbstractSearchEndpoint $endpoint */
291
        $endpoint = $this->getEndpoint($endpointName);
292
        $endpoint->setParameters($parameters);
293
294
        return $this;
295
    }
296
297
    /**
298
     * Adds a post filter to search.
299
     *
300
     * @param BuilderInterface $filter   Filter.
301
     * @param string           $boolType Example boolType values:
302
     *                                   - must
303
     *                                   - must_not
304
     *                                   - should.
305
     * @param string           $key
306
     *
307
     * @return $this.
0 ignored issues
show
Documentation introduced by
The doc-type $this. could not be parsed: Unknown type name "$this." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
308
     */
309
    public function addPostFilter(BuilderInterface $filter, $boolType = BoolQuery::MUST, $key = null)
310
    {
311
        $this
312
            ->getEndpoint(PostFilterEndpoint::NAME)
313
            ->addToBool($filter, $boolType, $key);
0 ignored issues
show
Documentation introduced by
$boolType is of type string, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
It seems like $key defined by parameter $key on line 309 can also be of type string; however, ONGR\ElasticsearchDSL\Se...tInterface::addToBool() does only seem to accept array|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
314
315
        return $this;
316
    }
317
318
    /**
319
     * Returns queries inside BoolFilter instance.
320
     *
321
     * @return BoolQuery
322
     */
323
    public function getPostFilters()
324
    {
325
        $endpoint = $this->getEndpoint(PostFilterEndpoint::NAME);
326
327
        return $endpoint->getBool();
328
    }
329
330
    /**
331
     * Sets post filter endpoint parameters.
332
     *
333
     * @param array $parameters
334
     *
335
     * @return $this
336
     */
337
    public function setPostFilterParameters(array $parameters)
338
    {
339
        $this->setEndpointParameters(PostFilterEndpoint::NAME, $parameters);
340
341
        return $this;
342
    }
343
344
    /**
345
     * Adds aggregation into search.
346
     *
347
     * @param AbstractAggregation $aggregation
348
     *
349
     * @return $this
350
     */
351
    public function addAggregation(AbstractAggregation $aggregation)
352
    {
353
        $this->getEndpoint(AggregationsEndpoint::NAME)->add($aggregation, $aggregation->getName());
354
355
        return $this;
356
    }
357
358
    /**
359
     * Returns all aggregations.
360
     *
361
     * @return BuilderInterface[]
362
     */
363
    public function getAggregations()
364
    {
365
        return $this->getEndpoint(AggregationsEndpoint::NAME)->getAll();
366
    }
367
368
    /**
369
     * Adds inner hit into search.
370
     *
371
     * @param NestedInnerHit $innerHit
372
     *
373
     * @return $this
374
     */
375
    public function addInnerHit(NestedInnerHit $innerHit)
376
    {
377
        $this->getEndpoint(InnerHitsEndpoint::NAME)->add($innerHit, $innerHit->getName());
378
379
        return $this;
380
    }
381
382
    /**
383
     * Returns all inner hits.
384
     *
385
     * @return BuilderInterface[]
386
     */
387
    public function getInnerHits()
388
    {
389
        return $this->getEndpoint(InnerHitsEndpoint::NAME)->getAll();
390
    }
391
392
    /**
393
     * Adds sort to search.
394
     *
395
     * @param BuilderInterface $sort
396
     *
397
     * @return $this
398
     */
399
    public function addSort(BuilderInterface $sort)
400
    {
401
        $this->getEndpoint(SortEndpoint::NAME)->add($sort);
402
403
        return $this;
404
    }
405
406
    /**
407
     * Returns all set sorts.
408
     *
409
     * @return BuilderInterface[]
410
     */
411
    public function getSorts()
412
    {
413
        return $this->getEndpoint(SortEndpoint::NAME)->getAll();
414
    }
415
416
    /**
417
     * Allows to highlight search results on one or more fields.
418
     *
419
     * @param Highlight $highlight
420
     *
421
     * @return $this.
0 ignored issues
show
Documentation introduced by
The doc-type $this. could not be parsed: Unknown type name "$this." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
422
     */
423
    public function addHighlight($highlight)
424
    {
425
        $this->getEndpoint(HighlightEndpoint::NAME)->add($highlight);
426
427
        return $this;
428
    }
429
430
    /**
431
     * Returns highlight builder.
432
     *
433
     * @return BuilderInterface
434
     */
435
    public function getHighlights()
436
    {
437
        /** @var HighlightEndpoint $highlightEndpoint */
438
        $highlightEndpoint = $this->getEndpoint(HighlightEndpoint::NAME);
439
440
        return $highlightEndpoint->getHighlight();
441
    }
442
443
    /**
444
    * Adds suggest into search.
445
    *
446
    * @param BuilderInterface $suggest
447
    *
448
    * @return $this
449
    */
450
    public function addSuggest(NamedBuilderInterface $suggest)
451
    {
452
        $this->getEndpoint(SuggestEndpoint::NAME)->add($suggest, $suggest->getName());
0 ignored issues
show
Documentation introduced by
$suggest->getName() is of type string, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
453
454
        return $this;
455
    }
456
457
    /**
458
    * Returns all suggests.
459
    *
460
    * @return BuilderInterface[]
461
    */
462
    public function getSuggests()
463
    {
464
        return $this->getEndpoint(SuggestEndpoint::NAME)->getAll();
465
    }
466
467
    /**
468
     * @return null|int
469
     */
470
    public function getFrom()
471
    {
472
        return $this->from;
473
    }
474
475
    /**
476
     * @param null|int $from
477
     *
478
     * @return $this
479
     */
480
    public function setFrom($from)
481
    {
482
        $this->from = $from;
483
484
        return $this;
485
    }
486
487
    /**
488
     * @return bool
489
     */
490
    public function isTrackTotalHits()
491
    {
492
        return $this->trackTotalHits;
493
    }
494
495
    /**
496
     * @param bool $trackTotalHits
497
     *
498
     * @return $this
499
     */
500
    public function setTrackTotalHits(bool $trackTotalHits)
501
    {
502
        $this->trackTotalHits = $trackTotalHits;
503
504
        return $this;
505
    }
506
507
    /**
508
     * @return null|int
509
     */
510
    public function getSize()
511
    {
512
        return $this->size;
513
    }
514
515
    /**
516
     * @param null|int $size
517
     *
518
     * @return $this
519
     */
520
    public function setSize($size)
521
    {
522
        $this->size = $size;
523
524
        return $this;
525
    }
526
527
    /**
528
     * @return bool
529
     */
530
    public function isSource()
531
    {
532
        return $this->source;
533
    }
534
535
    /**
536
     * @param bool $source
537
     *
538
     * @return $this
539
     */
540
    public function setSource($source)
541
    {
542
        $this->source = $source;
543
544
        return $this;
545
    }
546
547
    /**
548
     * @return array
549
     */
550
    public function getSourceFields()
551
    {
552
        return $this->sourceFields;
553
    }
554
555
    /**
556
     * @param array $sourceFields
557
     *
558
     * @return $this
559
     */
560
    public function setSourceFields($sourceFields)
561
    {
562
        $this->sourceFields = $sourceFields;
563
564
        return $this;
565
    }
566
567
    /**
568
     * @return array
569
     */
570
    public function getStoredFields()
571
    {
572
        return $this->storedFields;
573
    }
574
575
    /**
576
     * @param array $storedFields
577
     *
578
     * @return $this
579
     */
580
    public function setStoredFields($storedFields)
581
    {
582
        $this->storedFields = $storedFields;
583
584
        return $this;
585
    }
586
587
    /**
588
     * @return array
589
     */
590
    public function getScriptFields()
591
    {
592
        return $this->scriptFields;
593
    }
594
595
    /**
596
     * @param array $scriptFields
597
     *
598
     * @return $this
599
     */
600
    public function setScriptFields($scriptFields)
601
    {
602
        $this->scriptFields = $scriptFields;
603
604
        return $this;
605
    }
606
607
    /**
608
     * @return array
609
     */
610
    public function getDocValueFields()
611
    {
612
        return $this->docValueFields;
613
    }
614
615
    /**
616
     * @param array $docValueFields
617
     *
618
     * @return $this
619
     */
620
    public function setDocValueFields($docValueFields)
621
    {
622
        $this->docValueFields = $docValueFields;
623
624
        return $this;
625
    }
626
627
    /**
628
     * @return bool
629
     */
630
    public function isExplain()
631
    {
632
        return $this->explain;
633
    }
634
635
    /**
636
     * @param bool $explain
637
     *
638
     * @return $this
639
     */
640
    public function setExplain($explain)
641
    {
642
        $this->explain = $explain;
643
644
        return $this;
645
    }
646
647
    /**
648
     * @return bool
649
     */
650
    public function isVersion()
651
    {
652
        return $this->version;
653
    }
654
655
    /**
656
     * @param bool $version
657
     *
658
     * @return $this
659
     */
660
    public function setVersion($version)
661
    {
662
        $this->version = $version;
663
664
        return $this;
665
    }
666
667
    /**
668
     * @return array
669
     */
670
    public function getIndicesBoost()
671
    {
672
        return $this->indicesBoost;
673
    }
674
675
    /**
676
     * @param array $indicesBoost
677
     *
678
     * @return $this
679
     */
680
    public function setIndicesBoost($indicesBoost)
681
    {
682
        $this->indicesBoost = $indicesBoost;
683
684
        return $this;
685
    }
686
687
    /**
688
     * @return int
689
     */
690
    public function getMinScore()
691
    {
692
        return $this->minScore;
693
    }
694
695
    /**
696
     * @param int $minScore
697
     *
698
     * @return $this
699
     */
700
    public function setMinScore($minScore)
701
    {
702
        $this->minScore = $minScore;
703
704
        return $this;
705
    }
706
707
    /**
708
     * @return array
709
     */
710
    public function getSearchAfter()
711
    {
712
        return $this->searchAfter;
713
    }
714
715
    /**
716
     * @param array $searchAfter
717
     *
718
     * @return $this
719
     */
720
    public function setSearchAfter($searchAfter)
721
    {
722
        $this->searchAfter = $searchAfter;
723
724
        return $this;
725
    }
726
727
    /**
728
     * @return string
729
     */
730
    public function getScroll()
731
    {
732
        return $this->scroll;
733
    }
734
735
    /**
736
     * @param string $scroll
737
     *
738
     * @return $this
739
     */
740
    public function setScroll($scroll = '5m')
741
    {
742
        $this->scroll = $scroll;
743
744
        $this->addUriParam('scroll', $this->scroll);
745
746
        return $this;
747
    }
748
749
    /**
750
     * @param string $name
751
     * @param string|array|bool $value
752
     *
753
     * @return $this
754
     */
755
    public function addUriParam($name, $value)
756
    {
757
        if (in_array($name, [
758
            'q',
759
            'df',
760
            'analyzer',
761
            'analyze_wildcard',
762
            'default_operator',
763
            'lenient',
764
            'explain',
765
            '_source',
766
            '_source_exclude',
767
            '_source_include',
768
            'stored_fields',
769
            'sort',
770
            'track_scores',
771
            'timeout',
772
            'terminate_after',
773
            'from',
774
            'size',
775
            'search_type',
776
            'scroll',
777
            'allow_no_indices',
778
            'ignore_unavailable',
779
            'typed_keys',
780
            'pre_filter_shard_size',
781
            'ignore_unavailable',
782
        ])) {
783
            $this->uriParams[$name] = $value;
784
        } else {
785
            throw new \InvalidArgumentException(sprintf('Parameter %s is not supported.', $value));
786
        }
787
788
        return $this;
789
    }
790
791
    /**
792
     * Returns query url parameters.
793
     *
794
     * @return array
795
     */
796
    public function getUriParams()
797
    {
798
        return $this->uriParams;
799
    }
800
801
    /**
802
     * {@inheritdoc}
803
     */
804
    public function toArray()
805
    {
806
        $output = array_filter(static::$serializer->normalize($this->endpoints));
0 ignored issues
show
Bug introduced by
Since $serializer is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $serializer to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
807
808
        $params = [
809
            'from' => 'from',
810
            'size' => 'size',
811
            'source' => '_source',
812
            'storedFields' => 'stored_fields',
813
            'scriptFields' => 'script_fields',
814
            'docValueFields' => 'docvalue_fields',
815
            'explain' => 'explain',
816
            'version' => 'version',
817
            'indicesBoost' => 'indices_boost',
818
            'minScore' => 'min_score',
819
            'searchAfter' => 'search_after',
820
            'trackTotalHits' => 'track_total_hits',
821
        ];
822
823
        if(!empty($this->sourceFields))
824
        {
825
            unset($params['source']);
826
            $params['sourceFields'] = '_source';
827
        }
828
829
        foreach ($params as $field => $param) {
830
            if ($this->$field !== null) {
831
                $output[$param] = $this->$field;
832
            }
833
        }
834
835
        return $output;
836
    }
837
}
838