Completed
Pull Request — develop (#241)
by ANTHONIUS
07:36
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
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
namespace Solr\Bridge;
11
12
use Core\Entity\AbstractIdentifiableModificationDateAwareEntity as EntityType;
13
use Solr\Filter\AbstractPaginationQuery;
14
use Zend\ServiceManager\ServiceLocatorInterface;
15
16
/**
17
 * Class ResultConverter
18
 *
19
 * Convert SOLR query result into Doctrine ODM Entity
20
 * 
21
 * @author  Anthonius Munthi <[email protected]>
22
 * @package Solr\Bridge
23
 * @since   0.27
24
 */
25
class ResultConverter
26
{
27
    /**
28
     * Current filter used for conversion
29
     *
30
     * @var AbstractPaginationQuery
31
     */
32
    protected $filter;
33
34
    /**
35
     * Convert result into entity
36
     *
37
     * @param   AbstractPaginationQuery $filter
38
     * @param   \SolrQueryResponse $queryResponse
39
     * @return  EntityType[]
40
     */
41
    public function convert($filter, $queryResponse)
42
    {
43
        $this->filter = $filter;
44
        $response = $queryResponse->getResponse();
45
        $propertiesMap = $filter->getPropertiesMap();
46
        $class = $filter->getEntityClass();
47
        $entities = [];
48
        foreach($response['response']['docs'] as $doc){
49
            $ob = new $class();
50
            $properties = $doc->getPropertyNames();
51
            foreach($properties as $name){
52
                $setter = 'set'.$name;
53
                $value = $doc->$name;
54
                $value = $this->validateDate($value);
55
                if(method_exists($ob,$setter)){
56
                    call_user_func(array($ob,$setter),$value);
57
                }elseif(isset($propertiesMap[$name])){
58
                    $this->handleMappedProperty($propertiesMap[$name],$ob,$value);
59
                }
60
61
            }
62
            $entities[] = $ob;
63
        }
64
65
        return $entities;
66
    }
67
68
    /**
69
     * Handles mapped property defined by query filter
70
     * 
71
     * @param $property
72
     * @param $object
73
     * @param $value
74
     */
75
    public function handleMappedProperty($property,$object,$value)
76
    {
77
        $callback = array($this->filter,$property);
78
        if(is_callable($callback)){
79
            call_user_func($callback,$object,$value);
80
        }
81
    }
82
83
    /**
84
     * Convert date formatted string into a DateTime object
85
     *
86
     * @param   string  $value
87
     * @return  \DateTime|string
88
     */
89
    public function validateDate($value)
90
    {
91
        $value = trim($value);
92
        $date = \DateTime::createFromFormat(Manager::SOLR_DATE_FORMAT,$value);
93
        $check = $date && ($date->format(Manager::SOLR_DATE_FORMAT) === $value);
94
        if($check){
95
            return $date;
96
        }else{
97
            return $value;
98
        }
99
    }
100
101
    /**
102
     * Create a new instance of ResultConverter
103
     * @param   ServiceLocatorInterface $sl
104
     * @return  ResultConverter
105
     */
106
    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...
107
    {
108
        return new static();
109
    }
110
}