Completed
Pull Request — 2.x (#124)
by Christian
02:28
created

TranslatableChecker::isTranslatable()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 2
Metric Value
c 7
b 1
f 2
dl 0
loc 35
rs 5.3846
cc 8
eloc 20
nc 16
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\TranslationBundle\Checker;
13
14
/**
15
 * @author Nicolas Bastien <[email protected]>
16
 */
17
class TranslatableChecker
18
{
19
    /**
20
     * @var array
21
     */
22
    protected $supportedInterfaces = array();
23
24
    /**
25
     * @var array
26
     */
27
    protected $supportedModels = array();
28
29
    /**
30
     * @param array $supportedInterfaces
31
     */
32
    public function setSupportedInterfaces(array $supportedInterfaces)
33
    {
34
        $this->supportedInterfaces = $supportedInterfaces;
35
    }
36
37
    /**
38
     * @return array
39
     */
40
    public function getSupportedInterfaces()
41
    {
42
        return $this->supportedInterfaces;
43
    }
44
45
    /**
46
     * @param array $supportedModels
47
     */
48
    public function setSupportedModels($supportedModels)
49
    {
50
        $this->supportedModels = $supportedModels;
51
    }
52
53
    /**
54
     * @return array
55
     */
56
    public function getSupportedModels()
57
    {
58
        return $this->supportedModels;
59
    }
60
61
    /**
62
     * Check if $object is translatable.
63
     *
64
     * @param mixed $object
65
     *
66
     * @return bool
67
     */
68
    public function isTranslatable($object)
69
    {
70
        if ($object === null) {
71
            return false;
72
        }
73
74
        if (function_exists('class_uses')) {
75
            $translateTraits = array(
76
                'Sonata\TranslationBundle\Traits\Translatable',
77
                'Sonata\TranslationBundle\Traits\TranslatableTrait',
78
                'Sonata\TranslationBundle\Traits\Gedmo\PersonalTranslatable',
79
                'Sonata\TranslationBundle\Traits\Gedmo\PersonalTranslatableTrait',
80
            );
81
82
            $traits = class_uses($object);
83
            if (count(array_intersect($translateTraits, $traits)) > 0) {
84
                return true;
85
            }
86
        }
87
88
        $objectInterfaces = class_implements($object);
89
        foreach ($this->getSupportedInterfaces() as $interface) {
90
            if (in_array($interface, $objectInterfaces)) {
91
                return true;
92
            }
93
        }
94
95
        foreach ($this->getSupportedModels() as $model) {
96
            if ($object instanceof $model) {
97
                return true;
98
            }
99
        }
100
101
        return false;
102
    }
103
}
104