em = $em; $this->ll = $ll; } /** * @param LinguisticaInterface $element * @return bool */ public function quickUpdate(LinguisticaInterface $element){ if(empty($element)) return false; try { $this->em->persist($element); $this->em->flush(); } catch (ORMException $e) { return false; } return true; } /** * @param mixed $input * @param bool $fallback * @param null $locale * @return bool|string */ public function getTranslated(mixed $input, $fallback = true, $locale = null){ if(is_null($locale)) $locale = $this->ll->getLocale(); if(is_array($input)) return $this->getTranslatedArray($input,$locale, $fallback); if(is_object($input)) return $this->getTranslatedObject($input,$locale, $fallback); return false; } /** * Select an item in an array of string where the key is $locale * * @param array $input * @param $locale * @param bool $fallback * @return bool|mixed */ public function getTranslatedArray(array $input, $locale, $fallback){ if(isset($input[$locale])) return $input[$locale]; elseif(isset($input[$this->ll->getLocaleDefault()])){ if(!$fallback) return false; return $input[$this->ll->getLocaleDefault()]; } return false; } /** * Select an string value in a LinguisticaTradItem for $locale * * @param LinguisticaTradItem $input * @param $locale * @param $fallback * @return array|bool */ public function getTranslatedObject(LinguisticaTradItem $input, $locale, $fallback){ if($input->getStrValue($locale)) return $input->getStrValue($locale); elseif($input->getStrValue($this->ll->getLocaleDefault())){ if(!$fallback) return false; return $input->getStrValue($this->ll->getLocaleDefault()); } return false; } /** * @param string $value value of the string * @param string $moduleName on which module this item depends * @param null $locale locale of $value * @return null|LinguisticaTradItem */ public function createNewTranslatedObject($value, $moduleName, $locale = null){ if(is_null($locale)) $locale = $this->ll->getLocale(); $itm = new LinguisticaTradItem(); $itm->setModuleName($moduleName) ->addStrValues($value,$locale); try { if(!$this->em->contains($itm)) $this->em->persist($itm); $this->em->flush(); } catch (ORMException $e) { return null; } return $itm; } /** * @param int $id * @param $class * @return object|null */ public function getById($id, $class){ return $this->em->getRepository($class)->find($id); } public function addLinguisticaStrValue(LinguisticaInterface $linguistica, array $values){ foreach ($values as $key => $value){ $linguistica->addStrValues($value, $key); } if (!$this->quickUpdate($linguistica)) return null; return true; } }