Skip to content

Commit 15fb1bd

Browse files
authored
fixed date and boolean conversion issues (#770)
1 parent 29b9008 commit 15fb1bd

File tree

3 files changed

+164
-7
lines changed

3 files changed

+164
-7
lines changed

Result/Converter.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace ONGR\ElasticsearchBundle\Result;
1313

14-
use ONGR\ElasticsearchBundle\Annotation\HashMap;
1514
use ONGR\ElasticsearchBundle\Annotation\Nested;
1615
use ONGR\ElasticsearchBundle\Annotation\Object;
1716
use ONGR\ElasticsearchBundle\Collection\Collection;
@@ -41,7 +40,7 @@ public function __construct($metadataCollector)
4140
/**
4241
* Converts raw array to document.
4342
*
44-
* @param array $rawData
43+
* @param array $rawData
4544
* @param Manager $manager
4645
*
4746
* @return object
@@ -78,9 +77,9 @@ public function convertToDocument($rawData, Manager $manager)
7877
/**
7978
* Assigns all properties to object.
8079
*
81-
* @param array $array
82-
* @param \ReflectionClass $object
83-
* @param array $aliases
80+
* @param array $array
81+
* @param object $object
82+
* @param array $aliases
8483
*
8584
* @return object
8685
*/
@@ -94,6 +93,9 @@ public function assignArrayToObject(array $array, $object, array $aliases)
9493
if (isset($aliases[$name]['type'])) {
9594
switch ($aliases[$name]['type']) {
9695
case 'date':
96+
if (is_null($value) || (is_object($value) && $value instanceof \DateTimeInterface)) {
97+
continue;
98+
}
9799
if (is_numeric($value) && (int)$value == $value) {
98100
$time = $value;
99101
} else {
@@ -117,6 +119,11 @@ public function assignArrayToObject(array $array, $object, array $aliases)
117119
);
118120
}
119121
break;
122+
case 'boolean':
123+
if (!is_bool($value)) {
124+
$value = (bool)$value;
125+
}
126+
break;
120127
default:
121128
// Do nothing here. Default cas is required by our code style standard.
122129
break;
@@ -217,7 +224,7 @@ public function convertToArray($object, $aliases = [], $fields = [])
217224
* Check if class matches the expected one.
218225
*
219226
* @param object $object
220-
* @param array $expectedClasses
227+
* @param array $expectedClasses
221228
*
222229
* @throws \InvalidArgumentException
223230
*/
@@ -238,7 +245,7 @@ private function checkVariableType($object, array $expectedClasses)
238245
* Check if value is instance of Collection.
239246
*
240247
* @param string $property
241-
* @param mixed $value
248+
* @param mixed $value
242249
*
243250
* @throws \InvalidArgumentException
244251
*/

Tests/Functional/Result/DocumentWithNullObjectFieldTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ protected function getDataArray()
2727
'_id' => 'foo',
2828
'title' => 'Bar Product',
2929
'location' => null,
30+
'released' => null,
3031
],
3132
],
3233
],
@@ -47,5 +48,6 @@ public function testResultWithNullObjectField()
4748
);
4849

4950
$this->assertNull($document->getLocation());
51+
$this->assertNull($document->getReleased());
5052
}
5153
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\Tests\Unit\Result;
13+
14+
use ONGR\ElasticsearchBundle\Mapping\Caser;
15+
use ONGR\ElasticsearchBundle\Mapping\MetadataCollector;
16+
use ONGR\ElasticsearchBundle\Result\Converter;
17+
use ONGR\ElasticsearchBundle\Tests\app\fixture\TestBundle\Document\Product;
18+
19+
class ConverterTest extends \PHPUnit_Framework_TestCase
20+
{
21+
/**
22+
* @var MetadataCollector
23+
*/
24+
private $metadataCollector;
25+
26+
/**
27+
* @var Converter
28+
*/
29+
private $converter;
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
public function setUp()
35+
{
36+
$this->metadataCollector = $this->getMockBuilder('ONGR\ElasticsearchBundle\Mapping\MetadataCollector')
37+
->disableOriginalConstructor()
38+
->getMock();
39+
40+
$this->converter = new Converter($this->metadataCollector);
41+
}
42+
43+
public function dataProviderForAssignArrayToObjectWhenDateIsObject()
44+
{
45+
return [
46+
# Case 0.
47+
[
48+
[
49+
'title' => 'Foo',
50+
],
51+
],
52+
# Case 1.
53+
[
54+
[
55+
'title' => 'Boo',
56+
'released' => new \DateTime(),
57+
],
58+
],
59+
# Case 2.
60+
[
61+
[
62+
'title' => 'Bar',
63+
'released' => null,
64+
]
65+
],
66+
# Case 3.
67+
[
68+
[
69+
'limited' => null,
70+
],
71+
[
72+
'limited' => false,
73+
]
74+
],
75+
# Case 4.
76+
[
77+
[
78+
'limited' => 1,
79+
],
80+
[
81+
'limited' => true,
82+
]
83+
],
84+
# Case 5.
85+
[
86+
[
87+
'limited' => true,
88+
],
89+
[
90+
'limited' => true,
91+
]
92+
]
93+
];
94+
}
95+
96+
/**
97+
* Tests array conversion to the object.
98+
*
99+
* @param array $product
100+
* @param array $expected
101+
*
102+
* @dataProvider dataProviderForAssignArrayToObjectWhenDateIsObject
103+
*/
104+
public function testAssignArrayToObjectWhenDateIsObject($product, $expected = [])
105+
{
106+
$aliases = [
107+
'title' => [
108+
'propertyName' => 'title',
109+
'type' => 'text',
110+
'hashmap' => false,
111+
'methods' => [
112+
'getter' => 'getTitle',
113+
'setter' => 'setTitle',
114+
],
115+
'propertyType' => 'private',
116+
],
117+
'released' => [
118+
'propertyName' => 'released',
119+
'type' => 'datetime',
120+
'hashmap' => false,
121+
'methods' => [
122+
'getter' => 'getReleased',
123+
'setter' => 'setReleased',
124+
],
125+
'propertyType' => 'private',
126+
127+
],
128+
'limited' => [
129+
'propertyName' => 'limited',
130+
'type' => 'boolean',
131+
'hashmap' => false,
132+
'methods' => [
133+
'getter' => 'getLimited',
134+
'setter' => 'setLimited',
135+
],
136+
'propertyType' => 'private',
137+
138+
]
139+
];
140+
/** @var Product $productDocument */
141+
$productDocument = $this->converter->assignArrayToObject($product, new Product(), $aliases);
142+
143+
foreach (array_keys($product) as $key) {
144+
$expect = isset($expected[$key]) ? $expected[$key] : $product[$key];
145+
$this->assertEquals($expect, $productDocument->{'get'.Caser::snake($key)}());
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)