Update::buildAttribute()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 14
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 1
crap 20
1
<?php
2
3
namespace PSolr\Request;
4
5
/**
6
 * @see http://wiki.apache.org/solr/UpdateXmlMessages
7
 */
8
class Update extends SolrRequest
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $handlerName = 'update';
14
15
    /**
16
     * $var string
17
     */
18
    protected $responseClass = '\PSolr\Response\Response';
19
20
    /**
21
     * @var string
22
     */
23
    protected $element = 'commit';
24
25
    /**
26
     * @var bool
27
     */
28
    protected $waitFlush;
29
30
    /**
31
     * @var bool
32
     */
33
    protected $waitSearcher;
34
35
    /**
36
     * @var bool
37
     */
38
    protected $softCommit;
39
40
    /**
41
     * @param bool $waitFlush
42
     *
43
     * @return \PSolr\Request\Commit
44
     *
45
     * @see http://wiki.apache.org/solr/UpdateXmlMessages#Optional_attributes_for_.22commit.22_and_.22optimize.22
46
     */
47
    public function waitFlush($waitFlush = true)
48
    {
49
        $this->waitFlush = (bool) $waitFlush;
50
        return $this;
51
    }
52
53
    /**
54
     * @param bool $waitSearcher
55
     *
56
     * @return \PSolr\Request\Commit
57
     *
58
     * @see http://wiki.apache.org/solr/UpdateXmlMessages#Optional_attributes_for_.22commit.22_and_.22optimize.22
59
     */
60
    public function waitSearcher($waitSearcher = true)
61
    {
62
        $this->waitSearcher = (bool) $waitSearcher;
63
        return $this;
64
    }
65
66
    /**
67
     * @param bool $softCommit
68
     *
69
     * @return \PSolr\Request\Commit
70
     *
71
     * @see http://wiki.apache.org/solr/UpdateXmlMessages#Optional_attributes_for_.22commit.22_and_.22optimize.22
72
     */
73
    public function softCommit($softCommit = true)
74
    {
75
        $this->softCommit = (bool) $softCommit;
76
        return $this;
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function buildAttributes()
83
    {
84
        $attributes  = '';
85
        $attributes .= $this->buildAttribute('waitFlush');
86
        $attributes .= $this->buildAttribute('waitSearcher');
87
        $attributes .= $this->buildAttribute('softCommit');
88
        return $attributes;
89
    }
90
91
    /**
92
     * Builds an attribute from a class property if it is set.
93
     *
94
     * @param string $property
95
     *
96
     * @return string
97
     */
98
    public function buildAttribute($property)
99
    {
100
        $attribute = '';
101
        if (isset($this->$property)) {
102
            $attribute .= ' ' . $property . '="';
103
            if (is_bool($this->$property)) {
104
                $attribute .= ($this->$property) ? 'true' : 'false';
105
            } else {
106
                $attribute .= SolrRequest::escapeXml($this->$property);
107
            }
108
            $attribute .= '"';
109
        }
110
        return $attribute;
111
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116
    public function renderBody()
117
    {
118
        return '<' . $this->element . $this->buildAttributes() . '/>';
119
    }
120
}
121