// Doctrine_Record::_get()
foreach ($ this ->_table->getFilters() as $filter) {
try {
$ value = $filter->filterGet($ this , $fieldName);
$success = true ;
} catch (Doctrine_Exception $e) {}
}
if ($success) {
return $ value ;
} else {
throw $e;
}
/**
* EasyAccess package filter. Implements access to record's properties as for translated in I18n.
*
* Can be used as a part of Of_ExtDoctrine_I18n_Helper system, or as stand-alone filter both.
*
* @author OmeZ
* @version 1.0
* @license www.opensource.org/licenses/lgpl-license.php LGPL
* @package Of_ExtDoctrine_I18n_EasyAccess
*/
class Of_ExtDoctrine_I18n_EasyAccess_Filter extends Doctrine_Record_Filter {
/**
* Fields
*
* @var array
*/
protected $_fields = array();
/**
* Language
*
* @var string
*/
protected $_language;
/**
* @var Of_ExtDoctrine_I18n_EasyAccess_OwnerInterface
*/
protected $_owner;
/**
* Constructs new filter with options
*
* @param array $options
* @return void
*/
public function __construct(array $options) {
if (isset($options[ 'fields' ])) $ this ->setFields($options[ 'fields' ]);
if (isset($options[ 'language' ])) $ this ->setLanguage($options[ 'language' ]);
if (isset($options[ 'owner' ])) $ this ->setOwner($options[ 'owner' ]);
}
/**
* Returns owner of filter
*
* @return Of_ExtDoctrine_I18n_EasyAccess_OwnerInterface
*/
public function getOwner() {
return $ this ->_owner;
}
/**
* Sets owner for filter
*
* @param Of_ExtDoctrine_I18n_EasyAccess_OwnerInterface $owner
* @return Of_ExtDoctrine_I18n_EasyAccess_Filter
*/
public function setOwner(Of_ExtDoctrine_I18n_EasyAccess_OwnerInterface $owner) {
$ this ->_owner = $owner;
return $ this ;
}
/**
* Returns fields aliases for filter
*
* @return array
*/
public function getFields() {
return $ this ->_fields;
}
/**
* Sets fields aliases for filter
*
* @param $fields
* @return Of_ExtDoctrine_I18n_EasyAccess_Filter
*/
public function setFields($fields) {
$ this ->_fields = (array)$fields;
return $ this ;
}
/**
* Returns default language for filter
*
* @return string
*/
public function getLanguage() {
if ($ this ->_language !== null ) {
return $ this ->_language;
} elseif ($ this ->_owner) {
return $ this ->_owner->getLanguage();
} else {
require_once 'Of/ExtDoctrine/I18n/EasyAccess/Exception.php' ;
throw new Of_ExtDoctrine_I18n_EasyAccess_Exception( 'Impossible to detect language' );
}
}
/**
* Sets language to filter
*
* @return Of_ExtDoctrine_I18n_EasyAccess_Filter
*/
public function setLanguage($language) {
$ this ->_language = $language;
return $ this ;
}
/**
* Returns value of translatable attribute
*
* @param Doctrine_Record $record
* @param string $name
* @return mixed
*/
public function filterGet(Doctrine_Record $record, $name) {
return $ this ->getTranslation($record, $name, $ this ->getLanguage());
}
/**
* Sets value to translatable attribute
*
* @param Doctrine_Record $record
* @param string $name
* @param mixed $value
* @return void
*/
public function filterSet(Doctrine_Record $record, $name, $ value ) {
return $ this ->setTranslation($record, $name, $ value , $ this ->getLanguage());
}
/**
* Language dependent getter to translatable attribute
*
* @param Doctrine_Record $record
* @param string $name
* @param string $language
* @param boolean $return_first_found
* @return mixed
*/
public function getTranslation(Doctrine_Record $record, $name, $language = null , $return_first_found = true ) {
if (in_array($name, $ this ->_fields)) {
$language = empty($language)?( string )$ this ->getLanguage():( string )$language;
if ($record->hasRelation( 'Translation' )) {
if ($record->Translation->contains($language)) {
return $record->Translation[$language][$name];
} elseif ($return_first_found && $record->Translation->count()) {
foreach ($record->Translation as $translation)
if (!empty($translation[$name]))
return $translation[$name];
return null ;
} else return null ;
} else return null ;
} else {
require_once 'Of/ExtDoctrine/I18n/EasyAccess/NotTranslatableException.php' ;
throw new Of_ExtDoctrine_I18n_EasyAccess_NotTranslatableException( "Field {$name} is not marked as easy getter to tranlsatable attribute in " .get_class($record));
}
}
/**
* Language dependent setter to translatable attribute
*
* @param Doctrine_Record $record
* @param string $name
* @param mixed $value
* @param string $language
* @return void
*/
public function setTranslation(Doctrine_Record $record, $name, $ value , $language = null ) {
if (in_array($name, $ this ->_fields)) {
$language = empty($language)?( string )$ this ->getLanguage():( string )$language;
if ($record->hasRelation( 'Translation' )) {
$record->Translation[$language][$name] = $ value ;
}
} else {
require_once 'Of/ExtDoctrine/I18n/EasyAccess/NotTranslatableException.php' ;
throw new Of_ExtDoctrine_I18n_EasyAccess_NotTranslatableException( "Field {$name} is not marked as easy setter to tranlsatable attribute in " .get_class($record));
}
}
}
/**
* Constructs new filter with options
*
* @param array $options
* @return void
*/
public function __construct(array $options) {
if (isset($options[ 'fields' ])) $ this ->setFields($options[ 'fields' ]);
if (isset($options[ 'language' ])) $ this ->setLanguage($options[ 'language' ]);
if (isset($options[ 'owner' ])) $ this ->setOwner($options[ 'owner' ]);
}
/**
* Returns default language for filter
*
* @return string
*/
public function getLanguage() {
if ($ this ->_language !== null ) {
return $ this ->_language;
} elseif ($ this ->_owner) {
return $ this ->_owner->getLanguage();
} else {
require_once 'Of/ExtDoctrine/I18n/EasyAccess/Exception.php' ;
throw new Of_ExtDoctrine_I18n_EasyAccess_Exception( 'Impossible to detect language' );
}
}
interface Of_ExtDoctrine_I18n_EasyAccess_OwnerInterface {
/**
* Returns language for inherited components
*
* @return string
*/
public function getLanguage();
}
/**
* Returns value of translatable attribute
*
* @param Doctrine_Record $record
* @param string $name
* @return mixed
*/
public function filterGet(Doctrine_Record $record, $name) {
return $ this ->getTranslation($record, $name, $ this ->getLanguage());
}
/**
* Sets value to translatable attribute
*
* @param Doctrine_Record $record
* @param string $name
* @param mixed $value
* @return void
*/
public function filterSet(Doctrine_Record $record, $name, $ value ) {
return $ this ->setTranslation($record, $name, $ value , $ this ->getLanguage());
}
/**
* Language dependent getter to translatable attribute
*
* @param Doctrine_Record $record
* @param string $name
* @param string $language
* @param boolean $return_first_found
* @return mixed
*/
public function getTranslation(Doctrine_Record $record, $name, $language = null , $return_first_found = true ) {
if (in_array($name, $ this ->_fields)) {
$language = empty($language)?( string )$ this ->getLanguage():( string )$language;
if ($record->hasRelation( 'Translation' )) {
if ($record->Translation->contains($language)) {
return $record->Translation[$language][$name];
} elseif ($return_first_found && $record->Translation->count()) {
foreach ($record->Translation as $translation)
if (!empty($translation[$name]))
return $translation[$name];
return null ;
} else return null ;
} else return null ;
} else {
require_once 'Of/ExtDoctrine/I18n/EasyAccess/NotTranslatableException.php' ;
throw new Of_ExtDoctrine_I18n_EasyAccess_NotTranslatableException( "Field {$name} is not marked as easy getter to tranlsatable attribute in " .get_class($record));
}
}
/**
* Language dependent setter to translatable attribute
*
* @param Doctrine_Record $record
* @param string $name
* @param mixed $value
* @param string $language
* @return void
*/
public function setTranslation(Doctrine_Record $record, $name, $ value , $language = null ) {
if (in_array($name, $ this ->_fields)) {
$language = empty($language)?( string )$ this ->getLanguage():( string )$language;
if ($record->hasRelation( 'Translation' )) {
$record->Translation[$language][$name] = $ value ;
}
} else {
require_once 'Of/ExtDoctrine/I18n/EasyAccess/NotTranslatableException.php' ;
throw new Of_ExtDoctrine_I18n_EasyAccess_NotTranslatableException( "Field {$name} is not marked as easy setter to tranlsatable attribute in " .get_class($record));
}
}
class Product extends Doctrine_Record {
public function setTableDefinition() {
//....
$ this ->hasColumn( 'name' , 'string' , 255);
$ this ->hasColumn( 'description' , 'string' );
//....
}
public function setUp() {
$ this ->actAs( 'I18n' , array(
'fields' =>array( 'name' , 'description' )
));
$i18nFilter = new Of_ExtDoctrine_I18n_EasyAccess_Filter(array(
'language' => 'en' ,
'fields' =>array( 'name' , 'description' ) //
));
$ this ->getTable()->unshiftFilter($i18nFilter);
}
}
$record = new Product();
$record->description = 'my description' ; // $record->Translation['en']->description
echo $record->description; // echo $record->Translation[ 'en' ]->description
try {
echo $record->lol; //
} catch (Of_ExtDoctrine_I18n_EasyAccess_NotTranslatableException $e) {
//...
}
/**
* Temlate implements extrabehavior for standard Doctrine I18n template.
*
* @author OmeZ
* @version 1.7
* @license www.opensource.org/licenses/lgpl-license.php LGPL
* @package Of_ExtDoctrine_I18n_EasyAccess
*/
class Of_ExtDoctrine_I18n_EasyAccess_Helper extends Doctrine_Template implements Of_ExtDoctrine_I18n_EasyAccess_OwnerInterface {
protected $_options = array(
'language' => null ,
'fields' => null ,
'disableFilter' => false ,
);
/**
* Holds default language for all behaviors
*
* @var string
*/
static protected $_defaultLanguage;
/**
* Holds language for current model behavior
*
* @var string
*/
protected $_language;
/**
* EasyAccess filter
*
* @var Of_ExtDoctrine_I18n_EasyAccess_Filter
*/
protected $_easyaccess_filter;
public function setUp() {
$language = $ this ->getOption( 'language' );
if ($language) $ this ->setLanguage($language);
// Adds filter for access to properties, this can be used as stand-alone plugin
if (!$ this ->getOption( 'disableFilter' )) {
require_once 'Of/ExtDoctrine/I18n/EasyAccess/Filter.php' ;
$ this ->_easyaccess_filter = new Of_ExtDoctrine_I18n_EasyAccess_Filter(array(
'owner' =>$ this ,
'language' => null , // this value will make filter access to template getLanguage() method
'fields' =>$ this ->getOption( 'fields' , array())
));
$ this ->_table->unshiftFilter($ this ->_easyaccess_filter);
}
// adds listener to manage Doctrine_Query hydrations. Will add translatable props as keys in
// array when HYDRATE_ARRAY, or mapped values in records. This can be used as stand-alone plugin
}
/**
* Returns default language for all behaviors
*
* @return string
*/
static public function getDefaultLanguage() {
return ( string )self::$_defaultLanguage;
}
/**
* Sets default language for all behaviors
*
* @param string $language
* @return void
*/
static public function setDefaultLanguage($language) {
self::$_defaultLanguage = $language;
}
/**
* Returns current language behavior
*
* @return void
*/
public function getLanguage($without_static = false ) {
if ($ this ->_language === null && !$without_static) return self::getDefaultLanguage();
else return ( string )$ this ->_language;
}
/**
* Sets current behavior language
*
* @param $language
* @return string
*/
public function setLanguage($language) {
$ this ->_language = $language;
}
}
class Product extends Doctrine_Record {
public function setTableDefinition() {
//....
$ this ->hasColumn( 'name' , 'string' , 255);
$ this ->hasColumn( 'description' , 'string' );
//....
}
public function setUp() {
$ this ->actAs( 'I18n' , array(
'fields' =>array( 'name' , 'description' )
));
$ this ->actAs( new Of_ExtDoctrine_I18n_EasyAccess_Helper(array(
'fields' =>array( 'name' , 'description' ) //
)));
}
}
$record = new Product();
$record->description = 'my description' ; // $record->Translation['en']->description
echo $record->description; // echo $record->Translation['en']->description
$record->setLanguage( 'ru' ); //
echo $record->description; // 'my description', ..
Source: https://habr.com/ru/post/80330/