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

TranslatableChecker::isTranslatable()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 36
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 36
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
            // NEXT_MAJOR: remove Translateable and PersonalTrait
76
            $translateTraits = array(
77
                'Sonata\TranslationBundle\Traits\Translatable',
78
                'Sonata\TranslationBundle\Traits\TranslatableTrait',
79
                'Sonata\TranslationBundle\Traits\Gedmo\PersonalTranslatable',
80
                'Sonata\TranslationBundle\Traits\Gedmo\PersonalTranslatableTrait',
81
            );
82
83
            $traits = class_uses($object);
84
            if (count(array_intersect($translateTraits, $traits)) > 0) {
85
                return true;
86
            }
87
        }
88
89
        $objectInterfaces = class_implements($object);
90
        foreach ($this->getSupportedInterfaces() as $interface) {
91
            if (in_array($interface, $objectInterfaces)) {
92
                return true;
93
            }
94
        }
95
96
        foreach ($this->getSupportedModels() as $model) {
97
            if ($object instanceof $model) {
98
                return true;
99
            }
100
        }
101
102
        return false;
103
    }
104
}
105