/** * @param string $name * @param mixed $default optional default value * @return mixed */ public function get($name, $default = null) { if ($this->offsetExists($name)) { return parent::offsetGet($name); } return $default; }
/** * * @param object $translator * @param string $locale * @param string $textDomain * @return array */ public static function getMessages($translator, $locale = null, $textDomain = 'default') { if (is_array($translator)) { return $translator; } if (is_object($translator) && method_exists($translator, 'getMessages')) { return $translator->getMessages($locale); } $translator->translate('n', 'default', $locale); $messages = var_export($translator, true); #$messages = preg_replace('#(\w+\\\\)*\w+\:\:__set_state#ui', 'array', $messages); $messages = preg_replace('#([\\w\\\\]+::__set_state)#', 'array', $messages); $messages = '?><?php return ' . $messages . ';'; $messages = eval($messages); $messages = EhrlichAndreas_Util_Array::objectToArray($messages); $messages = $messages[0]['messages']; $messagesTmp = $messages; foreach ($messagesTmp as $line1 => $array1) { foreach ($array1 as $line2 => $array2) { $messagesTmp[$line1][$line2] = array(); foreach ($array2 as $array3) { $messagesTmp[$line1][$line2] = array_merge($messagesTmp[$line1][$line2], $array3); } } } $messages = $messagesTmp; if (!isset($messages[$textDomain])) { return array(); } $messages = $messages[$textDomain]; if (!is_null($locale) && !isset($messages[$locale])) { return array(); } if (!is_null($locale)) { $messages = $messages[$locale]; } return $messages; }
/** * * @param array $options * @param int $zfVersion * @return transportName * @throws Zend_Application_Resource_Exception */ protected function setupTransport($options = array(), $zfVersion = 0) { $options = EhrlichAndreas_Util_Array::objectToArray($options); $type = ''; $name = ''; $host = ''; $port = ''; $auth = ''; $username = ''; $password = ''; $ssl = ''; $path = ''; $callback = ''; if (isset($options['type'])) { $type = $options['type']; } if (isset($options['smtp']) && !empty($options['smtp'])) { $type = 'smtp'; } if (isset($options['name'])) { $name = $options['name']; } if (isset($options['host'])) { $host = $options['host']; } if (isset($options['port'])) { $port = $options['port']; } if (isset($options['auth'])) { $auth = $options['auth']; } elseif (isset($options['connection_class'])) { $auth = $options['connection_class']; } if (isset($options['user'])) { $username = $options['user']; } elseif (isset($options['username'])) { $username = $options['username']; } if (isset($options['pass'])) { $password = $options['pass']; } elseif (isset($options['password'])) { $password = $options['password']; } if (isset($options['ssl'])) { $ssl = $options['ssl']; } if (isset($options['path'])) { $path = $options['path']; } if (isset($options['callback'])) { $callback = $options['callback']; } if (is_array($options)) { foreach ($options as $key => $value) { if (is_scalar($value)) { if ($type === '' && stripos($key, 'type') !== false) { $type = $value; } if ($type === '' && stripos($key, 'smtp') !== false && !empty($value)) { $type = 'smpt'; } if ($name === '' && stripos($key, 'name') !== false && stripos($key, 'host') === false && stripos($key, 'user') === false) { $name = $value; } if ($host === '' && stripos($key, 'host') !== false) { $host = $value; } if ($port === '' && stripos($key, 'port') !== false) { $port = $value; } if ($auth === '' && stripos($key, 'auth') !== false) { $auth = $value; } if ($auth === '' && stripos($key, 'connection') !== false && stripos($key, 'class') !== false) { $auth = $value; } if ($username === '' && stripos($key, 'user') !== false) { $username = $value; } if ($password === '' && stripos($key, 'pass') !== false) { $password = $value; } if ($ssl === '' && stripos($key, 'ssl') !== false) { $ssl = $value; } if ($path === '' && stripos($key, 'path') !== false) { $path = $value; } if ($callback === '' && stripos($key, 'call') !== false && stripos($key, 'back') !== false) { $callback = $value; } } elseif (($username === '' || $password === '') && stripos($key, 'connection') !== false && stripos($key, 'config') !== false) { foreach ($value as $k => $v) { if (is_scalar($v)) { if ($username === '' && stripos($k, 'user') !== false) { $username = $v; } if ($password === '' && stripos($k, 'pass') !== false) { $password = $v; } if ($ssl === '' && stripos($k, 'ssl') !== false) { $ssl = $v; } } } } } } if (empty($zfVersion)) { $zfVersion = 1; } $zfVersion = intval($zfVersion); $type = strtolower($type); $port = strtolower($port); $auth = strtolower($auth); $ssl = strtolower($ssl); if (empty($port)) { $port = 25; } if ($zfVersion == 1) { //require_once 'Zend/Loader/Autoloader.php'; $options = array('host' => $host, 'port' => $port, 'auth' => $auth, 'username' => $username, 'password' => $password); if (!empty($type) && in_array($type, array('smtp', 'file', 'sendmail'))) { $options['type'] = $type; } else { $options['type'] = 'sendmail'; } if (!empty($ssl) && in_array($ssl, array('tls', 'ssl'))) { $options['ssl'] = $ssl; } if (!empty($path)) { $options['path'] = $path; } if (!empty($callback) && is_callable($callback)) { $options['callback'] = $callback; } if (!isset($options['type'])) { $options['type'] = 'sendmail'; } $transportName = $options['type']; ob_start(); if (!Zend_Loader_Autoloader::autoload($transportName)) { $transportName = ucfirst(strtolower($transportName)); if (!Zend_Loader_Autoloader::autoload($transportName)) { $transportName = 'Zend_Mail_Transport_' . $transportName; if (!Zend_Loader_Autoloader::autoload($transportName)) { throw new Zend_Application_Resource_Exception("Specified Mail Transport '{$transportName}'" . 'could not be found'); } } } ob_end_clean(); unset($options['type']); switch ($transportName) { case 'Zend_Mail_Transport_Smtp': if (!isset($options['host'])) { throw new Zend_Application_Resource_Exception('A host is necessary for smtp transport,' . ' but none was given'); } $optionsTmp = $options; unset($optionsTmp['smtp']); unset($optionsTmp['host']); if (!isset($optionsTmp['name'])) { $optionsTmp['name'] = '127.0.0.1'; } $transport = new $transportName($options['host'], $optionsTmp); break; case 'Zend_Mail_Transport_Sendmail': default: $options['port'] = ''; $transport = new $transportName($options); break; } return $transport; } elseif ($zfVersion == 2) { switch ($type) { case 'smtp': $options = array('name' => $name, 'host' => $host, 'port' => $port, 'connection_class' => $auth, 'connection_config' => array('username' => $username, 'password' => $password)); if (!empty($ssl) && in_array($ssl, array('tls', 'ssl'))) { $options['connection_config']['ssl'] = $ssl; } $classSmtpOptions = 'Zend\\Mail\\Transport\\SmtpOptions'; $transportOptions = new $classSmtpOptions(); $transportOptions->setFromArray($options); $classSmtp = 'Zend\\Mail\\Transport\\Smtp'; $transport = new $classSmtp(); $transport->setOptions($transportOptions); break; case 'file': $options = array('path' => $path); if (!empty($callback) && is_callable($callback)) { $options['callback'] = $callback; } $classFileOptions = 'Zend\\Mail\\Transport\\FileOptions'; $transportOptions = new $classFileOptions(); $transportOptions->setFromArray($options); $classFile = 'Zend\\Mail\\Transport\\File'; $transport = new $classFile(); $transport->setOptions($transportOptions); break; default: $classSendmail = 'Zend\\Mail\\Transport\\Sendmail'; $transport = new $classSendmail(); break; } return $transport; } }
/** * Instantiates route based on passed config structure * * @param array $config Configuration object */ public static function getInstance($config) { $config = EhrlichAndreas_Util_Array::objectToArray($config); $route = ''; $defs = array(); $map = array(); $reverse = null; $callbacks = array(); if (isset($config['route'])) { $route = $config['route']; } if (isset($config['defaults'])) { $defs = $config['defaults']; } if (isset($config['map'])) { $map = $config['map']; } if (isset($config['reverse'])) { $reverse = $config['reverse']; } if (isset($config['callbacks'])) { $callbacks = $config['callbacks']; } return new self($route, $defs, $map, $reverse, $callbacks); }
public function runByParameter($invokeParams = null) { if (is_null($invokeParams)) { return false; } if (!EhrlichAndreas_Util_Object::isInstanceOf($invokeParams, 'EhrlichAndreas_Mvc_Request')) { $invokeParams = EhrlichAndreas_Util_Array::objectToArray($invokeParams); $request = new EhrlichAndreas_Mvc_Request(); $this->getRouter()->setRequestParams($request, $invokeParams); } else { $request = $invokeParams; } $class = implode('_', array(ucfirst($request->getModuleName()), ucfirst($request->getSubmoduleName()), ucfirst($request->getControllerName()) . 'Controller')); $action = $request->getActionName(); $controller = new $class($request); $controller->setView($this->getView()); return $controller->dispatch($action); }
/** * * @staticvar array $names * @staticvar array $_prefix * @staticvar array $_prefixLength * @staticvar array $_prefixLengthMax * @staticvar array $_prefixOffset * @staticvar array $_suffix * @staticvar array $_suffixLength * @staticvar array $_suffixLengthMax * @staticvar array $_suffixOffset * @param type $table * @param type $key * @return string */ public function getTableNameMd5($table = '', $key = '') { /*static*/ $names = array(); /*static*/ $_prefix = array(); /*static*/ $_prefixLength = array(); /*static*/ $_prefixLengthMax = array(); /*static*/ $_prefixOffset = array(); /*static*/ $_suffix = array(); /*static*/ $_suffixLength = array(); /*static*/ $_suffixLengthMax = array(); /*static*/ $_suffixOffset = array(); if (!array_key_exists($table, $_prefix)) { $prefix = $this->getTablePrefix($table); $_prefix[$table] = $prefix; } else { $prefix = $_prefix[$table]; } if (!array_key_exists($table, $_prefixLength)) { $prefixLength = $this->getTablePrefixLength($table); if ($prefixLength < 0) { $prefixLength = 0; } $_prefixLength[$table] = $prefixLength; } else { $prefixLength = $_prefixLength[$table]; } if (!array_key_exists($table, $_prefixLengthMax)) { $prefixLengthMax = $this->getTablePrefixLengthMax($table); if ($prefixLengthMax < $prefixLength) { $prefixLengthMax = $prefixLength; } $_prefixLengthMax[$table] = $prefixLengthMax; } else { $prefixLengthMax = $_prefixLengthMax[$table]; } if (!array_key_exists($table, $_prefixOffset)) { $prefixOffset = $this->getTablePrefixOffset($table); if ($prefixOffset < 0) { $prefixOffset = 0; } $_prefixOffset[$table] = $prefixOffset; } else { $prefixOffset = $_prefixOffset[$table]; } if (!array_key_exists($table, $_suffix)) { $suffix = $this->getTableSuffix($table); $_suffix[$table] = $suffix; } else { $suffix = $_suffix[$table]; } if (!array_key_exists($table, $_suffixLength)) { $suffixLength = $this->getTableSuffixLength($table); if ($suffixLength < 0) { $suffixLength = 0; } $_suffixLength[$table] = $suffixLength; } else { $suffixLength = $_suffixLength[$table]; } if (!array_key_exists($table, $_suffixLengthMax)) { $suffixLengthMax = $this->getTableSuffixLengthMax($table); if ($suffixLengthMax < $suffixLength) { $suffixLengthMax = $suffixLength; } $_suffixLengthMax[$table] = $suffixLengthMax; } else { $suffixLengthMax = $_suffixLengthMax[$table]; } if (!array_key_exists($table, $_suffixOffset)) { $suffixOffset = $this->getTableSuffixOffset($table); if ($suffixOffset < 0) { $suffixOffset = 0; } $_suffixOffset[$table] = $suffixOffset; } else { $suffixOffset = $_suffixOffset[$table]; } $key = EhrlichAndreas_Util_Array::objectToArray($key); if (is_array($key)) { $key = implode('-', $key); } $key = strval($key); $keyMD5 = md5($key); $varPrefix = ''; if ($prefixLength > 0) { $varPrefix = $keyMD5; $varPrefix = substr($varPrefix, $prefixOffset, $prefixLength); $varPrefix = str_repeat('0', $prefixLengthMax - $prefixLength) . $varPrefix; } $varSuffix = ''; if ($suffixLength > 0) { $varSuffix = $keyMD5; $varSuffix = substr($varSuffix, $suffixOffset, $suffixLength); $varSuffix = str_repeat('0', $suffixLengthMax - $suffixLength) . $varSuffix; } $index = $table; $index .= '-' . $varPrefix . '-' . $prefix . '-' . $prefixLength . '-' . $prefixLengthMax . '-' . $prefixOffset; $index .= '-' . $varSuffix . '-' . $suffix . '-' . $suffixLength . '-' . $suffixLengthMax . '-' . $suffixOffset; if (isset($names[$index])) { return $names[$index]; } $varTable = ''; if (strlen($prefix) > 0) { if (strlen($varTable) > 0) { $varTable = $varTable . '_' . $prefix; } else { $varTable = $prefix; } } if (strlen($varPrefix) > 0) { if (strlen($varTable) > 0) { $varTable = $varTable . '_' . $varPrefix; } else { $varTable = $varPrefix; } } if (strlen($varTable) > 0) { $varTable = $varTable . '_' . $table; } else { $varTable = $table; } if (strlen($suffix) > 0) { if (strlen($varTable) > 0) { $varTable = $varTable . '_' . $suffix; } else { $varTable = $suffix; } } if (strlen($varSuffix) > 0) { if (strlen($varTable) > 0) { $varTable = $varTable . '_' . $varSuffix; } else { $varTable = $varSuffix; } } $names[$index] = $varTable; return $varTable; }
/** * Assigns variables to the view script via differing strategies. * * EhrlichAndreas_Mvc_View::assign('name', $value) assigns a variable * called 'name' with the corresponding $value. * * EhrlichAndreas_Mvc_View::assign($array) assigns the array keys * as variable names (with the corresponding array values). * * @see __set() * @param string|array The assignment strategy to use. * @param mixed (Optional) If assigning a named variable, use this * as the value. * @return EhrlichAndreas_Mvc_View Fluent interface * @throws EhrlichAndreas_Mvc_Exception if $spec is neither a string * nor an array, or if an attempt to set a private or protected member * is detected */ public function assign($spec, $value = null) { // which strategy to use? if (is_object($spec)) { $spec = EhrlichAndreas_Util_Array::objectToArray($spec); } if (is_string($spec)) { // assign by name and value $this->__set($spec, $value); } elseif (is_array($spec)) { // assign from associative array foreach ($spec as $key => $val) { $this->__set($key, $val); } } else { $message = 'assign() expects a string or array, received ' . gettype($spec); $e = new EhrlichAndreas_Mvc_Exception($message); throw $e; } return $this; }
/** * Create routes out of array configuration * * Example INI: * routes.archive.route = "archive/:year/*" * routes.archive.defaults.controller = archive * routes.archive.defaults.action = show * routes.archive.defaults.year = 2000 * routes.archive.reqs.year = "\d+" * * routes.news.type = "EhrlichAndreas_Mvc_Route" * routes.news.route = "news" * routes.news.defaults.controller = "news" * routes.news.defaults.action = "list" * * And finally after you have created an config array with above ini: * $router = new EhrlichAndreas_Mvc_Router(); * $router->addConfig($config, 'routes'); * * @param array $config Configuration object * @param string $section Name of the config section containing route's definitions * @throws EhrlichAndreas_Mvc_Exception * @return EhrlichAndreas_Mvc_Router */ public function addConfig($config, $section = null) { if ($section !== null) { $config = EhrlichAndreas_Util_Array::objectToArray($config); if (!isset($config[$section]) || is_null($config[$section])) { throw new EhrlichAndreas_Mvc_Exception("No route configuration in section '{$section}'"); } $config = $config[$section]; } foreach ($config as $name => $info) { $route = $this->_getRouteFromConfig($info); $this->addRoute($name, $route); } return $this; }
/** * Constructs a parent ArrayObject with default * ARRAY_AS_PROPS to allow acces as an object * * @param array $array data array * @param integer $flags ArrayObject flags */ public function __construct($array = array(), $flags = parent::ARRAY_AS_PROPS) { parent::__construct($array, $flags); }