1 | <?php |
||
29 | class TranslationManager |
||
30 | { |
||
31 | /** |
||
32 | * @var Repository |
||
33 | */ |
||
34 | private $repository; |
||
35 | |||
36 | /** |
||
37 | * @var EventDispatcherInterface |
||
38 | */ |
||
39 | private $dispatcher; |
||
40 | |||
41 | /** |
||
42 | * @param Repository $repository |
||
43 | * @param EventDispatcherInterface $dispatcher |
||
44 | */ |
||
45 | public function __construct(Repository $repository, EventDispatcherInterface $dispatcher) |
||
50 | |||
51 | /** |
||
52 | * @param string $id |
||
53 | * |
||
54 | * @return Translation |
||
55 | */ |
||
56 | public function get($id) |
||
57 | { |
||
58 | return $this->repository->find($id); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Returns all active tags from translations |
||
63 | * @return array |
||
64 | */ |
||
65 | public function getTags() |
||
66 | { |
||
67 | return $this->getItems('tags'); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Returns all active domains from translations |
||
72 | * @return array |
||
73 | */ |
||
74 | public function getDomains() |
||
75 | { |
||
76 | return $this->getItems('domain'); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Returns all translations if filters are not specified |
||
81 | * |
||
82 | * @param array $filters An array with specified limitations for results |
||
83 | * |
||
84 | * @return DocumentIterator |
||
85 | */ |
||
86 | public function getAll(array $filters = null) |
||
87 | { |
||
88 | $search = $this->repository->createSearch(); |
||
89 | $search->addQuery(new MatchAllQuery()); |
||
90 | $search->setScroll('2m'); |
||
91 | |||
92 | if ($filters) { |
||
93 | foreach ($filters as $field => $value) { |
||
94 | $search->addFilter(new TermsQuery($field, $value)); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | return $this->repository->findDocuments($search); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Edits object from translation. |
||
103 | * |
||
104 | * @param string $id |
||
105 | * @param Request $request Http request object. |
||
106 | */ |
||
107 | public function update($id, Request $request) |
||
108 | { |
||
109 | $content = json_decode($request->getContent(), true); |
||
110 | |||
111 | if (empty($content)) { |
||
112 | return; |
||
113 | } |
||
114 | |||
115 | $document = $this->get($id); |
||
116 | |||
117 | if (isset($content['messages'])) { |
||
118 | $this->updateMessages($document, $content['messages']); |
||
|
|||
119 | unset($content['messages']); |
||
120 | } |
||
121 | |||
122 | try { |
||
123 | foreach ($content as $key => $value) { |
||
124 | $document->{'set'.ucfirst($key)}($value); |
||
125 | } |
||
126 | |||
127 | $document->setUpdatedAt(new \DateTime()); |
||
128 | } catch (\Error $e) { |
||
129 | throw new \LogicException('Illegal variable provided for translation'); |
||
130 | } |
||
131 | |||
132 | $this->repository->getManager()->persist($document); |
||
133 | $this->repository->getManager()->commit(); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param Translation[] $translations |
||
138 | */ |
||
139 | public function save($translations) |
||
147 | |||
148 | /** |
||
149 | * @param Translation $document |
||
150 | * @param array $messages |
||
151 | */ |
||
152 | private function updateMessages(Translation $document, array $messages) |
||
174 | |||
175 | /** |
||
176 | * @param Message $message |
||
177 | * @param string $locale |
||
178 | * @param string $text |
||
179 | * |
||
180 | * @return Message |
||
181 | */ |
||
182 | private function updateMessageData(Message $message, $locale, $text) |
||
191 | |||
192 | /** |
||
193 | * Returns a list of available tags or domains |
||
194 | * |
||
195 | * @param string $type |
||
196 | * @return array |
||
197 | */ |
||
198 | private function getItems($type) |
||
212 | } |
||
213 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: