|
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.26 |
|
24
|
|
|
*/ |
|
25
|
|
|
class ResultConverter |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Current filter used for conversion |
|
29
|
|
|
* |
|
30
|
|
|
* @var AbstractPaginationQuery |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $filter; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* if set, the city name of the found location overwrites the general job location |
|
36
|
|
|
* |
|
37
|
|
|
* @var bool |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $useGeoLocation=false; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Convert result into entity |
|
43
|
|
|
* |
|
44
|
|
|
* @param AbstractPaginationQuery $filter |
|
45
|
|
|
* @param \SolrQueryResponse $queryResponse |
|
46
|
|
|
* @return EntityType[] |
|
47
|
|
|
*/ |
|
48
|
|
|
public function convert($filter, $queryResponse) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->filter = $filter; |
|
51
|
|
|
$response = $queryResponse->getResponse(); |
|
|
|
|
|
|
52
|
|
|
$propertiesMap = $filter->getPropertiesMap(); |
|
53
|
|
|
$class = $filter->getEntityClass(); |
|
54
|
|
|
$entities = []; |
|
55
|
|
|
foreach($response['response']['docs'] as $doc){ |
|
56
|
|
|
$ob = new $class(); |
|
57
|
|
|
$properties = $doc->getPropertyNames(); |
|
58
|
|
|
foreach($properties as $name){ |
|
59
|
|
|
$setter = 'set'.$name; |
|
60
|
|
|
$value = $doc->$name; |
|
61
|
|
|
$value = $this->validateDate($value); |
|
62
|
|
|
if ($value instanceof \SolrObject) { |
|
63
|
|
|
if ($name == 'locations') { |
|
64
|
|
|
$this->useGeoLocation=true; |
|
65
|
|
|
} |
|
66
|
|
|
$this->handleMappedProperty($propertiesMap[$name],$ob,$value); |
|
67
|
|
|
} elseif (method_exists($ob,$setter) && !$value instanceof \SolrObject){ |
|
68
|
|
|
if ($name != 'location') { |
|
69
|
|
|
call_user_func(array($ob, $setter), $value); |
|
70
|
|
|
}elseif (!$this->useGeoLocation) { |
|
71
|
|
|
call_user_func(array($ob, $setter), $value); |
|
72
|
|
|
} |
|
73
|
|
|
}elseif(isset($propertiesMap[$name])){ |
|
74
|
|
|
$this->handleMappedProperty($propertiesMap[$name],$ob,$value); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
$entities[] = $ob; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $entities; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Handles mapped property defined by query filter |
|
86
|
|
|
* |
|
87
|
|
|
* @param $property |
|
88
|
|
|
* @param $object |
|
89
|
|
|
* @param $value |
|
90
|
|
|
*/ |
|
91
|
|
|
public function handleMappedProperty($property,$object,$value) |
|
92
|
|
|
{ |
|
93
|
|
|
$callback = array($this->filter,$property); |
|
94
|
|
|
if(is_callable($callback)){ |
|
95
|
|
|
call_user_func($callback,$object,$value); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Convert date formatted string into a DateTime object |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $value |
|
103
|
|
|
* @return \DateTime|string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function validateDate($value) |
|
106
|
|
|
{ |
|
107
|
|
|
if ($value instanceof \SolrObject){ |
|
108
|
|
|
return $value; |
|
109
|
|
|
} |
|
110
|
|
|
$value = trim($value); |
|
111
|
|
|
$date = \DateTime::createFromFormat(Manager::SOLR_DATE_FORMAT,$value); |
|
112
|
|
|
$check = $date && ($date->format(Manager::SOLR_DATE_FORMAT) === $value); |
|
113
|
|
|
if($check){ |
|
114
|
|
|
return $date; |
|
115
|
|
|
}else{ |
|
116
|
|
|
return $value; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Create a new instance of ResultConverter |
|
122
|
|
|
* @param ServiceLocatorInterface $sl |
|
123
|
|
|
* @return ResultConverter |
|
124
|
|
|
*/ |
|
125
|
|
|
static public function factory(ServiceLocatorInterface $sl) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
return new static(); |
|
128
|
|
|
} |
|
129
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.