EntityFieldFilter::getEntityFieldValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Mdiyakov\DoctrineSolrBundle\Filter\Field;
4
5
use Mdiyakov\DoctrineSolrBundle\Exception\FilterConfigException;
6
use Mdiyakov\DoctrineSolrBundle\Filter\EntityFilterInterface;
7
use Symfony\Component\PropertyAccess\PropertyAccess;
8
9
abstract class EntityFieldFilter implements EntityFilterInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    private $entityFieldName;
15
16
    /**
17
     * @var mixed
18
     */
19
    private $entityFieldValue;
20
21
    /**
22
     * @param string $entityFieldName
23
     */
24
    public function setEntityFieldName($entityFieldName)
25
    {
26
        $this->entityFieldName = $entityFieldName;
27
    }
28
29
    /**
30
     * @param mixed $entityFieldValue
31
     */
32
    public function setEntityFieldValue($entityFieldValue)
33
    {
34
        $this->entityFieldValue = $entityFieldValue;
35
    }
36
37
    /**
38
     * @param object $entity
39
     * @return bool
40
     * @throws FilterConfigException
41
     */
42
    public function isFilterValid($entity)
43
    {
44
        if (!is_object($entity)) {
45
            throw new FilterConfigException('Entity must be an object to be filtered');
46
        }
47
48
        $value = PropertyAccess::createPropertyAccessor()->getValue($entity, $this->getEntityFieldName());
49
        if (!empty($value) && !is_scalar($value)) {
50
            if (method_exists($value, '__toString')) {
51
                $value = call_user_func([$value, '__toString']);
52
            } else {
53
                throw new FilterConfigException('Entity field must have scalar value to be filtered');
54
            }
55
        }
56
57
        return $this->validate($value);
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getEntityFieldName()
64
    {
65
        return $this->entityFieldName;
66
    }
67
68
    /**
69
     * @return mixed
70
     */
71
    public function getEntityFieldValue()
72
    {
73
        return $this->entityFieldValue;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    abstract public function getSupportedOperator();
80
81
    /**
82
     * @param $value
83
     * @return bool
84
     */
85
    abstract protected function validate($value);
86
87
}