/** * Test the Notices::add_unique(...) method * * @test * @dataProvider providerAddUnique */ public function testAddUnique($type, $message, $persist, $allowed) { // Put an initial Notice in the Notices queue $original = Notices::add('success', 'You have succeeded!', FALSE); // Try to create a unique Notice $result = Notices::add_unique($type, $message, $persist); // Retrieve the Notices queue $notices = Session::instance()->get('notices', array()); // Make sure that uniqueness is enforced $this->assertSame($allowed, (bool) $result); // If it was unique, perform assertions similar to testAdd if ($result) { // Make sure the result is a Notice $this->assertTrue($result instanceof Notice); // Make sure the result was added to the session $this->assertTrue(isset($notices[$result->hash])); // Make sure the notice in the session matches the result $this->assertTrue($result->similar_to($notices[$result->hash])); } }
/** * Return messages recorded by this object * * @param string|array $options One or more of array elements or space separated string of: * first: only first item will be returned (string) * last: only last item will be returned (string) * all: include all items of type (messages or errors) beyond the scope of this object * clear: clear out all items that are returned from this method (includes both local and global) * errors: returns errors rather than messages. * @return Notices|string Array of NoticeError error messages or string if last, first or str option was specified. * */ public function messages($options = array()) { if (!is_array($options)) { $options = explode(' ', strtolower($options)); } $type = in_array('errors', $options) ? 'errors' : 'messages'; $clear = in_array('clear', $options); if (in_array('all', $options)) { // get all of either messages or errors (either in or out of this object instance) $value = new Notices(); foreach ($this->wire('notices') as $notice) { if ($notice->getName() != $type) { continue; } $value->add($notice); if ($clear) { $this->wire('notices')->remove($notice); } // clear global } if ($clear) { $this->_notices[$type] = null; } // clear local } else { // get messages or errors specific to this object instance $value = is_null($this->_notices[$type]) ? new Notices() : $this->_notices[$type]; if (in_array('first', $options)) { $value = $clear ? $value->shift() : $value->first(); } else { if (in_array('last', $options)) { $value = $clear ? $value->pop() : $value->last(); } else { if ($clear) { $this->_notices[$type] = null; } } } if ($clear && $value) { $this->wire('notices')->removeItems($value); } // clear from global notices } return $value; }
/** * The `__callStatic()` allows the creation of notices using the shorter * syntax: `Notices::success('message');` This works for PHP 5.3+ only * * @param string $method Method name * @param array $args method arguments * @return mixed */ public static function __callStatic($method, $args) { if (strpos($method, Notices::UNIQUE_PREFIX) === 0) { return Notices::add_unique(substr($method, strlen(Notices::UNIQUE_PREFIX)), Arr::get($args, 0), Arr::get($args, 1), Arr::get($args, 2)); } else { return Notices::add($method, Arr::get($args, 0), Arr::get($args, 1), Arr::get($args, 2)); } }
/** * Adds a new Notice and sends the rendered HTML as a repsonse * * @ajax * @param string type * @param string message * @param string persist */ public function action_add() { if (!Request::$is_ajax) { throw new Kohana_Request_Exception('Trying to access an AJAX method without AJAX.'); } $type = strtolower(urldecode($this->request->param('type'))); $message = urldecode($this->request->param('message')); $persist = (bool) $this->request->param('persist'); $response = array('status' => 'success', 'message' => 'A Notice was added.', 'data' => NULL); try { $type = urldecode($type); $message = urldecode($message); $persist = (bool) $persist == 'TRUE'; $notice = Notices::add($type, $message, $persist); $response['message'] = 'Notice ' . $notice->hash . ' was added.'; $response['data'] = $notice->render(); Notices::save(); } catch (Exception $e) { $response['status'] = 'error'; $response['message'] = 'The was a problem adding the Notice.'; } $this->request->response = json_encode($response); }