Completed
Pull Request — dev (#6)
by Arnaud
07:28 queued 04:28
created

EntityLabelTrait::getEntityLabel()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 20
rs 8.2222
cc 7
eloc 16
nc 7
nop 1
1
<?php
2
3
namespace LAG\AdminBundle\Admin\Behaviors;
4
5
use BlueBear\BaseBundle\Entity\Behaviors\Label;
6
use Symfony\Component\PropertyAccess\PropertyAccess;
7
8
trait EntityLabelTrait
9
{
10
    public function getEntityLabel($entity)
11
    {
12
        $label = '';
13
        $accessor = PropertyAccess::createPropertyAccessor();
14
15
        if ($accessor->isReadable($entity, 'label')) {
16
            $label = $entity->getLabel();
17
        } else if ($accessor->isReadable($entity, 'title')) {
18
            $label = $entity->getTitle();
19
        } else if ($accessor->isReadable($entity, 'name')) {
20
            $label = $entity->getName();
21
        } else if ($accessor->isReadable($entity, '__toString')) {
22
            $label = $entity->__toString();
23
        } else if ($accessor->isReadable($entity, 'content')) {
24
            $label = strip_tags(substr($entity->getContent(), 0, 100));
25
        } else if ($accessor->isReadable($entity, 'id')) {
26
            $label = $entity->getId();
27
        }
28
        return $label;
29
    }
30
}
31