public function testAdd() { $this->eventSystem->add(function (Message $message) { return isset($message); }, function (Message $message) { return isset($message); }); $this->assertAttributeNotEmpty('events', $this->eventSystem); }
/** * Use this method to add command. Parameters will be automatically parsed and passed to closure. * * @param string $name * @param \Closure $action * * @return \Pathetic\TgBot\Bot */ public function command($name, Closure $action) { $check = function (Message $message) use($name) { if (!isset($message->text)) { return false; } $regexp = '/^\\/([^\\s@]+)(@\\S+)?\\s?(.*)$/'; preg_match($regexp, $message->text, $matches); return !empty($matches) && $matches[1] == $name; }; $event = function (Message $message) use($action) { $regexp = '/^\\/([^\\s@]+)(@\\S+)?\\s?(.*)$/'; preg_match($regexp, $message->text, $matches); if (isset($matches[3]) && !empty($matches[3])) { $parameters = str_getcsv($matches[3], chr(32)); } else { $parameters = []; } array_unshift($parameters, $message); $action = new ReflectionFunction($action); if (count($parameters) >= $action->getNumberOfRequiredParameters()) { $action->invokeArgs($parameters); } return false; }; $this->events->add($check, $event); return $this; }