-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocaleAwareFactoryTrait.php
More file actions
60 lines (50 loc) · 1.66 KB
/
LocaleAwareFactoryTrait.php
File metadata and controls
60 lines (50 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/*
* This file is part of the PhpMob package.
*
* (c) Ishmael Doss <nukboon@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace PhpMob\DataFixture;
use Sylius\Component\Resource\Model\TranslatableInterface;
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
trait LocaleAwareFactoryTrait
{
/**
* @var TranslationLocaleProviderInterface
*/
private $translationProvider;
/**
* @param TranslationLocaleProviderInterface $translationProvider
*/
public function setTranslationProvider(TranslationLocaleProviderInterface $translationProvider)
{
$this->translationProvider = $translationProvider;
}
/**
* @param TranslatableInterface $object
* @param array $properties
* @param array $data
*
* @throws \TypeError
*/
protected function setLocalizedData(TranslatableInterface $object, array $properties, array $data)
{
$accessor = PropertyAccess::createPropertyAccessor();
foreach ($this->translationProvider->getDefinedLocalesCodes() as $localeCode) {
$object->setCurrentLocale($localeCode);
$object->setFallbackLocale($localeCode);
foreach ($properties as $property) {
$value = $data[$property];
$value = is_string($value)
? $value
: $value[$localeCode];
$accessor->setValue($object, $property, $value);
}
}
}
}