/** * Supports a simple form of Fluent Interfaces. Allows you to assign variables to the view by using the variable * name as the method name. If the method name is a setter method the setter will be called instead. * * For example : $view->data(array('foo' => 'bar'))->title('name')->render() * * @param string $method Method name * @param array $args Array containing all the arguments for the original call * @return KViewAbstract * * @see http://martinfowler.com/bliki/FluentInterface.html */ public function __call($method, $args) { if (!isset($this->_mixed_methods[$method])) { //If one argument is passed we assume a setter method is being called if (count($args) == 1) { if (!method_exists($this, 'set' . ucfirst($method))) { $this->{$method} = $args[0]; return $this; } else { return $this->{'set' . ucfirst($method)}($args[0]); } } //Check if a behavior is mixed $parts = KStringInflector::explode($method); if ($parts[0] == 'is' && isset($parts[1])) { return false; } } return parent::__call($method, $args); }
/** * Execute a controller action by it's name. * * Function is also capable of checking is a behavior has been mixed successfully using is[Behavior] function. If * the behavior exists the function will return TRUE, otherwise FALSE. * * @param string $method Method name * @param array $args Array containing all the arguments for the original call * @return mixed * @see execute() */ public function __call($method, $args) { //Handle action alias method if (in_array($method, $this->getActions())) { //Get the data $data = !empty($args) ? $args[0] : array(); //Create a context object if (!$data instanceof KCommandInterface) { $context = $this->getContext(); //Store the parameters in the context $context->param = $data; //Force the result to false before executing $context->result = false; } else { $context = $data; } //Execute the action return $this->execute($method, $context); } if (!isset($this->_mixed_methods[$method])) { //Check if a behavior is mixed $parts = KStringInflector::explode($method); if ($parts[0] == 'is' && isset($parts[1])) { return false; } } return parent::__call($method, $args); }
/** * Forward the call to the current row * * Search the mixin method map and call the method or forward the call to each row * * This function implements a just in time mixin strategy. Available table behaviors are only mixed when needed. * Lazy mixing is triggered by calling KDatabaseRowTable::is[Behaviorable](); * * @param string $method The function name * @param array $arguments The function arguments * @throws \BadMethodCallException If method could not be found * @return mixed The result of the function */ public function __call($method, $arguments) { $result = null; if ($this->isConnected()) { $parts = KStringInflector::explode($method); //Check if a behavior is mixed if ($parts[0] == 'is' && isset($parts[1])) { $row = $this->getIterator()->current(); if ($row && !in_array($method, $row->getMethods())) { //Lazy mix behaviors $behavior = strtolower($parts[1]); if ($row->getTable()->hasBehavior($behavior)) { $row->mixin($row->getTable()->getBehavior($behavior)); } else { return false; } } } } if ($row = $this->getIterator()->current()) { // Call_user_func_array is ~3 times slower than direct method calls. switch (count($arguments)) { case 0: $result = $row->{$method}(); break; case 1: $result = $row->{$method}($arguments[0]); break; case 2: $result = $row->{$method}($arguments[0], $arguments[1]); break; case 3: $result = $row->{$method}($arguments[0], $arguments[1], $arguments[2]); break; default: // Resort to using call_user_func_array for many segments $result = call_user_func_array(array($row, $method), $arguments); } } return $result; }
/** * Add a command by it's name * * @param string $method Method name * @param array $args Array containing all the arguments for the original call * @return mixed * @see addCommand() */ public function __call($method, $args) { $parts = KStringInflector::explode($method); if ($parts[0] == 'add' && isset($parts[1])) { $config = isset($args[0]) ? $args[0] : array(); $command = $this->addCommand(strtolower($parts[1]), $config); return $command; } return parent::__call($method, $args); }
/** * Search the behaviors to see if this table behaves as. * * Function is also capable of checking is a behavior has been mixed successfully using is[Behavior] function. * If the behavior exists the function will return TRUE, otherwise FALSE. * * @param string $method The function name * @param array $arguments The function arguments * @throws \BadMethodCallException If method could not be found * @return mixed The result of the function */ public function __call($method, $arguments) { if (!isset($this->_mixed_methods[$method])) { // If the method is of the form is[Bahavior] handle it. $parts = KStringInflector::explode($method); if ($parts[0] == 'is' && isset($parts[1])) { return false; } } return parent::__call($method, $arguments); }
/** * @dataProvider provideNames */ public function testExplode($classified, $separator, $split, $exploded) { $this->assertEquals(KStringInflector::explode($classified), $exploded); }
/** * Search the mixin method map and call the method or trigger an error * * This function implements a just in time mixin strategy. Available table behaviors are only mixed when needed. * Lazy mixing is triggered by calling KDatabaseRowsetTable::is[Behaviorable](); * * @param string $method The function name * @param array $arguments The function arguments * @throws \BadMethodCallException If method could not be found * @return mixed The result of the function */ public function __call($method, $arguments) { if ($this->isConnected()) { $parts = KStringInflector::explode($method); //Check if a behavior is mixed if ($parts[0] == 'is' && isset($parts[1])) { if (!isset($this->_mixed_methods[$method])) { //Lazy mix behaviors $behavior = strtolower($parts[1]); if ($this->getTable()->hasBehavior($behavior)) { $this->mixin($this->getTable()->getBehavior($behavior)); } else { return false; } } } } return parent::__call($method, $arguments); }