@ 2014-07-30T05:06:54Z <?php
/**
* DbTable based preference persistence adapter
*
* @package ManipleCore_Prefs
* @uses Zefram_Db_Table
* @uses Zend_Serializer
* @author xemlock
* @version 2014-07-30
*/
class ManipleCore_Prefs_Adapter_DbTable
implements ManipleCore_Prefs_AdapterInterface
{
/**
* @var ManipleCore_Prefs_Adapter_DbTable_UserPrefs
*/
protected $_table;
/**
* @var Zend_Serializer_Adapter_Interface
*/
protected $_serializer;
/**
* Constructor.
*
* @param Zefram_Db_Table_FactoryInterface $factory
* @param Zend_Serializer_Adapter_Interface|string $serializer
* @return void
*/
public function __construct(Zefram_Db_Table_FactoryInterface $factory, $serializer = null)
{
$this->_table = $factory->getTable('ManipleCore_Prefs_Adapter_DbTable_UserPrefs');
if ($serializer !== null) {
if (!$serializer instanceof Zend_Serializer_Adapter_Interface) {
$serializer = Zend_Serializer::factory($serializer);
}
$this->_serializer = $serializer;
}
}
/**
* Load user preference from database.
*
* @param int|string $userId
* @param string $name
* @return mixed
*/
public function loadUserPref($userId, $name)
{
$name = (string) $name;
$row = $this->_table->fetchRow(array(
'user_id = ?' => $userId,
'pref_name = ?' => $name,
));
if ($row) {
return $this->_unserialize($row->pref_value);
}
return null;
}
/**
* Load user preferences matching given prefix or, if none given,
* all user preferences.
*
* @param int|string $userId
* @param string $prefix OPTIONAL
* @return array
*/
public function loadUserPrefs($userId, $prefix = null)
{
$where = array('user_id = ?' => $userId);
if ($prefix !== null) {
$prefix = str_replace(array('%', '_', '^', '[', ']'), '', $prefix);
$where['pref_name LIKE ?'] = $prefix . '%';
}
$prefs = array();
foreach ($this->_table->fetchAll($where) as $row) {
$prefs[$row->pref_name] = $this->_unserialize($row->pref_value);
}
return $prefs;
}
/**
* Save single user preference.
*
* @param int|string $userId
* @param string $name
* @param mixed $value
* @return ManipleCore_Prefs_Adapter_DbTable
*/
public function saveUserPref($userId, $name, $value)
{
$name = (string) $name;
$this->_table->delete(array(
'user_id = ?' => $userId,
'pref_name = ?' => $name,
));
$this->_table->insert(array(
'user_id' => $userId,
'pref_name' => $name,
'pref_value' => $this->_serialize($value),
));
return $this;
}
/**
* Save multiple user preferences.
*
* @param int|string $userId
* @param array $prefs
* @return ManipleCore_Prefs_Adapter_DbTable
*/
public function saveUserPrefs($userId, array $prefs)
{
$this->_table->delete(array(
'user_id = ?' => $userId,
'pref_name IN (?)' => array_map('strval', array_keys($prefs)),
));
foreach ($prefs as $name => $value) {
$name = (string) $name;
$this->_table->insert(array(
'user_id' => $userId,
'pref_name' => $name,
'pref_value' => $this->_serialize($value),
));
}
return $this;
}
/**
* @param mixed $value
* @return mixed
*/
protected function _unserialize($value)
{
if ($this->_serializer) {
try {
$value = $this->_serializer->unserialize($value);
} catch (Exception $e) {
$value = null;
}
}
return $value;
}
/**
* @param mixed $value
* @return string
*/
protected function _serialize($value)
{
if ($this->_serializer) {
$value = $this->_serializer->serialize($value);
} elseif (is_bool($value)) {
$value = (int) $value;
} elseif (is_float($value)) {
// do not cast to float, as during conversion from float to string
// E notation is used for big numbers
$value = sprintf('%F', $value);
}
return (string) $value;
}
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Fatal error: Uncaught Error: Interface "ManipleCore_Prefs_AdapterInterface" not found in /in/I5KMt:12
Stack trace:
#0 {main}
thrown in /in/I5KMt on line 12
Process exited with code 255 . Output for 7.4.0 - 7.4.33 Fatal error: Uncaught Error: Interface 'ManipleCore_Prefs_AdapterInterface' not found in /in/I5KMt:12
Stack trace:
#0 {main}
thrown in /in/I5KMt on line 12
Process exited with code 255 . Output for 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 Fatal error: Interface 'ManipleCore_Prefs_AdapterInterface' not found in /in/I5KMt on line 12
Process exited with code 255 . Output for 5.3.0 - 5.3.29 , 5.4.0 - 5.4.45 , 5.5.0 - 5.5.38 , 5.6.0 - 5.6.40 Fatal error: Interface 'ManipleCore_Prefs_AdapterInterface' not found in /in/I5KMt on line 14
Process exited with code 255 . Output for 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 Fatal error: Interface 'ManipleCore_Prefs_AdapterInterface' not found in /in/I5KMt on line 13
Process exited with code 255 . Output for 5.0.0 - 5.0.5 Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/I5KMt on line 118
Process exited with code 255 . Output for 4.4.2 - 4.4.9 Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/I5KMt on line 13
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 Parse error: parse error, unexpected T_STRING, expecting '{' in /in/I5KMt on line 13
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error, expecting `'{'' in /in/I5KMt on line 13
Process exited with code 255 . preferences:dark mode live preview
336.68 ms | 401 KiB | 456 Q