Field::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
crap 6
1
<?php
2
3
namespace PSolr\Request;
4
5
/**
6
 * @see http://wiki.apache.org/solr/UpdateXmlMessages#The_Update_Schema
7
 */
8
class Field
9
{
10
    const UPDATE_REPLACE   = 'set';
11
    const UPDATE_ADD       = 'add';
12
    const UPDATE_INCREMENT = 'inc';
13
14
    /**
15
     * @var string
16
     */
17
    protected $name;
18
19
    /**
20
     * @var array
21
     */
22
    protected $values;
23
24
    /**
25
     * @var float
26
     */
27
    protected $boost;
28
29
    /**
30
     * @var string
31
     */
32
    protected $update;
33
34
    /**
35
     * @param string $name
36
     * @param string|array $values
37
     * @param float $boost
38
     */
39
    public function __construct($name, $values, $boost = 0.0)
40
    {
41
        $this->name   = $name;
42
        $this->values = !is_array($values) ? (array) $values : $values;
43
        $this->boost  = $boost;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getName()
50
    {
51
        return $this->name;
52
    }
53
54
    /**
55
     * @param float $boost
56
     *
57
     * @return \PSolr\Request\Document
58
     */
59
    public function setBoost($boost)
60
    {
61
        $this->boost = $boost;
62
        return $this;
63
    }
64
65
    /**
66
     * @return float
67
     */
68
    public function getBoost($boost)
0 ignored issues
show
Unused Code introduced by
The parameter $boost is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
    {
70
        return $this->boost;
71
    }
72
73
    /**
74
     * @param string $values
75
     *
76
     * @return \PSolr\Request\Document
77
     */
78
    public function setValues($values)
79
    {
80
        $this->values = $values;
0 ignored issues
show
Documentation Bug introduced by
It seems like $values of type string is incompatible with the declared type array of property $values.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
81
        return $this;
82
    }
83
84
    /**
85
     * @return array
86
     */
87
    public function getValues()
88
    {
89
        return $this->values;
90
    }
91
92
    /**
93
     * @return string
94
     *
95
     * @return \PSolr\Request\Document
96
     */
97
    public function addValue($value)
98
    {
99
        return $this->values[] = $value;
100
    }
101
102
    /**
103
     * @param string $update
104
     *
105
     * @return \PSolr\Request\Document
106
     *
107
     * @see http://wiki.apache.org/solr/UpdateXmlMessages#Optional_attributes_for_.22field.22
108
     */
109
    public function setAtomicUpdate($update)
110
    {
111
        $this->update = $update;
112
        return $this;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function asXml()
119
    {
120
        $xml = '';
121
        foreach ($this->values as $key => $value) {
122
            $attributes = '';
123
124
            if (!$key && $this->boost > 0.0) {
125
                $attributes = 'boost="' . (float) $this->boost . '" ';
126
            }
127
128
            if (isset($this->update)) {
129
                $attributes = 'update="' . SolrRequest::escapeXml($this->update) . '" ';
130
            }
131
132
            $xml .= '<field ' . $attributes . 'name="'. SolrRequest::escapeXml($this->name) . '">';
133
            $xml .= SolrRequest::escapeXml($value);
134
            $xml .= '</field>';
135
        }
136
137
        return $xml;
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public function __toString()
144
    {
145
        return $this->asXml();
146
    }
147
}
148