Add::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Solr Client Symfony package.
7
 *
8
 * (c) ingatlan.com Zrt. <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace iCom\SolrClient\Query\Command;
15
16
use iCom\SolrClient\JsonHelper;
17
use iCom\SolrClient\Query\Command;
18
19
/**
20
 * @see https://lucene.apache.org/solr/guide/8_3/uploading-data-with-index-handlers.html#adding-documents
21
 *
22
 * @psalm-immutable
23
 */
24
final class Add implements Command
25
{
26
    use JsonHelper;
27
28
    /**
29
     * @psalm-var array{commitWithin: ?int, doc: ?array, overwrite: ?bool}
30
     */
31
    private array $options = [
32
        'doc' => null,
33
        'commitWithin' => null,
34
        'overwrite' => null,
35
    ];
36
37
    public function __construct(array $document)
38
    {
39
        $this->options['doc'] = $document;
40
    }
41
42
    /**
43
     * @psalm-pure
44
     */
45
    public static function create(array $document): self
46
    {
47
        return new self($document);
48
    }
49
50
    public function commitWithin(int $commitWithin): self
51
    {
52
        $add = clone $this;
53
        $add->options['commitWithin'] = $commitWithin;
54
55
        return $add;
56
    }
57
58
    public function enableOverWrite(): self
59
    {
60
        $add = clone $this;
61
        $add->options['overwrite'] = true;
62
63
        return $add;
64
    }
65
66
    public function disableOverWrite(): self
67
    {
68
        $add = clone $this;
69
        $add->options['overwrite'] = false;
70
71
        return $add;
72
    }
73
74
    public function toJson(): string
75
    {
76
        return self::jsonEncode(array_filter($this->options, static function ($option): bool { return null !== $option; }));
77
    }
78
79
    public function getName(): string
80
    {
81
        return 'add';
82
    }
83
}
84