public function testPopulate() { // Properties $properties = array('var' => 'value1', 'variableTwo' => 'value2', 'private' => 'value3', 'hidden' => 'value4'); $bean = new Bean(); \Phruts\Util\BeanUtils::populate($bean, $properties); $this->assertEquals('value1', $bean->getVar()); $this->assertEquals('value2', $bean->variableTwo); $this->setExpectedException('\\Exception'); \Phruts\Util\BeanUtils::populate(null, $properties); }
/** * Populate the properties of the specified PHPBean from the specified HTTP * request, based on matching each parameter name (plus an optional prefix * and/or suffix) against the corresponding PHPBeans "property setter" * methods in the bean's class. * * If you specify a non-null prefix and non-null suffix, the parameter name * must match <b>both</b> conditions for its value(s) to be used in populating * bean properties. * * @param object $bean The PHPBean whose properties are to be set * @param string $prefix The prefix (if any) to be prepend to bean property * names when looking for matching parameters * @param string $suffix The suffix (if any) to be appended to bean property * names when looking for matching parameters * @param \Symfony\Component\HttpFoundation\Request $request The HTTP request whose parameters * are to be used to populate bean properties * @throws \Phruts\Exception - If an exception is thrown while setting * property values */ public static function populate($bean, $prefix, $suffix, \Symfony\Component\HttpFoundation\Request $request) { $prefix = (string) $prefix; $suffix = (string) $suffix; $prefixLength = strlen($prefix); $suffixLength = strlen($suffix); // Build a list of revelant request parameters from this request $properties = self::treatRequestProperties($request, $prefix, $suffix, $request->query->keys()); $properties = array_merge($properties, self::treatRequestProperties($request, $prefix, $suffix, $request->request->keys())); // Set the corresponding properties of our bean try { \Phruts\Util\BeanUtils::populate($bean, $properties); } catch (\Exception $e) { throw new \Phruts\Exception('\\Phruts\\Util\\BeanUtils->populate() - ' . $e->getMessage()); } }
protected function initModulePlugIns(\Phruts\Config\ModuleConfig $config) { if (!empty($this->log)) { $this->log->debug('Initializing module "' . $config->getPrefix() . '" plug ins'); } $plugInConfigs = $config->findPlugInConfigs(); $plugIns = array(); foreach ($plugInConfigs as $plugInConfig) { /* @var $plugInConfig \Phruts\Config\PlugInConfig */ try { /* @var $plugIn \Phruts\Action\PlugInInterface */ $plugIn = \Phruts\Util\ClassLoader::newInstance($plugInConfig->getClassName(), '\\Phruts\\Action\\PlugInInterface'); \Phruts\Util\BeanUtils::populate($plugIn, $plugInConfig->getProperties()); $plugIn->init($this, $config); $plugIns[] = $plugIn; } catch (\Exception $e) { $msg = $this->getInternal()->getMessage(null, 'plugIn.init', $plugInConfig->getClassName()); if (!empty($this->log)) { $this->log->error($msg . ' - ' . $e->getMessage()); } throw new \Phruts\Exception($msg); } } $this->application[\Phruts\Util\Globals::PLUG_INS_KEY . $config->getPrefix()] = $plugIns; }