1<?php
2
3namespace Elastica\Processor;
4
5/**
6 * Elastica Foreach Processor.
7 *
8 * @author Federico Panini <fpanini@gmail.com>
9 * @author Thibaut Simon-Fine <tsimonfine@gmail.com>
10 *
11 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/foreach-processor.html
12 */
13class ForeachProcessor extends AbstractProcessor
14{
15    use Traits\FieldTrait;
16    use Traits\IgnoreFailureTrait;
17    use Traits\IgnoreMissingTrait;
18
19    public const DEFAULT_IGNORE_MISSING_VALUE = false;
20
21    /**
22     * @param AbstractProcessor|array $processor
23     */
24    public function __construct(string $field, $processor)
25    {
26        $this->setField($field);
27
28        if ($processor instanceof AbstractProcessor) {
29            $this->setProcessor($processor);
30        } elseif (\is_array($processor)) {
31            $this->setRawProcessor($processor);
32        } else {
33            throw new \TypeError(\sprintf('Argument 2 passed to %s::__construct() must be of type %s|array, %s given.', self::class, AbstractProcessor::class, \is_object($processor) ? \get_class($processor) : \gettype($processor)));
34        }
35    }
36
37    /**
38     * Set processor.
39     *
40     * @return $this
41     */
42    public function setProcessor(AbstractProcessor $processor): self
43    {
44        return $this->setParam('processor', $processor);
45    }
46
47    /**
48     * Set raw processor.
49     * Example : ['remove' => ['field' => 'user_agent']].
50     *
51     * @return $this
52     */
53    public function setRawProcessor(array $processor): self
54    {
55        return $this->setParam('processor', $processor);
56    }
57}
58