Skip to content

Commit 292d39b

Browse files
committedMar 24, 2018
fixed null behaviour for fields and filters
1 parent d2f71ac commit 292d39b

File tree

7 files changed

+30
-166
lines changed

7 files changed

+30
-166
lines changed
 

‎Filter/Field/EntityFieldEqualFilter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class EntityFieldEqualFilter extends EntityFieldFilter
99
* @param mixed $value
1010
* @return bool
1111
*/
12-
public function validate($value)
12+
protected function validate($value)
1313
{
1414
return $value === $this->getEntityFieldValue();
1515
}

‎Filter/Field/EntityFieldFilter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function isFilterValid($entity)
4646
}
4747

4848
$value = PropertyAccess::createPropertyAccessor()->getValue($entity, $this->getEntityFieldName());
49-
if (!is_scalar($value)) {
49+
if (!empty($value) && !is_scalar($value)) {
5050
if (method_exists($value, '__toString')) {
5151
$value = call_user_func([$value, '__toString']);
5252
} else {

‎Schema/Field/Entity/ArrayField.php

+13-11
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@ class ArrayField extends Field
1212
*/
1313
public function getDocumentFieldValue($entity)
1414
{
15+
$result = [];
1516
$entityValue = $this->getEntityFieldValue($entity);
16-
if (is_scalar($entityValue)) {
17-
$entityValue = [ $entityValue ];
18-
} elseif (
19-
!is_array($entityValue) &&
20-
((!$entityValue instanceof \Iterator) && (!$entityValue instanceof \IteratorAggregate))
21-
) {
22-
throw new InvalidFieldValueException('Field value must be \Iterator or \IteratorAggregate instance');
23-
}
17+
if (!is_null($entityValue)) {
18+
if (is_scalar($entityValue)) {
19+
$entityValue = [ $entityValue ];
20+
} elseif (
21+
!is_array($entityValue) &&
22+
((!$entityValue instanceof \Iterator) && (!$entityValue instanceof \IteratorAggregate))
23+
) {
24+
throw new InvalidFieldValueException('Field value must be \Iterator or \IteratorAggregate instance');
25+
}
2426

25-
$result = [];
26-
foreach ($entityValue as $value) {
27-
$result[] = strval($value);
27+
foreach ($entityValue as $value) {
28+
$result[] = strval($value);
29+
}
2830
}
2931

3032
return $result;

‎Schema/Field/Entity/DateField.php

+14-11
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,25 @@ class DateField extends Field
1010

1111
/**
1212
* @param object $entity
13-
* @return string
13+
* @return string|null
1414
*/
1515
public function getDocumentFieldValue($entity)
1616
{
17+
$result = null;
1718
$entityValue = $this->getEntityFieldValue($entity);
18-
19-
if (!$entityValue instanceof \DateTime) {
20-
throw new InvalidFieldValueException(
21-
sprintf(
22-
'"%s" field value of "%s" must be a DateTime instance',
23-
$this->getEntityFieldName(),
24-
get_class($entity)
25-
)
26-
);
19+
if (!is_null($entityValue)) {
20+
if (!$entityValue instanceof \DateTime) {
21+
throw new InvalidFieldValueException(
22+
sprintf(
23+
'"%s" field value of "%s" must be a DateTime instance',
24+
$this->getEntityFieldName(),
25+
get_class($entity)
26+
)
27+
);
28+
}
29+
$result = $entityValue->format(self::FORMAT);
2730
}
2831

29-
return $entityValue->format(self::FORMAT);
32+
return $result;
3033
}
3134
}

‎Schema/Field/Entity/StringField.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function getDocumentFieldValue($entity)
1313
$entityValue = $this->getEntityFieldValue($entity);
1414
$documentValue = '';
1515
if (is_scalar($entityValue)) {
16-
$documentValue = $entityValue;
16+
$documentValue = strval($entityValue);
1717
} elseif (is_array($entityValue) || $entityValue instanceof \Iterator || $entityValue instanceof \IteratorAggregate) {
1818
$documentValue = [];
1919
foreach ($entityValue as $value) {

‎Schema/Field/Field.php

-111
This file was deleted.

‎Schema/Field/StringField.php

-30
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.