Completed
Pull Request — master (#12)
by Adam
03:38
created

Insert::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace BestServedCold\LaravelZendSearch\Lucene\Store;
4
5
use BestServedCold\LaravelZendSearch\Lucene\Index;
6
use ZendSearch\Lucene\Document;
7
use ZendSearch\Lucene\Document\Field;
8
9
/**
10
 * Class Insert
11
 *
12
 * @package BestServedCold\LaravelZendSearch\Lucene\Store
13
 */
14
class Insert
15
{
16
    /**
17
     * @var Document
18
     */
19
    private $document;
20
21
    /**
22
     * @var float
23
     */
24
    private $defaultBoost = 1.0;
25
26 21
    /**
27
     * Insert constructor.
28 21
     *
29 21
     * @param Document $document
30
     */
31
    public function __construct(Document $document)
32
    {
33
        $this->document = $document;
34
    }
35
36
    /**
37
     * Insert
38
     *
39
     * @param  integer $id
40 1
     * @param  Index          $index
41
     * @param  array          $fields      fields that are indexed
42 1
     * @param  boolean|string $uid         unique identifier, if required
43 1
     * @param  array          $boostFields
44 1
     * @return mixed
45 1
     */
46
    public function insert(Index $index, $id, array $fields, $uid = false, array $boostFields = [])
47
    {
48
        $this->document->addField($this->field('xref_id', $id));
49
        $this->document = $this->addUid($this->document, $uid);
50
        $this->document = $this->addFields($this->document, $fields, $boostFields);
51
        return $index->get()->addDocument($this->document);
52
    }
53
54 1
    /**
55
     * @param  string|integer $field
56 1
     * @param  string         $value
57
     * @param  string         $type
58
     * @return Field
59
     */
60
    private function field($field, $value, $type = 'keyword', $boost = null)
61
    {
62
        $field = Field::$type($field, strtolower(strip_tags($value)));
63
        $field->boost = $boost ?: $this->defaultBoost;
64 1
        return $field;
65
    }
66 1
67 1
    /**
68
     * @param  Document       $document
69
     * @param  boolean|string $uid
70
     * @return Document
71
     */
72
    private function addUid(Document $document, $uid = false)
73
    {
74
        $uid && is_string($uid) ? $document->addField($this->field('uid', $uid)) : null;
75 1
        return $document;
76
    }
77 1
78 1
    /**
79 1
     * @param  Document $document
80
     * @param  array    $fields
81 1
     * @param  array    $boostFields
82
     * @return Document
83
     */
84
    private function addFields(Document $document, array $fields, array $boostFields = [])
85
    {
86
        foreach ($fields as $field => $text) {
87
            $document->addField($this->field($field, $text, 'text', $this->boost($field, $boostFields)));
88
        }
89
90
        return $document;
91
    }
92
93
    /**
94
     * @param  $field
95
     * @param  array      $boostFields
96
     * @return mixed|null
97
     */
98
    private function boost($field, array $boostFields = [])
99
    {
100
        if (empty($boostFields)) {
101
            return null;
102
        }
103
        return array_key_exists($field, $boostFields) ? $boostFields[$field] : null;
104
    }
105
}
106