This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
<?php
namespace Gedmo\Translator\Document;
use Gedmo\Translator\Translation as BaseTranslation;
use Doctrine\ODM\MongoDB\Mapping\Annotations\MappedSuperclass;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/**
* Document translation class.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*
* @MappedSuperclass
*/
abstract class Translation extends BaseTranslation
{
/**
* @Id
*/
protected $id;
/**
* @var string $locale
*
* @ODM\Field(type="string")
*/
protected $locale;
/**
* @var string $property
*
* @ODM\Field(type="string")
*/
protected $property;
/**
* @var string $value
*
* @ODM\Field(type="string")
*/
protected $value;
/**
* Get id
*
* @return integer $id
*/
public function getId()
{
return $this->id;
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace Gedmo\Translator\Entity;
use Gedmo\Translator\Translation as BaseTranslation;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\MappedSuperclass;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\GeneratedValue;
/**
* Entity translation class.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*
* @MappedSuperclass
*/
abstract class Translation extends BaseTranslation
{
/**
* @var integer $id
*
* @Column(type="integer")
* @Id
* @GeneratedValue
*/
protected $id;
/**
* @var string $locale
*
* @Column(type="string", length=8)
*/
protected $locale;
/**
* @var string $property
*
* @Column(type="string", length=32)
*/
protected $property;
/**
* @var string $value
*
* @Column(type="text", nullable=true)
*/
protected $value;
/**
* Get id
*
* @return integer $id
*/
public function getId()
{
return $this->id;
}
}

View File

@@ -0,0 +1,101 @@
<?php
namespace Gedmo\Translator;
/**
* Base translation class.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
abstract class Translation implements TranslationInterface
{
protected $translatable;
protected $locale;
protected $property;
protected $value;
/**
* Set translatable
*
* @param string $translatable
*/
public function setTranslatable($translatable)
{
$this->translatable = $translatable;
}
/**
* Get translatable
*
* @return string
*/
public function getTranslatable()
{
return $this->translatable;
}
/**
* Set locale
*
* @param string $locale
*/
public function setLocale($locale)
{
$this->locale = $locale;
}
/**
* Get locale
*
* @return string
*/
public function getLocale()
{
return $this->locale;
}
/**
* Set property
*
* @param string $property
*/
public function setProperty($property)
{
$this->property = $property;
}
/**
* Get property
*
* @return string
*/
public function getProperty()
{
return $this->property;
}
/**
* Set value
*
* @param string $value
*
* @return static
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* Get value
*
* @return string
*/
public function getValue()
{
return $this->value;
}
}

View File

@@ -0,0 +1,70 @@
<?php
namespace Gedmo\Translator;
/**
* Entity/Document translation interface.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
interface TranslationInterface
{
/**
* Set translatable
*
* @param string $translatable
*/
public function setTranslatable($translatable);
/**
* Get translatable
*
* @return string
*/
public function getTranslatable();
/**
* Set locale
*
* @param string $locale
*/
public function setLocale($locale);
/**
* Get locale
*
* @return string
*/
public function getLocale();
/**
* Set property
*
* @param string $property
*/
public function setProperty($property);
/**
* Get property
*
* @return string
*/
public function getProperty();
/**
* Set value
*
* @param string $value
*
* @return static
*/
public function setValue($value);
/**
* Get value
*
* @return string
*/
public function getValue();
}

View File

@@ -0,0 +1,174 @@
<?php
namespace Gedmo\Translator;
use Doctrine\Common\Collections\Collection;
/**
* Proxy class for Entity/Document translations.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class TranslationProxy
{
protected $locale;
protected $translatable;
protected $properties = array();
protected $class;
/**
* @var Collection|TranslationInterface[]
*/
protected $coll;
/**
* Initializes translations collection
*
* @param object $translatable object to translate
* @param string $locale translation name
* @param array $properties object properties to translate
* @param string $class translation entity|document class
* @param Collection $coll translations collection
*
* @throws \InvalidArgumentException Translation class doesn't implement TranslationInterface
*/
public function __construct($translatable, $locale, array $properties, $class, Collection $coll)
{
$this->translatable = $translatable;
$this->locale = $locale;
$this->properties = $properties;
$this->class = $class;
$this->coll = $coll;
$translationClass = new \ReflectionClass($class);
if (!$translationClass->implementsInterface('Gedmo\Translator\TranslationInterface')) {
throw new \InvalidArgumentException(sprintf(
'Translation class should implement Gedmo\Translator\TranslationInterface, "%s" given',
$class
));
}
}
public function __call($method, $arguments)
{
$matches = array();
if (preg_match('/^(set|get)(.*)$/', $method, $matches)) {
$property = lcfirst($matches[2]);
if (in_array($property, $this->properties)) {
switch ($matches[1]) {
case 'get':
return $this->getTranslatedValue($property);
case 'set':
if (isset($arguments[0])) {
$this->setTranslatedValue($property, $arguments[0]);
return $this;
}
}
}
}
$return = call_user_func_array(array($this->translatable, $method), $arguments);
if ($this->translatable === $return) {
return $this;
}
return $return;
}
public function __get($property)
{
if (in_array($property, $this->properties)) {
if (method_exists($this, $getter = 'get'.ucfirst($property))) {
return $this->$getter;
}
return $this->getTranslatedValue($property);
}
return $this->translatable->$property;
}
public function __set($property, $value)
{
if (in_array($property, $this->properties)) {
if (method_exists($this, $setter = 'set'.ucfirst($property))) {
return $this->$setter($value);
}
return $this->setTranslatedValue($property, $value);
}
$this->translatable->$property = $value;
}
public function __isset($property)
{
return in_array($property, $this->properties);
}
/**
* Returns locale name for the current translation proxy instance.
*
* @return string
*/
public function getProxyLocale()
{
return $this->locale;
}
/**
* Returns translated value for specific property.
*
* @param string $property property name
*
* @return mixed
*/
public function getTranslatedValue($property)
{
return $this
->findOrCreateTranslationForProperty($property, $this->getProxyLocale())
->getValue();
}
/**
* Sets translated value for specific property.
*
* @param string $property property name
* @param string $value value
*/
public function setTranslatedValue($property, $value)
{
$this
->findOrCreateTranslationForProperty($property, $this->getProxyLocale())
->setValue($value);
}
/**
* Finds existing or creates new translation for specified property
*
* @param string $property object property name
* @param string $locale locale name
*
* @return Translation
*/
private function findOrCreateTranslationForProperty($property, $locale)
{
foreach ($this->coll as $translation) {
if ($locale === $translation->getLocale() && $property === $translation->getProperty()) {
return $translation;
}
}
/** @var TranslationInterface $translation */
$translation = new $this->class;
$translation->setTranslatable($this->translatable);
$translation->setProperty($property);
$translation->setLocale($locale);
$this->coll->add($translation);
return $translation;
}
}