Completed
Push — master ( 707688...6da863 )
by Mike
04:47 queued 02:08
created

lib/Doctrine/ODM/CouchDB/View/ODMQuery.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
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license. For more information, see
18
 * <http://www.doctrine-project.org>.
19
 */
20
21
namespace Doctrine\ODM\CouchDB\View;
22
23
use Doctrine\CouchDB\View\Query;
24
use Doctrine\ODM\CouchDB\DocumentManager;
25
26
class ODMQuery extends Query
27
{
28
    /**
29
     * @var DocumentManager
30
     */
31
    private $dm;
32
33
    /**
34
     * @var bool
35
     */
36
    private $onlyDocs = false;
37
38
    private $toArray = false;
39
40
    public function execute()
41
    {
42
        $response = $this->doExecute();
43
        $data = array();
44
        if ($this->dm && $this->getParameter('include_docs') === true) {
45
            $uow = $this->dm->getUnitOfWork();
46
            foreach ($response->body['rows'] AS $k => $v) {
47
                $doc = $uow->createDocument(null, $v['doc']);
48
                if ($this->toArray) {
49
                    $data[] = $doc;
50 View Code Duplication
                } else if ($this->onlyDocs) {
51
                    $response->body['rows'][$k] = $doc;
52
                } else {
53
                    $response->body['rows'][$k]['doc'] = $doc;
54
                }
55
            }
56
        }
57
58
        return ($this->toArray) ? $data : $this->createResult($response);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->toArray ? $data :...reateResult($response); of type array|Doctrine\CouchDB\View\Result adds the type array to the return on line 58 which is incompatible with the return type of the parent method Doctrine\CouchDB\View\AbstractQuery::execute of type Doctrine\CouchDB\View\Result.
Loading history...
59
    }
60
61
62
    /**
63
     * @param DocumentManager $dm
64
     */
65
    public function setDocumentManager(DocumentManager $dm)
66
    {
67
        $this->dm = $dm;
68
    }
69
70
    /**
71
     * @param  bool $flag
72
     * @return Query
73
     */
74
    public function onlyDocs($flag)
75
    {
76
        if ($flag) {
77
            $this->setIncludeDocs(true);
78
        }
79
        $this->onlyDocs = $flag;
80
        return $this;
81
    }
82
83
    public function toArray($flag)
84
    {
85
        $this->toArray = $flag;
86
        return $this;
87
    }
88
}
89