Completed
Pull Request — master (#1079)
by Guillaume
07:38
created

CastToStringPropertyAccessor::cleanValue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace FOS\ElasticaBundle\PropertyAccessor;
4
5
use Symfony\Component\PropertyAccess\PropertyAccessor;
6
7
/**
8
 * Allow to access a property value and directly call it's __toString() method
9
 * Very useful for objects used as id (like UUID)
10
 */
11
class CastToStringPropertyAccessor extends PropertyAccessor
12
{
13
    /**
14
     * @inheritDoc
15
     */
16
    public function getValue($objectOrArray, $propertyPath)
17
    {
18
        $value = parent::getValue($objectOrArray, $propertyPath);
19
20
        return $this->cleanValue($value);
21
    }
22
23
    /**
24
     * @param mixed $value
25
     *
26
     * @return string
27
     */
28
    private function cleanValue($value)
29
    {
30
        if (true === is_object($value) && method_exists($value, '__toString')) {
31
            $value = (string) $value;
32
        }
33
34
        return $value;
35
    }
36
}
37