Completed
Push — master ( 0d45ed...ea3502 )
by Nicolas
02:38
created

src/Bulk/Action.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Elastica\Bulk;
4
5
use Elastica\Bulk;
6
use Elastica\Index;
7
use Elastica\JSON;
8
9
class Action
10
{
11
    public const OP_TYPE_CREATE = 'create';
12
    public const OP_TYPE_INDEX = 'index';
13
    public const OP_TYPE_DELETE = 'delete';
14
    public const OP_TYPE_UPDATE = 'update';
15
16
    /**
17
     * @var array
18
     */
19
    public static $opTypes = [
20
        self::OP_TYPE_CREATE,
21
        self::OP_TYPE_INDEX,
22
        self::OP_TYPE_DELETE,
23
        self::OP_TYPE_UPDATE,
24
    ];
25
26
    /**
27
     * @var string
28
     */
29
    protected $_opType;
30
31
    /**
32
     * @var array
33
     */
34
    protected $_metadata = [];
35
36
    /**
37
     * @var array
38
     */
39
    protected $_source = [];
40
41
    public function __construct(string $opType = self::OP_TYPE_INDEX, array $metadata = [], array $source = [])
42
    {
43
        $this->setOpType($opType);
44
        $this->setMetadata($metadata);
45
        $this->setSource($source);
46
    }
47
48
    /**
49
     * @return $this
50
     */
51
    public function setOpType(string $type): self
52
    {
53
        $this->_opType = $type;
54
55
        return $this;
56
    }
57
58
    public function getOpType(): string
59
    {
60
        return $this->_opType;
61
    }
62
63
    /**
64
     * @return $this
65
     */
66
    public function setMetadata(array $metadata): self
67
    {
68
        $this->_metadata = $metadata;
69
70
        return $this;
71
    }
72
73
    public function getMetadata(): array
74
    {
75
        return $this->_metadata;
76
    }
77
78
    public function getActionMetadata(): array
79
    {
80
        return [$this->_opType => $this->getMetadata()];
81
    }
82
83
    /**
84
     * @param array|string $source
85
     *
86
     * @return $this
87
     */
88
    public function setSource($source): self
89
    {
90
        $this->_source = $source;
91
92
        return $this;
93
    }
94
95
    /**
96
     * @return array|string
97
     */
98
    public function getSource()
99
    {
100
        return $this->_source;
101
    }
102
103
    public function hasSource(): bool
104
    {
105
        return !empty($this->_source);
106
    }
107
108
    /**
109
     * @param Index|string $index
110
     *
111
     * @return $this
112
     */
113
    public function setIndex($index): self
114
    {
115
        if ($index instanceof Index) {
116
            $index = $index->getName();
117
        }
118
        $this->_metadata['_index'] = $index;
119
120
        return $this;
121
    }
122
123
    /**
124
     * @return $this
125
     */
126
    public function setId(string $id): self
127
    {
128
        $this->_metadata['_id'] = $id;
129
130
        return $this;
131
    }
132
133
    /**
134
     * @param int|string $routing
135
     *
136
     * @return $this
137
     */
138
    public function setRouting($routing): self
139
    {
140
        $this->_metadata['routing'] = $routing;
141
142
        return $this;
143
    }
144
145
    public function toArray(): array
146
    {
147
        $data[] = $this->getActionMetadata();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
148
        if ($this->hasSource()) {
149
            $data[] = $this->getSource();
150
        }
151
152
        return $data;
153
    }
154
155
    public function toString(): string
156
    {
157
        $string = JSON::stringify($this->getActionMetadata(), JSON_FORCE_OBJECT).Bulk::DELIMITER;
158
        if ($this->hasSource()) {
159
            $source = $this->getSource();
160
            if (\is_string($source)) {
161
                $string .= $source;
162
            } elseif (\is_array($source) && \array_key_exists('doc', $source) && \is_string($source['doc'])) {
163
                if (isset($source['doc_as_upsert'])) {
164
                    $docAsUpsert = ', "doc_as_upsert": '.($source['doc_as_upsert'] ? 'true' : 'false');
165
                } else {
166
                    $docAsUpsert = '';
167
                }
168
                $string .= '{"doc": '.$source['doc'].$docAsUpsert.'}';
169
            } else {
170
                $string .= JSON::stringify($source, JSON_UNESCAPED_UNICODE);
171
            }
172
            $string .= Bulk::DELIMITER;
173
        }
174
175
        return $string;
176
    }
177
178
    public static function isValidOpType(?string $opType = null): bool
179
    {
180
        return \in_array($opType, self::$opTypes, true);
181
    }
182
}
183