Completed
Pull Request — develop (#241)
by ANTHONIUS
08:03
created

ResultConverter::convert()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 26
rs 8.439
cc 5
eloc 19
nc 5
nop 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
6
 * @license   MIT
7
 */
8
9
namespace Solr\Bridge;
10
11
use Core\Entity\AbstractIdentifiableModificationDateAwareEntity as EntityType;
12
use Solr\Filter\AbstractPaginationQuery;
13
use Zend\ServiceManager\ServiceLocatorInterface;
14
15
/**
16
 * Class ResultConverter
17
 *
18
 * Convert SOLR query result into Doctrine ODM Entity
19
 * 
20
 * @package Solr\Bridge
21
 */
22
class ResultConverter
23
{
24
    /**
25
     * Current filter used for conversion
26
     *
27
     * @var AbstractPaginationQuery
28
     */
29
    protected $filter;
30
31
    /**
32
     * Convert result into entity
33
     *
34
     * @param   AbstractPaginationQuery $filter
35
     * @param   \SolrQueryResponse $queryResponse
36
     * @return  EntityType[]
37
     */
38
    public function convert($filter, $queryResponse)
39
    {
40
        $this->filter = $filter;
41
        $response = $queryResponse->getResponse();
42
        $propertiesMap = $filter->getPropertiesMap();
43
        $class = $filter->getEntityClass();
44
        $entities = [];
45
        foreach($response['response']['docs'] as $doc){
46
            $ob = new $class();
47
            $properties = $doc->getPropertyNames();
48
            foreach($properties as $name){
49
                $setter = 'set'.$name;
50
                $value = $doc->$name;
51
                $value = $this->validateDate($value);
52
                if(method_exists($ob,$setter)){
53
                    call_user_func(array($ob,$setter),$value);
54
                }elseif(isset($propertiesMap[$name])){
55
                    $this->handleMappedProperty($propertiesMap[$name],$ob,$value);
56
                }
57
58
            }
59
            $entities[] = $ob;
60
        }
61
62
        return $entities;
63
    }
64
65
    /**
66
     * Handles mapped property defined by query filter
67
     * 
68
     * @param $property
69
     * @param $object
70
     * @param $value
71
     */
72
    public function handleMappedProperty($property,$object,$value)
73
    {
74
        $callback = array($this->filter,$property);
75
        if(is_callable($callback)){
76
            call_user_func($callback,$object,$value);
77
        }
78
    }
79
80
    /**
81
     * Convert date formatted string into a DateTime object
82
     *
83
     * @param   string  $value
84
     * @return  \DateTime|string
85
     */
86
    public function validateDate($value)
87
    {
88
        $value = trim($value);
89
        $date = \DateTime::createFromFormat(Manager::SOLR_DATE_FORMAT,$value);
90
        $check = $date && ($date->format(Manager::SOLR_DATE_FORMAT) === $value);
91
        if($check){
92
            return $date;
93
        }else{
94
            return $value;
95
        }
96
    }
97
98
    /**
99
     * Create a new instance of ResultConverter
100
     * @param   ServiceLocatorInterface $sl
101
     * @return  ResultConverter
102
     */
103
    static public function factory(ServiceLocatorInterface $sl)
0 ignored issues
show
Unused Code introduced by
The parameter $sl 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...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
104
    {
105
        return new static();
106
    }
107
}