1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BestServedCold\LaravelZendSearch\Lucene\Store; |
4
|
|
|
|
5
|
|
|
use BestServedCold\LaravelZendSearch\Lucene\Index; |
6
|
|
|
use BestServedCold\LaravelZendSearch\Lucene\Search; |
7
|
|
|
use ZendSearch\Lucene\Document; |
8
|
|
|
use ZendSearch\Lucene\Document\Field; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Insert |
12
|
|
|
* |
13
|
|
|
* @package BestServedCold\LaravelZendSearch\Lucene\Store |
14
|
|
|
*/ |
15
|
|
|
final class Insert |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Index |
19
|
|
|
*/ |
20
|
|
|
private $index; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Document |
24
|
|
|
*/ |
25
|
|
|
private $document; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Insert constructor. |
29
|
|
|
* |
30
|
|
|
* @param Search $search |
31
|
|
|
* @param Index $index |
32
|
|
|
* @param Document $document |
33
|
|
|
*/ |
34
|
|
|
public function __construct(Search $search, Index $index, Document $document) |
35
|
|
|
{ |
36
|
|
|
$this->search = $search; |
|
|
|
|
37
|
|
|
$this->index = $index; |
38
|
|
|
$this->document = $document; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Insert |
43
|
|
|
* |
44
|
|
|
* @param $id |
45
|
|
|
* @param array $fields fields that are indexed |
46
|
|
|
* @param array $parameters fields that aren't indexed |
47
|
|
|
* @param boolean|string $uid unique identifier, if required |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
public function insert($id, array $fields, array $parameters = [ ], $uid = false) |
51
|
|
|
{ |
52
|
|
|
$this->document->addField($this->field('xref_id', $id)); |
53
|
|
|
$this->document = $this->addUid($this->document, $uid); |
|
|
|
|
54
|
|
|
$this->document = $this->addFields($this->document, $fields); |
55
|
|
|
$this->document = $this->addParameters($this->document, $parameters); |
56
|
|
|
return $this->index->get()->addDocument($this->document); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Document $document |
61
|
|
|
* @param array $parameters |
62
|
|
|
* @return Document |
63
|
|
|
*/ |
64
|
|
|
private function addParameters(Document $document, array $parameters = []) |
65
|
|
|
{ |
66
|
|
|
if (! empty($parameters)) { |
67
|
|
|
$document->addField($this->field('_parameters', $this->flattenParameters($parameters), 'unIndexed')); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $document; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $keyword |
75
|
|
|
* @param string $value |
76
|
|
|
* @param string $type |
77
|
|
|
* @return Field |
78
|
|
|
*/ |
79
|
|
|
private function field($keyword, $value, $type = 'keyword') |
80
|
|
|
{ |
81
|
|
|
return Field::$type($keyword, $value); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Document $document |
86
|
|
|
* @param bool $uid |
87
|
|
|
* @return Document |
88
|
|
|
*/ |
89
|
|
|
private function addUid(Document $document, $uid = false) |
90
|
|
|
{ |
91
|
|
|
if ($uid) { |
92
|
|
|
$document->addField($this->field('uid', strtoupper($uid))); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $document; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Document $document |
100
|
|
|
* @param array $fields |
101
|
|
|
* @return Document |
102
|
|
|
*/ |
103
|
|
|
private function addFields(Document $document, array $fields) |
104
|
|
|
{ |
105
|
|
|
foreach ($fields as $key => $field) { |
106
|
|
|
$document->addField($this->field($key, strtoupper($field))); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $document; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $parameters |
114
|
|
|
* @return string |
115
|
|
|
* @todo I'm not sure this is the right thing to do here, perhaps do some more investigation. |
116
|
|
|
*/ |
117
|
|
|
private function flattenParameters($parameters) |
118
|
|
|
{ |
119
|
|
|
return base64_encode(json_encode($parameters)); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: