1<?php 2 3namespace Elastica\Query; 4 5use Elastica\Query as BaseQuery; 6 7/** 8 * Returns child documents having parent docs matching the query. 9 * 10 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html 11 * @phpstan-import-type TCreateQueryArgsMatching from BaseQuery 12 */ 13class HasParent extends AbstractQuery 14{ 15 /** 16 * @param AbstractQuery|array|BaseQuery|string|null $query 17 * @phpstan-param TCreateQueryArgsMatching $query 18 * 19 * @param string $type Parent document type 20 */ 21 public function __construct($query, string $type) 22 { 23 $this->setQuery($query); 24 $this->setType($type); 25 } 26 27 /** 28 * Sets query object. 29 * 30 * @param AbstractQuery|array|BaseQuery|string|null $query 31 * @phpstan-param TCreateQueryArgsMatching $query 32 * 33 * @return $this 34 */ 35 public function setQuery($query): self 36 { 37 return $this->setParam('query', BaseQuery::create($query)); 38 } 39 40 /** 41 * Set type of the parent document. 42 * 43 * @param string $type Parent document type 44 * 45 * @return $this 46 */ 47 public function setType(string $type): self 48 { 49 return $this->setParam('parent_type', $type); 50 } 51 52 /** 53 * Sets the scope. 54 * 55 * @param string $scope Scope 56 * 57 * @return $this 58 */ 59 public function setScope(string $scope): self 60 { 61 return $this->setParam('_scope', $scope); 62 } 63 64 /** 65 * {@inheritdoc} 66 */ 67 public function toArray(): array 68 { 69 $array = parent::toArray(); 70 71 $baseName = $this->_getBaseName(); 72 73 if (isset($array[$baseName]['query'])) { 74 $array[$baseName]['query'] = $array[$baseName]['query']['query']; 75 } 76 77 return $array; 78 } 79} 80