TranslatableChecker::isTranslatable()   B
last analyzed

Complexity

Conditions 7
Paths 9

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

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