本文整理汇总了PHP中when函数的典型用法代码示例。如果您正苦于以下问题:PHP when函数的具体用法?PHP when怎么用?PHP when使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了when函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: shouldReturnAJsonRepresentationOfASession
/**
* @test
* @group mocking
*/
public function shouldReturnAJsonRepresentationOfASession()
{
$session = mock('WhiteboardSession');
when($session)->info()->thenReturn(array('pretend-that' => 'this is a valid session info'));
when($this->request)->getParameter('token')->thenReturn('aTokenValue');
when($this->whiteboardSessionPeer)->loadByToken('aTokenValue')->thenReturn($session);
$this->actions->executeInfo($this->request);
$json = $this->getResponseContents();
$this->assertEquals('{"pretend-that":"this is a valid session info"}', $json);
}
示例2: __invoke
function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
$this->redirection->setRequest($request);
$base = $this->settings->urlPrefix();
$base = $base ? "{$base}..." : '*';
return $this->router->set([$base => [when($this->settings->requireAuthentication(), AuthenticationMiddleware::class), '.' => page('platform/home.html'), 'settings...' => [when($this->settings->enableUsersManagement(), ['users-management...' => ['users' => factory(function (UsersPage $page) {
// This is done here just to show off this possibility
$page->templateUrl = 'platform/users/users.html';
return $page;
}), 'users/@id' => UserPage::class, 'profile' => factory(function (UserPage $page) {
$page->editingSelf = true;
return $page;
})]])]]])->__invoke($request, $response, $next);
}
示例3: loop_when
public static function loop_when()
{
$j = self::$lista->from();
$k = 0;
do {
if ($j < self::$lista->getSize()) {
when(self::$lista, $j);
$j = $j + 1;
$k = $k + 1;
} else {
$k = self::$lista->getQuantidadePorPagina();
}
} while ($k < self::$lista->getQuantidadePorPagina());
}
示例4: create
/**
* Function: create
* Attempts to create a comment using the passed information. If a Defensio API key is present, it will check it.
*
* Parameters:
* $author - The name of the commenter.
* $email - The commenter's email.
* $url - The commenter's website.
* $body - The comment.
* $post - The <Post> they're commenting on.
* $type - The type of comment. Optional, used for trackbacks/pingbacks.
*/
static function create($author, $email, $url, $body, $post, $type = null)
{
if (!self::user_can($post->id) and !in_array($type, array("trackback", "pingback"))) {
return;
}
$config = Config::current();
$route = Route::current();
$visitor = Visitor::current();
if (!$type) {
$status = $post->user_id == $visitor->id ? "approved" : $config->default_comment_status;
$type = "comment";
} else {
$status = $type;
}
if (!empty($config->defensio_api_key)) {
$comment = array("user-ip" => $_SERVER['REMOTE_ADDR'], "article-date" => when("Y/m/d", $post->created_at), "comment-author" => $author, "comment-type" => $type, "comment-content" => $body, "comment-author-email" => $email, "comment-author-url" => $url, "permalink" => $post->url(), "referrer" => $_SERVER['HTTP_REFERER'], "user-logged-in" => logged_in());
$defensio = new Defensio($config->url, $config->defensio_api_key);
list($spam, $spaminess, $signature) = $defensio->auditComment($comment);
if ($spam) {
self::add($body, $author, $url, $email, $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT'], "spam", $signature, null, null, $post, $visitor->id);
error(__("Spam Comment"), __("Your comment has been marked as spam. It will have to be approved before it will show up.", "comments"));
} else {
$comment = self::add($body, $author, $url, $email, $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT'], $status, $signature, null, null, $post, $visitor->id);
fallback($_SESSION['comments'], array());
$_SESSION['comments'][] = $comment->id;
if (isset($_POST['ajax'])) {
exit("{ comment_id: " . $comment->id . ", comment_timestamp: \"" . $comment->created_at . "\" }");
}
Flash::notice(__("Comment added."), $post->url() . "#comment_" . $comment->id);
}
} else {
$comment = self::add($body, $author, $url, $email, $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT'], $status, "", null, null, $post, $visitor->id);
fallback($_SESSION['comments'], array());
$_SESSION['comments'][] = $comment->id;
if (isset($_POST['ajax'])) {
exit("{ comment_id: " . $comment->id . ", comment_timestamp: \"" . $comment->created_at . "\" }");
}
Flash::notice(__("Comment added."), $post->url() . "#comment_" . $comment->id);
}
}
示例5: __construct
/**
* Function: __construct
* Prepares an array for pagination.
*
* Parameters:
* $array - The array to paginate.
* $per_page - Number of items per page.
* $name - The name of the $_GET parameter to use for determining the current page.
* $model - If this is true, each item in $array that gets shown on the page will be
* initialized as a model of whatever is passed as the second argument to $array.
* The first argument of $array is expected to be an array of IDs.
* $page - Page number to start at.
*
* Returns:
* A paginated array of length $per_page or smaller.
*/
public function __construct($array, $per_page = 5, $name = "page", $model = null, $page = null)
{
self::$names[] = $name;
$this->array = (array) $array;
$this->per_page = $per_page;
$this->name = $name;
$this->model = fallback($model, count($this->array) == 2 and is_array($this->array[0]) and is_string($this->array[1]) and class_exists($this->array[1]));
if ($model) {
list($this->array, $model_name) = $this->array;
}
$this->total = count($this->array);
$this->page = oneof($page, @$_GET[$name], 1);
$this->pages = ceil($this->total / $this->per_page);
$offset = ($this->page - 1) * $this->per_page;
$this->result = array();
if ($model) {
for ($i = $offset; $i < $offset + $this->per_page; $i++) {
if (isset($this->array[$i])) {
$this->result[] = new $model_name(null, array("read_from" => $this->array[$i]));
}
}
} else {
$this->result = array_slice($this->array, $offset, $this->per_page);
}
$shown_dates = array();
if ($model) {
foreach ($this->result as &$result) {
if (isset($result->created_at)) {
$pinned = (isset($result->pinned) and $result->pinned);
$shown = in_array(when("m-d-Y", $result->created_at), $shown_dates);
$result->first_of_day = (!$pinned and !$shown and !AJAX);
if (!$pinned and !$shown) {
$shown_dates[] = when("m-d-Y", $result->created_at);
}
}
}
}
$this->paginated = $this->paginate = $this->list =& $this->result;
}
示例6: twig_strftime_format_filter
function twig_strftime_format_filter($timestamp, $format = '%x %X')
{
return when($format, $timestamp, true);
}
示例7: function
<?php
/**
* Somewhere within your application:
*
* This is just an example of what you can do.
*/
namespace FileSystem;
class Space extends Directory
{
}
$application->bind('FileSystem\\Space', function (User $user) {
return new Space($user->allocatedSpacePath);
});
// In your business logic:
use FileSystem\File;
use FileSystem\FileSystem;
use FileSystem\Space;
when('i want to make a directory inside my space', then(apply(a(function (FileSystem $fileSystem, Space $space, File $file) {
$fileSystem->write($file, inside($space));
}))));
示例8: when
<?php
use FileSystem\File;
use FileSystem\FileSystem;
when('i want to rename a file', then(apply(a(function (FileSystem $fileSystem, File $file) {
$fileSystem->rename($file);
}))));
/*
|--------------------------------------------------------------------------
| Notes: renaming behind the scenes.
|--------------------------------------------------------------------------
|
| Behind the scenes in your technical code you call:
| $file->renameTo ( 'new name' );
|
| The business code here can choose to save it to the file
| system as it has done in the above code. Other options
| might include just ignoring the technical provided name
| and renaming the file to something else.
*/
示例9: selected
<option value="spam"<?php
selected($comment->status, "spam");
?>
><?php
echo __("Spam", "comments");
?>
</option>
</select>
</p>
<p>
<label for="created_at"><?php
echo __("Timestamp");
?>
</label>
<input class="text" type="text" name="created_at" value="<?php
echo when("F jS, Y H:i:s", $comment->created_at);
?>
" id="created_at" />
</p>
<div class="clear"></div>
</div>
<br />
<input type="hidden" name="id" value="<?php
echo fix($comment->id);
?>
" id="id" />
<input type="hidden" name="ajax" value="true" id="ajax" />
<div class="buttons">
<input type="submit" value="<?php
echo __("Update");
?>
示例10: export
/**
* Function: export
* Export posts, pages, etc.
*/
public function export()
{
if (!Visitor::current()->group->can("add_post")) {
show_403(__("Access Denied"), __("You do not have sufficient privileges to export content."));
}
if (empty($_POST)) {
return $this->display("export");
}
$config = Config::current();
$trigger = Trigger::current();
$route = Route::current();
$exports = array();
if (isset($_POST['posts'])) {
list($where, $params) = keywords($_POST['filter_posts'], "post_attributes.value LIKE :query OR url LIKE :query", "post_attributes");
if (!empty($_GET['month'])) {
$where["created_at like"] = $_GET['month'] . "-%";
}
$visitor = Visitor::current();
if (!$visitor->group->can("view_draft", "edit_draft", "edit_post", "delete_draft", "delete_post")) {
$where["user_id"] = $visitor->id;
}
$results = Post::find(array("placeholders" => true, "drafts" => true, "where" => $where, "params" => $params));
$ids = array();
foreach ($results[0] as $result) {
$ids[] = $result["id"];
}
if (!empty($ids)) {
$posts = Post::find(array("drafts" => true, "where" => array("id" => $ids), "order" => "id ASC"), array("filter" => false));
} else {
$posts = new Paginator(array());
}
$latest_timestamp = 0;
foreach ($posts as $post) {
if (strtotime($post->created_at) > $latest_timestamp) {
$latest_timestamp = strtotime($post->created_at);
}
}
$id = substr(strstr($config->url, "//"), 2);
$id = str_replace("#", "/", $id);
$id = preg_replace("/(" . preg_quote(parse_url($config->url, PHP_URL_HOST)) . ")/", "\\1," . date("Y", $latest_timestamp) . ":", $id, 1);
$posts_atom = '<?xml version="1.0" encoding="utf-8"?>' . "\r";
$posts_atom .= '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:chyrp="http://chyrp.net/export/1.0/">' . "\r";
$posts_atom .= ' <title>' . fix($config->name) . ' Posts</title>' . "\r";
$posts_atom .= ' <subtitle>' . fix($config->description) . '</subtitle>' . "\r";
$posts_atom .= ' <id>tag:' . parse_url($config->url, PHP_URL_HOST) . ',' . date("Y", $latest_timestamp) . ':Chyrp</id>' . "\r";
$posts_atom .= ' <updated>' . date("c", $latest_timestamp) . '</updated>' . "\r";
$posts_atom .= ' <link href="' . $config->url . '" rel="self" type="application/atom+xml" />' . "\r";
$posts_atom .= ' <generator uri="http://chyrp.net/" version="' . CHYRP_VERSION . '">Chyrp</generator>' . "\r";
foreach ($posts as $post) {
$title = fix($post->title(), false);
fallback($title, ucfirst($post->feather) . " Post #" . $post->id);
$updated = $post->updated ? $post->updated_at : $post->created_at;
$tagged = substr(strstr(url("id/" . $post->id), "//"), 2);
$tagged = str_replace("#", "/", $tagged);
$tagged = preg_replace("/(" . preg_quote(parse_url($post->url(), PHP_URL_HOST)) . ")/", "\\1," . when("Y-m-d", $updated) . ":", $tagged, 1);
$url = $post->url();
$posts_atom .= ' <entry xml:base="' . fix($url) . '">' . "\r";
$posts_atom .= ' <title type="html">' . $title . '</title>' . "\r";
$posts_atom .= ' <id>tag:' . $tagged . '</id>' . "\r";
$posts_atom .= ' <updated>' . when("c", $updated) . '</updated>' . "\r";
$posts_atom .= ' <published>' . when("c", $post->created_at) . '</published>' . "\r";
$posts_atom .= ' <link href="' . fix($trigger->filter($url, "post_export_url", $post)) . '" />' . "\r";
$posts_atom .= ' <author chyrp:user_id="' . $post->user_id . '">' . "\r";
$posts_atom .= ' <name>' . fix(oneof($post->user->full_name, $post->user->login)) . '</name>' . "\r";
if (!empty($post->user->website)) {
$posts_atom .= ' <uri>' . fix($post->user->website) . '</uri>' . "\r";
}
$posts_atom .= ' <chyrp:login>' . fix($post->user->login) . '</chyrp:login>' . "\r";
$posts_atom .= ' </author>' . "\r";
$posts_atom .= ' <content>' . "\r";
foreach ($post->attributes as $key => $val) {
$posts_atom .= ' <' . $key . '>' . fix($val) . '</' . $key . '>' . "\r";
}
$posts_atom .= ' </content>' . "\r";
foreach (array("feather", "clean", "url", "pinned", "status") as $attr) {
$posts_atom .= ' <chyrp:' . $attr . '>' . fix($post->{$attr}) . '</chyrp:' . $attr . '>' . "\r";
}
$trigger->filter($posts_atom, "posts_export", $post);
$posts_atom .= ' </entry>' . "\r";
}
$posts_atom .= '</feed>' . "\r";
$exports["posts.atom"] = $posts_atom;
}
if (isset($_POST['pages'])) {
list($where, $params) = keywords($_POST['filter_pages'], "title LIKE :query OR body LIKE :query", "pages");
$pages = Page::find(array("where" => $where, "params" => $params, "order" => "id ASC"), array("filter" => false));
$latest_timestamp = 0;
foreach ($pages as $page) {
if (strtotime($page->created_at) > $latest_timestamp) {
$latest_timestamp = strtotime($page->created_at);
}
}
$pages_atom = '<?xml version="1.0" encoding="utf-8"?>' . "\r";
$pages_atom .= '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:chyrp="http://chyrp.net/export/1.0/">' . "\r";
$pages_atom .= ' <title>' . fix($config->name) . ' Pages</title>' . "\r";
$pages_atom .= ' <subtitle>' . fix($config->description) . '</subtitle>' . "\r";
//.........这里部分代码省略.........
示例11: renderMenuItem
private function renderMenuItem(\Iterator $links, $xi, $parentIsActive, $depth = 1)
{
$links->rewind();
if (!$links->valid() || $depth >= $this->props->depth) {
return null;
}
return h('ul.nav.collapse.' . $this->depthClass[$depth], map($links, function (NavigationLinkInterface $link) use($xi, $depth, $parentIsActive) {
if (!$link->isActuallyVisible() || $link->isGroup() && $link->title() == '-' && $this->props->excludeDividers) {
return null;
}
$children = $link->getMenu();
$sub = '';
/** @var NavigationLinkInterface $child */
foreach ($children as $child) {
if ($child->isActuallyVisible()) {
$sub = '.sub';
break;
}
}
$children->rewind();
$active = $link->isActive() ? '.active' : '';
$current = $link->isSelected() ? '.current' : '';
$disabled = !$link->isActuallyEnabled();
$url = $disabled || $link->isGroup() && !isset($link->defaultURI) ? null : $link->url();
$disabledClass = $disabled ? '.disabled' : '';
return [h("li{$active}{$sub}{$current}", [h("a{$active}{$disabledClass}", ['href' => $url], [when($link->icon(), h('i', ['class' => $link->icon()])), $link->title(), when(isset($xi) && $sub, h("span.{$xi}"))]), when($sub, $this->renderMenuItem($children, $xi, false, $depth + 1))])];
}));
}
示例12: posts_export
public function posts_export($atom, $post)
{
$comments = Comment::find(array("where" => array("post_id" => $post->id)), array("filter" => false));
foreach ($comments as $comment) {
$updated = $comment->updated ? $comment->updated_at : $comment->created_at;
$atom .= " <chyrp:comment>\r";
$atom .= ' <updated>' . when("c", $updated) . '</updated>' . "\r";
$atom .= ' <published>' . when("c", $comment->created_at) . '</published>' . "\r";
$atom .= ' <author chyrp:user_id="' . $comment->user_id . '">' . "\r";
$atom .= " <name>" . fix($comment->author) . "</name>\r";
if (!empty($comment->author_url)) {
$atom .= " <uri>" . fix($comment->author_url) . "</uri>\r";
}
$atom .= " <email>" . fix($comment->author_email) . "</email>\r";
$atom .= " <chyrp:login>" . fix(@$comment->user->login) . "</chyrp:login>\r";
$atom .= " <chyrp:ip>" . long2ip($comment->author_ip) . "</chyrp:ip>\r";
$atom .= " <chyrp:agent>" . fix($comment->author_agent) . "</chyrp:agent>\r";
$atom .= " </author>\r";
$atom .= " <content>" . fix($comment->body) . "</content>\r";
foreach (array("status", "signature") as $attr) {
$atom .= " <chyrp:" . $attr . ">" . fix($comment->{$attr}) . "</chyrp:" . $attr . ">\r";
}
$atom .= " </chyrp:comment>\r";
}
return $atom;
}
示例13: describe
<?php
require 'Stack.php';
describe('Stack', function () {
Given('stack', function ($initial_contents) {
return new Stack($initial_contents);
});
context('with no items', function () {
given('initial_contents', function () {
return [];
});
when(function (Stack $stack) {
$stack->push(3);
});
then(function (Stack $stack) {
return $stack->size() === 1;
});
});
context('with one item', function () {
given('initial_contents', function () {
return ['an item'];
});
when(function (Stack $stack) {
$stack->push('another item');
});
then(function (Stack $stack) {
return $stack->size() === 2;
});
});
});
示例14: describe
});
});
describe('calling #reportEnd', function () {
given('errors', array());
given('labels', array());
given('results', array());
given('an instance of the tap reporter', 'reporter', new TapReporter());
context('no tests were executed', function () {
given('total', 0);
when('reporter #reportEnd is called', 'result', function ($reporter, $total, $errors, $labels, $results) {
ob_start();
$reporter->reportEnd($total, $errors, $labels, $results);
return ob_get_clean();
});
then('result should be a valid string', function ($result) {
return empty($result);
});
});
context('11 tests with no errors was executed', function () {
given('total', 11);
when('reporter #reportEnd is called', 'result', function ($reporter, $total, $errors, $labels, $results) {
ob_start();
$reporter->reportEnd($total, $errors, $labels, $results);
return ob_get_clean();
});
then('result should be a valid string', function ($result) {
return !(false === strpos($result, '1..11'));
});
});
});
});
示例15: testCanStub
function testCanStub()
{
$mock = mock(MockMe::class);
when($mock->Foo())->return(1);
$this->assertEquals($mock->Foo(), 1);
}