Completed
Push — dev ( e5bbcf...4306de )
by Arnaud
05:15
created

EntityLabelTrait::getEntityLabel()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 23
ccs 16
cts 16
cp 1
rs 9.0856
cc 3
eloc 15
nc 3
nop 1
crap 3
1
<?php
2
3
namespace LAG\AdminBundle\Admin\Behaviors;
4
5
use Symfony\Component\PropertyAccess\PropertyAccess;
6
7
trait EntityLabelTrait
8
{
9
    /**
10
     * Try to find a property to get a label from an entity. If found, it returns the property value through the
11
     * property accessor.
12
     *
13
     * @param $entity
14
     * @return string
15
     */
16 1
    public function getEntityLabel($entity)
17
    {
18 1
        $label = '';
19 1
        $accessor = PropertyAccess::createPropertyAccessor();
20
        $properties = [
21 1
            'label',
22 1
            'title',
23 1
            'name',
24 1
            '__toString',
25 1
            'content',
26 1
            'id',
27 1
        ];
28
29 1
        foreach ($properties as $property) {
30
31 1
            if ($accessor->isReadable($entity, $property)) {
32 1
                $label = $accessor->getValue($entity, $property);
33 1
                break;
34
            }               
35 1
        }
36
        
37 1
        return $label;
38
    }
39
}
40