/** * Attaches the defined closure object to the $__objectMap, as well as saving its options to $_classes. * * @uses Titon\Utility\Inflector * * @param array|string $options { * @type string $alias The alias name to use for object linking * @type string $class The fully qualified class name to use for instantiation * @type bool $register Should the instance be stored in Registry * @type bool $callback Should event callbacks be allowed * @type string $interface The fully qualified interface name that all all attachments must inherit * } * @param object|callable $object * @return $this * @throws \Titon\Common\Exception\InvalidObjectException */ public function attachObject($options, $object = null) { if (is_string($options)) { $options = ['alias' => $options]; } $options = $options + ['alias' => null, 'class' => null, 'register' => true, 'callback' => true, 'interface' => null]; if (empty($options['alias'])) { throw new InvalidObjectException('You must define an alias to reference the attached object'); } else { if ($object === null && empty($options['class'])) { throw new InvalidObjectException(sprintf('You must supply an object, Closure or a class name for %s', $options['alias'])); } else { $options['alias'] = Inflector::variable($options['alias']); } } // If closure if ($object instanceof Closure) { $this->__objectMap[$options['alias']] = $object; // If object } else { if (is_object($object)) { $options['class'] = get_class($object); $this->_attached[$options['alias']] =& $object; } } $this->_classes[$options['alias']] = $options; return $this; }
/** * Attaches the defined closure object to the $__objectMap, as well as saving its options to $_classes. * * @access public * @param string|array $options * @param Closure $object * @return void */ public final function attachObject($options, Closure $object) { if (is_string($options)) { $options = array('alias' => $options); } $options = $options + array('alias' => null, 'namespace' => null, 'callback' => true); if (empty($options['alias'])) { throw new Exception('You must define an alias to reference the passed object.'); } else { $options['alias'] = Inflector::variable($options['alias']); } $this->_classes[$options['alias']] = $options; $this->__objectMap[$options['alias']] = $object; }
/** * Set a variable to the view. The variable name will be inflected if it is invalid. * * @access public * @param string|array $keys * @param mixed $value * @return boolean */ public final function set($keys, $value = null) { if (!is_array($keys)) { $keys = array($keys => $value); } foreach ($keys as $key => $value) { $this->_config['data'][Inflector::variable($key)] = $value; } return true; }
/** * Test that variable() returns strings as acceptable $variable names. */ public function testVariable() { $this->assertEquals('camelCase', Inflector::variable('camel Case')); $this->assertEquals('StuDlyCaSe', Inflector::variable('StuDly CaSe')); $this->assertEquals('TitleCase', Inflector::variable('Title Case')); $this->assertEquals('Normalcase', Inflector::variable('Normal case')); $this->assertEquals('lowercase', Inflector::variable('lowercase')); $this->assertEquals('UPPERCASE', Inflector::variable('UPPERCASE')); $this->assertEquals('under_score', Inflector::variable('under_score')); $this->assertEquals('dashes', Inflector::variable('dash-es')); $this->assertEquals('_123numbers', Inflector::variable('123 numbers')); $this->assertEquals('withEXTxml', Inflector::variable('with EXT.xml')); $this->assertEquals('lotsofwhitespace', Inflector::variable('lots of white space')); }