|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Extensions\Gedmo; |
|
4
|
|
|
|
|
5
|
|
|
use Gedmo\Translatable\Mapping\Driver\Fluent as FluentDriver; |
|
6
|
|
|
use LaravelDoctrine\Fluent\Buildable; |
|
7
|
|
|
use LaravelDoctrine\Fluent\Builders\Entity; |
|
8
|
|
|
use LaravelDoctrine\Fluent\Builders\Field; |
|
9
|
|
|
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata; |
|
10
|
|
|
|
|
11
|
|
|
class Translatable implements Buildable |
|
12
|
|
|
{ |
|
13
|
|
|
const MACRO_METHOD = 'translatable'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var ExtensibleClassMetadata |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $classMetadata; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $fieldName; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string|null |
|
27
|
|
|
*/ |
|
28
|
|
|
protected static $translationClass; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string|null |
|
32
|
|
|
*/ |
|
33
|
|
|
protected static $locale; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param ExtensibleClassMetadata $classMetadata |
|
37
|
|
|
* @param string $fieldName |
|
38
|
|
|
*/ |
|
39
|
4 |
|
public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName) |
|
40
|
|
|
{ |
|
41
|
4 |
|
$this->classMetadata = $classMetadata; |
|
42
|
4 |
|
$this->fieldName = $fieldName; |
|
43
|
4 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return null|string |
|
47
|
|
|
*/ |
|
48
|
3 |
|
public static function getLocale() |
|
49
|
|
|
{ |
|
50
|
3 |
|
return self::$locale; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return null|string |
|
55
|
|
|
*/ |
|
56
|
3 |
|
public static function getTranslationClass() |
|
57
|
|
|
{ |
|
58
|
3 |
|
return self::$translationClass; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Return the name of the actual extension. |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
3 |
|
public function getExtensionName() |
|
67
|
|
|
{ |
|
68
|
3 |
|
return FluentDriver::EXTENSION_NAME; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
3 |
|
public static function enable() |
|
75
|
|
|
{ |
|
76
|
|
|
Field::macro(static::MACRO_METHOD, function (Field $builder) { |
|
77
|
1 |
|
return new static($builder->getClassMetadata(), $builder->getName()); |
|
78
|
3 |
|
}); |
|
79
|
|
|
|
|
80
|
|
|
Field::macro('locale', function (Field $field) { |
|
81
|
1 |
|
self::locale($field->getName()); |
|
82
|
3 |
|
}); |
|
83
|
|
|
|
|
84
|
3 |
|
Entity::macro('translationClass', function (Entity $entity, $class) { |
|
85
|
1 |
|
self::translationClass($class); |
|
86
|
3 |
|
}); |
|
87
|
3 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Execute the build process |
|
91
|
|
|
*/ |
|
92
|
3 |
|
public function build() |
|
93
|
|
|
{ |
|
94
|
3 |
|
$extension = $this->classMetadata->getExtension($this->getExtensionName()); |
|
95
|
|
|
|
|
96
|
3 |
|
if ($class = self::getTranslationClass()) { |
|
97
|
1 |
|
$extension['translationClass'] = $class; |
|
98
|
1 |
|
} |
|
99
|
|
|
|
|
100
|
3 |
|
if ($locale = self::getLocale()) { |
|
101
|
2 |
|
$extension['locale'] = $locale; |
|
102
|
2 |
|
} |
|
103
|
|
|
|
|
104
|
3 |
|
$extension['fields'][] = $this->fieldName; |
|
105
|
|
|
|
|
106
|
3 |
|
$this->classMetadata->addExtension($this->getExtensionName(), $extension); |
|
107
|
3 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string $translationClass |
|
111
|
|
|
* @return $this |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public static function translationClass($translationClass) |
|
114
|
|
|
{ |
|
115
|
1 |
|
static::$translationClass = $translationClass; |
|
116
|
1 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param string $locale |
|
120
|
|
|
* @return Translatable |
|
121
|
|
|
*/ |
|
122
|
1 |
|
public static function locale($locale) |
|
123
|
|
|
{ |
|
124
|
1 |
|
static::$locale = $locale; |
|
125
|
1 |
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|