id
int32 0
241k
| repo
stringlengths 6
63
| path
stringlengths 5
140
| func_name
stringlengths 3
151
| original_string
stringlengths 84
13k
| language
stringclasses 1
value | code
stringlengths 84
13k
| code_tokens
list | docstring
stringlengths 3
47.2k
| docstring_tokens
list | sha
stringlengths 40
40
| url
stringlengths 91
247
|
|---|---|---|---|---|---|---|---|---|---|---|---|
27,100
|
drupal/core-utility
|
Html.php
|
Html.load
|
public static function load($html) {
$document = <<<EOD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<body>!html</body>
</html>
EOD;
// PHP's \DOMDocument serialization adds extra whitespace when the markup
// of the wrapping document contains newlines, so ensure we remove all
// newlines before injecting the actual HTML body to be processed.
$document = strtr($document, ["\n" => '', '!html' => $html]);
$dom = new \DOMDocument();
// Ignore warnings during HTML soup loading.
@$dom->loadHTML($document);
return $dom;
}
|
php
|
public static function load($html) {
$document = <<<EOD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<body>!html</body>
</html>
EOD;
// PHP's \DOMDocument serialization adds extra whitespace when the markup
// of the wrapping document contains newlines, so ensure we remove all
// newlines before injecting the actual HTML body to be processed.
$document = strtr($document, ["\n" => '', '!html' => $html]);
$dom = new \DOMDocument();
// Ignore warnings during HTML soup loading.
@$dom->loadHTML($document);
return $dom;
}
|
[
"public",
"static",
"function",
"load",
"(",
"$",
"html",
")",
"{",
"$",
"document",
"=",
" <<<EOD\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head>\n<body>!html</body>\n</html>\nEOD",
";",
"// PHP's \\DOMDocument serialization adds extra whitespace when the markup",
"// of the wrapping document contains newlines, so ensure we remove all",
"// newlines before injecting the actual HTML body to be processed.",
"$",
"document",
"=",
"strtr",
"(",
"$",
"document",
",",
"[",
"\"\\n\"",
"=>",
"''",
",",
"'!html'",
"=>",
"$",
"html",
"]",
")",
";",
"$",
"dom",
"=",
"new",
"\\",
"DOMDocument",
"(",
")",
";",
"// Ignore warnings during HTML soup loading.",
"@",
"$",
"dom",
"->",
"loadHTML",
"(",
"$",
"document",
")",
";",
"return",
"$",
"dom",
";",
"}"
] |
Parses an HTML snippet and returns it as a DOM object.
This function loads the body part of a partial (X)HTML document and returns
a full \DOMDocument object that represents this document.
Use \Drupal\Component\Utility\Html::serialize() to serialize this
\DOMDocument back to a string.
@param string $html
The partial (X)HTML snippet to load. Invalid markup will be corrected on
import.
@return \DOMDocument
A \DOMDocument that represents the loaded (X)HTML snippet.
|
[
"Parses",
"an",
"HTML",
"snippet",
"and",
"returns",
"it",
"as",
"a",
"DOM",
"object",
"."
] |
fb1c262da83fb691bcdf1680c3a69791a1d1a01a
|
https://github.com/drupal/core-utility/blob/fb1c262da83fb691bcdf1680c3a69791a1d1a01a/Html.php#L273-L291
|
27,101
|
drupal/core-utility
|
Html.php
|
Html.transformRootRelativeUrlsToAbsolute
|
public static function transformRootRelativeUrlsToAbsolute($html, $scheme_and_host) {
assert('empty(array_diff(array_keys(parse_url($scheme_and_host)), ["scheme", "host", "port"]))', '$scheme_and_host contains scheme, host and port at most.');
assert('isset(parse_url($scheme_and_host)["scheme"])', '$scheme_and_host is absolute and hence has a scheme.');
assert('isset(parse_url($scheme_and_host)["host"])', '$base_url is absolute and hence has a host.');
$html_dom = Html::load($html);
$xpath = new \DOMXpath($html_dom);
// Update all root-relative URLs to absolute URLs in the given HTML.
foreach (static::$uriAttributes as $attr) {
foreach ($xpath->query("//*[starts-with(@$attr, '/') and not(starts-with(@$attr, '//'))]") as $node) {
$node->setAttribute($attr, $scheme_and_host . $node->getAttribute($attr));
}
foreach ($xpath->query("//*[@srcset]") as $node) {
// @see https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-srcset
// @see https://html.spec.whatwg.org/multipage/embedded-content.html#image-candidate-string
$image_candidate_strings = explode(',', $node->getAttribute('srcset'));
$image_candidate_strings = array_map('trim', $image_candidate_strings);
for ($i = 0; $i < count($image_candidate_strings); $i++) {
$image_candidate_string = $image_candidate_strings[$i];
if ($image_candidate_string[0] === '/' && $image_candidate_string[1] !== '/') {
$image_candidate_strings[$i] = $scheme_and_host . $image_candidate_string;
}
}
$node->setAttribute('srcset', implode(', ', $image_candidate_strings));
}
}
return Html::serialize($html_dom);
}
|
php
|
public static function transformRootRelativeUrlsToAbsolute($html, $scheme_and_host) {
assert('empty(array_diff(array_keys(parse_url($scheme_and_host)), ["scheme", "host", "port"]))', '$scheme_and_host contains scheme, host and port at most.');
assert('isset(parse_url($scheme_and_host)["scheme"])', '$scheme_and_host is absolute and hence has a scheme.');
assert('isset(parse_url($scheme_and_host)["host"])', '$base_url is absolute and hence has a host.');
$html_dom = Html::load($html);
$xpath = new \DOMXpath($html_dom);
// Update all root-relative URLs to absolute URLs in the given HTML.
foreach (static::$uriAttributes as $attr) {
foreach ($xpath->query("//*[starts-with(@$attr, '/') and not(starts-with(@$attr, '//'))]") as $node) {
$node->setAttribute($attr, $scheme_and_host . $node->getAttribute($attr));
}
foreach ($xpath->query("//*[@srcset]") as $node) {
// @see https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-srcset
// @see https://html.spec.whatwg.org/multipage/embedded-content.html#image-candidate-string
$image_candidate_strings = explode(',', $node->getAttribute('srcset'));
$image_candidate_strings = array_map('trim', $image_candidate_strings);
for ($i = 0; $i < count($image_candidate_strings); $i++) {
$image_candidate_string = $image_candidate_strings[$i];
if ($image_candidate_string[0] === '/' && $image_candidate_string[1] !== '/') {
$image_candidate_strings[$i] = $scheme_and_host . $image_candidate_string;
}
}
$node->setAttribute('srcset', implode(', ', $image_candidate_strings));
}
}
return Html::serialize($html_dom);
}
|
[
"public",
"static",
"function",
"transformRootRelativeUrlsToAbsolute",
"(",
"$",
"html",
",",
"$",
"scheme_and_host",
")",
"{",
"assert",
"(",
"'empty(array_diff(array_keys(parse_url($scheme_and_host)), [\"scheme\", \"host\", \"port\"]))'",
",",
"'$scheme_and_host contains scheme, host and port at most.'",
")",
";",
"assert",
"(",
"'isset(parse_url($scheme_and_host)[\"scheme\"])'",
",",
"'$scheme_and_host is absolute and hence has a scheme.'",
")",
";",
"assert",
"(",
"'isset(parse_url($scheme_and_host)[\"host\"])'",
",",
"'$base_url is absolute and hence has a host.'",
")",
";",
"$",
"html_dom",
"=",
"Html",
"::",
"load",
"(",
"$",
"html",
")",
";",
"$",
"xpath",
"=",
"new",
"\\",
"DOMXpath",
"(",
"$",
"html_dom",
")",
";",
"// Update all root-relative URLs to absolute URLs in the given HTML.",
"foreach",
"(",
"static",
"::",
"$",
"uriAttributes",
"as",
"$",
"attr",
")",
"{",
"foreach",
"(",
"$",
"xpath",
"->",
"query",
"(",
"\"//*[starts-with(@$attr, '/') and not(starts-with(@$attr, '//'))]\"",
")",
"as",
"$",
"node",
")",
"{",
"$",
"node",
"->",
"setAttribute",
"(",
"$",
"attr",
",",
"$",
"scheme_and_host",
".",
"$",
"node",
"->",
"getAttribute",
"(",
"$",
"attr",
")",
")",
";",
"}",
"foreach",
"(",
"$",
"xpath",
"->",
"query",
"(",
"\"//*[@srcset]\"",
")",
"as",
"$",
"node",
")",
"{",
"// @see https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-srcset",
"// @see https://html.spec.whatwg.org/multipage/embedded-content.html#image-candidate-string",
"$",
"image_candidate_strings",
"=",
"explode",
"(",
"','",
",",
"$",
"node",
"->",
"getAttribute",
"(",
"'srcset'",
")",
")",
";",
"$",
"image_candidate_strings",
"=",
"array_map",
"(",
"'trim'",
",",
"$",
"image_candidate_strings",
")",
";",
"for",
"(",
"$",
"i",
"=",
"0",
";",
"$",
"i",
"<",
"count",
"(",
"$",
"image_candidate_strings",
")",
";",
"$",
"i",
"++",
")",
"{",
"$",
"image_candidate_string",
"=",
"$",
"image_candidate_strings",
"[",
"$",
"i",
"]",
";",
"if",
"(",
"$",
"image_candidate_string",
"[",
"0",
"]",
"===",
"'/'",
"&&",
"$",
"image_candidate_string",
"[",
"1",
"]",
"!==",
"'/'",
")",
"{",
"$",
"image_candidate_strings",
"[",
"$",
"i",
"]",
"=",
"$",
"scheme_and_host",
".",
"$",
"image_candidate_string",
";",
"}",
"}",
"$",
"node",
"->",
"setAttribute",
"(",
"'srcset'",
",",
"implode",
"(",
"', '",
",",
"$",
"image_candidate_strings",
")",
")",
";",
"}",
"}",
"return",
"Html",
"::",
"serialize",
"(",
"$",
"html_dom",
")",
";",
"}"
] |
Converts all root-relative URLs to absolute URLs.
Does not change any existing protocol-relative or absolute URLs. Does not
change other relative URLs because they would result in different absolute
URLs depending on the current path. For example: when the same content
containing such a relative URL (for example 'image.png'), is served from
its canonical URL (for example 'http://example.com/some-article') or from
a listing or feed (for example 'http://example.com/all-articles') their
"current path" differs, resulting in different absolute URLs:
'http://example.com/some-article/image.png' versus
'http://example.com/all-articles/image.png'. Only one can be correct.
Therefore relative URLs that are not root-relative cannot be safely
transformed and should generally be avoided.
Necessary for HTML that is served outside of a website, for example, RSS
and e-mail.
@param string $html
The partial (X)HTML snippet to load. Invalid markup will be corrected on
import.
@param string $scheme_and_host
The root URL, which has a URI scheme, host and optional port.
@return string
The updated (X)HTML snippet.
|
[
"Converts",
"all",
"root",
"-",
"relative",
"URLs",
"to",
"absolute",
"URLs",
"."
] |
fb1c262da83fb691bcdf1680c3a69791a1d1a01a
|
https://github.com/drupal/core-utility/blob/fb1c262da83fb691bcdf1680c3a69791a1d1a01a/Html.php#L453-L481
|
27,102
|
bitpressio/blade-extensions
|
src/Container/BladeRegistrar.php
|
BladeRegistrar.register
|
public static function register($extension, $concrete = null)
{
app()->singleton($extension, $concrete);
app()->tag($extension, 'blade.extension');
}
|
php
|
public static function register($extension, $concrete = null)
{
app()->singleton($extension, $concrete);
app()->tag($extension, 'blade.extension');
}
|
[
"public",
"static",
"function",
"register",
"(",
"$",
"extension",
",",
"$",
"concrete",
"=",
"null",
")",
"{",
"app",
"(",
")",
"->",
"singleton",
"(",
"$",
"extension",
",",
"$",
"concrete",
")",
";",
"app",
"(",
")",
"->",
"tag",
"(",
"$",
"extension",
",",
"'blade.extension'",
")",
";",
"}"
] |
Register and tag a blade service in the container
@param string|array $abstract
@param \Closure|string|null $concrete
|
[
"Register",
"and",
"tag",
"a",
"blade",
"service",
"in",
"the",
"container"
] |
eeb4f3acb7253ee28302530c0bd25c5113248507
|
https://github.com/bitpressio/blade-extensions/blob/eeb4f3acb7253ee28302530c0bd25c5113248507/src/Container/BladeRegistrar.php#L13-L17
|
27,103
|
axiom-labs/rivescript-php
|
src/Cortex/Tags/Tag.php
|
Tag.getMatches
|
protected function getMatches($source)
{
if ($this->hasMatches($source)) {
preg_match_all($this->pattern, $source, $matches, PREG_SET_ORDER);
return $matches;
}
}
|
php
|
protected function getMatches($source)
{
if ($this->hasMatches($source)) {
preg_match_all($this->pattern, $source, $matches, PREG_SET_ORDER);
return $matches;
}
}
|
[
"protected",
"function",
"getMatches",
"(",
"$",
"source",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"hasMatches",
"(",
"$",
"source",
")",
")",
"{",
"preg_match_all",
"(",
"$",
"this",
"->",
"pattern",
",",
"$",
"source",
",",
"$",
"matches",
",",
"PREG_SET_ORDER",
")",
";",
"return",
"$",
"matches",
";",
"}",
"}"
] |
Get the regular expression matches from the source.
@param string $source
@return array
|
[
"Get",
"the",
"regular",
"expression",
"matches",
"from",
"the",
"source",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Cortex/Tags/Tag.php#L60-L67
|
27,104
|
axiom-labs/rivescript-php
|
src/Cortex/Output.php
|
Output.process
|
public function process()
{
synapse()->brain->topic()->triggers()->each(function ($data, $trigger) {
$this->searchTriggers($trigger);
if ($this->output !== 'Error: Response could not be determined.') {
return false;
}
});
return $this->output;
}
|
php
|
public function process()
{
synapse()->brain->topic()->triggers()->each(function ($data, $trigger) {
$this->searchTriggers($trigger);
if ($this->output !== 'Error: Response could not be determined.') {
return false;
}
});
return $this->output;
}
|
[
"public",
"function",
"process",
"(",
")",
"{",
"synapse",
"(",
")",
"->",
"brain",
"->",
"topic",
"(",
")",
"->",
"triggers",
"(",
")",
"->",
"each",
"(",
"function",
"(",
"$",
"data",
",",
"$",
"trigger",
")",
"{",
"$",
"this",
"->",
"searchTriggers",
"(",
"$",
"trigger",
")",
";",
"if",
"(",
"$",
"this",
"->",
"output",
"!==",
"'Error: Response could not be determined.'",
")",
"{",
"return",
"false",
";",
"}",
"}",
")",
";",
"return",
"$",
"this",
"->",
"output",
";",
"}"
] |
Process the correct output response by the interpreter.
@return mixed
|
[
"Process",
"the",
"correct",
"output",
"response",
"by",
"the",
"interpreter",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Cortex/Output.php#L37-L48
|
27,105
|
axiom-labs/rivescript-php
|
src/Cortex/Output.php
|
Output.searchTriggers
|
protected function searchTriggers($trigger)
{
synapse()->triggers->each(function ($class) use ($trigger) {
$triggerClass = "\\Axiom\\Rivescript\\Cortex\\Triggers\\$class";
$triggerClass = new $triggerClass();
$found = $triggerClass->parse($trigger, $this->input);
if ($found === true) {
$this->getResponse($trigger);
return false;
}
});
}
|
php
|
protected function searchTriggers($trigger)
{
synapse()->triggers->each(function ($class) use ($trigger) {
$triggerClass = "\\Axiom\\Rivescript\\Cortex\\Triggers\\$class";
$triggerClass = new $triggerClass();
$found = $triggerClass->parse($trigger, $this->input);
if ($found === true) {
$this->getResponse($trigger);
return false;
}
});
}
|
[
"protected",
"function",
"searchTriggers",
"(",
"$",
"trigger",
")",
"{",
"synapse",
"(",
")",
"->",
"triggers",
"->",
"each",
"(",
"function",
"(",
"$",
"class",
")",
"use",
"(",
"$",
"trigger",
")",
"{",
"$",
"triggerClass",
"=",
"\"\\\\Axiom\\\\Rivescript\\\\Cortex\\\\Triggers\\\\$class\"",
";",
"$",
"triggerClass",
"=",
"new",
"$",
"triggerClass",
"(",
")",
";",
"$",
"found",
"=",
"$",
"triggerClass",
"->",
"parse",
"(",
"$",
"trigger",
",",
"$",
"this",
"->",
"input",
")",
";",
"if",
"(",
"$",
"found",
"===",
"true",
")",
"{",
"$",
"this",
"->",
"getResponse",
"(",
"$",
"trigger",
")",
";",
"return",
"false",
";",
"}",
"}",
")",
";",
"}"
] |
Search through available triggers to find a possible match.
@param string $trigger
@return bool
|
[
"Search",
"through",
"available",
"triggers",
"to",
"find",
"a",
"possible",
"match",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Cortex/Output.php#L57-L71
|
27,106
|
axiom-labs/rivescript-php
|
src/Cortex/Output.php
|
Output.getResponse
|
protected function getResponse($trigger)
{
$trigger = synapse()->brain->topic()->triggers()->get($trigger);
if (isset($trigger['redirect'])) {
return $this->getResponse($trigger['redirect']);
}
$key = array_rand($trigger['responses']);
$this->output = $this->parseResponse($trigger['responses'][$key]);
}
|
php
|
protected function getResponse($trigger)
{
$trigger = synapse()->brain->topic()->triggers()->get($trigger);
if (isset($trigger['redirect'])) {
return $this->getResponse($trigger['redirect']);
}
$key = array_rand($trigger['responses']);
$this->output = $this->parseResponse($trigger['responses'][$key]);
}
|
[
"protected",
"function",
"getResponse",
"(",
"$",
"trigger",
")",
"{",
"$",
"trigger",
"=",
"synapse",
"(",
")",
"->",
"brain",
"->",
"topic",
"(",
")",
"->",
"triggers",
"(",
")",
"->",
"get",
"(",
"$",
"trigger",
")",
";",
"if",
"(",
"isset",
"(",
"$",
"trigger",
"[",
"'redirect'",
"]",
")",
")",
"{",
"return",
"$",
"this",
"->",
"getResponse",
"(",
"$",
"trigger",
"[",
"'redirect'",
"]",
")",
";",
"}",
"$",
"key",
"=",
"array_rand",
"(",
"$",
"trigger",
"[",
"'responses'",
"]",
")",
";",
"$",
"this",
"->",
"output",
"=",
"$",
"this",
"->",
"parseResponse",
"(",
"$",
"trigger",
"[",
"'responses'",
"]",
"[",
"$",
"key",
"]",
")",
";",
"}"
] |
Fetch a response from the found trigger.
@param string $trigger;
@return void
|
[
"Fetch",
"a",
"response",
"from",
"the",
"found",
"trigger",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Cortex/Output.php#L80-L90
|
27,107
|
axiom-labs/rivescript-php
|
src/Rivescript.php
|
Rivescript.load
|
public function load($files)
{
$files = (! is_array($files)) ? (array) $files : $files;
foreach ($files as $file) {
synapse()->brain->teach($file);
}
}
|
php
|
public function load($files)
{
$files = (! is_array($files)) ? (array) $files : $files;
foreach ($files as $file) {
synapse()->brain->teach($file);
}
}
|
[
"public",
"function",
"load",
"(",
"$",
"files",
")",
"{",
"$",
"files",
"=",
"(",
"!",
"is_array",
"(",
"$",
"files",
")",
")",
"?",
"(",
"array",
")",
"$",
"files",
":",
"$",
"files",
";",
"foreach",
"(",
"$",
"files",
"as",
"$",
"file",
")",
"{",
"synapse",
"(",
")",
"->",
"brain",
"->",
"teach",
"(",
"$",
"file",
")",
";",
"}",
"}"
] |
Load RiveScript documents from files.
@param array|string $files
|
[
"Load",
"RiveScript",
"documents",
"from",
"files",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Rivescript.php#L23-L30
|
27,108
|
axiom-labs/rivescript-php
|
src/Console/ChatCommand.php
|
ChatCommand.waitForUserInput
|
protected function waitForUserInput(InputInterface $input, OutputInterface $output)
{
$helper = $this->getHelper('question');
$question = new Question('<info>You > </info>');
$message = $helper->ask($input, $output, $question);
$this->listenForConsoleCommands($input, $output, $message);
$this->getBotResponse($input, $output, $message);
}
|
php
|
protected function waitForUserInput(InputInterface $input, OutputInterface $output)
{
$helper = $this->getHelper('question');
$question = new Question('<info>You > </info>');
$message = $helper->ask($input, $output, $question);
$this->listenForConsoleCommands($input, $output, $message);
$this->getBotResponse($input, $output, $message);
}
|
[
"protected",
"function",
"waitForUserInput",
"(",
"InputInterface",
"$",
"input",
",",
"OutputInterface",
"$",
"output",
")",
"{",
"$",
"helper",
"=",
"$",
"this",
"->",
"getHelper",
"(",
"'question'",
")",
";",
"$",
"question",
"=",
"new",
"Question",
"(",
"'<info>You > </info>'",
")",
";",
"$",
"message",
"=",
"$",
"helper",
"->",
"ask",
"(",
"$",
"input",
",",
"$",
"output",
",",
"$",
"question",
")",
";",
"$",
"this",
"->",
"listenForConsoleCommands",
"(",
"$",
"input",
",",
"$",
"output",
",",
"$",
"message",
")",
";",
"$",
"this",
"->",
"getBotResponse",
"(",
"$",
"input",
",",
"$",
"output",
",",
"$",
"message",
")",
";",
"}"
] |
Wait and listen for user input.
@param InputInterface $input
@param OutputInterface $output
@return null
|
[
"Wait",
"and",
"listen",
"for",
"user",
"input",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Console/ChatCommand.php#L78-L88
|
27,109
|
axiom-labs/rivescript-php
|
src/Console/ChatCommand.php
|
ChatCommand.listenForConsoleCommands
|
protected function listenForConsoleCommands(InputInterface $input, OutputInterface $output, $message)
{
if ($message === '/quit') {
$output->writeln('Exiting...');
die();
}
if ($message === '/reload') {
return $this->execute($input, $output);
}
if ($message === '/help') {
$output->writeln('');
$output->writeln('<comment>Usage:</comment>');
$output->writeln(' Type a message and press Return to send.');
$output->writeln('');
$output->writeln('<comment>Commands:</comment>');
$output->writeln(' <info>/help</info> Show this text');
$output->writeln(' <info>/reload</info> Reload the interactive console');
$output->writeln(' <info>/quit</info> Quit the interative console');
$output->writeln('');
$this->waitForUserInput($input, $output);
}
return null;
}
|
php
|
protected function listenForConsoleCommands(InputInterface $input, OutputInterface $output, $message)
{
if ($message === '/quit') {
$output->writeln('Exiting...');
die();
}
if ($message === '/reload') {
return $this->execute($input, $output);
}
if ($message === '/help') {
$output->writeln('');
$output->writeln('<comment>Usage:</comment>');
$output->writeln(' Type a message and press Return to send.');
$output->writeln('');
$output->writeln('<comment>Commands:</comment>');
$output->writeln(' <info>/help</info> Show this text');
$output->writeln(' <info>/reload</info> Reload the interactive console');
$output->writeln(' <info>/quit</info> Quit the interative console');
$output->writeln('');
$this->waitForUserInput($input, $output);
}
return null;
}
|
[
"protected",
"function",
"listenForConsoleCommands",
"(",
"InputInterface",
"$",
"input",
",",
"OutputInterface",
"$",
"output",
",",
"$",
"message",
")",
"{",
"if",
"(",
"$",
"message",
"===",
"'/quit'",
")",
"{",
"$",
"output",
"->",
"writeln",
"(",
"'Exiting...'",
")",
";",
"die",
"(",
")",
";",
"}",
"if",
"(",
"$",
"message",
"===",
"'/reload'",
")",
"{",
"return",
"$",
"this",
"->",
"execute",
"(",
"$",
"input",
",",
"$",
"output",
")",
";",
"}",
"if",
"(",
"$",
"message",
"===",
"'/help'",
")",
"{",
"$",
"output",
"->",
"writeln",
"(",
"''",
")",
";",
"$",
"output",
"->",
"writeln",
"(",
"'<comment>Usage:</comment>'",
")",
";",
"$",
"output",
"->",
"writeln",
"(",
"' Type a message and press Return to send.'",
")",
";",
"$",
"output",
"->",
"writeln",
"(",
"''",
")",
";",
"$",
"output",
"->",
"writeln",
"(",
"'<comment>Commands:</comment>'",
")",
";",
"$",
"output",
"->",
"writeln",
"(",
"' <info>/help</info> Show this text'",
")",
";",
"$",
"output",
"->",
"writeln",
"(",
"' <info>/reload</info> Reload the interactive console'",
")",
";",
"$",
"output",
"->",
"writeln",
"(",
"' <info>/quit</info> Quit the interative console'",
")",
";",
"$",
"output",
"->",
"writeln",
"(",
"''",
")",
";",
"$",
"this",
"->",
"waitForUserInput",
"(",
"$",
"input",
",",
"$",
"output",
")",
";",
"}",
"return",
"null",
";",
"}"
] |
Listen for console commands before passing message to interpreter.
@param InputInterface $input
@param OutputInterface $output
@param string $message
@return null
|
[
"Listen",
"for",
"console",
"commands",
"before",
"passing",
"message",
"to",
"interpreter",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Console/ChatCommand.php#L98-L124
|
27,110
|
axiom-labs/rivescript-php
|
src/Console/ChatCommand.php
|
ChatCommand.getBotResponse
|
protected function getBotResponse(InputInterface $input, OutputInterface $output, $message)
{
$bot = 'Bot > ';
$reply = $this->rivescript->reply($message);
$response = "<info>{$reply}</info>";
$output->writeln($bot.$response);
$this->waitForUserInput($input, $output);
}
|
php
|
protected function getBotResponse(InputInterface $input, OutputInterface $output, $message)
{
$bot = 'Bot > ';
$reply = $this->rivescript->reply($message);
$response = "<info>{$reply}</info>";
$output->writeln($bot.$response);
$this->waitForUserInput($input, $output);
}
|
[
"protected",
"function",
"getBotResponse",
"(",
"InputInterface",
"$",
"input",
",",
"OutputInterface",
"$",
"output",
",",
"$",
"message",
")",
"{",
"$",
"bot",
"=",
"'Bot > '",
";",
"$",
"reply",
"=",
"$",
"this",
"->",
"rivescript",
"->",
"reply",
"(",
"$",
"message",
")",
";",
"$",
"response",
"=",
"\"<info>{$reply}</info>\"",
";",
"$",
"output",
"->",
"writeln",
"(",
"$",
"bot",
".",
"$",
"response",
")",
";",
"$",
"this",
"->",
"waitForUserInput",
"(",
"$",
"input",
",",
"$",
"output",
")",
";",
"}"
] |
Pass along user message to interpreter and fetch a reply.
@param InputInterface $input
@param OutputInterface $output
@param string $message
@return null
|
[
"Pass",
"along",
"user",
"message",
"to",
"interpreter",
"and",
"fetch",
"a",
"reply",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Console/ChatCommand.php#L134-L143
|
27,111
|
axiom-labs/rivescript-php
|
src/Console/ChatCommand.php
|
ChatCommand.loadFiles
|
private function loadFiles($files)
{
if (is_dir($files)) {
$directory = realpath($files);
$files = [];
$brains = glob($directory.'/*.rive');
foreach ($brains as $brain) {
$files[] = $brain;
}
return $files;
}
return (array) $files;
}
|
php
|
private function loadFiles($files)
{
if (is_dir($files)) {
$directory = realpath($files);
$files = [];
$brains = glob($directory.'/*.rive');
foreach ($brains as $brain) {
$files[] = $brain;
}
return $files;
}
return (array) $files;
}
|
[
"private",
"function",
"loadFiles",
"(",
"$",
"files",
")",
"{",
"if",
"(",
"is_dir",
"(",
"$",
"files",
")",
")",
"{",
"$",
"directory",
"=",
"realpath",
"(",
"$",
"files",
")",
";",
"$",
"files",
"=",
"[",
"]",
";",
"$",
"brains",
"=",
"glob",
"(",
"$",
"directory",
".",
"'/*.rive'",
")",
";",
"foreach",
"(",
"$",
"brains",
"as",
"$",
"brain",
")",
"{",
"$",
"files",
"[",
"]",
"=",
"$",
"brain",
";",
"}",
"return",
"$",
"files",
";",
"}",
"return",
"(",
"array",
")",
"$",
"files",
";",
"}"
] |
Load and return an array of files.
@param string $files
@return array
|
[
"Load",
"and",
"return",
"an",
"array",
"of",
"files",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Console/ChatCommand.php#L151-L166
|
27,112
|
axiom-labs/rivescript-php
|
src/Cortex/Memory.php
|
Memory.user
|
public function user($user = 0)
{
if (! $this->user->has($user)) {
$data = new Collection([]);
$this->user->put($user, $data);
}
return $this->user->get($user);
}
|
php
|
public function user($user = 0)
{
if (! $this->user->has($user)) {
$data = new Collection([]);
$this->user->put($user, $data);
}
return $this->user->get($user);
}
|
[
"public",
"function",
"user",
"(",
"$",
"user",
"=",
"0",
")",
"{",
"if",
"(",
"!",
"$",
"this",
"->",
"user",
"->",
"has",
"(",
"$",
"user",
")",
")",
"{",
"$",
"data",
"=",
"new",
"Collection",
"(",
"[",
"]",
")",
";",
"$",
"this",
"->",
"user",
"->",
"put",
"(",
"$",
"user",
",",
"$",
"data",
")",
";",
"}",
"return",
"$",
"this",
"->",
"user",
"->",
"get",
"(",
"$",
"user",
")",
";",
"}"
] |
Stored user data.
@return Collection
|
[
"Stored",
"user",
"data",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Cortex/Memory.php#L91-L100
|
27,113
|
axiom-labs/rivescript-php
|
src/Cortex/Node.php
|
Node.determineCommand
|
protected function determineCommand()
{
if (mb_strlen($this->source) === 0) {
$this->isInterrupted = true;
return;
}
$this->command = mb_substr($this->source, 0, 1);
}
|
php
|
protected function determineCommand()
{
if (mb_strlen($this->source) === 0) {
$this->isInterrupted = true;
return;
}
$this->command = mb_substr($this->source, 0, 1);
}
|
[
"protected",
"function",
"determineCommand",
"(",
")",
"{",
"if",
"(",
"mb_strlen",
"(",
"$",
"this",
"->",
"source",
")",
"===",
"0",
")",
"{",
"$",
"this",
"->",
"isInterrupted",
"=",
"true",
";",
"return",
";",
"}",
"$",
"this",
"->",
"command",
"=",
"mb_substr",
"(",
"$",
"this",
"->",
"source",
",",
"0",
",",
"1",
")",
";",
"}"
] |
Determine the command type of the node.
@return void
|
[
"Determine",
"the",
"command",
"type",
"of",
"the",
"node",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Cortex/Node.php#L108-L117
|
27,114
|
axiom-labs/rivescript-php
|
src/Cortex/Node.php
|
Node.determineComment
|
protected function determineComment()
{
if (starts_with($this->source, '//')) {
$this->isInterrupted = true;
} elseif (starts_with($this->source, '#')) {
log_warning('Using the # symbol for comments is deprecated');
$this->isInterrupted = true;
} elseif (starts_with($this->source, '/*')) {
if (ends_with($this->source, '*/')) {
return;
}
$this->isComment = true;
} elseif (ends_with($this->source, '*/')) {
$this->isComment = false;
}
}
|
php
|
protected function determineComment()
{
if (starts_with($this->source, '//')) {
$this->isInterrupted = true;
} elseif (starts_with($this->source, '#')) {
log_warning('Using the # symbol for comments is deprecated');
$this->isInterrupted = true;
} elseif (starts_with($this->source, '/*')) {
if (ends_with($this->source, '*/')) {
return;
}
$this->isComment = true;
} elseif (ends_with($this->source, '*/')) {
$this->isComment = false;
}
}
|
[
"protected",
"function",
"determineComment",
"(",
")",
"{",
"if",
"(",
"starts_with",
"(",
"$",
"this",
"->",
"source",
",",
"'//'",
")",
")",
"{",
"$",
"this",
"->",
"isInterrupted",
"=",
"true",
";",
"}",
"elseif",
"(",
"starts_with",
"(",
"$",
"this",
"->",
"source",
",",
"'#'",
")",
")",
"{",
"log_warning",
"(",
"'Using the # symbol for comments is deprecated'",
")",
";",
"$",
"this",
"->",
"isInterrupted",
"=",
"true",
";",
"}",
"elseif",
"(",
"starts_with",
"(",
"$",
"this",
"->",
"source",
",",
"'/*'",
")",
")",
"{",
"if",
"(",
"ends_with",
"(",
"$",
"this",
"->",
"source",
",",
"'*/'",
")",
")",
"{",
"return",
";",
"}",
"$",
"this",
"->",
"isComment",
"=",
"true",
";",
"}",
"elseif",
"(",
"ends_with",
"(",
"$",
"this",
"->",
"source",
",",
"'*/'",
")",
")",
"{",
"$",
"this",
"->",
"isComment",
"=",
"false",
";",
"}",
"}"
] |
Determine if the current node source is a comment.
@return void
|
[
"Determine",
"if",
"the",
"current",
"node",
"source",
"is",
"a",
"comment",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Cortex/Node.php#L124-L139
|
27,115
|
Happyr/GoogleSiteAuthenticatorBundle
|
Controller/AdminController.php
|
AdminController.authenticateAction
|
public function authenticateAction(Request $request, $name)
{
/* @var \Google_Client $client */
$clientProvider = $this->get('happyr.google_site_authenticator.client_provider');
$client = $clientProvider->getClient($name);
// This will allow us to get refresh the token
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$request->getSession()->set(self::SESSION_KEY, $name);
return $this->redirect($client->createAuthUrl());
}
|
php
|
public function authenticateAction(Request $request, $name)
{
/* @var \Google_Client $client */
$clientProvider = $this->get('happyr.google_site_authenticator.client_provider');
$client = $clientProvider->getClient($name);
// This will allow us to get refresh the token
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$request->getSession()->set(self::SESSION_KEY, $name);
return $this->redirect($client->createAuthUrl());
}
|
[
"public",
"function",
"authenticateAction",
"(",
"Request",
"$",
"request",
",",
"$",
"name",
")",
"{",
"/* @var \\Google_Client $client */",
"$",
"clientProvider",
"=",
"$",
"this",
"->",
"get",
"(",
"'happyr.google_site_authenticator.client_provider'",
")",
";",
"$",
"client",
"=",
"$",
"clientProvider",
"->",
"getClient",
"(",
"$",
"name",
")",
";",
"// This will allow us to get refresh the token",
"$",
"client",
"->",
"setAccessType",
"(",
"'offline'",
")",
";",
"$",
"client",
"->",
"setApprovalPrompt",
"(",
"'force'",
")",
";",
"$",
"request",
"->",
"getSession",
"(",
")",
"->",
"set",
"(",
"self",
"::",
"SESSION_KEY",
",",
"$",
"name",
")",
";",
"return",
"$",
"this",
"->",
"redirect",
"(",
"$",
"client",
"->",
"createAuthUrl",
"(",
")",
")",
";",
"}"
] |
This action starts the authentication.
@param Request $request
@param $name
@return Response
|
[
"This",
"action",
"starts",
"the",
"authentication",
"."
] |
dcf0f9706ab0864a37b7d8296531fb2971176c55
|
https://github.com/Happyr/GoogleSiteAuthenticatorBundle/blob/dcf0f9706ab0864a37b7d8296531fb2971176c55/Controller/AdminController.php#L41-L54
|
27,116
|
Happyr/GoogleSiteAuthenticatorBundle
|
Controller/AdminController.php
|
AdminController.revokeAction
|
public function revokeAction($name)
{
/* @var \Google_Client $client */
$clientProvider = $this->get('happyr.google_site_authenticator.client_provider');
$client = $clientProvider->getClient($name);
$client->revokeToken();
$clientProvider->setAccessToken(null, $name);
$this->get('session')->getFlashbag()->add('msg', 'Token was revoked.');
return $this->redirect($this->generateUrl('happyr.google_site_authenticator.index'));
}
|
php
|
public function revokeAction($name)
{
/* @var \Google_Client $client */
$clientProvider = $this->get('happyr.google_site_authenticator.client_provider');
$client = $clientProvider->getClient($name);
$client->revokeToken();
$clientProvider->setAccessToken(null, $name);
$this->get('session')->getFlashbag()->add('msg', 'Token was revoked.');
return $this->redirect($this->generateUrl('happyr.google_site_authenticator.index'));
}
|
[
"public",
"function",
"revokeAction",
"(",
"$",
"name",
")",
"{",
"/* @var \\Google_Client $client */",
"$",
"clientProvider",
"=",
"$",
"this",
"->",
"get",
"(",
"'happyr.google_site_authenticator.client_provider'",
")",
";",
"$",
"client",
"=",
"$",
"clientProvider",
"->",
"getClient",
"(",
"$",
"name",
")",
";",
"$",
"client",
"->",
"revokeToken",
"(",
")",
";",
"$",
"clientProvider",
"->",
"setAccessToken",
"(",
"null",
",",
"$",
"name",
")",
";",
"$",
"this",
"->",
"get",
"(",
"'session'",
")",
"->",
"getFlashbag",
"(",
")",
"->",
"add",
"(",
"'msg'",
",",
"'Token was revoked.'",
")",
";",
"return",
"$",
"this",
"->",
"redirect",
"(",
"$",
"this",
"->",
"generateUrl",
"(",
"'happyr.google_site_authenticator.index'",
")",
")",
";",
"}"
] |
This action revokes the authentication token. This make sure the token can not be used on any other site.
@param Request $request
@param $name
@return Response
|
[
"This",
"action",
"revokes",
"the",
"authentication",
"token",
".",
"This",
"make",
"sure",
"the",
"token",
"can",
"not",
"be",
"used",
"on",
"any",
"other",
"site",
"."
] |
dcf0f9706ab0864a37b7d8296531fb2971176c55
|
https://github.com/Happyr/GoogleSiteAuthenticatorBundle/blob/dcf0f9706ab0864a37b7d8296531fb2971176c55/Controller/AdminController.php#L64-L76
|
27,117
|
Happyr/GoogleSiteAuthenticatorBundle
|
Controller/AdminController.php
|
AdminController.removeAction
|
public function removeAction($name)
{
/* @var \Google_Client $client */
$clientProvider = $this->get('happyr.google_site_authenticator.client_provider');
$clientProvider->setAccessToken(null, $name);
$this->get('session')->getFlashbag()->add('msg', 'Token was removed.');
return $this->redirect($this->generateUrl('happyr.google_site_authenticator.index'));
}
|
php
|
public function removeAction($name)
{
/* @var \Google_Client $client */
$clientProvider = $this->get('happyr.google_site_authenticator.client_provider');
$clientProvider->setAccessToken(null, $name);
$this->get('session')->getFlashbag()->add('msg', 'Token was removed.');
return $this->redirect($this->generateUrl('happyr.google_site_authenticator.index'));
}
|
[
"public",
"function",
"removeAction",
"(",
"$",
"name",
")",
"{",
"/* @var \\Google_Client $client */",
"$",
"clientProvider",
"=",
"$",
"this",
"->",
"get",
"(",
"'happyr.google_site_authenticator.client_provider'",
")",
";",
"$",
"clientProvider",
"->",
"setAccessToken",
"(",
"null",
",",
"$",
"name",
")",
";",
"$",
"this",
"->",
"get",
"(",
"'session'",
")",
"->",
"getFlashbag",
"(",
")",
"->",
"add",
"(",
"'msg'",
",",
"'Token was removed.'",
")",
";",
"return",
"$",
"this",
"->",
"redirect",
"(",
"$",
"this",
"->",
"generateUrl",
"(",
"'happyr.google_site_authenticator.index'",
")",
")",
";",
"}"
] |
This action removes the authentication token form the storage.
@param Request $request
@param $name
@return Response
|
[
"This",
"action",
"removes",
"the",
"authentication",
"token",
"form",
"the",
"storage",
"."
] |
dcf0f9706ab0864a37b7d8296531fb2971176c55
|
https://github.com/Happyr/GoogleSiteAuthenticatorBundle/blob/dcf0f9706ab0864a37b7d8296531fb2971176c55/Controller/AdminController.php#L86-L95
|
27,118
|
Happyr/GoogleSiteAuthenticatorBundle
|
Controller/AdminController.php
|
AdminController.returnAction
|
public function returnAction(Request $request)
{
$name = $request->getSession()->get(self::SESSION_KEY, null);
/* @var \Google_Client $client */
$clientProvider = $this->get('happyr.google_site_authenticator.client_provider');
$client = $clientProvider->getClient($name);
$flashBag = $this->get('session')->getFlashbag();
if ($request->query->has('code')) {
try {
$client->authenticate($request->query->get('code'));
$clientProvider->setAccessToken($client->getAccessToken(), $name);
$flashBag->add('msg', 'Successfully authenticated!');
} catch (\Google_Auth_Exception $e) {
$flashBag->add('error', $e->getMessage());
}
} else {
$flashBag->add('error', 'Authentication aborted.');
}
return $this->redirect($this->generateUrl('happyr.google_site_authenticator.index'));
}
|
php
|
public function returnAction(Request $request)
{
$name = $request->getSession()->get(self::SESSION_KEY, null);
/* @var \Google_Client $client */
$clientProvider = $this->get('happyr.google_site_authenticator.client_provider');
$client = $clientProvider->getClient($name);
$flashBag = $this->get('session')->getFlashbag();
if ($request->query->has('code')) {
try {
$client->authenticate($request->query->get('code'));
$clientProvider->setAccessToken($client->getAccessToken(), $name);
$flashBag->add('msg', 'Successfully authenticated!');
} catch (\Google_Auth_Exception $e) {
$flashBag->add('error', $e->getMessage());
}
} else {
$flashBag->add('error', 'Authentication aborted.');
}
return $this->redirect($this->generateUrl('happyr.google_site_authenticator.index'));
}
|
[
"public",
"function",
"returnAction",
"(",
"Request",
"$",
"request",
")",
"{",
"$",
"name",
"=",
"$",
"request",
"->",
"getSession",
"(",
")",
"->",
"get",
"(",
"self",
"::",
"SESSION_KEY",
",",
"null",
")",
";",
"/* @var \\Google_Client $client */",
"$",
"clientProvider",
"=",
"$",
"this",
"->",
"get",
"(",
"'happyr.google_site_authenticator.client_provider'",
")",
";",
"$",
"client",
"=",
"$",
"clientProvider",
"->",
"getClient",
"(",
"$",
"name",
")",
";",
"$",
"flashBag",
"=",
"$",
"this",
"->",
"get",
"(",
"'session'",
")",
"->",
"getFlashbag",
"(",
")",
";",
"if",
"(",
"$",
"request",
"->",
"query",
"->",
"has",
"(",
"'code'",
")",
")",
"{",
"try",
"{",
"$",
"client",
"->",
"authenticate",
"(",
"$",
"request",
"->",
"query",
"->",
"get",
"(",
"'code'",
")",
")",
";",
"$",
"clientProvider",
"->",
"setAccessToken",
"(",
"$",
"client",
"->",
"getAccessToken",
"(",
")",
",",
"$",
"name",
")",
";",
"$",
"flashBag",
"->",
"add",
"(",
"'msg'",
",",
"'Successfully authenticated!'",
")",
";",
"}",
"catch",
"(",
"\\",
"Google_Auth_Exception",
"$",
"e",
")",
"{",
"$",
"flashBag",
"->",
"add",
"(",
"'error'",
",",
"$",
"e",
"->",
"getMessage",
"(",
")",
")",
";",
"}",
"}",
"else",
"{",
"$",
"flashBag",
"->",
"add",
"(",
"'error'",
",",
"'Authentication aborted.'",
")",
";",
"}",
"return",
"$",
"this",
"->",
"redirect",
"(",
"$",
"this",
"->",
"generateUrl",
"(",
"'happyr.google_site_authenticator.index'",
")",
")",
";",
"}"
] |
This action is used when the user has authenticated with google.
@param Request $request
@return Response
|
[
"This",
"action",
"is",
"used",
"when",
"the",
"user",
"has",
"authenticated",
"with",
"google",
"."
] |
dcf0f9706ab0864a37b7d8296531fb2971176c55
|
https://github.com/Happyr/GoogleSiteAuthenticatorBundle/blob/dcf0f9706ab0864a37b7d8296531fb2971176c55/Controller/AdminController.php#L104-L127
|
27,119
|
Happyr/GoogleSiteAuthenticatorBundle
|
Model/TokenConfig.php
|
TokenConfig.getKey
|
public function getKey(string $key = null): string
{
if ($key === null) {
return $this->defaultKey;
}
if (!isset($this->tokens[$key])) {
throw new \LogicException(sprintf('Token with name %s could not be found', $key));
}
return $key;
}
|
php
|
public function getKey(string $key = null): string
{
if ($key === null) {
return $this->defaultKey;
}
if (!isset($this->tokens[$key])) {
throw new \LogicException(sprintf('Token with name %s could not be found', $key));
}
return $key;
}
|
[
"public",
"function",
"getKey",
"(",
"string",
"$",
"key",
"=",
"null",
")",
":",
"string",
"{",
"if",
"(",
"$",
"key",
"===",
"null",
")",
"{",
"return",
"$",
"this",
"->",
"defaultKey",
";",
"}",
"if",
"(",
"!",
"isset",
"(",
"$",
"this",
"->",
"tokens",
"[",
"$",
"key",
"]",
")",
")",
"{",
"throw",
"new",
"\\",
"LogicException",
"(",
"sprintf",
"(",
"'Token with name %s could not be found'",
",",
"$",
"key",
")",
")",
";",
"}",
"return",
"$",
"key",
";",
"}"
] |
Get the key from the argument or the default key.
|
[
"Get",
"the",
"key",
"from",
"the",
"argument",
"or",
"the",
"default",
"key",
"."
] |
dcf0f9706ab0864a37b7d8296531fb2971176c55
|
https://github.com/Happyr/GoogleSiteAuthenticatorBundle/blob/dcf0f9706ab0864a37b7d8296531fb2971176c55/Model/TokenConfig.php#L38-L49
|
27,120
|
craftcms/commerce-omnipay
|
src/base/Gateway.php
|
Gateway.createCard
|
public function createCard(BasePaymentForm $paymentForm, Order $order = null): CreditCard {
$card = new CreditCard;
if ($paymentForm instanceof CreditCardPaymentForm) {
$this->populateCard($card, $paymentForm);
}
if ($order) {
if ($billingAddress = $order->getBillingAddress()) {
// Set top level names to the billing names
$card->setFirstName($billingAddress->firstName);
$card->setLastName($billingAddress->lastName);
$card->setBillingFirstName($billingAddress->firstName);
$card->setBillingLastName($billingAddress->lastName);
$card->setBillingAddress1($billingAddress->address1);
$card->setBillingAddress2($billingAddress->address2);
$card->setBillingCity($billingAddress->city);
$card->setBillingPostcode($billingAddress->zipCode);
if ($billingAddress->getCountry()) {
$card->setBillingCountry($billingAddress->getCountry()->iso);
}
if ($billingAddress->getState()) {
$state = $billingAddress->getState()->abbreviation ?: $billingAddress->getState()->name;
$card->setBillingState($state);
}
$card->setBillingPhone($billingAddress->phone);
$card->setBillingCompany($billingAddress->businessName);
$card->setCompany($billingAddress->businessName);
}
if ($shippingAddress = $order->getShippingAddress()) {
$card->setShippingFirstName($shippingAddress->firstName);
$card->setShippingLastName($shippingAddress->lastName);
$card->setShippingAddress1($shippingAddress->address1);
$card->setShippingAddress2($shippingAddress->address2);
$card->setShippingCity($shippingAddress->city);
$card->setShippingPostcode($shippingAddress->zipCode);
if ($shippingAddress->getCountry()) {
$card->setShippingCountry($shippingAddress->getCountry()->iso);
}
if ($shippingAddress->getState()) {
$state = $shippingAddress->getState()->abbreviation ?: $shippingAddress->getState()->name;
$card->setShippingState($state);
}
$card->setShippingPhone($shippingAddress->phone);
$card->setShippingCompany($shippingAddress->businessName);
}
$card->setEmail($order->getEmail());
}
return $card;
}
|
php
|
public function createCard(BasePaymentForm $paymentForm, Order $order = null): CreditCard {
$card = new CreditCard;
if ($paymentForm instanceof CreditCardPaymentForm) {
$this->populateCard($card, $paymentForm);
}
if ($order) {
if ($billingAddress = $order->getBillingAddress()) {
// Set top level names to the billing names
$card->setFirstName($billingAddress->firstName);
$card->setLastName($billingAddress->lastName);
$card->setBillingFirstName($billingAddress->firstName);
$card->setBillingLastName($billingAddress->lastName);
$card->setBillingAddress1($billingAddress->address1);
$card->setBillingAddress2($billingAddress->address2);
$card->setBillingCity($billingAddress->city);
$card->setBillingPostcode($billingAddress->zipCode);
if ($billingAddress->getCountry()) {
$card->setBillingCountry($billingAddress->getCountry()->iso);
}
if ($billingAddress->getState()) {
$state = $billingAddress->getState()->abbreviation ?: $billingAddress->getState()->name;
$card->setBillingState($state);
}
$card->setBillingPhone($billingAddress->phone);
$card->setBillingCompany($billingAddress->businessName);
$card->setCompany($billingAddress->businessName);
}
if ($shippingAddress = $order->getShippingAddress()) {
$card->setShippingFirstName($shippingAddress->firstName);
$card->setShippingLastName($shippingAddress->lastName);
$card->setShippingAddress1($shippingAddress->address1);
$card->setShippingAddress2($shippingAddress->address2);
$card->setShippingCity($shippingAddress->city);
$card->setShippingPostcode($shippingAddress->zipCode);
if ($shippingAddress->getCountry()) {
$card->setShippingCountry($shippingAddress->getCountry()->iso);
}
if ($shippingAddress->getState()) {
$state = $shippingAddress->getState()->abbreviation ?: $shippingAddress->getState()->name;
$card->setShippingState($state);
}
$card->setShippingPhone($shippingAddress->phone);
$card->setShippingCompany($shippingAddress->businessName);
}
$card->setEmail($order->getEmail());
}
return $card;
}
|
[
"public",
"function",
"createCard",
"(",
"BasePaymentForm",
"$",
"paymentForm",
",",
"Order",
"$",
"order",
"=",
"null",
")",
":",
"CreditCard",
"{",
"$",
"card",
"=",
"new",
"CreditCard",
";",
"if",
"(",
"$",
"paymentForm",
"instanceof",
"CreditCardPaymentForm",
")",
"{",
"$",
"this",
"->",
"populateCard",
"(",
"$",
"card",
",",
"$",
"paymentForm",
")",
";",
"}",
"if",
"(",
"$",
"order",
")",
"{",
"if",
"(",
"$",
"billingAddress",
"=",
"$",
"order",
"->",
"getBillingAddress",
"(",
")",
")",
"{",
"// Set top level names to the billing names",
"$",
"card",
"->",
"setFirstName",
"(",
"$",
"billingAddress",
"->",
"firstName",
")",
";",
"$",
"card",
"->",
"setLastName",
"(",
"$",
"billingAddress",
"->",
"lastName",
")",
";",
"$",
"card",
"->",
"setBillingFirstName",
"(",
"$",
"billingAddress",
"->",
"firstName",
")",
";",
"$",
"card",
"->",
"setBillingLastName",
"(",
"$",
"billingAddress",
"->",
"lastName",
")",
";",
"$",
"card",
"->",
"setBillingAddress1",
"(",
"$",
"billingAddress",
"->",
"address1",
")",
";",
"$",
"card",
"->",
"setBillingAddress2",
"(",
"$",
"billingAddress",
"->",
"address2",
")",
";",
"$",
"card",
"->",
"setBillingCity",
"(",
"$",
"billingAddress",
"->",
"city",
")",
";",
"$",
"card",
"->",
"setBillingPostcode",
"(",
"$",
"billingAddress",
"->",
"zipCode",
")",
";",
"if",
"(",
"$",
"billingAddress",
"->",
"getCountry",
"(",
")",
")",
"{",
"$",
"card",
"->",
"setBillingCountry",
"(",
"$",
"billingAddress",
"->",
"getCountry",
"(",
")",
"->",
"iso",
")",
";",
"}",
"if",
"(",
"$",
"billingAddress",
"->",
"getState",
"(",
")",
")",
"{",
"$",
"state",
"=",
"$",
"billingAddress",
"->",
"getState",
"(",
")",
"->",
"abbreviation",
"?",
":",
"$",
"billingAddress",
"->",
"getState",
"(",
")",
"->",
"name",
";",
"$",
"card",
"->",
"setBillingState",
"(",
"$",
"state",
")",
";",
"}",
"$",
"card",
"->",
"setBillingPhone",
"(",
"$",
"billingAddress",
"->",
"phone",
")",
";",
"$",
"card",
"->",
"setBillingCompany",
"(",
"$",
"billingAddress",
"->",
"businessName",
")",
";",
"$",
"card",
"->",
"setCompany",
"(",
"$",
"billingAddress",
"->",
"businessName",
")",
";",
"}",
"if",
"(",
"$",
"shippingAddress",
"=",
"$",
"order",
"->",
"getShippingAddress",
"(",
")",
")",
"{",
"$",
"card",
"->",
"setShippingFirstName",
"(",
"$",
"shippingAddress",
"->",
"firstName",
")",
";",
"$",
"card",
"->",
"setShippingLastName",
"(",
"$",
"shippingAddress",
"->",
"lastName",
")",
";",
"$",
"card",
"->",
"setShippingAddress1",
"(",
"$",
"shippingAddress",
"->",
"address1",
")",
";",
"$",
"card",
"->",
"setShippingAddress2",
"(",
"$",
"shippingAddress",
"->",
"address2",
")",
";",
"$",
"card",
"->",
"setShippingCity",
"(",
"$",
"shippingAddress",
"->",
"city",
")",
";",
"$",
"card",
"->",
"setShippingPostcode",
"(",
"$",
"shippingAddress",
"->",
"zipCode",
")",
";",
"if",
"(",
"$",
"shippingAddress",
"->",
"getCountry",
"(",
")",
")",
"{",
"$",
"card",
"->",
"setShippingCountry",
"(",
"$",
"shippingAddress",
"->",
"getCountry",
"(",
")",
"->",
"iso",
")",
";",
"}",
"if",
"(",
"$",
"shippingAddress",
"->",
"getState",
"(",
")",
")",
"{",
"$",
"state",
"=",
"$",
"shippingAddress",
"->",
"getState",
"(",
")",
"->",
"abbreviation",
"?",
":",
"$",
"shippingAddress",
"->",
"getState",
"(",
")",
"->",
"name",
";",
"$",
"card",
"->",
"setShippingState",
"(",
"$",
"state",
")",
";",
"}",
"$",
"card",
"->",
"setShippingPhone",
"(",
"$",
"shippingAddress",
"->",
"phone",
")",
";",
"$",
"card",
"->",
"setShippingCompany",
"(",
"$",
"shippingAddress",
"->",
"businessName",
")",
";",
"}",
"$",
"card",
"->",
"setEmail",
"(",
"$",
"order",
"->",
"getEmail",
"(",
")",
")",
";",
"}",
"return",
"$",
"card",
";",
"}"
] |
Create a card object using the payment form and the optional order
@param BasePaymentForm $paymentForm
@param Order $order
@return CreditCard
|
[
"Create",
"a",
"card",
"object",
"using",
"the",
"payment",
"form",
"and",
"the",
"optional",
"order"
] |
91fd24ebe3fc19a776915e7636e432a41d7deace
|
https://github.com/craftcms/commerce-omnipay/blob/91fd24ebe3fc19a776915e7636e432a41d7deace/src/base/Gateway.php#L189-L246
|
27,121
|
craftcms/commerce-omnipay
|
src/base/Gateway.php
|
Gateway.populateCard
|
public function populateCard($card, CreditCardPaymentForm $paymentForm)
{
if (!$card instanceof CreditCard) {
return;
}
$card->setFirstName($paymentForm->firstName);
$card->setLastName($paymentForm->lastName);
$card->setNumber($paymentForm->number);
$card->setExpiryMonth($paymentForm->month);
$card->setExpiryYear($paymentForm->year);
$card->setCvv($paymentForm->cvv);
}
|
php
|
public function populateCard($card, CreditCardPaymentForm $paymentForm)
{
if (!$card instanceof CreditCard) {
return;
}
$card->setFirstName($paymentForm->firstName);
$card->setLastName($paymentForm->lastName);
$card->setNumber($paymentForm->number);
$card->setExpiryMonth($paymentForm->month);
$card->setExpiryYear($paymentForm->year);
$card->setCvv($paymentForm->cvv);
}
|
[
"public",
"function",
"populateCard",
"(",
"$",
"card",
",",
"CreditCardPaymentForm",
"$",
"paymentForm",
")",
"{",
"if",
"(",
"!",
"$",
"card",
"instanceof",
"CreditCard",
")",
"{",
"return",
";",
"}",
"$",
"card",
"->",
"setFirstName",
"(",
"$",
"paymentForm",
"->",
"firstName",
")",
";",
"$",
"card",
"->",
"setLastName",
"(",
"$",
"paymentForm",
"->",
"lastName",
")",
";",
"$",
"card",
"->",
"setNumber",
"(",
"$",
"paymentForm",
"->",
"number",
")",
";",
"$",
"card",
"->",
"setExpiryMonth",
"(",
"$",
"paymentForm",
"->",
"month",
")",
";",
"$",
"card",
"->",
"setExpiryYear",
"(",
"$",
"paymentForm",
"->",
"year",
")",
";",
"$",
"card",
"->",
"setCvv",
"(",
"$",
"paymentForm",
"->",
"cvv",
")",
";",
"}"
] |
Populate a credit card from the paymnent form.
@param CreditCard $card The credit card to populate.
@param CreditCardPaymentForm $paymentForm The payment form.
@return void
|
[
"Populate",
"a",
"credit",
"card",
"from",
"the",
"paymnent",
"form",
"."
] |
91fd24ebe3fc19a776915e7636e432a41d7deace
|
https://github.com/craftcms/commerce-omnipay/blob/91fd24ebe3fc19a776915e7636e432a41d7deace/src/base/Gateway.php#L320-L332
|
27,122
|
craftcms/commerce-omnipay
|
src/base/Gateway.php
|
Gateway.createItemBagForOrder
|
protected function createItemBagForOrder(Order $order)
{
if (!$this->sendCartInfo) {
return null;
}
$items = $this->getItemListForOrder($order);
$itemBagClassName = $this->getItemBagClassName();
return new $itemBagClassName($items);
}
|
php
|
protected function createItemBagForOrder(Order $order)
{
if (!$this->sendCartInfo) {
return null;
}
$items = $this->getItemListForOrder($order);
$itemBagClassName = $this->getItemBagClassName();
return new $itemBagClassName($items);
}
|
[
"protected",
"function",
"createItemBagForOrder",
"(",
"Order",
"$",
"order",
")",
"{",
"if",
"(",
"!",
"$",
"this",
"->",
"sendCartInfo",
")",
"{",
"return",
"null",
";",
"}",
"$",
"items",
"=",
"$",
"this",
"->",
"getItemListForOrder",
"(",
"$",
"order",
")",
";",
"$",
"itemBagClassName",
"=",
"$",
"this",
"->",
"getItemBagClassName",
"(",
")",
";",
"return",
"new",
"$",
"itemBagClassName",
"(",
"$",
"items",
")",
";",
"}"
] |
Create a gateway specific item bag for the order.
@param Order $order The order.
@return ItemBag|null
|
[
"Create",
"a",
"gateway",
"specific",
"item",
"bag",
"for",
"the",
"order",
"."
] |
91fd24ebe3fc19a776915e7636e432a41d7deace
|
https://github.com/craftcms/commerce-omnipay/blob/91fd24ebe3fc19a776915e7636e432a41d7deace/src/base/Gateway.php#L472-L482
|
27,123
|
craftcms/commerce-omnipay
|
src/base/Gateway.php
|
Gateway.createPaymentRequest
|
protected function createPaymentRequest(Transaction $transaction, $card = null, $itemBag = null): array
{
$params = ['commerceTransactionId' => $transaction->id, 'commerceTransactionHash' => $transaction->hash];
$request = [
'amount' => $transaction->paymentAmount,
'currency' => $transaction->paymentCurrency,
'transactionId' => $transaction->hash,
'description' => Craft::t('commerce', 'Order').' #'.$transaction->orderId,
'clientIp' => Craft::$app->getRequest()->userIP,
'transactionReference' => $transaction->hash,
'returnUrl' => UrlHelper::actionUrl('commerce/payments/complete-payment', $params),
'cancelUrl' => UrlHelper::siteUrl($transaction->order->cancelUrl),
];
// Set the webhook url.
if ($this->supportsWebhooks()) {
$request['notifyUrl'] = $this->getWebhookUrl($params);
$request['notifyUrl'] = str_replace('rc.craft.local', 'umbushka.eu.ngrok.io', $request['notifyUrl']);
}
// Do not use IPv6 loopback
if ($request['clientIp'] === '::1') {
$request['clientIp'] = '127.0.0.1';
}
// custom gateways may wish to access the order directly
$request['order'] = $transaction->order;
$request['orderId'] = $transaction->order->id;
// Stripe only params
$request['receiptEmail'] = $transaction->order->email;
// Paypal only params
$request['noShipping'] = 1;
$request['allowNote'] = 0;
$request['addressOverride'] = 1;
$request['buttonSource'] = 'ccommerce_SP';
if ($card) {
$request['card'] = $card;
}
if ($itemBag) {
$request['items'] = $itemBag;
}
return $request;
}
|
php
|
protected function createPaymentRequest(Transaction $transaction, $card = null, $itemBag = null): array
{
$params = ['commerceTransactionId' => $transaction->id, 'commerceTransactionHash' => $transaction->hash];
$request = [
'amount' => $transaction->paymentAmount,
'currency' => $transaction->paymentCurrency,
'transactionId' => $transaction->hash,
'description' => Craft::t('commerce', 'Order').' #'.$transaction->orderId,
'clientIp' => Craft::$app->getRequest()->userIP,
'transactionReference' => $transaction->hash,
'returnUrl' => UrlHelper::actionUrl('commerce/payments/complete-payment', $params),
'cancelUrl' => UrlHelper::siteUrl($transaction->order->cancelUrl),
];
// Set the webhook url.
if ($this->supportsWebhooks()) {
$request['notifyUrl'] = $this->getWebhookUrl($params);
$request['notifyUrl'] = str_replace('rc.craft.local', 'umbushka.eu.ngrok.io', $request['notifyUrl']);
}
// Do not use IPv6 loopback
if ($request['clientIp'] === '::1') {
$request['clientIp'] = '127.0.0.1';
}
// custom gateways may wish to access the order directly
$request['order'] = $transaction->order;
$request['orderId'] = $transaction->order->id;
// Stripe only params
$request['receiptEmail'] = $transaction->order->email;
// Paypal only params
$request['noShipping'] = 1;
$request['allowNote'] = 0;
$request['addressOverride'] = 1;
$request['buttonSource'] = 'ccommerce_SP';
if ($card) {
$request['card'] = $card;
}
if ($itemBag) {
$request['items'] = $itemBag;
}
return $request;
}
|
[
"protected",
"function",
"createPaymentRequest",
"(",
"Transaction",
"$",
"transaction",
",",
"$",
"card",
"=",
"null",
",",
"$",
"itemBag",
"=",
"null",
")",
":",
"array",
"{",
"$",
"params",
"=",
"[",
"'commerceTransactionId'",
"=>",
"$",
"transaction",
"->",
"id",
",",
"'commerceTransactionHash'",
"=>",
"$",
"transaction",
"->",
"hash",
"]",
";",
"$",
"request",
"=",
"[",
"'amount'",
"=>",
"$",
"transaction",
"->",
"paymentAmount",
",",
"'currency'",
"=>",
"$",
"transaction",
"->",
"paymentCurrency",
",",
"'transactionId'",
"=>",
"$",
"transaction",
"->",
"hash",
",",
"'description'",
"=>",
"Craft",
"::",
"t",
"(",
"'commerce'",
",",
"'Order'",
")",
".",
"' #'",
".",
"$",
"transaction",
"->",
"orderId",
",",
"'clientIp'",
"=>",
"Craft",
"::",
"$",
"app",
"->",
"getRequest",
"(",
")",
"->",
"userIP",
",",
"'transactionReference'",
"=>",
"$",
"transaction",
"->",
"hash",
",",
"'returnUrl'",
"=>",
"UrlHelper",
"::",
"actionUrl",
"(",
"'commerce/payments/complete-payment'",
",",
"$",
"params",
")",
",",
"'cancelUrl'",
"=>",
"UrlHelper",
"::",
"siteUrl",
"(",
"$",
"transaction",
"->",
"order",
"->",
"cancelUrl",
")",
",",
"]",
";",
"// Set the webhook url.",
"if",
"(",
"$",
"this",
"->",
"supportsWebhooks",
"(",
")",
")",
"{",
"$",
"request",
"[",
"'notifyUrl'",
"]",
"=",
"$",
"this",
"->",
"getWebhookUrl",
"(",
"$",
"params",
")",
";",
"$",
"request",
"[",
"'notifyUrl'",
"]",
"=",
"str_replace",
"(",
"'rc.craft.local'",
",",
"'umbushka.eu.ngrok.io'",
",",
"$",
"request",
"[",
"'notifyUrl'",
"]",
")",
";",
"}",
"// Do not use IPv6 loopback",
"if",
"(",
"$",
"request",
"[",
"'clientIp'",
"]",
"===",
"'::1'",
")",
"{",
"$",
"request",
"[",
"'clientIp'",
"]",
"=",
"'127.0.0.1'",
";",
"}",
"// custom gateways may wish to access the order directly",
"$",
"request",
"[",
"'order'",
"]",
"=",
"$",
"transaction",
"->",
"order",
";",
"$",
"request",
"[",
"'orderId'",
"]",
"=",
"$",
"transaction",
"->",
"order",
"->",
"id",
";",
"// Stripe only params",
"$",
"request",
"[",
"'receiptEmail'",
"]",
"=",
"$",
"transaction",
"->",
"order",
"->",
"email",
";",
"// Paypal only params",
"$",
"request",
"[",
"'noShipping'",
"]",
"=",
"1",
";",
"$",
"request",
"[",
"'allowNote'",
"]",
"=",
"0",
";",
"$",
"request",
"[",
"'addressOverride'",
"]",
"=",
"1",
";",
"$",
"request",
"[",
"'buttonSource'",
"]",
"=",
"'ccommerce_SP'",
";",
"if",
"(",
"$",
"card",
")",
"{",
"$",
"request",
"[",
"'card'",
"]",
"=",
"$",
"card",
";",
"}",
"if",
"(",
"$",
"itemBag",
")",
"{",
"$",
"request",
"[",
"'items'",
"]",
"=",
"$",
"itemBag",
";",
"}",
"return",
"$",
"request",
";",
"}"
] |
Create the parameters for a payment request based on a trasaction and optional card and item list.
@param Transaction $transaction The transaction that is basis for this request.
@param CreditCard $card The credit card being used
@param ItemBag $itemBag The item list.
@return array
@throws \yii\base\Exception
|
[
"Create",
"the",
"parameters",
"for",
"a",
"payment",
"request",
"based",
"on",
"a",
"trasaction",
"and",
"optional",
"card",
"and",
"item",
"list",
"."
] |
91fd24ebe3fc19a776915e7636e432a41d7deace
|
https://github.com/craftcms/commerce-omnipay/blob/91fd24ebe3fc19a776915e7636e432a41d7deace/src/base/Gateway.php#L494-L542
|
27,124
|
craftcms/commerce-omnipay
|
src/base/Gateway.php
|
Gateway.createRequest
|
protected function createRequest(Transaction $transaction, BasePaymentForm $form = null)
{
// For authorize and capture we're referring to a transaction that already took place so no card or item shenanigans.
if (in_array($transaction->type, [TransactionRecord::TYPE_REFUND, TransactionRecord::TYPE_CAPTURE], false)) {
$request = $this->createPaymentRequest($transaction);
} else {
$order = $transaction->getOrder();
$card = null;
if ($form) {
$card = $this->createCard($form, $order);
}
$itemBag = $this->getItemBagForOrder($order);
$request = $this->createPaymentRequest($transaction, $card, $itemBag);
$this->populateRequest($request, $form);
}
return $request;
}
|
php
|
protected function createRequest(Transaction $transaction, BasePaymentForm $form = null)
{
// For authorize and capture we're referring to a transaction that already took place so no card or item shenanigans.
if (in_array($transaction->type, [TransactionRecord::TYPE_REFUND, TransactionRecord::TYPE_CAPTURE], false)) {
$request = $this->createPaymentRequest($transaction);
} else {
$order = $transaction->getOrder();
$card = null;
if ($form) {
$card = $this->createCard($form, $order);
}
$itemBag = $this->getItemBagForOrder($order);
$request = $this->createPaymentRequest($transaction, $card, $itemBag);
$this->populateRequest($request, $form);
}
return $request;
}
|
[
"protected",
"function",
"createRequest",
"(",
"Transaction",
"$",
"transaction",
",",
"BasePaymentForm",
"$",
"form",
"=",
"null",
")",
"{",
"// For authorize and capture we're referring to a transaction that already took place so no card or item shenanigans.",
"if",
"(",
"in_array",
"(",
"$",
"transaction",
"->",
"type",
",",
"[",
"TransactionRecord",
"::",
"TYPE_REFUND",
",",
"TransactionRecord",
"::",
"TYPE_CAPTURE",
"]",
",",
"false",
")",
")",
"{",
"$",
"request",
"=",
"$",
"this",
"->",
"createPaymentRequest",
"(",
"$",
"transaction",
")",
";",
"}",
"else",
"{",
"$",
"order",
"=",
"$",
"transaction",
"->",
"getOrder",
"(",
")",
";",
"$",
"card",
"=",
"null",
";",
"if",
"(",
"$",
"form",
")",
"{",
"$",
"card",
"=",
"$",
"this",
"->",
"createCard",
"(",
"$",
"form",
",",
"$",
"order",
")",
";",
"}",
"$",
"itemBag",
"=",
"$",
"this",
"->",
"getItemBagForOrder",
"(",
"$",
"order",
")",
";",
"$",
"request",
"=",
"$",
"this",
"->",
"createPaymentRequest",
"(",
"$",
"transaction",
",",
"$",
"card",
",",
"$",
"itemBag",
")",
";",
"$",
"this",
"->",
"populateRequest",
"(",
"$",
"request",
",",
"$",
"form",
")",
";",
"}",
"return",
"$",
"request",
";",
"}"
] |
Prepare a request for execution by transaction and a populated payment form.
@param Transaction $transaction
@param BasePaymentForm $form Optional for capture/refund requests.
@return mixed
@throws \yii\base\Exception
|
[
"Prepare",
"a",
"request",
"for",
"execution",
"by",
"transaction",
"and",
"a",
"populated",
"payment",
"form",
"."
] |
91fd24ebe3fc19a776915e7636e432a41d7deace
|
https://github.com/craftcms/commerce-omnipay/blob/91fd24ebe3fc19a776915e7636e432a41d7deace/src/base/Gateway.php#L554-L575
|
27,125
|
craftcms/commerce-omnipay
|
src/base/Gateway.php
|
Gateway.extractCardReference
|
protected function extractCardReference(ResponseInterface $response): string
{
if (!$response->isSuccessful()) {
throw new PaymentException($response->getMessage());
}
return (string) $response->getTransactionReference();
}
|
php
|
protected function extractCardReference(ResponseInterface $response): string
{
if (!$response->isSuccessful()) {
throw new PaymentException($response->getMessage());
}
return (string) $response->getTransactionReference();
}
|
[
"protected",
"function",
"extractCardReference",
"(",
"ResponseInterface",
"$",
"response",
")",
":",
"string",
"{",
"if",
"(",
"!",
"$",
"response",
"->",
"isSuccessful",
"(",
")",
")",
"{",
"throw",
"new",
"PaymentException",
"(",
"$",
"response",
"->",
"getMessage",
"(",
")",
")",
";",
"}",
"return",
"(",
"string",
")",
"$",
"response",
"->",
"getTransactionReference",
"(",
")",
";",
"}"
] |
Extract a card reference from a response
@param ResponseInterface $response The response to use
@return string
@throws PaymentException on failure
|
[
"Extract",
"a",
"card",
"reference",
"from",
"a",
"response"
] |
91fd24ebe3fc19a776915e7636e432a41d7deace
|
https://github.com/craftcms/commerce-omnipay/blob/91fd24ebe3fc19a776915e7636e432a41d7deace/src/base/Gateway.php#L585-L592
|
27,126
|
craftcms/commerce-omnipay
|
src/base/Gateway.php
|
Gateway.getItemBagForOrder
|
protected function getItemBagForOrder(Order $order)
{
$itemBag = $this->createItemBagForOrder($order);
$event = new ItemBagEvent([
'items' => $itemBag,
'order' => $order
]);
$this->trigger(self::EVENT_AFTER_CREATE_ITEM_BAG, $event);
return $event->items;
}
|
php
|
protected function getItemBagForOrder(Order $order)
{
$itemBag = $this->createItemBagForOrder($order);
$event = new ItemBagEvent([
'items' => $itemBag,
'order' => $order
]);
$this->trigger(self::EVENT_AFTER_CREATE_ITEM_BAG, $event);
return $event->items;
}
|
[
"protected",
"function",
"getItemBagForOrder",
"(",
"Order",
"$",
"order",
")",
"{",
"$",
"itemBag",
"=",
"$",
"this",
"->",
"createItemBagForOrder",
"(",
"$",
"order",
")",
";",
"$",
"event",
"=",
"new",
"ItemBagEvent",
"(",
"[",
"'items'",
"=>",
"$",
"itemBag",
",",
"'order'",
"=>",
"$",
"order",
"]",
")",
";",
"$",
"this",
"->",
"trigger",
"(",
"self",
"::",
"EVENT_AFTER_CREATE_ITEM_BAG",
",",
"$",
"event",
")",
";",
"return",
"$",
"event",
"->",
"items",
";",
"}"
] |
Get the item bag for the order.
@param Order $order
@return mixed
|
[
"Get",
"the",
"item",
"bag",
"for",
"the",
"order",
"."
] |
91fd24ebe3fc19a776915e7636e432a41d7deace
|
https://github.com/craftcms/commerce-omnipay/blob/91fd24ebe3fc19a776915e7636e432a41d7deace/src/base/Gateway.php#L641-L652
|
27,127
|
craftcms/commerce-omnipay
|
src/base/Gateway.php
|
Gateway.getItemListForOrder
|
protected function getItemListForOrder(Order $order): array
{
$items = [];
$priceCheck = 0;
$count = -1;
/** @var LineItem $item */
foreach ($order->lineItems as $item) {
$price = Currency::round($item->salePrice);
// Can not accept zero amount items. See item (4) here:
// https://developer.paypal.com/docs/classic/express-checkout/integration-guide/ECCustomizing/#setting-order-details-on-the-paypal-review-page
if ($price !== 0) {
$count++;
/** @var Purchasable $purchasable */
$purchasable = $item->getPurchasable();
$defaultDescription = Craft::t('commerce', 'Item ID').' '.$item->id;
$purchasableDescription = $purchasable ? $purchasable->getDescription() : $defaultDescription;
$description = isset($item->snapshot['description']) ? $item->snapshot['description'] : $purchasableDescription;
$description = empty($description) ? 'Item '.$count : $description;
$items[] = [
'name' => $description,
'description' => $description,
'quantity' => $item->qty,
'price' => $price,
];
$priceCheck += ($item->qty * $item->salePrice);
}
}
$count = -1;
/** @var OrderAdjustment $adjustment */
foreach ($order->adjustments as $adjustment) {
$price = Currency::round($adjustment->amount);
// Do not include the 'included' adjustments, and do not send zero value items
// See item (4) https://developer.paypal.com/docs/classic/express-checkout/integration-guide/ECCustomizing/#setting-order-details-on-the-paypal-review-page
if (($adjustment->included == 0 || $adjustment->included == false) && $price !== 0) {
$count++;
$items[] = [
'name' => empty($adjustment->name) ? $adjustment->type." ".$count : $adjustment->name,
'description' => empty($adjustment->description) ? $adjustment->type.' '.$count : $adjustment->description,
'quantity' => 1,
'price' => $price,
];
$priceCheck += $adjustment->amount;
}
}
$priceCheck = Currency::round($priceCheck);
$totalPrice = Currency::round($order->totalPrice);
$same = (bool)($priceCheck === $totalPrice);
if (!$same) {
Craft::error('Item bag total price does not equal the orders totalPrice, some payment gateways will complain.', __METHOD__);
}
return $items;
}
|
php
|
protected function getItemListForOrder(Order $order): array
{
$items = [];
$priceCheck = 0;
$count = -1;
/** @var LineItem $item */
foreach ($order->lineItems as $item) {
$price = Currency::round($item->salePrice);
// Can not accept zero amount items. See item (4) here:
// https://developer.paypal.com/docs/classic/express-checkout/integration-guide/ECCustomizing/#setting-order-details-on-the-paypal-review-page
if ($price !== 0) {
$count++;
/** @var Purchasable $purchasable */
$purchasable = $item->getPurchasable();
$defaultDescription = Craft::t('commerce', 'Item ID').' '.$item->id;
$purchasableDescription = $purchasable ? $purchasable->getDescription() : $defaultDescription;
$description = isset($item->snapshot['description']) ? $item->snapshot['description'] : $purchasableDescription;
$description = empty($description) ? 'Item '.$count : $description;
$items[] = [
'name' => $description,
'description' => $description,
'quantity' => $item->qty,
'price' => $price,
];
$priceCheck += ($item->qty * $item->salePrice);
}
}
$count = -1;
/** @var OrderAdjustment $adjustment */
foreach ($order->adjustments as $adjustment) {
$price = Currency::round($adjustment->amount);
// Do not include the 'included' adjustments, and do not send zero value items
// See item (4) https://developer.paypal.com/docs/classic/express-checkout/integration-guide/ECCustomizing/#setting-order-details-on-the-paypal-review-page
if (($adjustment->included == 0 || $adjustment->included == false) && $price !== 0) {
$count++;
$items[] = [
'name' => empty($adjustment->name) ? $adjustment->type." ".$count : $adjustment->name,
'description' => empty($adjustment->description) ? $adjustment->type.' '.$count : $adjustment->description,
'quantity' => 1,
'price' => $price,
];
$priceCheck += $adjustment->amount;
}
}
$priceCheck = Currency::round($priceCheck);
$totalPrice = Currency::round($order->totalPrice);
$same = (bool)($priceCheck === $totalPrice);
if (!$same) {
Craft::error('Item bag total price does not equal the orders totalPrice, some payment gateways will complain.', __METHOD__);
}
return $items;
}
|
[
"protected",
"function",
"getItemListForOrder",
"(",
"Order",
"$",
"order",
")",
":",
"array",
"{",
"$",
"items",
"=",
"[",
"]",
";",
"$",
"priceCheck",
"=",
"0",
";",
"$",
"count",
"=",
"-",
"1",
";",
"/** @var LineItem $item */",
"foreach",
"(",
"$",
"order",
"->",
"lineItems",
"as",
"$",
"item",
")",
"{",
"$",
"price",
"=",
"Currency",
"::",
"round",
"(",
"$",
"item",
"->",
"salePrice",
")",
";",
"// Can not accept zero amount items. See item (4) here:",
"// https://developer.paypal.com/docs/classic/express-checkout/integration-guide/ECCustomizing/#setting-order-details-on-the-paypal-review-page",
"if",
"(",
"$",
"price",
"!==",
"0",
")",
"{",
"$",
"count",
"++",
";",
"/** @var Purchasable $purchasable */",
"$",
"purchasable",
"=",
"$",
"item",
"->",
"getPurchasable",
"(",
")",
";",
"$",
"defaultDescription",
"=",
"Craft",
"::",
"t",
"(",
"'commerce'",
",",
"'Item ID'",
")",
".",
"' '",
".",
"$",
"item",
"->",
"id",
";",
"$",
"purchasableDescription",
"=",
"$",
"purchasable",
"?",
"$",
"purchasable",
"->",
"getDescription",
"(",
")",
":",
"$",
"defaultDescription",
";",
"$",
"description",
"=",
"isset",
"(",
"$",
"item",
"->",
"snapshot",
"[",
"'description'",
"]",
")",
"?",
"$",
"item",
"->",
"snapshot",
"[",
"'description'",
"]",
":",
"$",
"purchasableDescription",
";",
"$",
"description",
"=",
"empty",
"(",
"$",
"description",
")",
"?",
"'Item '",
".",
"$",
"count",
":",
"$",
"description",
";",
"$",
"items",
"[",
"]",
"=",
"[",
"'name'",
"=>",
"$",
"description",
",",
"'description'",
"=>",
"$",
"description",
",",
"'quantity'",
"=>",
"$",
"item",
"->",
"qty",
",",
"'price'",
"=>",
"$",
"price",
",",
"]",
";",
"$",
"priceCheck",
"+=",
"(",
"$",
"item",
"->",
"qty",
"*",
"$",
"item",
"->",
"salePrice",
")",
";",
"}",
"}",
"$",
"count",
"=",
"-",
"1",
";",
"/** @var OrderAdjustment $adjustment */",
"foreach",
"(",
"$",
"order",
"->",
"adjustments",
"as",
"$",
"adjustment",
")",
"{",
"$",
"price",
"=",
"Currency",
"::",
"round",
"(",
"$",
"adjustment",
"->",
"amount",
")",
";",
"// Do not include the 'included' adjustments, and do not send zero value items",
"// See item (4) https://developer.paypal.com/docs/classic/express-checkout/integration-guide/ECCustomizing/#setting-order-details-on-the-paypal-review-page",
"if",
"(",
"(",
"$",
"adjustment",
"->",
"included",
"==",
"0",
"||",
"$",
"adjustment",
"->",
"included",
"==",
"false",
")",
"&&",
"$",
"price",
"!==",
"0",
")",
"{",
"$",
"count",
"++",
";",
"$",
"items",
"[",
"]",
"=",
"[",
"'name'",
"=>",
"empty",
"(",
"$",
"adjustment",
"->",
"name",
")",
"?",
"$",
"adjustment",
"->",
"type",
".",
"\" \"",
".",
"$",
"count",
":",
"$",
"adjustment",
"->",
"name",
",",
"'description'",
"=>",
"empty",
"(",
"$",
"adjustment",
"->",
"description",
")",
"?",
"$",
"adjustment",
"->",
"type",
".",
"' '",
".",
"$",
"count",
":",
"$",
"adjustment",
"->",
"description",
",",
"'quantity'",
"=>",
"1",
",",
"'price'",
"=>",
"$",
"price",
",",
"]",
";",
"$",
"priceCheck",
"+=",
"$",
"adjustment",
"->",
"amount",
";",
"}",
"}",
"$",
"priceCheck",
"=",
"Currency",
"::",
"round",
"(",
"$",
"priceCheck",
")",
";",
"$",
"totalPrice",
"=",
"Currency",
"::",
"round",
"(",
"$",
"order",
"->",
"totalPrice",
")",
";",
"$",
"same",
"=",
"(",
"bool",
")",
"(",
"$",
"priceCheck",
"===",
"$",
"totalPrice",
")",
";",
"if",
"(",
"!",
"$",
"same",
")",
"{",
"Craft",
"::",
"error",
"(",
"'Item bag total price does not equal the orders totalPrice, some payment gateways will complain.'",
",",
"__METHOD__",
")",
";",
"}",
"return",
"$",
"items",
";",
"}"
] |
Generate the item list for an Order.
@param Order $order
@return array
|
[
"Generate",
"the",
"item",
"list",
"for",
"an",
"Order",
"."
] |
91fd24ebe3fc19a776915e7636e432a41d7deace
|
https://github.com/craftcms/commerce-omnipay/blob/91fd24ebe3fc19a776915e7636e432a41d7deace/src/base/Gateway.php#L661-L722
|
27,128
|
craftcms/commerce-omnipay
|
src/base/Gateway.php
|
Gateway.performRequest
|
protected function performRequest($request, $transaction): RequestResponseInterface
{
//raising event
$event = new GatewayRequestEvent([
'type' => $transaction->type,
'request' => $request,
'transaction' => $transaction
]);
// Raise 'beforeGatewayRequestSend' event
$this->trigger(self::EVENT_BEFORE_GATEWAY_REQUEST_SEND, $event);
$response = $this->sendRequest($request);
return $this->prepareResponse($response, $transaction);
}
|
php
|
protected function performRequest($request, $transaction): RequestResponseInterface
{
//raising event
$event = new GatewayRequestEvent([
'type' => $transaction->type,
'request' => $request,
'transaction' => $transaction
]);
// Raise 'beforeGatewayRequestSend' event
$this->trigger(self::EVENT_BEFORE_GATEWAY_REQUEST_SEND, $event);
$response = $this->sendRequest($request);
return $this->prepareResponse($response, $transaction);
}
|
[
"protected",
"function",
"performRequest",
"(",
"$",
"request",
",",
"$",
"transaction",
")",
":",
"RequestResponseInterface",
"{",
"//raising event",
"$",
"event",
"=",
"new",
"GatewayRequestEvent",
"(",
"[",
"'type'",
"=>",
"$",
"transaction",
"->",
"type",
",",
"'request'",
"=>",
"$",
"request",
",",
"'transaction'",
"=>",
"$",
"transaction",
"]",
")",
";",
"// Raise 'beforeGatewayRequestSend' event",
"$",
"this",
"->",
"trigger",
"(",
"self",
"::",
"EVENT_BEFORE_GATEWAY_REQUEST_SEND",
",",
"$",
"event",
")",
";",
"$",
"response",
"=",
"$",
"this",
"->",
"sendRequest",
"(",
"$",
"request",
")",
";",
"return",
"$",
"this",
"->",
"prepareResponse",
"(",
"$",
"response",
",",
"$",
"transaction",
")",
";",
"}"
] |
Perform a request and return the response.
@param $request
@param $transaction
@return RequestResponseInterface
@throws GatewayRequestCancelledException
|
[
"Perform",
"a",
"request",
"and",
"return",
"the",
"response",
"."
] |
91fd24ebe3fc19a776915e7636e432a41d7deace
|
https://github.com/craftcms/commerce-omnipay/blob/91fd24ebe3fc19a776915e7636e432a41d7deace/src/base/Gateway.php#L733-L748
|
27,129
|
craftcms/commerce-omnipay
|
src/base/Gateway.php
|
Gateway.prepareCaptureRequest
|
protected function prepareCaptureRequest($request, string $reference): RequestInterface
{
/** @var AbstractRequest $captureRequest */
$captureRequest = $this->gateway()->capture($request);
$captureRequest->setTransactionReference($reference);
return $captureRequest;
}
|
php
|
protected function prepareCaptureRequest($request, string $reference): RequestInterface
{
/** @var AbstractRequest $captureRequest */
$captureRequest = $this->gateway()->capture($request);
$captureRequest->setTransactionReference($reference);
return $captureRequest;
}
|
[
"protected",
"function",
"prepareCaptureRequest",
"(",
"$",
"request",
",",
"string",
"$",
"reference",
")",
":",
"RequestInterface",
"{",
"/** @var AbstractRequest $captureRequest */",
"$",
"captureRequest",
"=",
"$",
"this",
"->",
"gateway",
"(",
")",
"->",
"capture",
"(",
"$",
"request",
")",
";",
"$",
"captureRequest",
"->",
"setTransactionReference",
"(",
"$",
"reference",
")",
";",
"return",
"$",
"captureRequest",
";",
"}"
] |
Prepare a capture request from request data and reference of the transaction being captured.
@param array $request
@param string $reference
@return RequestInterface
|
[
"Prepare",
"a",
"capture",
"request",
"from",
"request",
"data",
"and",
"reference",
"of",
"the",
"transaction",
"being",
"captured",
"."
] |
91fd24ebe3fc19a776915e7636e432a41d7deace
|
https://github.com/craftcms/commerce-omnipay/blob/91fd24ebe3fc19a776915e7636e432a41d7deace/src/base/Gateway.php#L794-L801
|
27,130
|
craftcms/commerce-omnipay
|
src/base/Gateway.php
|
Gateway.prepareRefundRequest
|
protected function prepareRefundRequest($request, string $reference): RequestInterface
{
/** @var AbstractRequest $refundRequest */
$refundRequest = $this->gateway()->refund($request);
$refundRequest->setTransactionReference($reference);
return $refundRequest;
}
|
php
|
protected function prepareRefundRequest($request, string $reference): RequestInterface
{
/** @var AbstractRequest $refundRequest */
$refundRequest = $this->gateway()->refund($request);
$refundRequest->setTransactionReference($reference);
return $refundRequest;
}
|
[
"protected",
"function",
"prepareRefundRequest",
"(",
"$",
"request",
",",
"string",
"$",
"reference",
")",
":",
"RequestInterface",
"{",
"/** @var AbstractRequest $refundRequest */",
"$",
"refundRequest",
"=",
"$",
"this",
"->",
"gateway",
"(",
")",
"->",
"refund",
"(",
"$",
"request",
")",
";",
"$",
"refundRequest",
"->",
"setTransactionReference",
"(",
"$",
"reference",
")",
";",
"return",
"$",
"refundRequest",
";",
"}"
] |
Prepare a refund request from request data and reference of the transaction being refunded.
@param array $request
@param string $reference
@return RequestInterface
|
[
"Prepare",
"a",
"refund",
"request",
"from",
"request",
"data",
"and",
"reference",
"of",
"the",
"transaction",
"being",
"refunded",
"."
] |
91fd24ebe3fc19a776915e7636e432a41d7deace
|
https://github.com/craftcms/commerce-omnipay/blob/91fd24ebe3fc19a776915e7636e432a41d7deace/src/base/Gateway.php#L837-L845
|
27,131
|
craftcms/commerce-omnipay
|
src/base/Gateway.php
|
Gateway.sendRequest
|
protected function sendRequest(RequestInterface $request): ResponseInterface
{
$data = $request->getData();
$event = new SendPaymentRequestEvent([
'requestData' => $data
]);
// Raise 'beforeSendPaymentRequest' event
$this->trigger(self::EVENT_BEFORE_SEND_PAYMENT_REQUEST, $event);
// We can't merge the $data with $modifiedData since the $data is not always an array.
// For example it could be a XML object, json, or anything else really.
if ($event->modifiedRequestData !== null) {
return $request->sendData($event->modifiedRequestData);
}
return $request->send();
}
|
php
|
protected function sendRequest(RequestInterface $request): ResponseInterface
{
$data = $request->getData();
$event = new SendPaymentRequestEvent([
'requestData' => $data
]);
// Raise 'beforeSendPaymentRequest' event
$this->trigger(self::EVENT_BEFORE_SEND_PAYMENT_REQUEST, $event);
// We can't merge the $data with $modifiedData since the $data is not always an array.
// For example it could be a XML object, json, or anything else really.
if ($event->modifiedRequestData !== null) {
return $request->sendData($event->modifiedRequestData);
}
return $request->send();
}
|
[
"protected",
"function",
"sendRequest",
"(",
"RequestInterface",
"$",
"request",
")",
":",
"ResponseInterface",
"{",
"$",
"data",
"=",
"$",
"request",
"->",
"getData",
"(",
")",
";",
"$",
"event",
"=",
"new",
"SendPaymentRequestEvent",
"(",
"[",
"'requestData'",
"=>",
"$",
"data",
"]",
")",
";",
"// Raise 'beforeSendPaymentRequest' event",
"$",
"this",
"->",
"trigger",
"(",
"self",
"::",
"EVENT_BEFORE_SEND_PAYMENT_REQUEST",
",",
"$",
"event",
")",
";",
"// We can't merge the $data with $modifiedData since the $data is not always an array.",
"// For example it could be a XML object, json, or anything else really.",
"if",
"(",
"$",
"event",
"->",
"modifiedRequestData",
"!==",
"null",
")",
"{",
"return",
"$",
"request",
"->",
"sendData",
"(",
"$",
"event",
"->",
"modifiedRequestData",
")",
";",
"}",
"return",
"$",
"request",
"->",
"send",
"(",
")",
";",
"}"
] |
Send a request to the actual gateway.
@param RequestInterface $request
@return ResponseInterface
|
[
"Send",
"a",
"request",
"to",
"the",
"actual",
"gateway",
"."
] |
91fd24ebe3fc19a776915e7636e432a41d7deace
|
https://github.com/craftcms/commerce-omnipay/blob/91fd24ebe3fc19a776915e7636e432a41d7deace/src/base/Gateway.php#L854-L872
|
27,132
|
craftcms/commerce-omnipay
|
src/base/Gateway.php
|
Gateway.createOmnipayGateway
|
protected static function createOmnipayGateway(string $gatewayClassName): GatewayInterface
{
$craftClient = Craft::createGuzzleClient();
$adapter = new Client($craftClient);
$httpClient = new OmnipayClient($adapter);
return Omnipay::create($gatewayClassName, $httpClient);
}
|
php
|
protected static function createOmnipayGateway(string $gatewayClassName): GatewayInterface
{
$craftClient = Craft::createGuzzleClient();
$adapter = new Client($craftClient);
$httpClient = new OmnipayClient($adapter);
return Omnipay::create($gatewayClassName, $httpClient);
}
|
[
"protected",
"static",
"function",
"createOmnipayGateway",
"(",
"string",
"$",
"gatewayClassName",
")",
":",
"GatewayInterface",
"{",
"$",
"craftClient",
"=",
"Craft",
"::",
"createGuzzleClient",
"(",
")",
";",
"$",
"adapter",
"=",
"new",
"Client",
"(",
"$",
"craftClient",
")",
";",
"$",
"httpClient",
"=",
"new",
"OmnipayClient",
"(",
"$",
"adapter",
")",
";",
"return",
"Omnipay",
"::",
"create",
"(",
"$",
"gatewayClassName",
",",
"$",
"httpClient",
")",
";",
"}"
] |
Create the omnipay gateway.
@param string $gatewayClassName
@return GatewayInterface
|
[
"Create",
"the",
"omnipay",
"gateway",
"."
] |
91fd24ebe3fc19a776915e7636e432a41d7deace
|
https://github.com/craftcms/commerce-omnipay/blob/91fd24ebe3fc19a776915e7636e432a41d7deace/src/base/Gateway.php#L880-L887
|
27,133
|
ddeboer/transcoder
|
src/Transcoder.php
|
Transcoder.create
|
public static function create($defaultEncoding = 'UTF-8')
{
if (isset(self::$chain[$defaultEncoding])) {
return self::$chain[$defaultEncoding];
}
$transcoders = [];
try {
$transcoders[] = new MbTranscoder($defaultEncoding);
} catch (ExtensionMissingException $mb) {
// Ignore missing mbstring extension; fall back to iconv
}
try {
$transcoders[] = new IconvTranscoder($defaultEncoding);
} catch (ExtensionMissingException $iconv) {
// Neither mbstring nor iconv
throw $iconv;
}
self::$chain[$defaultEncoding] = new self($transcoders);
return self::$chain[$defaultEncoding];
}
|
php
|
public static function create($defaultEncoding = 'UTF-8')
{
if (isset(self::$chain[$defaultEncoding])) {
return self::$chain[$defaultEncoding];
}
$transcoders = [];
try {
$transcoders[] = new MbTranscoder($defaultEncoding);
} catch (ExtensionMissingException $mb) {
// Ignore missing mbstring extension; fall back to iconv
}
try {
$transcoders[] = new IconvTranscoder($defaultEncoding);
} catch (ExtensionMissingException $iconv) {
// Neither mbstring nor iconv
throw $iconv;
}
self::$chain[$defaultEncoding] = new self($transcoders);
return self::$chain[$defaultEncoding];
}
|
[
"public",
"static",
"function",
"create",
"(",
"$",
"defaultEncoding",
"=",
"'UTF-8'",
")",
"{",
"if",
"(",
"isset",
"(",
"self",
"::",
"$",
"chain",
"[",
"$",
"defaultEncoding",
"]",
")",
")",
"{",
"return",
"self",
"::",
"$",
"chain",
"[",
"$",
"defaultEncoding",
"]",
";",
"}",
"$",
"transcoders",
"=",
"[",
"]",
";",
"try",
"{",
"$",
"transcoders",
"[",
"]",
"=",
"new",
"MbTranscoder",
"(",
"$",
"defaultEncoding",
")",
";",
"}",
"catch",
"(",
"ExtensionMissingException",
"$",
"mb",
")",
"{",
"// Ignore missing mbstring extension; fall back to iconv",
"}",
"try",
"{",
"$",
"transcoders",
"[",
"]",
"=",
"new",
"IconvTranscoder",
"(",
"$",
"defaultEncoding",
")",
";",
"}",
"catch",
"(",
"ExtensionMissingException",
"$",
"iconv",
")",
"{",
"// Neither mbstring nor iconv",
"throw",
"$",
"iconv",
";",
"}",
"self",
"::",
"$",
"chain",
"[",
"$",
"defaultEncoding",
"]",
"=",
"new",
"self",
"(",
"$",
"transcoders",
")",
";",
"return",
"self",
"::",
"$",
"chain",
"[",
"$",
"defaultEncoding",
"]",
";",
"}"
] |
Create a transcoder
@param string $defaultEncoding
@return TranscoderInterface
@throws ExtensionMissingException
|
[
"Create",
"a",
"transcoder"
] |
e56652fe3b97908def6f53d97498952bbf13a9f6
|
https://github.com/ddeboer/transcoder/blob/e56652fe3b97908def6f53d97498952bbf13a9f6/src/Transcoder.php#L47-L71
|
27,134
|
axiom-labs/rivescript-php
|
src/Cortex/Tags/Bot.php
|
Bot.parse
|
public function parse($source, Input $input)
{
if (! $this->sourceAllowed()) {
return $source;
}
if ($this->hasMatches($source)) {
$matches = $this->getMatches($source);
$variables = synapse()->memory->variables();
foreach ($matches as $match) {
$source = str_replace($match[0], $variables[$match[1]], $source);
}
}
return $source;
}
|
php
|
public function parse($source, Input $input)
{
if (! $this->sourceAllowed()) {
return $source;
}
if ($this->hasMatches($source)) {
$matches = $this->getMatches($source);
$variables = synapse()->memory->variables();
foreach ($matches as $match) {
$source = str_replace($match[0], $variables[$match[1]], $source);
}
}
return $source;
}
|
[
"public",
"function",
"parse",
"(",
"$",
"source",
",",
"Input",
"$",
"input",
")",
"{",
"if",
"(",
"!",
"$",
"this",
"->",
"sourceAllowed",
"(",
")",
")",
"{",
"return",
"$",
"source",
";",
"}",
"if",
"(",
"$",
"this",
"->",
"hasMatches",
"(",
"$",
"source",
")",
")",
"{",
"$",
"matches",
"=",
"$",
"this",
"->",
"getMatches",
"(",
"$",
"source",
")",
";",
"$",
"variables",
"=",
"synapse",
"(",
")",
"->",
"memory",
"->",
"variables",
"(",
")",
";",
"foreach",
"(",
"$",
"matches",
"as",
"$",
"match",
")",
"{",
"$",
"source",
"=",
"str_replace",
"(",
"$",
"match",
"[",
"0",
"]",
",",
"$",
"variables",
"[",
"$",
"match",
"[",
"1",
"]",
"]",
",",
"$",
"source",
")",
";",
"}",
"}",
"return",
"$",
"source",
";",
"}"
] |
Parse the source.
@param string $source
@return string
|
[
"Parse",
"the",
"source",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Cortex/Tags/Bot.php#L28-L44
|
27,135
|
bitpressio/blade-extensions
|
src/BladeExtensionServiceProvider.php
|
BladeExtensionServiceProvider.defineBladeExtensions
|
protected function defineBladeExtensions()
{
foreach ($this->app->tagged('blade.extension') as $extension) {
if (! $extension instanceof BladeExtension) {
throw new InvalidBladeExtension($extension);
}
foreach ($extension->getDirectives() as $name => $callable) {
$this->app['blade.compiler']->directive($name, $callable);
}
foreach ($extension->getConditionals() as $name => $callable) {
$this->app['blade.compiler']->if($name, $callable);
}
}
}
|
php
|
protected function defineBladeExtensions()
{
foreach ($this->app->tagged('blade.extension') as $extension) {
if (! $extension instanceof BladeExtension) {
throw new InvalidBladeExtension($extension);
}
foreach ($extension->getDirectives() as $name => $callable) {
$this->app['blade.compiler']->directive($name, $callable);
}
foreach ($extension->getConditionals() as $name => $callable) {
$this->app['blade.compiler']->if($name, $callable);
}
}
}
|
[
"protected",
"function",
"defineBladeExtensions",
"(",
")",
"{",
"foreach",
"(",
"$",
"this",
"->",
"app",
"->",
"tagged",
"(",
"'blade.extension'",
")",
"as",
"$",
"extension",
")",
"{",
"if",
"(",
"!",
"$",
"extension",
"instanceof",
"BladeExtension",
")",
"{",
"throw",
"new",
"InvalidBladeExtension",
"(",
"$",
"extension",
")",
";",
"}",
"foreach",
"(",
"$",
"extension",
"->",
"getDirectives",
"(",
")",
"as",
"$",
"name",
"=>",
"$",
"callable",
")",
"{",
"$",
"this",
"->",
"app",
"[",
"'blade.compiler'",
"]",
"->",
"directive",
"(",
"$",
"name",
",",
"$",
"callable",
")",
";",
"}",
"foreach",
"(",
"$",
"extension",
"->",
"getConditionals",
"(",
")",
"as",
"$",
"name",
"=>",
"$",
"callable",
")",
"{",
"$",
"this",
"->",
"app",
"[",
"'blade.compiler'",
"]",
"->",
"if",
"(",
"$",
"name",
",",
"$",
"callable",
")",
";",
"}",
"}",
"}"
] |
Register Blade Extension directives and conditionals with the blade compiler
@return void
|
[
"Register",
"Blade",
"Extension",
"directives",
"and",
"conditionals",
"with",
"the",
"blade",
"compiler"
] |
eeb4f3acb7253ee28302530c0bd25c5113248507
|
https://github.com/bitpressio/blade-extensions/blob/eeb4f3acb7253ee28302530c0bd25c5113248507/src/BladeExtensionServiceProvider.php#L48-L63
|
27,136
|
axiom-labs/rivescript-php
|
src/Cortex/Commands/Trigger.php
|
Trigger.determineTriggerType
|
protected function determineTriggerType($trigger)
{
$wildcards = [
'alphabetic' => '/_/',
'numeric' => '/#/',
'global' => '/\*/',
];
foreach ($wildcards as $type => $pattern) {
if (@preg_match_all($pattern, $trigger, $stars)) {
return $type;
}
}
return 'atomic';
}
|
php
|
protected function determineTriggerType($trigger)
{
$wildcards = [
'alphabetic' => '/_/',
'numeric' => '/#/',
'global' => '/\*/',
];
foreach ($wildcards as $type => $pattern) {
if (@preg_match_all($pattern, $trigger, $stars)) {
return $type;
}
}
return 'atomic';
}
|
[
"protected",
"function",
"determineTriggerType",
"(",
"$",
"trigger",
")",
"{",
"$",
"wildcards",
"=",
"[",
"'alphabetic'",
"=>",
"'/_/'",
",",
"'numeric'",
"=>",
"'/#/'",
",",
"'global'",
"=>",
"'/\\*/'",
",",
"]",
";",
"foreach",
"(",
"$",
"wildcards",
"as",
"$",
"type",
"=>",
"$",
"pattern",
")",
"{",
"if",
"(",
"@",
"preg_match_all",
"(",
"$",
"pattern",
",",
"$",
"trigger",
",",
"$",
"stars",
")",
")",
"{",
"return",
"$",
"type",
";",
"}",
"}",
"return",
"'atomic'",
";",
"}"
] |
Determine the type of trigger to aid in sorting.
@param string $trigger
@return string
|
[
"Determine",
"the",
"type",
"of",
"trigger",
"to",
"aid",
"in",
"sorting",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Cortex/Commands/Trigger.php#L45-L60
|
27,137
|
axiom-labs/rivescript-php
|
src/Cortex/Commands/Trigger.php
|
Trigger.sortTriggers
|
protected function sortTriggers($triggers)
{
$triggers = $this->determineWordCount($triggers);
$triggers = $this->determineTypeCount($triggers);
$triggers = $triggers->sort(function ($current, $previous) {
return ($current['order'] < $previous['order']) ? -1 : 1;
})->reverse();
return $triggers;
}
|
php
|
protected function sortTriggers($triggers)
{
$triggers = $this->determineWordCount($triggers);
$triggers = $this->determineTypeCount($triggers);
$triggers = $triggers->sort(function ($current, $previous) {
return ($current['order'] < $previous['order']) ? -1 : 1;
})->reverse();
return $triggers;
}
|
[
"protected",
"function",
"sortTriggers",
"(",
"$",
"triggers",
")",
"{",
"$",
"triggers",
"=",
"$",
"this",
"->",
"determineWordCount",
"(",
"$",
"triggers",
")",
";",
"$",
"triggers",
"=",
"$",
"this",
"->",
"determineTypeCount",
"(",
"$",
"triggers",
")",
";",
"$",
"triggers",
"=",
"$",
"triggers",
"->",
"sort",
"(",
"function",
"(",
"$",
"current",
",",
"$",
"previous",
")",
"{",
"return",
"(",
"$",
"current",
"[",
"'order'",
"]",
"<",
"$",
"previous",
"[",
"'order'",
"]",
")",
"?",
"-",
"1",
":",
"1",
";",
"}",
")",
"->",
"reverse",
"(",
")",
";",
"return",
"$",
"triggers",
";",
"}"
] |
Sort triggers based on type and word count from
largest to smallest.
@param Collection $triggers
@return Collection
|
[
"Sort",
"triggers",
"based",
"on",
"type",
"and",
"word",
"count",
"from",
"largest",
"to",
"smallest",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Cortex/Commands/Trigger.php#L70-L80
|
27,138
|
axiom-labs/rivescript-php
|
src/Cortex/Commands/Trigger.php
|
Trigger.determineWordCount
|
protected function determineWordCount($triggers)
{
$triggers = $triggers->each(function ($data, $trigger) use ($triggers) {
$data['order'] = count(explode(' ', $trigger));
$triggers->put($trigger, $data);
});
return $triggers;
}
|
php
|
protected function determineWordCount($triggers)
{
$triggers = $triggers->each(function ($data, $trigger) use ($triggers) {
$data['order'] = count(explode(' ', $trigger));
$triggers->put($trigger, $data);
});
return $triggers;
}
|
[
"protected",
"function",
"determineWordCount",
"(",
"$",
"triggers",
")",
"{",
"$",
"triggers",
"=",
"$",
"triggers",
"->",
"each",
"(",
"function",
"(",
"$",
"data",
",",
"$",
"trigger",
")",
"use",
"(",
"$",
"triggers",
")",
"{",
"$",
"data",
"[",
"'order'",
"]",
"=",
"count",
"(",
"explode",
"(",
"' '",
",",
"$",
"trigger",
")",
")",
";",
"$",
"triggers",
"->",
"put",
"(",
"$",
"trigger",
",",
"$",
"data",
")",
";",
"}",
")",
";",
"return",
"$",
"triggers",
";",
"}"
] |
Sort triggers based on word count from
largest to smallest.
@param Collection $triggers
@return Collection
|
[
"Sort",
"triggers",
"based",
"on",
"word",
"count",
"from",
"largest",
"to",
"smallest",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Cortex/Commands/Trigger.php#L114-L123
|
27,139
|
axiom-labs/rivescript-php
|
src/Cortex/Input.php
|
Input.cleanOriginalSource
|
protected function cleanOriginalSource()
{
$patterns = synapse()->memory->substitute()->keys()->all();
$replacements = synapse()->memory->substitute()->values()->all();
$this->source = mb_strtolower($this->original);
$this->source = preg_replace($patterns, $replacements, $this->source);
$this->source = preg_replace('/[^\pL\d\s]+/u', '', $this->source);
$this->source = remove_whitespace($this->source);
}
|
php
|
protected function cleanOriginalSource()
{
$patterns = synapse()->memory->substitute()->keys()->all();
$replacements = synapse()->memory->substitute()->values()->all();
$this->source = mb_strtolower($this->original);
$this->source = preg_replace($patterns, $replacements, $this->source);
$this->source = preg_replace('/[^\pL\d\s]+/u', '', $this->source);
$this->source = remove_whitespace($this->source);
}
|
[
"protected",
"function",
"cleanOriginalSource",
"(",
")",
"{",
"$",
"patterns",
"=",
"synapse",
"(",
")",
"->",
"memory",
"->",
"substitute",
"(",
")",
"->",
"keys",
"(",
")",
"->",
"all",
"(",
")",
";",
"$",
"replacements",
"=",
"synapse",
"(",
")",
"->",
"memory",
"->",
"substitute",
"(",
")",
"->",
"values",
"(",
")",
"->",
"all",
"(",
")",
";",
"$",
"this",
"->",
"source",
"=",
"mb_strtolower",
"(",
"$",
"this",
"->",
"original",
")",
";",
"$",
"this",
"->",
"source",
"=",
"preg_replace",
"(",
"$",
"patterns",
",",
"$",
"replacements",
",",
"$",
"this",
"->",
"source",
")",
";",
"$",
"this",
"->",
"source",
"=",
"preg_replace",
"(",
"'/[^\\pL\\d\\s]+/u'",
",",
"''",
",",
"$",
"this",
"->",
"source",
")",
";",
"$",
"this",
"->",
"source",
"=",
"remove_whitespace",
"(",
"$",
"this",
"->",
"source",
")",
";",
"}"
] |
Clean the source input, so its in a state easily readable
by the interpreter.
@return void
|
[
"Clean",
"the",
"source",
"input",
"so",
"its",
"in",
"a",
"state",
"easily",
"readable",
"by",
"the",
"interpreter",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Cortex/Input.php#L62-L71
|
27,140
|
GeniusesOfSymfony/ReactAMQP
|
src/Producer.php
|
Producer.publish
|
public function publish(string $message, string $routingKey, ?int $flags = null, $attributes = []): void
{
if ($this->closed) {
throw new BadMethodCallException('This Producer object is closed and cannot send any more messages.');
}
$this->messages[] = [
'message' => $message,
'routingKey' => $routingKey,
'flags' => $flags,
'attributes' => $attributes,
];
}
|
php
|
public function publish(string $message, string $routingKey, ?int $flags = null, $attributes = []): void
{
if ($this->closed) {
throw new BadMethodCallException('This Producer object is closed and cannot send any more messages.');
}
$this->messages[] = [
'message' => $message,
'routingKey' => $routingKey,
'flags' => $flags,
'attributes' => $attributes,
];
}
|
[
"public",
"function",
"publish",
"(",
"string",
"$",
"message",
",",
"string",
"$",
"routingKey",
",",
"?",
"int",
"$",
"flags",
"=",
"null",
",",
"$",
"attributes",
"=",
"[",
"]",
")",
":",
"void",
"{",
"if",
"(",
"$",
"this",
"->",
"closed",
")",
"{",
"throw",
"new",
"BadMethodCallException",
"(",
"'This Producer object is closed and cannot send any more messages.'",
")",
";",
"}",
"$",
"this",
"->",
"messages",
"[",
"]",
"=",
"[",
"'message'",
"=>",
"$",
"message",
",",
"'routingKey'",
"=>",
"$",
"routingKey",
",",
"'flags'",
"=>",
"$",
"flags",
",",
"'attributes'",
"=>",
"$",
"attributes",
",",
"]",
";",
"}"
] |
Method to publish a message to an AMQP exchange. Has the same method
signature as the exchange objects publish method.
@param string $message Message
@param string $routingKey Routing key
@param int|null $flags Flags
@param array $attributes Attributes
@throws BadMethodCallException
|
[
"Method",
"to",
"publish",
"a",
"message",
"to",
"an",
"AMQP",
"exchange",
".",
"Has",
"the",
"same",
"method",
"signature",
"as",
"the",
"exchange",
"objects",
"publish",
"method",
"."
] |
256fe50ab01cfb5baf24309bd9825a08c93c63df
|
https://github.com/GeniusesOfSymfony/ReactAMQP/blob/256fe50ab01cfb5baf24309bd9825a08c93c63df/src/Producer.php#L101-L112
|
27,141
|
GeniusesOfSymfony/ReactAMQP
|
src/Producer.php
|
Producer.close
|
public function close(): void
{
if ($this->closed) {
return;
}
$this->emit('end', [$this]);
$this->loop->cancelTimer($this->timer);
$this->removeAllListeners();
$this->exchange = null;
$this->closed = true;
}
|
php
|
public function close(): void
{
if ($this->closed) {
return;
}
$this->emit('end', [$this]);
$this->loop->cancelTimer($this->timer);
$this->removeAllListeners();
$this->exchange = null;
$this->closed = true;
}
|
[
"public",
"function",
"close",
"(",
")",
":",
"void",
"{",
"if",
"(",
"$",
"this",
"->",
"closed",
")",
"{",
"return",
";",
"}",
"$",
"this",
"->",
"emit",
"(",
"'end'",
",",
"[",
"$",
"this",
"]",
")",
";",
"$",
"this",
"->",
"loop",
"->",
"cancelTimer",
"(",
"$",
"this",
"->",
"timer",
")",
";",
"$",
"this",
"->",
"removeAllListeners",
"(",
")",
";",
"$",
"this",
"->",
"exchange",
"=",
"null",
";",
"$",
"this",
"->",
"closed",
"=",
"true",
";",
"}"
] |
Method to call when stopping listening to messages.
|
[
"Method",
"to",
"call",
"when",
"stopping",
"listening",
"to",
"messages",
"."
] |
256fe50ab01cfb5baf24309bd9825a08c93c63df
|
https://github.com/GeniusesOfSymfony/ReactAMQP/blob/256fe50ab01cfb5baf24309bd9825a08c93c63df/src/Producer.php#L152-L163
|
27,142
|
axiom-labs/rivescript-php
|
src/Support/Str.php
|
Str.startsWith
|
public static function startsWith($haystack, $needle)
{
return $needle === '' or mb_strrpos($haystack, $needle, -mb_strlen($haystack)) !== false;
}
|
php
|
public static function startsWith($haystack, $needle)
{
return $needle === '' or mb_strrpos($haystack, $needle, -mb_strlen($haystack)) !== false;
}
|
[
"public",
"static",
"function",
"startsWith",
"(",
"$",
"haystack",
",",
"$",
"needle",
")",
"{",
"return",
"$",
"needle",
"===",
"''",
"or",
"mb_strrpos",
"(",
"$",
"haystack",
",",
"$",
"needle",
",",
"-",
"mb_strlen",
"(",
"$",
"haystack",
")",
")",
"!==",
"false",
";",
"}"
] |
Determine if string starts with the supplied needle.
@param string $haystack
@param string $needle
@return bool
|
[
"Determine",
"if",
"string",
"starts",
"with",
"the",
"supplied",
"needle",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Support/Str.php#L28-L31
|
27,143
|
axiom-labs/rivescript-php
|
src/Support/Str.php
|
Str.endsWith
|
public static function endsWith($haystack, $needle)
{
return $needle === '' or (($temp = mb_strlen($haystack) - mb_strlen($needle)) >= 0 and mb_strpos($haystack, $needle, $temp) !== false);
}
|
php
|
public static function endsWith($haystack, $needle)
{
return $needle === '' or (($temp = mb_strlen($haystack) - mb_strlen($needle)) >= 0 and mb_strpos($haystack, $needle, $temp) !== false);
}
|
[
"public",
"static",
"function",
"endsWith",
"(",
"$",
"haystack",
",",
"$",
"needle",
")",
"{",
"return",
"$",
"needle",
"===",
"''",
"or",
"(",
"(",
"$",
"temp",
"=",
"mb_strlen",
"(",
"$",
"haystack",
")",
"-",
"mb_strlen",
"(",
"$",
"needle",
")",
")",
">=",
"0",
"and",
"mb_strpos",
"(",
"$",
"haystack",
",",
"$",
"needle",
",",
"$",
"temp",
")",
"!==",
"false",
")",
";",
"}"
] |
Determine if string ends with the supplied needle.
@param string $haystack
@param string $needle
@return bool
|
[
"Determine",
"if",
"string",
"ends",
"with",
"the",
"supplied",
"needle",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Support/Str.php#L41-L44
|
27,144
|
Happyr/GoogleSiteAuthenticatorBundle
|
Service/ClientProvider.php
|
ClientProvider.isTokenValid
|
public function isTokenValid(string $tokenName = null): bool
{
// We must fetch the client here. A client will automatically refresh the stored access token.
$client = $this->getClient($tokenName);
if (null === $accessToken = $client->getAccessToken()) {
return false;
}
// Get the token string from access token
$token = \json_decode($accessToken)->access_token;
$url = \sprintf('https://www.google.com/accounts/AuthSubTokenInfo?bearer_token=%s', $token);
if (false === @\file_get_contents($url)) {
return false;
}
// Retrieve HTTP status code
list($version, $statusCode, $msg) = \explode(' ', $http_response_header[0], 3);
return 200 === (int) $statusCode;
}
|
php
|
public function isTokenValid(string $tokenName = null): bool
{
// We must fetch the client here. A client will automatically refresh the stored access token.
$client = $this->getClient($tokenName);
if (null === $accessToken = $client->getAccessToken()) {
return false;
}
// Get the token string from access token
$token = \json_decode($accessToken)->access_token;
$url = \sprintf('https://www.google.com/accounts/AuthSubTokenInfo?bearer_token=%s', $token);
if (false === @\file_get_contents($url)) {
return false;
}
// Retrieve HTTP status code
list($version, $statusCode, $msg) = \explode(' ', $http_response_header[0], 3);
return 200 === (int) $statusCode;
}
|
[
"public",
"function",
"isTokenValid",
"(",
"string",
"$",
"tokenName",
"=",
"null",
")",
":",
"bool",
"{",
"// We must fetch the client here. A client will automatically refresh the stored access token.",
"$",
"client",
"=",
"$",
"this",
"->",
"getClient",
"(",
"$",
"tokenName",
")",
";",
"if",
"(",
"null",
"===",
"$",
"accessToken",
"=",
"$",
"client",
"->",
"getAccessToken",
"(",
")",
")",
"{",
"return",
"false",
";",
"}",
"// Get the token string from access token",
"$",
"token",
"=",
"\\",
"json_decode",
"(",
"$",
"accessToken",
")",
"->",
"access_token",
";",
"$",
"url",
"=",
"\\",
"sprintf",
"(",
"'https://www.google.com/accounts/AuthSubTokenInfo?bearer_token=%s'",
",",
"$",
"token",
")",
";",
"if",
"(",
"false",
"===",
"@",
"\\",
"file_get_contents",
"(",
"$",
"url",
")",
")",
"{",
"return",
"false",
";",
"}",
"// Retrieve HTTP status code",
"list",
"(",
"$",
"version",
",",
"$",
"statusCode",
",",
"$",
"msg",
")",
"=",
"\\",
"explode",
"(",
"' '",
",",
"$",
"http_response_header",
"[",
"0",
"]",
",",
"3",
")",
";",
"return",
"200",
"===",
"(",
"int",
")",
"$",
"statusCode",
";",
"}"
] |
Check if a token is valid.
This is an expensive operation that makes multiple API calls.
|
[
"Check",
"if",
"a",
"token",
"is",
"valid",
".",
"This",
"is",
"an",
"expensive",
"operation",
"that",
"makes",
"multiple",
"API",
"calls",
"."
] |
dcf0f9706ab0864a37b7d8296531fb2971176c55
|
https://github.com/Happyr/GoogleSiteAuthenticatorBundle/blob/dcf0f9706ab0864a37b7d8296531fb2971176c55/Service/ClientProvider.php#L56-L75
|
27,145
|
Happyr/GoogleSiteAuthenticatorBundle
|
Service/ClientProvider.php
|
ClientProvider.setAccessToken
|
public function setAccessToken(?string $accessToken, string $tokenName = null)
{
$name = $this->config->getKey($tokenName);
$cacheKey = $this->creteCacheKey($name);
if (null === $accessToken) {
$this->pool->deleteItem($cacheKey);
return;
}
$item = $this->pool->getItem($cacheKey)->set(new AccessToken($name, $accessToken));
$this->pool->save($item);
}
|
php
|
public function setAccessToken(?string $accessToken, string $tokenName = null)
{
$name = $this->config->getKey($tokenName);
$cacheKey = $this->creteCacheKey($name);
if (null === $accessToken) {
$this->pool->deleteItem($cacheKey);
return;
}
$item = $this->pool->getItem($cacheKey)->set(new AccessToken($name, $accessToken));
$this->pool->save($item);
}
|
[
"public",
"function",
"setAccessToken",
"(",
"?",
"string",
"$",
"accessToken",
",",
"string",
"$",
"tokenName",
"=",
"null",
")",
"{",
"$",
"name",
"=",
"$",
"this",
"->",
"config",
"->",
"getKey",
"(",
"$",
"tokenName",
")",
";",
"$",
"cacheKey",
"=",
"$",
"this",
"->",
"creteCacheKey",
"(",
"$",
"name",
")",
";",
"if",
"(",
"null",
"===",
"$",
"accessToken",
")",
"{",
"$",
"this",
"->",
"pool",
"->",
"deleteItem",
"(",
"$",
"cacheKey",
")",
";",
"return",
";",
"}",
"$",
"item",
"=",
"$",
"this",
"->",
"pool",
"->",
"getItem",
"(",
"$",
"cacheKey",
")",
"->",
"set",
"(",
"new",
"AccessToken",
"(",
"$",
"name",
",",
"$",
"accessToken",
")",
")",
";",
"$",
"this",
"->",
"pool",
"->",
"save",
"(",
"$",
"item",
")",
";",
"}"
] |
Store the access token in the storage.
|
[
"Store",
"the",
"access",
"token",
"in",
"the",
"storage",
"."
] |
dcf0f9706ab0864a37b7d8296531fb2971176c55
|
https://github.com/Happyr/GoogleSiteAuthenticatorBundle/blob/dcf0f9706ab0864a37b7d8296531fb2971176c55/Service/ClientProvider.php#L80-L93
|
27,146
|
Happyr/GoogleSiteAuthenticatorBundle
|
Service/ClientProvider.php
|
ClientProvider.getAccessToken
|
protected function getAccessToken(string $tokenName = null): ?AccessToken
{
$cacheKey = $this->creteCacheKey($this->config->getKey($tokenName));
$item = $this->pool->getItem($cacheKey);
if (!$item->isHit()) {
return null;
}
return $item->get();
}
|
php
|
protected function getAccessToken(string $tokenName = null): ?AccessToken
{
$cacheKey = $this->creteCacheKey($this->config->getKey($tokenName));
$item = $this->pool->getItem($cacheKey);
if (!$item->isHit()) {
return null;
}
return $item->get();
}
|
[
"protected",
"function",
"getAccessToken",
"(",
"string",
"$",
"tokenName",
"=",
"null",
")",
":",
"?",
"AccessToken",
"{",
"$",
"cacheKey",
"=",
"$",
"this",
"->",
"creteCacheKey",
"(",
"$",
"this",
"->",
"config",
"->",
"getKey",
"(",
"$",
"tokenName",
")",
")",
";",
"$",
"item",
"=",
"$",
"this",
"->",
"pool",
"->",
"getItem",
"(",
"$",
"cacheKey",
")",
";",
"if",
"(",
"!",
"$",
"item",
"->",
"isHit",
"(",
")",
")",
"{",
"return",
"null",
";",
"}",
"return",
"$",
"item",
"->",
"get",
"(",
")",
";",
"}"
] |
Get access token from storage.
|
[
"Get",
"access",
"token",
"from",
"storage",
"."
] |
dcf0f9706ab0864a37b7d8296531fb2971176c55
|
https://github.com/Happyr/GoogleSiteAuthenticatorBundle/blob/dcf0f9706ab0864a37b7d8296531fb2971176c55/Service/ClientProvider.php#L98-L108
|
27,147
|
Happyr/GoogleSiteAuthenticatorBundle
|
Service/ClientProvider.php
|
ClientProvider.refreshToken
|
private function refreshToken(\Google_Client $client): bool
{
$accessToken = $client->getAccessToken();
$data = \json_decode($accessToken, true);
try {
if (isset($data['refresh_token'])) {
$client->refreshToken($data['refresh_token']);
return true;
}
} catch (\Google_Auth_Exception $e) {
}
return false;
}
|
php
|
private function refreshToken(\Google_Client $client): bool
{
$accessToken = $client->getAccessToken();
$data = \json_decode($accessToken, true);
try {
if (isset($data['refresh_token'])) {
$client->refreshToken($data['refresh_token']);
return true;
}
} catch (\Google_Auth_Exception $e) {
}
return false;
}
|
[
"private",
"function",
"refreshToken",
"(",
"\\",
"Google_Client",
"$",
"client",
")",
":",
"bool",
"{",
"$",
"accessToken",
"=",
"$",
"client",
"->",
"getAccessToken",
"(",
")",
";",
"$",
"data",
"=",
"\\",
"json_decode",
"(",
"$",
"accessToken",
",",
"true",
")",
";",
"try",
"{",
"if",
"(",
"isset",
"(",
"$",
"data",
"[",
"'refresh_token'",
"]",
")",
")",
"{",
"$",
"client",
"->",
"refreshToken",
"(",
"$",
"data",
"[",
"'refresh_token'",
"]",
")",
";",
"return",
"true",
";",
"}",
"}",
"catch",
"(",
"\\",
"Google_Auth_Exception",
"$",
"e",
")",
"{",
"}",
"return",
"false",
";",
"}"
] |
If we got a refresh token, use it to retrieve a good access token.
|
[
"If",
"we",
"got",
"a",
"refresh",
"token",
"use",
"it",
"to",
"retrieve",
"a",
"good",
"access",
"token",
"."
] |
dcf0f9706ab0864a37b7d8296531fb2971176c55
|
https://github.com/Happyr/GoogleSiteAuthenticatorBundle/blob/dcf0f9706ab0864a37b7d8296531fb2971176c55/Service/ClientProvider.php#L113-L128
|
27,148
|
axiom-labs/rivescript-php
|
src/Cortex/Brain.php
|
Brain.teach
|
public function teach($file)
{
$commands = synapse()->commands;
$file = new SplFileObject($file);
$lineNumber = 0;
while (! $file->eof()) {
$currentCommand = null;
$node = new Node($file->fgets(), $lineNumber++);
if ($node->isInterrupted() or $node->isComment()) {
continue;
}
$commands->each(function ($command) use ($node, $currentCommand) {
$class = "\\Axiom\\Rivescript\\Cortex\\Commands\\$command";
$commandClass = new $class();
$result = $commandClass->parse($node, $currentCommand);
if (isset($result['command'])) {
$currentCommand = $result['command'];
return false;
}
});
}
}
|
php
|
public function teach($file)
{
$commands = synapse()->commands;
$file = new SplFileObject($file);
$lineNumber = 0;
while (! $file->eof()) {
$currentCommand = null;
$node = new Node($file->fgets(), $lineNumber++);
if ($node->isInterrupted() or $node->isComment()) {
continue;
}
$commands->each(function ($command) use ($node, $currentCommand) {
$class = "\\Axiom\\Rivescript\\Cortex\\Commands\\$command";
$commandClass = new $class();
$result = $commandClass->parse($node, $currentCommand);
if (isset($result['command'])) {
$currentCommand = $result['command'];
return false;
}
});
}
}
|
[
"public",
"function",
"teach",
"(",
"$",
"file",
")",
"{",
"$",
"commands",
"=",
"synapse",
"(",
")",
"->",
"commands",
";",
"$",
"file",
"=",
"new",
"SplFileObject",
"(",
"$",
"file",
")",
";",
"$",
"lineNumber",
"=",
"0",
";",
"while",
"(",
"!",
"$",
"file",
"->",
"eof",
"(",
")",
")",
"{",
"$",
"currentCommand",
"=",
"null",
";",
"$",
"node",
"=",
"new",
"Node",
"(",
"$",
"file",
"->",
"fgets",
"(",
")",
",",
"$",
"lineNumber",
"++",
")",
";",
"if",
"(",
"$",
"node",
"->",
"isInterrupted",
"(",
")",
"or",
"$",
"node",
"->",
"isComment",
"(",
")",
")",
"{",
"continue",
";",
"}",
"$",
"commands",
"->",
"each",
"(",
"function",
"(",
"$",
"command",
")",
"use",
"(",
"$",
"node",
",",
"$",
"currentCommand",
")",
"{",
"$",
"class",
"=",
"\"\\\\Axiom\\\\Rivescript\\\\Cortex\\\\Commands\\\\$command\"",
";",
"$",
"commandClass",
"=",
"new",
"$",
"class",
"(",
")",
";",
"$",
"result",
"=",
"$",
"commandClass",
"->",
"parse",
"(",
"$",
"node",
",",
"$",
"currentCommand",
")",
";",
"if",
"(",
"isset",
"(",
"$",
"result",
"[",
"'command'",
"]",
")",
")",
"{",
"$",
"currentCommand",
"=",
"$",
"result",
"[",
"'command'",
"]",
";",
"return",
"false",
";",
"}",
"}",
")",
";",
"}",
"}"
] |
Teach the brain contents of a new file source.
@param string $file
|
[
"Teach",
"the",
"brain",
"contents",
"of",
"a",
"new",
"file",
"source",
"."
] |
44989e73556ab2cac9a68ec5f6fb340affd4c724
|
https://github.com/axiom-labs/rivescript-php/blob/44989e73556ab2cac9a68ec5f6fb340affd4c724/src/Cortex/Brain.php#L27-L54
|
27,149
|
doganoo/PHPAlgorithms
|
src/Datastructure/Stackqueue/Stack.php
|
Stack.sort
|
public function sort(): void {
$r = new Stack();
while (!$r->isEmpty()) {
$tmp = $this->pop();
while ((!$r->isEmpty()) && (Comparator::lessThan($r->peek(), $tmp))) {
$this->push($r->pop());
}
$r->push($tmp);
}
while (!$r->isEmpty()) {
$this->push($r->pop());
}
}
|
php
|
public function sort(): void {
$r = new Stack();
while (!$r->isEmpty()) {
$tmp = $this->pop();
while ((!$r->isEmpty()) && (Comparator::lessThan($r->peek(), $tmp))) {
$this->push($r->pop());
}
$r->push($tmp);
}
while (!$r->isEmpty()) {
$this->push($r->pop());
}
}
|
[
"public",
"function",
"sort",
"(",
")",
":",
"void",
"{",
"$",
"r",
"=",
"new",
"Stack",
"(",
")",
";",
"while",
"(",
"!",
"$",
"r",
"->",
"isEmpty",
"(",
")",
")",
"{",
"$",
"tmp",
"=",
"$",
"this",
"->",
"pop",
"(",
")",
";",
"while",
"(",
"(",
"!",
"$",
"r",
"->",
"isEmpty",
"(",
")",
")",
"&&",
"(",
"Comparator",
"::",
"lessThan",
"(",
"$",
"r",
"->",
"peek",
"(",
")",
",",
"$",
"tmp",
")",
")",
")",
"{",
"$",
"this",
"->",
"push",
"(",
"$",
"r",
"->",
"pop",
"(",
")",
")",
";",
"}",
"$",
"r",
"->",
"push",
"(",
"$",
"tmp",
")",
";",
"}",
"while",
"(",
"!",
"$",
"r",
"->",
"isEmpty",
"(",
")",
")",
"{",
"$",
"this",
"->",
"push",
"(",
"$",
"r",
"->",
"pop",
"(",
")",
")",
";",
"}",
"}"
] |
sorts the stack in descending order
TODO add ascending order
|
[
"sorts",
"the",
"stack",
"in",
"descending",
"order"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Stackqueue/Stack.php#L74-L87
|
27,150
|
doganoo/PHPAlgorithms
|
src/Common/Abstracts/AbstractGraph.php
|
AbstractGraph.numberOfSubGraph
|
public function numberOfSubGraph(): int {
if (null === $this->getNodes()) return 0;
$c = 0;
$v = new ArrayList();
foreach ($this->getNodes() as $node) {
if ($v->containsValue($node)) continue;
$c++;
$this->flood($node, $v);
}
return $c;
}
|
php
|
public function numberOfSubGraph(): int {
if (null === $this->getNodes()) return 0;
$c = 0;
$v = new ArrayList();
foreach ($this->getNodes() as $node) {
if ($v->containsValue($node)) continue;
$c++;
$this->flood($node, $v);
}
return $c;
}
|
[
"public",
"function",
"numberOfSubGraph",
"(",
")",
":",
"int",
"{",
"if",
"(",
"null",
"===",
"$",
"this",
"->",
"getNodes",
"(",
")",
")",
"return",
"0",
";",
"$",
"c",
"=",
"0",
";",
"$",
"v",
"=",
"new",
"ArrayList",
"(",
")",
";",
"foreach",
"(",
"$",
"this",
"->",
"getNodes",
"(",
")",
"as",
"$",
"node",
")",
"{",
"if",
"(",
"$",
"v",
"->",
"containsValue",
"(",
"$",
"node",
")",
")",
"continue",
";",
"$",
"c",
"++",
";",
"$",
"this",
"->",
"flood",
"(",
"$",
"node",
",",
"$",
"v",
")",
";",
"}",
"return",
"$",
"c",
";",
"}"
] |
returns the number of sub graphs
@return int
|
[
"returns",
"the",
"number",
"of",
"sub",
"graphs"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Common/Abstracts/AbstractGraph.php#L181-L192
|
27,151
|
doganoo/PHPAlgorithms
|
src/Datastructure/Stackqueue/CircularBuffer.php
|
CircularBuffer.enqueue
|
public function enqueue($data): bool {
if (null === $data) {
return false;
}
if ($this->isFull()) {
return false;
}
$this->head = $this->head % $this->size;
$this->elements[$this->head] = $data;
$this->head++;
return true;
}
|
php
|
public function enqueue($data): bool {
if (null === $data) {
return false;
}
if ($this->isFull()) {
return false;
}
$this->head = $this->head % $this->size;
$this->elements[$this->head] = $data;
$this->head++;
return true;
}
|
[
"public",
"function",
"enqueue",
"(",
"$",
"data",
")",
":",
"bool",
"{",
"if",
"(",
"null",
"===",
"$",
"data",
")",
"{",
"return",
"false",
";",
"}",
"if",
"(",
"$",
"this",
"->",
"isFull",
"(",
")",
")",
"{",
"return",
"false",
";",
"}",
"$",
"this",
"->",
"head",
"=",
"$",
"this",
"->",
"head",
"%",
"$",
"this",
"->",
"size",
";",
"$",
"this",
"->",
"elements",
"[",
"$",
"this",
"->",
"head",
"]",
"=",
"$",
"data",
";",
"$",
"this",
"->",
"head",
"++",
";",
"return",
"true",
";",
"}"
] |
enqueues a data at the head of the circular buffer.
If the buffer is full, the method will insert the
data at the "beginning" without warning.
important notice: this method wastes one slot in order to differentiate
between full and empty.
This wasting is not necessary, but requires an addition boolean flag which
(in my mind) uglifies the code.
@param $data
@return bool
|
[
"enqueues",
"a",
"data",
"at",
"the",
"head",
"of",
"the",
"circular",
"buffer",
".",
"If",
"the",
"buffer",
"is",
"full",
"the",
"method",
"will",
"insert",
"the",
"data",
"at",
"the",
"beginning",
"without",
"warning",
"."
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Stackqueue/CircularBuffer.php#L77-L88
|
27,152
|
doganoo/PHPAlgorithms
|
src/Datastructure/Stackqueue/CircularBuffer.php
|
CircularBuffer.dequeue
|
public function dequeue() {
if ($this->isEmpty()) {
return false;
}
$this->tail = $this->tail % $this->size;
$data = $this->elements[$this->tail];
$this->tail++;
return $data;
}
|
php
|
public function dequeue() {
if ($this->isEmpty()) {
return false;
}
$this->tail = $this->tail % $this->size;
$data = $this->elements[$this->tail];
$this->tail++;
return $data;
}
|
[
"public",
"function",
"dequeue",
"(",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"isEmpty",
"(",
")",
")",
"{",
"return",
"false",
";",
"}",
"$",
"this",
"->",
"tail",
"=",
"$",
"this",
"->",
"tail",
"%",
"$",
"this",
"->",
"size",
";",
"$",
"data",
"=",
"$",
"this",
"->",
"elements",
"[",
"$",
"this",
"->",
"tail",
"]",
";",
"$",
"this",
"->",
"tail",
"++",
";",
"return",
"$",
"data",
";",
"}"
] |
dequeues a value from the tail of the circular buffer.
@return mixed
|
[
"dequeues",
"a",
"value",
"from",
"the",
"tail",
"of",
"the",
"circular",
"buffer",
"."
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Stackqueue/CircularBuffer.php#L110-L118
|
27,153
|
doganoo/PHPAlgorithms
|
src/Datastructure/Stackqueue/Queue.php
|
Queue.enqueue
|
public function enqueue($item): bool {
if (!$this->isValid()) return false;
$this->queue[$this->head] = $item;
$this->head++;
return true;
}
|
php
|
public function enqueue($item): bool {
if (!$this->isValid()) return false;
$this->queue[$this->head] = $item;
$this->head++;
return true;
}
|
[
"public",
"function",
"enqueue",
"(",
"$",
"item",
")",
":",
"bool",
"{",
"if",
"(",
"!",
"$",
"this",
"->",
"isValid",
"(",
")",
")",
"return",
"false",
";",
"$",
"this",
"->",
"queue",
"[",
"$",
"this",
"->",
"head",
"]",
"=",
"$",
"item",
";",
"$",
"this",
"->",
"head",
"++",
";",
"return",
"true",
";",
"}"
] |
this methods adds an item to the queue to the last index
@param $item
@return bool
|
[
"this",
"methods",
"adds",
"an",
"item",
"to",
"the",
"queue",
"to",
"the",
"last",
"index"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Stackqueue/Queue.php#L55-L61
|
27,154
|
doganoo/PHPAlgorithms
|
src/Datastructure/Stackqueue/Queue.php
|
Queue.dequeue
|
public function dequeue() {
if (null === $this->queue) return null;
if ($this->tail > $this->head) return null;
$item = $this->queue[$this->tail];
$this->tail++;
return $item;
}
|
php
|
public function dequeue() {
if (null === $this->queue) return null;
if ($this->tail > $this->head) return null;
$item = $this->queue[$this->tail];
$this->tail++;
return $item;
}
|
[
"public",
"function",
"dequeue",
"(",
")",
"{",
"if",
"(",
"null",
"===",
"$",
"this",
"->",
"queue",
")",
"return",
"null",
";",
"if",
"(",
"$",
"this",
"->",
"tail",
">",
"$",
"this",
"->",
"head",
")",
"return",
"null",
";",
"$",
"item",
"=",
"$",
"this",
"->",
"queue",
"[",
"$",
"this",
"->",
"tail",
"]",
";",
"$",
"this",
"->",
"tail",
"++",
";",
"return",
"$",
"item",
";",
"}"
] |
this method removes the first element from the queue and returns it
@return int
|
[
"this",
"method",
"removes",
"the",
"first",
"element",
"from",
"the",
"queue",
"and",
"returns",
"it"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Stackqueue/Queue.php#L78-L84
|
27,155
|
doganoo/PHPAlgorithms
|
src/Datastructure/Stackqueue/Queue.php
|
Queue.front
|
public function front() {
if (null === $this->queue) return null;
if ($this->tail > $this->head) return null;
return $this->queue[$this->tail];
}
|
php
|
public function front() {
if (null === $this->queue) return null;
if ($this->tail > $this->head) return null;
return $this->queue[$this->tail];
}
|
[
"public",
"function",
"front",
"(",
")",
"{",
"if",
"(",
"null",
"===",
"$",
"this",
"->",
"queue",
")",
"return",
"null",
";",
"if",
"(",
"$",
"this",
"->",
"tail",
">",
"$",
"this",
"->",
"head",
")",
"return",
"null",
";",
"return",
"$",
"this",
"->",
"queue",
"[",
"$",
"this",
"->",
"tail",
"]",
";",
"}"
] |
returns the first element from the queue
@return int
|
[
"returns",
"the",
"first",
"element",
"from",
"the",
"queue"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Stackqueue/Queue.php#L91-L95
|
27,156
|
doganoo/PHPAlgorithms
|
src/Datastructure/Stackqueue/Queue.php
|
Queue.rear
|
public function rear() {
if (null === $this->queue) return null;
if (!isset($this->queue[$this->head])) return null;
return $this->queue[$this->head];
}
|
php
|
public function rear() {
if (null === $this->queue) return null;
if (!isset($this->queue[$this->head])) return null;
return $this->queue[$this->head];
}
|
[
"public",
"function",
"rear",
"(",
")",
"{",
"if",
"(",
"null",
"===",
"$",
"this",
"->",
"queue",
")",
"return",
"null",
";",
"if",
"(",
"!",
"isset",
"(",
"$",
"this",
"->",
"queue",
"[",
"$",
"this",
"->",
"head",
"]",
")",
")",
"return",
"null",
";",
"return",
"$",
"this",
"->",
"queue",
"[",
"$",
"this",
"->",
"head",
"]",
";",
"}"
] |
returns the last item from the queue
@return int
|
[
"returns",
"the",
"last",
"item",
"from",
"the",
"queue"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Stackqueue/Queue.php#L102-L106
|
27,157
|
doganoo/PHPAlgorithms
|
src/Datastructure/Stackqueue/Queue.php
|
Queue.queueSize
|
public function queueSize(): int {
if ($this->tail > $this->head) return 0;
return $this->head - $this->tail;
}
|
php
|
public function queueSize(): int {
if ($this->tail > $this->head) return 0;
return $this->head - $this->tail;
}
|
[
"public",
"function",
"queueSize",
"(",
")",
":",
"int",
"{",
"if",
"(",
"$",
"this",
"->",
"tail",
">",
"$",
"this",
"->",
"head",
")",
"return",
"0",
";",
"return",
"$",
"this",
"->",
"head",
"-",
"$",
"this",
"->",
"tail",
";",
"}"
] |
stores the number of items of the queue to the size member of this class and returns it
@return int
@deprecated
|
[
"stores",
"the",
"number",
"of",
"items",
"of",
"the",
"queue",
"to",
"the",
"size",
"member",
"of",
"this",
"class",
"and",
"returns",
"it"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Stackqueue/Queue.php#L133-L136
|
27,158
|
doganoo/PHPAlgorithms
|
src/Algorithm/Various/Permutation.php
|
Permutation.permute
|
private function permute(array $objects, $prefix, array $result) {
$length = \count($objects);
//if there are no elements in the array,
//a permutation is found. The permutation is
//added to the array
if (0 === $length) {
$result[] = $prefix;
return $result;
}
//if the number of elements in the array
//is greater than 0, there are more elements
//to build an permutation.
// --------------------------------
//The length is decreased by each recursive function call
for ($i = 0; $i < $length; $i++) {
//new object in order to create the new prefix
$object = $objects[$i];
//a new prefix is created. The prefix consists of the
//actual prefix ("" at the beginning) and the next element
//in the objects array
$newPrefix = $prefix . $object;
//since the ith element in objects is used as a prefix,
//the remaining objects have to be sliced by exactly this
//object in order to prevent a reoccurrence of the element in
//the permutation
$newObjects = \array_merge(
\array_slice($objects, 0, $i),
\array_slice($objects, $i + 1)
);
//call the permute method with the new prefix and objects
$result = $this->permute($newObjects, $newPrefix, $result);
}
return $result;
}
|
php
|
private function permute(array $objects, $prefix, array $result) {
$length = \count($objects);
//if there are no elements in the array,
//a permutation is found. The permutation is
//added to the array
if (0 === $length) {
$result[] = $prefix;
return $result;
}
//if the number of elements in the array
//is greater than 0, there are more elements
//to build an permutation.
// --------------------------------
//The length is decreased by each recursive function call
for ($i = 0; $i < $length; $i++) {
//new object in order to create the new prefix
$object = $objects[$i];
//a new prefix is created. The prefix consists of the
//actual prefix ("" at the beginning) and the next element
//in the objects array
$newPrefix = $prefix . $object;
//since the ith element in objects is used as a prefix,
//the remaining objects have to be sliced by exactly this
//object in order to prevent a reoccurrence of the element in
//the permutation
$newObjects = \array_merge(
\array_slice($objects, 0, $i),
\array_slice($objects, $i + 1)
);
//call the permute method with the new prefix and objects
$result = $this->permute($newObjects, $newPrefix, $result);
}
return $result;
}
|
[
"private",
"function",
"permute",
"(",
"array",
"$",
"objects",
",",
"$",
"prefix",
",",
"array",
"$",
"result",
")",
"{",
"$",
"length",
"=",
"\\",
"count",
"(",
"$",
"objects",
")",
";",
"//if there are no elements in the array,",
"//a permutation is found. The permutation is",
"//added to the array",
"if",
"(",
"0",
"===",
"$",
"length",
")",
"{",
"$",
"result",
"[",
"]",
"=",
"$",
"prefix",
";",
"return",
"$",
"result",
";",
"}",
"//if the number of elements in the array",
"//is greater than 0, there are more elements",
"//to build an permutation.",
"// --------------------------------",
"//The length is decreased by each recursive function call",
"for",
"(",
"$",
"i",
"=",
"0",
";",
"$",
"i",
"<",
"$",
"length",
";",
"$",
"i",
"++",
")",
"{",
"//new object in order to create the new prefix",
"$",
"object",
"=",
"$",
"objects",
"[",
"$",
"i",
"]",
";",
"//a new prefix is created. The prefix consists of the",
"//actual prefix (\"\" at the beginning) and the next element",
"//in the objects array",
"$",
"newPrefix",
"=",
"$",
"prefix",
".",
"$",
"object",
";",
"//since the ith element in objects is used as a prefix,",
"//the remaining objects have to be sliced by exactly this",
"//object in order to prevent a reoccurrence of the element in",
"//the permutation",
"$",
"newObjects",
"=",
"\\",
"array_merge",
"(",
"\\",
"array_slice",
"(",
"$",
"objects",
",",
"0",
",",
"$",
"i",
")",
",",
"\\",
"array_slice",
"(",
"$",
"objects",
",",
"$",
"i",
"+",
"1",
")",
")",
";",
"//call the permute method with the new prefix and objects",
"$",
"result",
"=",
"$",
"this",
"->",
"permute",
"(",
"$",
"newObjects",
",",
"$",
"newPrefix",
",",
"$",
"result",
")",
";",
"}",
"return",
"$",
"result",
";",
"}"
] |
returns all permutations of an given array of objects
@param array $objects
@param $prefix
@param array $result
@return array
|
[
"returns",
"all",
"permutations",
"of",
"an",
"given",
"array",
"of",
"objects"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Algorithm/Various/Permutation.php#L67-L103
|
27,159
|
doganoo/PHPAlgorithms
|
src/Common/Abstracts/AbstractLinkedList.php
|
AbstractLinkedList.removeDuplicates
|
public function removeDuplicates() {
$tortoise = $this->head;
while ($tortoise !== null) {
$hare = $tortoise;
while ($hare->getNext() !== null) {
if (Comparator::equals($hare->getNext()->getValue(), $tortoise->getValue())) {
$hare->setNext($hare->getNext()->getNext());
} else {
$hare = $hare->getNext();
}
}
$tortoise = $tortoise->getNext();
}
}
|
php
|
public function removeDuplicates() {
$tortoise = $this->head;
while ($tortoise !== null) {
$hare = $tortoise;
while ($hare->getNext() !== null) {
if (Comparator::equals($hare->getNext()->getValue(), $tortoise->getValue())) {
$hare->setNext($hare->getNext()->getNext());
} else {
$hare = $hare->getNext();
}
}
$tortoise = $tortoise->getNext();
}
}
|
[
"public",
"function",
"removeDuplicates",
"(",
")",
"{",
"$",
"tortoise",
"=",
"$",
"this",
"->",
"head",
";",
"while",
"(",
"$",
"tortoise",
"!==",
"null",
")",
"{",
"$",
"hare",
"=",
"$",
"tortoise",
";",
"while",
"(",
"$",
"hare",
"->",
"getNext",
"(",
")",
"!==",
"null",
")",
"{",
"if",
"(",
"Comparator",
"::",
"equals",
"(",
"$",
"hare",
"->",
"getNext",
"(",
")",
"->",
"getValue",
"(",
")",
",",
"$",
"tortoise",
"->",
"getValue",
"(",
")",
")",
")",
"{",
"$",
"hare",
"->",
"setNext",
"(",
"$",
"hare",
"->",
"getNext",
"(",
")",
"->",
"getNext",
"(",
")",
")",
";",
"}",
"else",
"{",
"$",
"hare",
"=",
"$",
"hare",
"->",
"getNext",
"(",
")",
";",
"}",
"}",
"$",
"tortoise",
"=",
"$",
"tortoise",
"->",
"getNext",
"(",
")",
";",
"}",
"}"
] |
The method uses the runner technique in order to iterate over
head twice. If the list contains duplicates, the next-next is set
as the next node of the current node.
|
[
"The",
"method",
"uses",
"the",
"runner",
"technique",
"in",
"order",
"to",
"iterate",
"over",
"head",
"twice",
".",
"If",
"the",
"list",
"contains",
"duplicates",
"the",
"next",
"-",
"next",
"is",
"set",
"as",
"the",
"next",
"node",
"of",
"the",
"current",
"node",
"."
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Common/Abstracts/AbstractLinkedList.php#L143-L157
|
27,160
|
doganoo/PHPAlgorithms
|
src/Common/Abstracts/AbstractLinkedList.php
|
AbstractLinkedList.getNodeByValue
|
public function getNodeByValue($value): ?Node {
if (!$this->containsValue($value)) {
return null;
}
$tmp = $this->getHead();
while ($tmp !== null) {
$val = $tmp->getValue();
if (Comparator::equals($val, $value)) {
//if the value is found then return it
return $tmp;
}
$tmp = $tmp->getNext();
}
return null;
}
|
php
|
public function getNodeByValue($value): ?Node {
if (!$this->containsValue($value)) {
return null;
}
$tmp = $this->getHead();
while ($tmp !== null) {
$val = $tmp->getValue();
if (Comparator::equals($val, $value)) {
//if the value is found then return it
return $tmp;
}
$tmp = $tmp->getNext();
}
return null;
}
|
[
"public",
"function",
"getNodeByValue",
"(",
"$",
"value",
")",
":",
"?",
"Node",
"{",
"if",
"(",
"!",
"$",
"this",
"->",
"containsValue",
"(",
"$",
"value",
")",
")",
"{",
"return",
"null",
";",
"}",
"$",
"tmp",
"=",
"$",
"this",
"->",
"getHead",
"(",
")",
";",
"while",
"(",
"$",
"tmp",
"!==",
"null",
")",
"{",
"$",
"val",
"=",
"$",
"tmp",
"->",
"getValue",
"(",
")",
";",
"if",
"(",
"Comparator",
"::",
"equals",
"(",
"$",
"val",
",",
"$",
"value",
")",
")",
"{",
"//if the value is found then return it",
"return",
"$",
"tmp",
";",
"}",
"$",
"tmp",
"=",
"$",
"tmp",
"->",
"getNext",
"(",
")",
";",
"}",
"return",
"null",
";",
"}"
] |
searches the list for a node by a given key
@param $value
@return \doganoo\PHPAlgorithms\Datastructure\Lists\Node|null
|
[
"searches",
"the",
"list",
"for",
"a",
"node",
"by",
"a",
"given",
"key"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Common/Abstracts/AbstractLinkedList.php#L321-L335
|
27,161
|
doganoo/PHPAlgorithms
|
src/Common/Abstracts/AbstractLinkedList.php
|
AbstractLinkedList.containsValue
|
public function containsValue($value): bool {
$node = $this->getHead();
while ($node !== null) {
if (Comparator::equals($node->getValue(), $value)) {
return true;
}
$node = $node->getNext();
}
return false;
}
|
php
|
public function containsValue($value): bool {
$node = $this->getHead();
while ($node !== null) {
if (Comparator::equals($node->getValue(), $value)) {
return true;
}
$node = $node->getNext();
}
return false;
}
|
[
"public",
"function",
"containsValue",
"(",
"$",
"value",
")",
":",
"bool",
"{",
"$",
"node",
"=",
"$",
"this",
"->",
"getHead",
"(",
")",
";",
"while",
"(",
"$",
"node",
"!==",
"null",
")",
"{",
"if",
"(",
"Comparator",
"::",
"equals",
"(",
"$",
"node",
"->",
"getValue",
"(",
")",
",",
"$",
"value",
")",
")",
"{",
"return",
"true",
";",
"}",
"$",
"node",
"=",
"$",
"node",
"->",
"getNext",
"(",
")",
";",
"}",
"return",
"false",
";",
"}"
] |
searches the list for a given value
@param $value
@return bool
|
[
"searches",
"the",
"list",
"for",
"a",
"given",
"value"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Common/Abstracts/AbstractLinkedList.php#L343-L352
|
27,162
|
doganoo/PHPAlgorithms
|
src/Common/Abstracts/AbstractLinkedList.php
|
AbstractLinkedList.getNodeByKey
|
public function getNodeByKey($key): ?Node {
if (!$this->containsKey($key)) {
return null;
}
$head = $this->getHead();
while ($head !== null) {
if (Comparator::equals($head->getKey(), $key)) {
return $head;
}
$head = $head->getNext();
}
return null;
}
|
php
|
public function getNodeByKey($key): ?Node {
if (!$this->containsKey($key)) {
return null;
}
$head = $this->getHead();
while ($head !== null) {
if (Comparator::equals($head->getKey(), $key)) {
return $head;
}
$head = $head->getNext();
}
return null;
}
|
[
"public",
"function",
"getNodeByKey",
"(",
"$",
"key",
")",
":",
"?",
"Node",
"{",
"if",
"(",
"!",
"$",
"this",
"->",
"containsKey",
"(",
"$",
"key",
")",
")",
"{",
"return",
"null",
";",
"}",
"$",
"head",
"=",
"$",
"this",
"->",
"getHead",
"(",
")",
";",
"while",
"(",
"$",
"head",
"!==",
"null",
")",
"{",
"if",
"(",
"Comparator",
"::",
"equals",
"(",
"$",
"head",
"->",
"getKey",
"(",
")",
",",
"$",
"key",
")",
")",
"{",
"return",
"$",
"head",
";",
"}",
"$",
"head",
"=",
"$",
"head",
"->",
"getNext",
"(",
")",
";",
"}",
"return",
"null",
";",
"}"
] |
returns a node by a given key
@param $key
@return \doganoo\PHPAlgorithms\Datastructure\Lists\Node|null
|
[
"returns",
"a",
"node",
"by",
"a",
"given",
"key"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Common/Abstracts/AbstractLinkedList.php#L360-L372
|
27,163
|
doganoo/PHPAlgorithms
|
src/Common/Abstracts/AbstractLinkedList.php
|
AbstractLinkedList.containsKey
|
public function containsKey($key): bool {
$node = $this->getHead();
while (null !== $node) {
if (Comparator::equals($node->getKey(), $key)) {
return true;
}
$node = $node->getNext();
}
return false;
}
|
php
|
public function containsKey($key): bool {
$node = $this->getHead();
while (null !== $node) {
if (Comparator::equals($node->getKey(), $key)) {
return true;
}
$node = $node->getNext();
}
return false;
}
|
[
"public",
"function",
"containsKey",
"(",
"$",
"key",
")",
":",
"bool",
"{",
"$",
"node",
"=",
"$",
"this",
"->",
"getHead",
"(",
")",
";",
"while",
"(",
"null",
"!==",
"$",
"node",
")",
"{",
"if",
"(",
"Comparator",
"::",
"equals",
"(",
"$",
"node",
"->",
"getKey",
"(",
")",
",",
"$",
"key",
")",
")",
"{",
"return",
"true",
";",
"}",
"$",
"node",
"=",
"$",
"node",
"->",
"getNext",
"(",
")",
";",
"}",
"return",
"false",
";",
"}"
] |
searches the list for a given key
@param $key
@return bool
|
[
"searches",
"the",
"list",
"for",
"a",
"given",
"key"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Common/Abstracts/AbstractLinkedList.php#L380-L389
|
27,164
|
doganoo/PHPAlgorithms
|
src/Common/Abstracts/AbstractLinkedList.php
|
AbstractLinkedList.remove
|
public function remove($key): bool {
/** @var \doganoo\PHPAlgorithms\Datastructure\Lists\Node $previous */
$previous = $head = $this->getHead();
if ($head === null) {
return true;
}
$i = 1;
$headSize = $head->size();
/*
* The while loop iterates over all nodes until the
* value is found.
*/
while ($head !== null && $head->getKey() !== $key) {
/*
* since a node is going to be removed from the
* node chain, the previous element has to be
* on hold.
* After previous is set to the actual element,
* the current element pointer can moved to the
* next one.
*
* If the value is found, $previous points to
* the previous element of the value that is
* searched and current points to the next element
* after that one who should be deleted.
*/
$previous = $head;
$head = $head->getNext();
$i++;
}
/*
* If the value that should be deleted is not in the list,
* this set instruction assigns the next node to the actual.
*
* If the while loop has ended early, the next node is
* assigned to the previous node of the node that
* should be deleted (if there is a node present).
*/
if ($head !== null) {
$previous->setNext($head->getNext());
}
return $i !== $headSize;
}
|
php
|
public function remove($key): bool {
/** @var \doganoo\PHPAlgorithms\Datastructure\Lists\Node $previous */
$previous = $head = $this->getHead();
if ($head === null) {
return true;
}
$i = 1;
$headSize = $head->size();
/*
* The while loop iterates over all nodes until the
* value is found.
*/
while ($head !== null && $head->getKey() !== $key) {
/*
* since a node is going to be removed from the
* node chain, the previous element has to be
* on hold.
* After previous is set to the actual element,
* the current element pointer can moved to the
* next one.
*
* If the value is found, $previous points to
* the previous element of the value that is
* searched and current points to the next element
* after that one who should be deleted.
*/
$previous = $head;
$head = $head->getNext();
$i++;
}
/*
* If the value that should be deleted is not in the list,
* this set instruction assigns the next node to the actual.
*
* If the while loop has ended early, the next node is
* assigned to the previous node of the node that
* should be deleted (if there is a node present).
*/
if ($head !== null) {
$previous->setNext($head->getNext());
}
return $i !== $headSize;
}
|
[
"public",
"function",
"remove",
"(",
"$",
"key",
")",
":",
"bool",
"{",
"/** @var \\doganoo\\PHPAlgorithms\\Datastructure\\Lists\\Node $previous */",
"$",
"previous",
"=",
"$",
"head",
"=",
"$",
"this",
"->",
"getHead",
"(",
")",
";",
"if",
"(",
"$",
"head",
"===",
"null",
")",
"{",
"return",
"true",
";",
"}",
"$",
"i",
"=",
"1",
";",
"$",
"headSize",
"=",
"$",
"head",
"->",
"size",
"(",
")",
";",
"/*\n * The while loop iterates over all nodes until the\n * value is found.\n */",
"while",
"(",
"$",
"head",
"!==",
"null",
"&&",
"$",
"head",
"->",
"getKey",
"(",
")",
"!==",
"$",
"key",
")",
"{",
"/*\n * since a node is going to be removed from the\n * node chain, the previous element has to be\n * on hold.\n * After previous is set to the actual element,\n * the current element pointer can moved to the\n * next one.\n *\n * If the value is found, $previous points to\n * the previous element of the value that is\n * searched and current points to the next element\n * after that one who should be deleted.\n */",
"$",
"previous",
"=",
"$",
"head",
";",
"$",
"head",
"=",
"$",
"head",
"->",
"getNext",
"(",
")",
";",
"$",
"i",
"++",
";",
"}",
"/*\n * If the value that should be deleted is not in the list,\n * this set instruction assigns the next node to the actual.\n *\n * If the while loop has ended early, the next node is\n * assigned to the previous node of the node that\n * should be deleted (if there is a node present).\n */",
"if",
"(",
"$",
"head",
"!==",
"null",
")",
"{",
"$",
"previous",
"->",
"setNext",
"(",
"$",
"head",
"->",
"getNext",
"(",
")",
")",
";",
"}",
"return",
"$",
"i",
"!==",
"$",
"headSize",
";",
"}"
] |
removes a node from the list by a given key
@param $key
@return bool
|
[
"removes",
"a",
"node",
"from",
"the",
"list",
"by",
"a",
"given",
"key"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Common/Abstracts/AbstractLinkedList.php#L397-L441
|
27,165
|
doganoo/PHPAlgorithms
|
src/Common/Abstracts/AbstractLinkedList.php
|
AbstractLinkedList.replaceValue
|
public function replaceValue($key, $value): bool {
$replaced = false;
$node = $this->getHead();
while ($node !== null) {
if (Comparator::equals($node->getKey(), $key)) {
$node->setValue($value);
$replaced = true;
}
$node = $node->getNext();
}
return $replaced;
}
|
php
|
public function replaceValue($key, $value): bool {
$replaced = false;
$node = $this->getHead();
while ($node !== null) {
if (Comparator::equals($node->getKey(), $key)) {
$node->setValue($value);
$replaced = true;
}
$node = $node->getNext();
}
return $replaced;
}
|
[
"public",
"function",
"replaceValue",
"(",
"$",
"key",
",",
"$",
"value",
")",
":",
"bool",
"{",
"$",
"replaced",
"=",
"false",
";",
"$",
"node",
"=",
"$",
"this",
"->",
"getHead",
"(",
")",
";",
"while",
"(",
"$",
"node",
"!==",
"null",
")",
"{",
"if",
"(",
"Comparator",
"::",
"equals",
"(",
"$",
"node",
"->",
"getKey",
"(",
")",
",",
"$",
"key",
")",
")",
"{",
"$",
"node",
"->",
"setValue",
"(",
"$",
"value",
")",
";",
"$",
"replaced",
"=",
"true",
";",
"}",
"$",
"node",
"=",
"$",
"node",
"->",
"getNext",
"(",
")",
";",
"}",
"return",
"$",
"replaced",
";",
"}"
] |
replaces a value for a key
@param $key
@param $value
@return bool
|
[
"replaces",
"a",
"value",
"for",
"a",
"key"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Common/Abstracts/AbstractLinkedList.php#L450-L461
|
27,166
|
doganoo/PHPAlgorithms
|
src/Common/Abstracts/AbstractLinkedList.php
|
AbstractLinkedList.hasLoop
|
public function hasLoop(): bool {
$tortoise = $this->getHead();
$hare = $this->getHead();
while ($tortoise !== null && $hare->getNext() !== null) {
$hare = $hare->getNext()->getNext();
if (Comparator::equals($tortoise->getValue(), $hare->getValue())) {
return true;
}
$tortoise = $tortoise->getNext();
}
return false;
}
|
php
|
public function hasLoop(): bool {
$tortoise = $this->getHead();
$hare = $this->getHead();
while ($tortoise !== null && $hare->getNext() !== null) {
$hare = $hare->getNext()->getNext();
if (Comparator::equals($tortoise->getValue(), $hare->getValue())) {
return true;
}
$tortoise = $tortoise->getNext();
}
return false;
}
|
[
"public",
"function",
"hasLoop",
"(",
")",
":",
"bool",
"{",
"$",
"tortoise",
"=",
"$",
"this",
"->",
"getHead",
"(",
")",
";",
"$",
"hare",
"=",
"$",
"this",
"->",
"getHead",
"(",
")",
";",
"while",
"(",
"$",
"tortoise",
"!==",
"null",
"&&",
"$",
"hare",
"->",
"getNext",
"(",
")",
"!==",
"null",
")",
"{",
"$",
"hare",
"=",
"$",
"hare",
"->",
"getNext",
"(",
")",
"->",
"getNext",
"(",
")",
";",
"if",
"(",
"Comparator",
"::",
"equals",
"(",
"$",
"tortoise",
"->",
"getValue",
"(",
")",
",",
"$",
"hare",
"->",
"getValue",
"(",
")",
")",
")",
"{",
"return",
"true",
";",
"}",
"$",
"tortoise",
"=",
"$",
"tortoise",
"->",
"getNext",
"(",
")",
";",
"}",
"return",
"false",
";",
"}"
] |
also known as the Runner Technique
@return bool
|
[
"also",
"known",
"as",
"the",
"Runner",
"Technique"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Common/Abstracts/AbstractLinkedList.php#L481-L495
|
27,167
|
doganoo/PHPAlgorithms
|
src/Common/Abstracts/AbstractLinkedList.php
|
AbstractLinkedList.getMiddleNode
|
public function getMiddleNode(): ?IUnaryNode {
$head = $this->getHead();
if (null === $head) return null;
$p = $head;
$q = $head;
while (null !== $p &&
null !== $q && //actually not really necessary since $p and $q point to the same object
null !== $q->getNext()
) {
$p = $p->getNext();
$q = $q->getNext()->getNext();
}
return $p;
}
|
php
|
public function getMiddleNode(): ?IUnaryNode {
$head = $this->getHead();
if (null === $head) return null;
$p = $head;
$q = $head;
while (null !== $p &&
null !== $q && //actually not really necessary since $p and $q point to the same object
null !== $q->getNext()
) {
$p = $p->getNext();
$q = $q->getNext()->getNext();
}
return $p;
}
|
[
"public",
"function",
"getMiddleNode",
"(",
")",
":",
"?",
"IUnaryNode",
"{",
"$",
"head",
"=",
"$",
"this",
"->",
"getHead",
"(",
")",
";",
"if",
"(",
"null",
"===",
"$",
"head",
")",
"return",
"null",
";",
"$",
"p",
"=",
"$",
"head",
";",
"$",
"q",
"=",
"$",
"head",
";",
"while",
"(",
"null",
"!==",
"$",
"p",
"&&",
"null",
"!==",
"$",
"q",
"&&",
"//actually not really necessary since $p and $q point to the same object",
"null",
"!==",
"$",
"q",
"->",
"getNext",
"(",
")",
")",
"{",
"$",
"p",
"=",
"$",
"p",
"->",
"getNext",
"(",
")",
";",
"$",
"q",
"=",
"$",
"q",
"->",
"getNext",
"(",
")",
"->",
"getNext",
"(",
")",
";",
"}",
"return",
"$",
"p",
";",
"}"
] |
returns the middle node of the linked list
@return IUnaryNode|null
|
[
"returns",
"the",
"middle",
"node",
"of",
"the",
"linked",
"list"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Common/Abstracts/AbstractLinkedList.php#L502-L518
|
27,168
|
doganoo/PHPAlgorithms
|
src/Datastructure/Lists/ArrayLists/StringBuilder.php
|
StringBuilder.append
|
public function append(string $string) {
for ($i = 0; $i < \strlen($string); $i++) {
$this->arrayList->add($string[$i]);
}
}
|
php
|
public function append(string $string) {
for ($i = 0; $i < \strlen($string); $i++) {
$this->arrayList->add($string[$i]);
}
}
|
[
"public",
"function",
"append",
"(",
"string",
"$",
"string",
")",
"{",
"for",
"(",
"$",
"i",
"=",
"0",
";",
"$",
"i",
"<",
"\\",
"strlen",
"(",
"$",
"string",
")",
";",
"$",
"i",
"++",
")",
"{",
"$",
"this",
"->",
"arrayList",
"->",
"add",
"(",
"$",
"string",
"[",
"$",
"i",
"]",
")",
";",
"}",
"}"
] |
appends a string to the list
@param string $string
|
[
"appends",
"a",
"string",
"to",
"the",
"list"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Lists/ArrayLists/StringBuilder.php#L61-L65
|
27,169
|
doganoo/PHPAlgorithms
|
src/Datastructure/Lists/ArrayLists/StringBuilder.php
|
StringBuilder.arrayListToStringBuilder
|
private function arrayListToStringBuilder(ArrayList $arrayList, bool $reverse = false) {
$stringBuilder = new StringBuilder();
if ($reverse) {
$stack = new Stack();
foreach ($arrayList as $item) {
if (null !== $item) {
$stack->push($item);
}
}
while (!$stack->isEmpty()) {
$stringBuilder->append($stack->pop());
}
} else {
$queue = new Queue();
foreach ($arrayList as $item) {
if (null !== $item) {
$queue->enqueue($item);
}
}
while (!$queue->isEmpty()) {
$stringBuilder->append($queue->dequeue());
}
}
return $stringBuilder;
}
|
php
|
private function arrayListToStringBuilder(ArrayList $arrayList, bool $reverse = false) {
$stringBuilder = new StringBuilder();
if ($reverse) {
$stack = new Stack();
foreach ($arrayList as $item) {
if (null !== $item) {
$stack->push($item);
}
}
while (!$stack->isEmpty()) {
$stringBuilder->append($stack->pop());
}
} else {
$queue = new Queue();
foreach ($arrayList as $item) {
if (null !== $item) {
$queue->enqueue($item);
}
}
while (!$queue->isEmpty()) {
$stringBuilder->append($queue->dequeue());
}
}
return $stringBuilder;
}
|
[
"private",
"function",
"arrayListToStringBuilder",
"(",
"ArrayList",
"$",
"arrayList",
",",
"bool",
"$",
"reverse",
"=",
"false",
")",
"{",
"$",
"stringBuilder",
"=",
"new",
"StringBuilder",
"(",
")",
";",
"if",
"(",
"$",
"reverse",
")",
"{",
"$",
"stack",
"=",
"new",
"Stack",
"(",
")",
";",
"foreach",
"(",
"$",
"arrayList",
"as",
"$",
"item",
")",
"{",
"if",
"(",
"null",
"!==",
"$",
"item",
")",
"{",
"$",
"stack",
"->",
"push",
"(",
"$",
"item",
")",
";",
"}",
"}",
"while",
"(",
"!",
"$",
"stack",
"->",
"isEmpty",
"(",
")",
")",
"{",
"$",
"stringBuilder",
"->",
"append",
"(",
"$",
"stack",
"->",
"pop",
"(",
")",
")",
";",
"}",
"}",
"else",
"{",
"$",
"queue",
"=",
"new",
"Queue",
"(",
")",
";",
"foreach",
"(",
"$",
"arrayList",
"as",
"$",
"item",
")",
"{",
"if",
"(",
"null",
"!==",
"$",
"item",
")",
"{",
"$",
"queue",
"->",
"enqueue",
"(",
"$",
"item",
")",
";",
"}",
"}",
"while",
"(",
"!",
"$",
"queue",
"->",
"isEmpty",
"(",
")",
")",
"{",
"$",
"stringBuilder",
"->",
"append",
"(",
"$",
"queue",
"->",
"dequeue",
"(",
")",
")",
";",
"}",
"}",
"return",
"$",
"stringBuilder",
";",
"}"
] |
converts a array list to a string builder
@param ArrayList $arrayList
@param bool $reverse
@return StringBuilder
|
[
"converts",
"a",
"array",
"list",
"to",
"a",
"string",
"builder"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Lists/ArrayLists/StringBuilder.php#L155-L180
|
27,170
|
doganoo/PHPAlgorithms
|
src/Datastructure/Sets/HashSet.php
|
HashSet.containsAll
|
public function containsAll($elements): bool {
$contains = false;
if (\is_iterable($elements)) {
foreach ($elements as $element) {
$contains &= $this->contains($element);
}
}
return $contains;
}
|
php
|
public function containsAll($elements): bool {
$contains = false;
if (\is_iterable($elements)) {
foreach ($elements as $element) {
$contains &= $this->contains($element);
}
}
return $contains;
}
|
[
"public",
"function",
"containsAll",
"(",
"$",
"elements",
")",
":",
"bool",
"{",
"$",
"contains",
"=",
"false",
";",
"if",
"(",
"\\",
"is_iterable",
"(",
"$",
"elements",
")",
")",
"{",
"foreach",
"(",
"$",
"elements",
"as",
"$",
"element",
")",
"{",
"$",
"contains",
"&=",
"$",
"this",
"->",
"contains",
"(",
"$",
"element",
")",
";",
"}",
"}",
"return",
"$",
"contains",
";",
"}"
] |
Returns true if this set contains all of the elements of the specified collection.
@param $elements
@return bool
|
[
"Returns",
"true",
"if",
"this",
"set",
"contains",
"all",
"of",
"the",
"elements",
"of",
"the",
"specified",
"collection",
"."
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Sets/HashSet.php#L104-L112
|
27,171
|
doganoo/PHPAlgorithms
|
src/Algorithm/Sorting/SelectionSort.php
|
SelectionSort.sort
|
public function sort(array $array): array {
$array = \array_values($array);
$length = \count($array);
if (0 === $length || 1 === $length) return $array;
for ($i = 0; $i < $length; $i++) {
$min = $i;
for ($j = $i; $j < $length; $j++) {
if (Comparator::lessThan($array[$j], $array[$min])) {
$min = $j;
}
}
$tmp = $array[$min];
$array[$min] = $array[$i];
$array[$i] = $tmp;
}
return $array;
}
|
php
|
public function sort(array $array): array {
$array = \array_values($array);
$length = \count($array);
if (0 === $length || 1 === $length) return $array;
for ($i = 0; $i < $length; $i++) {
$min = $i;
for ($j = $i; $j < $length; $j++) {
if (Comparator::lessThan($array[$j], $array[$min])) {
$min = $j;
}
}
$tmp = $array[$min];
$array[$min] = $array[$i];
$array[$i] = $tmp;
}
return $array;
}
|
[
"public",
"function",
"sort",
"(",
"array",
"$",
"array",
")",
":",
"array",
"{",
"$",
"array",
"=",
"\\",
"array_values",
"(",
"$",
"array",
")",
";",
"$",
"length",
"=",
"\\",
"count",
"(",
"$",
"array",
")",
";",
"if",
"(",
"0",
"===",
"$",
"length",
"||",
"1",
"===",
"$",
"length",
")",
"return",
"$",
"array",
";",
"for",
"(",
"$",
"i",
"=",
"0",
";",
"$",
"i",
"<",
"$",
"length",
";",
"$",
"i",
"++",
")",
"{",
"$",
"min",
"=",
"$",
"i",
";",
"for",
"(",
"$",
"j",
"=",
"$",
"i",
";",
"$",
"j",
"<",
"$",
"length",
";",
"$",
"j",
"++",
")",
"{",
"if",
"(",
"Comparator",
"::",
"lessThan",
"(",
"$",
"array",
"[",
"$",
"j",
"]",
",",
"$",
"array",
"[",
"$",
"min",
"]",
")",
")",
"{",
"$",
"min",
"=",
"$",
"j",
";",
"}",
"}",
"$",
"tmp",
"=",
"$",
"array",
"[",
"$",
"min",
"]",
";",
"$",
"array",
"[",
"$",
"min",
"]",
"=",
"$",
"array",
"[",
"$",
"i",
"]",
";",
"$",
"array",
"[",
"$",
"i",
"]",
"=",
"$",
"tmp",
";",
"}",
"return",
"$",
"array",
";",
"}"
] |
sorts an array with selection sort.
Actually works only with numeric indices
@param array $array
@return array
|
[
"sorts",
"an",
"array",
"with",
"selection",
"sort",
"."
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Algorithm/Sorting/SelectionSort.php#L47-L65
|
27,172
|
acmephp/core
|
AcmeClient.php
|
AcmeClient.getResourceAccount
|
private function getResourceAccount()
{
if (!$this->account) {
$payload = [
'onlyReturnExisting' => true,
];
$this->requestResource('POST', ResourcesDirectory::NEW_ACCOUNT, $payload);
$this->account = $this->getHttpClient()->getLastLocation();
}
return $this->account;
}
|
php
|
private function getResourceAccount()
{
if (!$this->account) {
$payload = [
'onlyReturnExisting' => true,
];
$this->requestResource('POST', ResourcesDirectory::NEW_ACCOUNT, $payload);
$this->account = $this->getHttpClient()->getLastLocation();
}
return $this->account;
}
|
[
"private",
"function",
"getResourceAccount",
"(",
")",
"{",
"if",
"(",
"!",
"$",
"this",
"->",
"account",
")",
"{",
"$",
"payload",
"=",
"[",
"'onlyReturnExisting'",
"=>",
"true",
",",
"]",
";",
"$",
"this",
"->",
"requestResource",
"(",
"'POST'",
",",
"ResourcesDirectory",
"::",
"NEW_ACCOUNT",
",",
"$",
"payload",
")",
";",
"$",
"this",
"->",
"account",
"=",
"$",
"this",
"->",
"getHttpClient",
"(",
")",
"->",
"getLastLocation",
"(",
")",
";",
"}",
"return",
"$",
"this",
"->",
"account",
";",
"}"
] |
Retrieve the resource account.
@return string
|
[
"Retrieve",
"the",
"resource",
"account",
"."
] |
56af3e28926f4b7507397bfe7fb1ca5f83d73eb4
|
https://github.com/acmephp/core/blob/56af3e28926f4b7507397bfe7fb1ca5f83d73eb4/AcmeClient.php#L339-L351
|
27,173
|
acmephp/core
|
Challenge/Dns/DnsDataExtractor.php
|
DnsDataExtractor.getRecordValue
|
public function getRecordValue(AuthorizationChallenge $authorizationChallenge)
{
return $this->encoder->encode(hash('sha256', $authorizationChallenge->getPayload(), true));
}
|
php
|
public function getRecordValue(AuthorizationChallenge $authorizationChallenge)
{
return $this->encoder->encode(hash('sha256', $authorizationChallenge->getPayload(), true));
}
|
[
"public",
"function",
"getRecordValue",
"(",
"AuthorizationChallenge",
"$",
"authorizationChallenge",
")",
"{",
"return",
"$",
"this",
"->",
"encoder",
"->",
"encode",
"(",
"hash",
"(",
"'sha256'",
",",
"$",
"authorizationChallenge",
"->",
"getPayload",
"(",
")",
",",
"true",
")",
")",
";",
"}"
] |
Retrieves the value of the TXT record to register.
@param AuthorizationChallenge $authorizationChallenge
@return string
|
[
"Retrieves",
"the",
"value",
"of",
"the",
"TXT",
"record",
"to",
"register",
"."
] |
56af3e28926f4b7507397bfe7fb1ca5f83d73eb4
|
https://github.com/acmephp/core/blob/56af3e28926f4b7507397bfe7fb1ca5f83d73eb4/Challenge/Dns/DnsDataExtractor.php#L56-L59
|
27,174
|
doganoo/PHPAlgorithms
|
src/Datastructure/Maps/HashMap.php
|
HashMap.addNode
|
public function addNode(Node $node): bool {
$added = $this->add($node->getKey(), $node->getValue());
return $added;
}
|
php
|
public function addNode(Node $node): bool {
$added = $this->add($node->getKey(), $node->getValue());
return $added;
}
|
[
"public",
"function",
"addNode",
"(",
"Node",
"$",
"node",
")",
":",
"bool",
"{",
"$",
"added",
"=",
"$",
"this",
"->",
"add",
"(",
"$",
"node",
"->",
"getKey",
"(",
")",
",",
"$",
"node",
"->",
"getValue",
"(",
")",
")",
";",
"return",
"$",
"added",
";",
"}"
] |
adds a node to the hash map
@param Node $node
@return bool
@throws \doganoo\PHPAlgorithms\common\Exception\InvalidKeyTypeException
@throws \doganoo\PHPAlgorithms\common\Exception\UnsupportedKeyTypeException
|
[
"adds",
"a",
"node",
"to",
"the",
"hash",
"map"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Maps/HashMap.php#L82-L85
|
27,175
|
doganoo/PHPAlgorithms
|
src/Datastructure/Maps/HashMap.php
|
HashMap.add
|
public function add($key, $value): bool {
$arrayIndex = $this->getBucketIndex($key);
if (isset($this->bucket[$arrayIndex])) {
$list = $this->bucket[$arrayIndex];
} else {
$list = new SinglyLinkedList();
}
if ($list->containsKey($key)) {
$list->replaceValue($key, $value);
return true;
}
$list->add($key, $value);
$this->bucket[$arrayIndex] = $list;
return true;
}
|
php
|
public function add($key, $value): bool {
$arrayIndex = $this->getBucketIndex($key);
if (isset($this->bucket[$arrayIndex])) {
$list = $this->bucket[$arrayIndex];
} else {
$list = new SinglyLinkedList();
}
if ($list->containsKey($key)) {
$list->replaceValue($key, $value);
return true;
}
$list->add($key, $value);
$this->bucket[$arrayIndex] = $list;
return true;
}
|
[
"public",
"function",
"add",
"(",
"$",
"key",
",",
"$",
"value",
")",
":",
"bool",
"{",
"$",
"arrayIndex",
"=",
"$",
"this",
"->",
"getBucketIndex",
"(",
"$",
"key",
")",
";",
"if",
"(",
"isset",
"(",
"$",
"this",
"->",
"bucket",
"[",
"$",
"arrayIndex",
"]",
")",
")",
"{",
"$",
"list",
"=",
"$",
"this",
"->",
"bucket",
"[",
"$",
"arrayIndex",
"]",
";",
"}",
"else",
"{",
"$",
"list",
"=",
"new",
"SinglyLinkedList",
"(",
")",
";",
"}",
"if",
"(",
"$",
"list",
"->",
"containsKey",
"(",
"$",
"key",
")",
")",
"{",
"$",
"list",
"->",
"replaceValue",
"(",
"$",
"key",
",",
"$",
"value",
")",
";",
"return",
"true",
";",
"}",
"$",
"list",
"->",
"add",
"(",
"$",
"key",
",",
"$",
"value",
")",
";",
"$",
"this",
"->",
"bucket",
"[",
"$",
"arrayIndex",
"]",
"=",
"$",
"list",
";",
"return",
"true",
";",
"}"
] |
adds a new value assigned to a key. The key has to be a scalar
value otherwise the method throws an InvalidKeyTypeException.
@param $key
@param $value
@return bool
@throws \doganoo\PHPAlgorithms\Common\Exception\InvalidKeyTypeException
@throws \doganoo\PHPAlgorithms\Common\Exception\UnsupportedKeyTypeException
|
[
"adds",
"a",
"new",
"value",
"assigned",
"to",
"a",
"key",
".",
"The",
"key",
"has",
"to",
"be",
"a",
"scalar",
"value",
"otherwise",
"the",
"method",
"throws",
"an",
"InvalidKeyTypeException",
"."
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Maps/HashMap.php#L97-L111
|
27,176
|
doganoo/PHPAlgorithms
|
src/Datastructure/Maps/HashMap.php
|
HashMap.getBucketIndex
|
private function getBucketIndex($key) {
/*
* next, the keys hash is calculated by a
* private method. Next, the array index
* (bucket index) is calculated from this hash.
*
* Doing this avoids hash collisions.
*/
$hash = $this->getHash($key);
$arrayIndex = $this->getArrayIndex($hash);
return $arrayIndex;
}
|
php
|
private function getBucketIndex($key) {
/*
* next, the keys hash is calculated by a
* private method. Next, the array index
* (bucket index) is calculated from this hash.
*
* Doing this avoids hash collisions.
*/
$hash = $this->getHash($key);
$arrayIndex = $this->getArrayIndex($hash);
return $arrayIndex;
}
|
[
"private",
"function",
"getBucketIndex",
"(",
"$",
"key",
")",
"{",
"/*\n * next, the keys hash is calculated by a\n * private method. Next, the array index\n * (bucket index) is calculated from this hash.\n *\n * Doing this avoids hash collisions.\n */",
"$",
"hash",
"=",
"$",
"this",
"->",
"getHash",
"(",
"$",
"key",
")",
";",
"$",
"arrayIndex",
"=",
"$",
"this",
"->",
"getArrayIndex",
"(",
"$",
"hash",
")",
";",
"return",
"$",
"arrayIndex",
";",
"}"
] |
returns the bucket array index by using the "division method".
note that the division method has limitations: if the hash function
calculates the hashes in a constant way, the way how keys are created
can be chosen so that they hash to the same bucket. Thus, the worst-case
scenario of having n nodes in one chain would be true.
Solution: use universal hashing
@param $key
@return int
@throws \doganoo\PHPAlgorithms\Common\Exception\InvalidKeyTypeException
@throws \doganoo\PHPAlgorithms\Common\Exception\UnsupportedKeyTypeException
|
[
"returns",
"the",
"bucket",
"array",
"index",
"by",
"using",
"the",
"division",
"method",
"."
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Maps/HashMap.php#L127-L138
|
27,177
|
doganoo/PHPAlgorithms
|
src/Datastructure/Maps/HashMap.php
|
HashMap.getNodeByKey
|
public function getNodeByKey($key): ?Node {
$arrayIndex = $this->getBucketIndex($key);
/*
* the list is requested from the array based on
* the array index hash.
*/
/** @var SinglyLinkedList $list */
if (!isset($this->bucket[$arrayIndex])) {
return null;
}
$list = $this->bucket[$arrayIndex];
if (!$list->containsKey($key)) {
return null;
}
$node = $list->getNodeByKey($key);
return $node;
}
|
php
|
public function getNodeByKey($key): ?Node {
$arrayIndex = $this->getBucketIndex($key);
/*
* the list is requested from the array based on
* the array index hash.
*/
/** @var SinglyLinkedList $list */
if (!isset($this->bucket[$arrayIndex])) {
return null;
}
$list = $this->bucket[$arrayIndex];
if (!$list->containsKey($key)) {
return null;
}
$node = $list->getNodeByKey($key);
return $node;
}
|
[
"public",
"function",
"getNodeByKey",
"(",
"$",
"key",
")",
":",
"?",
"Node",
"{",
"$",
"arrayIndex",
"=",
"$",
"this",
"->",
"getBucketIndex",
"(",
"$",
"key",
")",
";",
"/*\n * the list is requested from the array based on\n * the array index hash.\n */",
"/** @var SinglyLinkedList $list */",
"if",
"(",
"!",
"isset",
"(",
"$",
"this",
"->",
"bucket",
"[",
"$",
"arrayIndex",
"]",
")",
")",
"{",
"return",
"null",
";",
"}",
"$",
"list",
"=",
"$",
"this",
"->",
"bucket",
"[",
"$",
"arrayIndex",
"]",
";",
"if",
"(",
"!",
"$",
"list",
"->",
"containsKey",
"(",
"$",
"key",
")",
")",
"{",
"return",
"null",
";",
"}",
"$",
"node",
"=",
"$",
"list",
"->",
"getNodeByKey",
"(",
"$",
"key",
")",
";",
"return",
"$",
"node",
";",
"}"
] |
searches the hash map for a node by a given key.
@param $key
@return Node|null
@throws \doganoo\PHPAlgorithms\common\Exception\InvalidKeyTypeException
@throws \doganoo\PHPAlgorithms\common\Exception\UnsupportedKeyTypeException
|
[
"searches",
"the",
"hash",
"map",
"for",
"a",
"node",
"by",
"a",
"given",
"key",
"."
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Maps/HashMap.php#L198-L214
|
27,178
|
doganoo/PHPAlgorithms
|
src/Datastructure/Maps/HashMap.php
|
HashMap.size
|
public function size(): int {
$size = 0;
/**
* @var string $hash
* @var AbstractLinkedList $list
*/
foreach ($this->bucket as $hash => $list) {
$size += $list->size();
}
return $size;
}
|
php
|
public function size(): int {
$size = 0;
/**
* @var string $hash
* @var AbstractLinkedList $list
*/
foreach ($this->bucket as $hash => $list) {
$size += $list->size();
}
return $size;
}
|
[
"public",
"function",
"size",
"(",
")",
":",
"int",
"{",
"$",
"size",
"=",
"0",
";",
"/**\n * @var string $hash\n * @var AbstractLinkedList $list\n */",
"foreach",
"(",
"$",
"this",
"->",
"bucket",
"as",
"$",
"hash",
"=>",
"$",
"list",
")",
"{",
"$",
"size",
"+=",
"$",
"list",
"->",
"size",
"(",
")",
";",
"}",
"return",
"$",
"size",
";",
"}"
] |
returns the number of elements in the map
@return int
|
[
"returns",
"the",
"number",
"of",
"elements",
"in",
"the",
"map"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Maps/HashMap.php#L222-L232
|
27,179
|
doganoo/PHPAlgorithms
|
src/Datastructure/Maps/HashMap.php
|
HashMap.containsValue
|
public function containsValue($value): bool {
/**
* @var string $arrayIndex
* @var SinglyLinkedList $list
*/
foreach ($this->bucket as $arrayIndex => $list) {
/* $list is the first element in the bucket. The bucket
* can contain max $maxSize entries and each entry has zero
* or one nodes which can have zero, one or multiple
* successors.
*/
if ($list->containsValue($value)) {
return true;
}
}
/*
* If no bucket contains the value then return false because
* the searched value is not in the list.
*/
return false;
}
|
php
|
public function containsValue($value): bool {
/**
* @var string $arrayIndex
* @var SinglyLinkedList $list
*/
foreach ($this->bucket as $arrayIndex => $list) {
/* $list is the first element in the bucket. The bucket
* can contain max $maxSize entries and each entry has zero
* or one nodes which can have zero, one or multiple
* successors.
*/
if ($list->containsValue($value)) {
return true;
}
}
/*
* If no bucket contains the value then return false because
* the searched value is not in the list.
*/
return false;
}
|
[
"public",
"function",
"containsValue",
"(",
"$",
"value",
")",
":",
"bool",
"{",
"/**\n * @var string $arrayIndex\n * @var SinglyLinkedList $list\n */",
"foreach",
"(",
"$",
"this",
"->",
"bucket",
"as",
"$",
"arrayIndex",
"=>",
"$",
"list",
")",
"{",
"/* $list is the first element in the bucket. The bucket\n * can contain max $maxSize entries and each entry has zero\n * or one nodes which can have zero, one or multiple\n * successors.\n */",
"if",
"(",
"$",
"list",
"->",
"containsValue",
"(",
"$",
"value",
")",
")",
"{",
"return",
"true",
";",
"}",
"}",
"/*\n * If no bucket contains the value then return false because\n * the searched value is not in the list.\n */",
"return",
"false",
";",
"}"
] |
determines whether the HashMap contains a value.
@param $value
@return bool
|
[
"determines",
"whether",
"the",
"HashMap",
"contains",
"a",
"value",
"."
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Maps/HashMap.php#L240-L261
|
27,180
|
doganoo/PHPAlgorithms
|
src/Datastructure/Maps/HashMap.php
|
HashMap.containsKey
|
public function containsKey($key): bool {
/**
* @var string $arrayIndex
* @var SinglyLinkedList $list
*/
foreach ($this->bucket as $arrayIndex => $list) {
if (null === $list) {
continue;
}
/* $list is the first element in the bucket. The bucket
* can contain max $maxSize entries and each entry has zero
* or one nodes which can have zero, one or multiple
* successors.
*/
if ($list->containsKey($key)) {
return true;
}
}
/*
* If no bucket contains the value then return false because
* the searched value is not in the list.
*/
return false;
}
|
php
|
public function containsKey($key): bool {
/**
* @var string $arrayIndex
* @var SinglyLinkedList $list
*/
foreach ($this->bucket as $arrayIndex => $list) {
if (null === $list) {
continue;
}
/* $list is the first element in the bucket. The bucket
* can contain max $maxSize entries and each entry has zero
* or one nodes which can have zero, one or multiple
* successors.
*/
if ($list->containsKey($key)) {
return true;
}
}
/*
* If no bucket contains the value then return false because
* the searched value is not in the list.
*/
return false;
}
|
[
"public",
"function",
"containsKey",
"(",
"$",
"key",
")",
":",
"bool",
"{",
"/**\n * @var string $arrayIndex\n * @var SinglyLinkedList $list\n */",
"foreach",
"(",
"$",
"this",
"->",
"bucket",
"as",
"$",
"arrayIndex",
"=>",
"$",
"list",
")",
"{",
"if",
"(",
"null",
"===",
"$",
"list",
")",
"{",
"continue",
";",
"}",
"/* $list is the first element in the bucket. The bucket\n * can contain max $maxSize entries and each entry has zero\n * or one nodes which can have zero, one or multiple\n * successors.\n */",
"if",
"(",
"$",
"list",
"->",
"containsKey",
"(",
"$",
"key",
")",
")",
"{",
"return",
"true",
";",
"}",
"}",
"/*\n * If no bucket contains the value then return false because\n * the searched value is not in the list.\n */",
"return",
"false",
";",
"}"
] |
determines whether the HashMap contains a key.
@param $key
@return bool
|
[
"determines",
"whether",
"the",
"HashMap",
"contains",
"a",
"key",
"."
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Maps/HashMap.php#L269-L292
|
27,181
|
doganoo/PHPAlgorithms
|
src/Datastructure/Maps/HashMap.php
|
HashMap.remove
|
public function remove($key): bool {
//get the corresponding index to key
$arrayIndex = $this->getBucketIndex($key);
/*
*if the array index is not available in the
* bucket list, end the method and return true.
* True due to the operation was successful, meaning
* that $key is not in the list.
* False would indicate that there was an error
* and the node is still in the list
*/
if (!isset($this->bucket[$arrayIndex])) {
return true;
}
/** @var SinglyLinkedList $list */
$list = $this->bucket[$arrayIndex];
/** @var Node $head */
$head = $list->getHead();
/*
* there is one special case for the HashMap:
* if there is only one node in the bucket, then
* check if the nodes key equals to the key that
* should be deleted.
* If this is the case, set the bucket to null
* because the only one node is removed.
* If this is not the key, return false as there
* is no node to remove.
*/
if ($list->size() == 1 && $head->getKey() === $key) {
//$this->bucket[$arrayIndex] = null;
unset($this->bucket[$arrayIndex]);
return true;
}
return $list->remove($key);
}
|
php
|
public function remove($key): bool {
//get the corresponding index to key
$arrayIndex = $this->getBucketIndex($key);
/*
*if the array index is not available in the
* bucket list, end the method and return true.
* True due to the operation was successful, meaning
* that $key is not in the list.
* False would indicate that there was an error
* and the node is still in the list
*/
if (!isset($this->bucket[$arrayIndex])) {
return true;
}
/** @var SinglyLinkedList $list */
$list = $this->bucket[$arrayIndex];
/** @var Node $head */
$head = $list->getHead();
/*
* there is one special case for the HashMap:
* if there is only one node in the bucket, then
* check if the nodes key equals to the key that
* should be deleted.
* If this is the case, set the bucket to null
* because the only one node is removed.
* If this is not the key, return false as there
* is no node to remove.
*/
if ($list->size() == 1 && $head->getKey() === $key) {
//$this->bucket[$arrayIndex] = null;
unset($this->bucket[$arrayIndex]);
return true;
}
return $list->remove($key);
}
|
[
"public",
"function",
"remove",
"(",
"$",
"key",
")",
":",
"bool",
"{",
"//get the corresponding index to key",
"$",
"arrayIndex",
"=",
"$",
"this",
"->",
"getBucketIndex",
"(",
"$",
"key",
")",
";",
"/*\n *if the array index is not available in the\n * bucket list, end the method and return true.\n * True due to the operation was successful, meaning\n * that $key is not in the list.\n * False would indicate that there was an error\n * and the node is still in the list\n */",
"if",
"(",
"!",
"isset",
"(",
"$",
"this",
"->",
"bucket",
"[",
"$",
"arrayIndex",
"]",
")",
")",
"{",
"return",
"true",
";",
"}",
"/** @var SinglyLinkedList $list */",
"$",
"list",
"=",
"$",
"this",
"->",
"bucket",
"[",
"$",
"arrayIndex",
"]",
";",
"/** @var Node $head */",
"$",
"head",
"=",
"$",
"list",
"->",
"getHead",
"(",
")",
";",
"/*\n * there is one special case for the HashMap:\n * if there is only one node in the bucket, then\n * check if the nodes key equals to the key that\n * should be deleted.\n * If this is the case, set the bucket to null\n * because the only one node is removed.\n * If this is not the key, return false as there\n * is no node to remove.\n */",
"if",
"(",
"$",
"list",
"->",
"size",
"(",
")",
"==",
"1",
"&&",
"$",
"head",
"->",
"getKey",
"(",
")",
"===",
"$",
"key",
")",
"{",
"//$this->bucket[$arrayIndex] = null;",
"unset",
"(",
"$",
"this",
"->",
"bucket",
"[",
"$",
"arrayIndex",
"]",
")",
";",
"return",
"true",
";",
"}",
"return",
"$",
"list",
"->",
"remove",
"(",
"$",
"key",
")",
";",
"}"
] |
removes a node by a given key
@param $key
@return bool
@throws \doganoo\PHPAlgorithms\Common\Exception\InvalidKeyTypeException
@throws \doganoo\PHPAlgorithms\Common\Exception\UnsupportedKeyTypeException
|
[
"removes",
"a",
"node",
"by",
"a",
"given",
"key"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Maps/HashMap.php#L333-L368
|
27,182
|
symbiote/silverstripe-components
|
src/Symbiote/Components/ComponentService.php
|
ComponentService.exportNestedDataForTemplates
|
private static function exportNestedDataForTemplates(array $array, $root = true)
{
// depth first
foreach ($array as $prop => &$value) {
if (is_array($value)) {
$value = self::exportNestedDataForTemplates($value, false);
}
}
unset($value);
// json data expected to be keyed with ints, over the usual strings
if (isset($array[0])) {
// replace array with exportable array list
$array = new ArrayListExportable($array);
}
return $root ? var_export($array, true) : $array;
}
|
php
|
private static function exportNestedDataForTemplates(array $array, $root = true)
{
// depth first
foreach ($array as $prop => &$value) {
if (is_array($value)) {
$value = self::exportNestedDataForTemplates($value, false);
}
}
unset($value);
// json data expected to be keyed with ints, over the usual strings
if (isset($array[0])) {
// replace array with exportable array list
$array = new ArrayListExportable($array);
}
return $root ? var_export($array, true) : $array;
}
|
[
"private",
"static",
"function",
"exportNestedDataForTemplates",
"(",
"array",
"$",
"array",
",",
"$",
"root",
"=",
"true",
")",
"{",
"// depth first",
"foreach",
"(",
"$",
"array",
"as",
"$",
"prop",
"=>",
"&",
"$",
"value",
")",
"{",
"if",
"(",
"is_array",
"(",
"$",
"value",
")",
")",
"{",
"$",
"value",
"=",
"self",
"::",
"exportNestedDataForTemplates",
"(",
"$",
"value",
",",
"false",
")",
";",
"}",
"}",
"unset",
"(",
"$",
"value",
")",
";",
"// json data expected to be keyed with ints, over the usual strings",
"if",
"(",
"isset",
"(",
"$",
"array",
"[",
"0",
"]",
")",
")",
"{",
"// replace array with exportable array list",
"$",
"array",
"=",
"new",
"ArrayListExportable",
"(",
"$",
"array",
")",
";",
"}",
"return",
"$",
"root",
"?",
"var_export",
"(",
"$",
"array",
",",
"true",
")",
":",
"$",
"array",
";",
"}"
] |
Recursively replace nonassociative arrays with ArrayListExportable and
output with 'var_export' to produce template logic for the nested data.
@param array $array The nested data to export
@param bool $root Ignore this
@return string Executable template logic
|
[
"Recursively",
"replace",
"nonassociative",
"arrays",
"with",
"ArrayListExportable",
"and",
"output",
"with",
"var_export",
"to",
"produce",
"template",
"logic",
"for",
"the",
"nested",
"data",
"."
] |
4c56b47242771816bdb46937543902290a4d753c
|
https://github.com/symbiote/silverstripe-components/blob/4c56b47242771816bdb46937543902290a4d753c/src/Symbiote/Components/ComponentService.php#L226-L241
|
27,183
|
doganoo/PHPAlgorithms
|
src/Datastructure/Graph/Tree/Heap/MinHeap.php
|
MinHeap.clear
|
public function clear(): bool {
$this->heap = \array_fill(0, $this->maxSize, null);
$this->heap[0] = \PHP_INT_MIN;
return \count($this->heap) === 1 && $this->heap[0] === \PHP_INT_MIN;
}
|
php
|
public function clear(): bool {
$this->heap = \array_fill(0, $this->maxSize, null);
$this->heap[0] = \PHP_INT_MIN;
return \count($this->heap) === 1 && $this->heap[0] === \PHP_INT_MIN;
}
|
[
"public",
"function",
"clear",
"(",
")",
":",
"bool",
"{",
"$",
"this",
"->",
"heap",
"=",
"\\",
"array_fill",
"(",
"0",
",",
"$",
"this",
"->",
"maxSize",
",",
"null",
")",
";",
"$",
"this",
"->",
"heap",
"[",
"0",
"]",
"=",
"\\",
"PHP_INT_MIN",
";",
"return",
"\\",
"count",
"(",
"$",
"this",
"->",
"heap",
")",
"===",
"1",
"&&",
"$",
"this",
"->",
"heap",
"[",
"0",
"]",
"===",
"\\",
"PHP_INT_MIN",
";",
"}"
] |
resets the heap
@return bool
|
[
"resets",
"the",
"heap"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Graph/Tree/Heap/MinHeap.php#L61-L65
|
27,184
|
doganoo/PHPAlgorithms
|
src/Datastructure/Graph/Tree/Heap/MinHeap.php
|
MinHeap.length
|
public function length(): int {
$array = \array_filter($this->heap, function ($v, $k) {
return $v !== null;
}, \ARRAY_FILTER_USE_BOTH);
return \count($array) - 1;
}
|
php
|
public function length(): int {
$array = \array_filter($this->heap, function ($v, $k) {
return $v !== null;
}, \ARRAY_FILTER_USE_BOTH);
return \count($array) - 1;
}
|
[
"public",
"function",
"length",
"(",
")",
":",
"int",
"{",
"$",
"array",
"=",
"\\",
"array_filter",
"(",
"$",
"this",
"->",
"heap",
",",
"function",
"(",
"$",
"v",
",",
"$",
"k",
")",
"{",
"return",
"$",
"v",
"!==",
"null",
";",
"}",
",",
"\\",
"ARRAY_FILTER_USE_BOTH",
")",
";",
"return",
"\\",
"count",
"(",
"$",
"array",
")",
"-",
"1",
";",
"}"
] |
returns the number of elements in the heap
@return int
|
[
"returns",
"the",
"number",
"of",
"elements",
"in",
"the",
"heap"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Graph/Tree/Heap/MinHeap.php#L101-L106
|
27,185
|
doganoo/PHPAlgorithms
|
src/Datastructure/Graph/Tree/Heap/MinHeap.php
|
MinHeap.swap
|
public function swap(int $current, int $parent): void {
$tmp = $this->heap[$current];
$this->heap[$current] = $this->heap[$parent];
$this->heap[$parent] = $tmp;
}
|
php
|
public function swap(int $current, int $parent): void {
$tmp = $this->heap[$current];
$this->heap[$current] = $this->heap[$parent];
$this->heap[$parent] = $tmp;
}
|
[
"public",
"function",
"swap",
"(",
"int",
"$",
"current",
",",
"int",
"$",
"parent",
")",
":",
"void",
"{",
"$",
"tmp",
"=",
"$",
"this",
"->",
"heap",
"[",
"$",
"current",
"]",
";",
"$",
"this",
"->",
"heap",
"[",
"$",
"current",
"]",
"=",
"$",
"this",
"->",
"heap",
"[",
"$",
"parent",
"]",
";",
"$",
"this",
"->",
"heap",
"[",
"$",
"parent",
"]",
"=",
"$",
"tmp",
";",
"}"
] |
swaps the value at the current position with the value at the parent position
@param int $current
@param int $parent
|
[
"swaps",
"the",
"value",
"at",
"the",
"current",
"position",
"with",
"the",
"value",
"at",
"the",
"parent",
"position"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Graph/Tree/Heap/MinHeap.php#L145-L149
|
27,186
|
doganoo/PHPAlgorithms
|
src/Datastructure/Graph/Tree/Heap/MinHeap.php
|
MinHeap.inHeap
|
public function inHeap(int $element): bool {
foreach ($this->heap as $key => $value) {
if (Comparator::equals($element, $value)) {
return true;
}
}
return false;
}
|
php
|
public function inHeap(int $element): bool {
foreach ($this->heap as $key => $value) {
if (Comparator::equals($element, $value)) {
return true;
}
}
return false;
}
|
[
"public",
"function",
"inHeap",
"(",
"int",
"$",
"element",
")",
":",
"bool",
"{",
"foreach",
"(",
"$",
"this",
"->",
"heap",
"as",
"$",
"key",
"=>",
"$",
"value",
")",
"{",
"if",
"(",
"Comparator",
"::",
"equals",
"(",
"$",
"element",
",",
"$",
"value",
")",
")",
"{",
"return",
"true",
";",
"}",
"}",
"return",
"false",
";",
"}"
] |
determines if a element is in the heap or not
@param int $element
@return bool
|
[
"determines",
"if",
"a",
"element",
"is",
"in",
"the",
"heap",
"or",
"not"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Graph/Tree/Heap/MinHeap.php#L157-L164
|
27,187
|
doganoo/PHPAlgorithms
|
src/Datastructure/Cache/LRUCache.php
|
LRUCache.put
|
public function put($key, $value): bool {
if ($this->hashMap->containsKey($key)) {
$node = $this->getNodeFromHead($key);
$node->setKey($key);
$node->setValue($value);
$this->unset($node);
$this->updateHead($node);
return true;
}
if ($this->hashMap->size() >= $this->capacity) {
$end = $this->getEnd();
if (null !== $end) {
$this->unset($end);
$this->hashMap->remove($end->getKey());
}
}
$node = new Node();
$node->setKey($key);
$node->setValue($value);
$this->updateHead($node);
$this->hashMap->add($key, $value);
return true;
}
|
php
|
public function put($key, $value): bool {
if ($this->hashMap->containsKey($key)) {
$node = $this->getNodeFromHead($key);
$node->setKey($key);
$node->setValue($value);
$this->unset($node);
$this->updateHead($node);
return true;
}
if ($this->hashMap->size() >= $this->capacity) {
$end = $this->getEnd();
if (null !== $end) {
$this->unset($end);
$this->hashMap->remove($end->getKey());
}
}
$node = new Node();
$node->setKey($key);
$node->setValue($value);
$this->updateHead($node);
$this->hashMap->add($key, $value);
return true;
}
|
[
"public",
"function",
"put",
"(",
"$",
"key",
",",
"$",
"value",
")",
":",
"bool",
"{",
"if",
"(",
"$",
"this",
"->",
"hashMap",
"->",
"containsKey",
"(",
"$",
"key",
")",
")",
"{",
"$",
"node",
"=",
"$",
"this",
"->",
"getNodeFromHead",
"(",
"$",
"key",
")",
";",
"$",
"node",
"->",
"setKey",
"(",
"$",
"key",
")",
";",
"$",
"node",
"->",
"setValue",
"(",
"$",
"value",
")",
";",
"$",
"this",
"->",
"unset",
"(",
"$",
"node",
")",
";",
"$",
"this",
"->",
"updateHead",
"(",
"$",
"node",
")",
";",
"return",
"true",
";",
"}",
"if",
"(",
"$",
"this",
"->",
"hashMap",
"->",
"size",
"(",
")",
">=",
"$",
"this",
"->",
"capacity",
")",
"{",
"$",
"end",
"=",
"$",
"this",
"->",
"getEnd",
"(",
")",
";",
"if",
"(",
"null",
"!==",
"$",
"end",
")",
"{",
"$",
"this",
"->",
"unset",
"(",
"$",
"end",
")",
";",
"$",
"this",
"->",
"hashMap",
"->",
"remove",
"(",
"$",
"end",
"->",
"getKey",
"(",
")",
")",
";",
"}",
"}",
"$",
"node",
"=",
"new",
"Node",
"(",
")",
";",
"$",
"node",
"->",
"setKey",
"(",
"$",
"key",
")",
";",
"$",
"node",
"->",
"setValue",
"(",
"$",
"value",
")",
";",
"$",
"this",
"->",
"updateHead",
"(",
"$",
"node",
")",
";",
"$",
"this",
"->",
"hashMap",
"->",
"add",
"(",
"$",
"key",
",",
"$",
"value",
")",
";",
"return",
"true",
";",
"}"
] |
adds a new key value pair
@param $key
@param $value
@return bool
@throws \doganoo\PHPAlgorithms\common\Exception\InvalidKeyTypeException
@throws \doganoo\PHPAlgorithms\common\Exception\UnsupportedKeyTypeException
|
[
"adds",
"a",
"new",
"key",
"value",
"pair"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Cache/LRUCache.php#L65-L87
|
27,188
|
doganoo/PHPAlgorithms
|
src/Datastructure/Cache/LRUCache.php
|
LRUCache.get
|
public function get($key) {
if ($this->hashMap->containsKey($key)) {
$node = $this->getNodeFromHead($key);
$this->unset($node);
$this->updateHead($node);
return $this->head->getValue();
}
return null;
}
|
php
|
public function get($key) {
if ($this->hashMap->containsKey($key)) {
$node = $this->getNodeFromHead($key);
$this->unset($node);
$this->updateHead($node);
return $this->head->getValue();
}
return null;
}
|
[
"public",
"function",
"get",
"(",
"$",
"key",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"hashMap",
"->",
"containsKey",
"(",
"$",
"key",
")",
")",
"{",
"$",
"node",
"=",
"$",
"this",
"->",
"getNodeFromHead",
"(",
"$",
"key",
")",
";",
"$",
"this",
"->",
"unset",
"(",
"$",
"node",
")",
";",
"$",
"this",
"->",
"updateHead",
"(",
"$",
"node",
")",
";",
"return",
"$",
"this",
"->",
"head",
"->",
"getValue",
"(",
")",
";",
"}",
"return",
"null",
";",
"}"
] |
retrieves a value
@param $key
@return mixed
|
[
"retrieves",
"a",
"value"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Cache/LRUCache.php#L152-L160
|
27,189
|
doganoo/PHPAlgorithms
|
src/Datastructure/Cache/LRUCache.php
|
LRUCache.delete
|
public function delete($key): bool {
$node = $this->getNodeFromHead($key);
if (null === $node) {
return false;
}
/** @var Node $head */
$head = $this->head;
while (null !== $head) {
if (Comparator::equals($head->getKey(), $node->getKey())) {
$this->unset($head);
$this->hashMap->remove($key);
return true;
}
$head = $head->getNext();
}
return false;
}
|
php
|
public function delete($key): bool {
$node = $this->getNodeFromHead($key);
if (null === $node) {
return false;
}
/** @var Node $head */
$head = $this->head;
while (null !== $head) {
if (Comparator::equals($head->getKey(), $node->getKey())) {
$this->unset($head);
$this->hashMap->remove($key);
return true;
}
$head = $head->getNext();
}
return false;
}
|
[
"public",
"function",
"delete",
"(",
"$",
"key",
")",
":",
"bool",
"{",
"$",
"node",
"=",
"$",
"this",
"->",
"getNodeFromHead",
"(",
"$",
"key",
")",
";",
"if",
"(",
"null",
"===",
"$",
"node",
")",
"{",
"return",
"false",
";",
"}",
"/** @var Node $head */",
"$",
"head",
"=",
"$",
"this",
"->",
"head",
";",
"while",
"(",
"null",
"!==",
"$",
"head",
")",
"{",
"if",
"(",
"Comparator",
"::",
"equals",
"(",
"$",
"head",
"->",
"getKey",
"(",
")",
",",
"$",
"node",
"->",
"getKey",
"(",
")",
")",
")",
"{",
"$",
"this",
"->",
"unset",
"(",
"$",
"head",
")",
";",
"$",
"this",
"->",
"hashMap",
"->",
"remove",
"(",
"$",
"key",
")",
";",
"return",
"true",
";",
"}",
"$",
"head",
"=",
"$",
"head",
"->",
"getNext",
"(",
")",
";",
"}",
"return",
"false",
";",
"}"
] |
deletes a given key
@param $key
@return bool
@throws \doganoo\PHPAlgorithms\common\Exception\InvalidKeyTypeException
@throws \doganoo\PHPAlgorithms\common\Exception\UnsupportedKeyTypeException
|
[
"deletes",
"a",
"given",
"key"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Cache/LRUCache.php#L179-L195
|
27,190
|
acmephp/core
|
Http/SecureHttpClientFactory.php
|
SecureHttpClientFactory.createSecureHttpClient
|
public function createSecureHttpClient(KeyPair $accountKeyPair)
{
return new SecureHttpClient(
$accountKeyPair,
$this->httpClient,
$this->base64Encoder,
$this->keyParser,
$this->dataSigner,
$this->errorHandler
);
}
|
php
|
public function createSecureHttpClient(KeyPair $accountKeyPair)
{
return new SecureHttpClient(
$accountKeyPair,
$this->httpClient,
$this->base64Encoder,
$this->keyParser,
$this->dataSigner,
$this->errorHandler
);
}
|
[
"public",
"function",
"createSecureHttpClient",
"(",
"KeyPair",
"$",
"accountKeyPair",
")",
"{",
"return",
"new",
"SecureHttpClient",
"(",
"$",
"accountKeyPair",
",",
"$",
"this",
"->",
"httpClient",
",",
"$",
"this",
"->",
"base64Encoder",
",",
"$",
"this",
"->",
"keyParser",
",",
"$",
"this",
"->",
"dataSigner",
",",
"$",
"this",
"->",
"errorHandler",
")",
";",
"}"
] |
Create a SecureHttpClient using a given account KeyPair.
@param KeyPair $accountKeyPair
@return SecureHttpClient
|
[
"Create",
"a",
"SecureHttpClient",
"using",
"a",
"given",
"account",
"KeyPair",
"."
] |
56af3e28926f4b7507397bfe7fb1ca5f83d73eb4
|
https://github.com/acmephp/core/blob/56af3e28926f4b7507397bfe7fb1ca5f83d73eb4/Http/SecureHttpClientFactory.php#L79-L89
|
27,191
|
doganoo/PHPAlgorithms
|
src/Datastructure/Lists/LinkedLists/DoublyLinkedList.php
|
DoublyLinkedList.prepend
|
public function prepend(?Node $node): bool {
if ($node === null) {
return false;
}
/*
* need to clone the object otherwise the object
* references are going crazy.
*
* Furthermore, setting previous and next to null
* as they will be set later.
*/
$newNode = clone $node;
$newNode->setPrevious(null);
$newNode->setNext(null);
if ($this->getHead() === null) {
$this->setHead($newNode);
return true;
}
$head = $this->getHead();
$head->setPrevious($newNode);
$newNode->setNext($head);
$this->setHead($newNode);
return true;
}
|
php
|
public function prepend(?Node $node): bool {
if ($node === null) {
return false;
}
/*
* need to clone the object otherwise the object
* references are going crazy.
*
* Furthermore, setting previous and next to null
* as they will be set later.
*/
$newNode = clone $node;
$newNode->setPrevious(null);
$newNode->setNext(null);
if ($this->getHead() === null) {
$this->setHead($newNode);
return true;
}
$head = $this->getHead();
$head->setPrevious($newNode);
$newNode->setNext($head);
$this->setHead($newNode);
return true;
}
|
[
"public",
"function",
"prepend",
"(",
"?",
"Node",
"$",
"node",
")",
":",
"bool",
"{",
"if",
"(",
"$",
"node",
"===",
"null",
")",
"{",
"return",
"false",
";",
"}",
"/*\n * need to clone the object otherwise the object\n * references are going crazy.\n *\n * Furthermore, setting previous and next to null\n * as they will be set later.\n */",
"$",
"newNode",
"=",
"clone",
"$",
"node",
";",
"$",
"newNode",
"->",
"setPrevious",
"(",
"null",
")",
";",
"$",
"newNode",
"->",
"setNext",
"(",
"null",
")",
";",
"if",
"(",
"$",
"this",
"->",
"getHead",
"(",
")",
"===",
"null",
")",
"{",
"$",
"this",
"->",
"setHead",
"(",
"$",
"newNode",
")",
";",
"return",
"true",
";",
"}",
"$",
"head",
"=",
"$",
"this",
"->",
"getHead",
"(",
")",
";",
"$",
"head",
"->",
"setPrevious",
"(",
"$",
"newNode",
")",
";",
"$",
"newNode",
"->",
"setNext",
"(",
"$",
"head",
")",
";",
"$",
"this",
"->",
"setHead",
"(",
"$",
"newNode",
")",
";",
"return",
"true",
";",
"}"
] |
prepends a node on top of the list
@param \doganoo\PHPAlgorithms\Datastructure\Lists\Node|null $node
@return bool
|
[
"prepends",
"a",
"node",
"on",
"top",
"of",
"the",
"list"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Lists/LinkedLists/DoublyLinkedList.php#L76-L100
|
27,192
|
doganoo/PHPAlgorithms
|
src/Algorithm/Traversal/InOrder.php
|
InOrder._traverse
|
public function _traverse(?IBinaryNode $node) {
if (null !== $node) {
if (null !== $node->getLeft()) {
$this->_traverse($node->getLeft());
}
parent::visit($node->getValue());
if (null !== $node->getRight()) {
$this->_traverse($node->getRight());
}
}
}
|
php
|
public function _traverse(?IBinaryNode $node) {
if (null !== $node) {
if (null !== $node->getLeft()) {
$this->_traverse($node->getLeft());
}
parent::visit($node->getValue());
if (null !== $node->getRight()) {
$this->_traverse($node->getRight());
}
}
}
|
[
"public",
"function",
"_traverse",
"(",
"?",
"IBinaryNode",
"$",
"node",
")",
"{",
"if",
"(",
"null",
"!==",
"$",
"node",
")",
"{",
"if",
"(",
"null",
"!==",
"$",
"node",
"->",
"getLeft",
"(",
")",
")",
"{",
"$",
"this",
"->",
"_traverse",
"(",
"$",
"node",
"->",
"getLeft",
"(",
")",
")",
";",
"}",
"parent",
"::",
"visit",
"(",
"$",
"node",
"->",
"getValue",
"(",
")",
")",
";",
"if",
"(",
"null",
"!==",
"$",
"node",
"->",
"getRight",
"(",
")",
")",
"{",
"$",
"this",
"->",
"_traverse",
"(",
"$",
"node",
"->",
"getRight",
"(",
")",
")",
";",
"}",
"}",
"}"
] |
helper method for traversing
@param IBinaryNode|null $node
|
[
"helper",
"method",
"for",
"traversing"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Algorithm/Traversal/InOrder.php#L63-L73
|
27,193
|
doganoo/PHPAlgorithms
|
src/Datastructure/Stackqueue/FixedQueue.php
|
FixedQueue.isValid
|
protected function isValid(): bool {
$parent = parent::isValid();
$maxSize = parent::size() < $this->maxSize;
return $parent && $maxSize;
}
|
php
|
protected function isValid(): bool {
$parent = parent::isValid();
$maxSize = parent::size() < $this->maxSize;
return $parent && $maxSize;
}
|
[
"protected",
"function",
"isValid",
"(",
")",
":",
"bool",
"{",
"$",
"parent",
"=",
"parent",
"::",
"isValid",
"(",
")",
";",
"$",
"maxSize",
"=",
"parent",
"::",
"size",
"(",
")",
"<",
"$",
"this",
"->",
"maxSize",
";",
"return",
"$",
"parent",
"&&",
"$",
"maxSize",
";",
"}"
] |
returns whether the element is valid or not.
Checks among other things also the number of elements
@return bool
|
[
"returns",
"whether",
"the",
"element",
"is",
"valid",
"or",
"not",
".",
"Checks",
"among",
"other",
"things",
"also",
"the",
"number",
"of",
"elements"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Stackqueue/FixedQueue.php#L66-L70
|
27,194
|
doganoo/PHPAlgorithms
|
src/Datastructure/Lists/ArrayLists/ArrayList.php
|
ArrayList.removeByValue
|
public function removeByValue($value): bool {
if (!$this->containsValue($value)) {
return true;
}
$key = $this->indexOf($value);
$removed = $this->remove($key);
return $removed;
}
|
php
|
public function removeByValue($value): bool {
if (!$this->containsValue($value)) {
return true;
}
$key = $this->indexOf($value);
$removed = $this->remove($key);
return $removed;
}
|
[
"public",
"function",
"removeByValue",
"(",
"$",
"value",
")",
":",
"bool",
"{",
"if",
"(",
"!",
"$",
"this",
"->",
"containsValue",
"(",
"$",
"value",
")",
")",
"{",
"return",
"true",
";",
"}",
"$",
"key",
"=",
"$",
"this",
"->",
"indexOf",
"(",
"$",
"value",
")",
";",
"$",
"removed",
"=",
"$",
"this",
"->",
"remove",
"(",
"$",
"key",
")",
";",
"return",
"$",
"removed",
";",
"}"
] |
removes a single value from the list
@param $value
@return bool
|
[
"removes",
"a",
"single",
"value",
"from",
"the",
"list"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Lists/ArrayLists/ArrayList.php#L154-L161
|
27,195
|
doganoo/PHPAlgorithms
|
src/Datastructure/Lists/ArrayLists/ArrayList.php
|
ArrayList.addAllArray
|
public function addAllArray(array $array): bool {
$added = true;
foreach ($array as $value) {
$added &= $this->add($value);
}
return $added;
}
|
php
|
public function addAllArray(array $array): bool {
$added = true;
foreach ($array as $value) {
$added &= $this->add($value);
}
return $added;
}
|
[
"public",
"function",
"addAllArray",
"(",
"array",
"$",
"array",
")",
":",
"bool",
"{",
"$",
"added",
"=",
"true",
";",
"foreach",
"(",
"$",
"array",
"as",
"$",
"value",
")",
"{",
"$",
"added",
"&=",
"$",
"this",
"->",
"add",
"(",
"$",
"value",
")",
";",
"}",
"return",
"$",
"added",
";",
"}"
] |
adds all elements of an array to the list
@param array $array
@return bool
|
[
"adds",
"all",
"elements",
"of",
"an",
"array",
"to",
"the",
"list"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Lists/ArrayLists/ArrayList.php#L325-L331
|
27,196
|
doganoo/PHPAlgorithms
|
src/Datastructure/Lists/ArrayLists/ArrayList.php
|
ArrayList.addAll
|
public function addAll(ArrayList $arrayList): bool {
$added = false;
foreach ($arrayList as $value) {
$added |= $this->add($value);
}
return $added;
}
|
php
|
public function addAll(ArrayList $arrayList): bool {
$added = false;
foreach ($arrayList as $value) {
$added |= $this->add($value);
}
return $added;
}
|
[
"public",
"function",
"addAll",
"(",
"ArrayList",
"$",
"arrayList",
")",
":",
"bool",
"{",
"$",
"added",
"=",
"false",
";",
"foreach",
"(",
"$",
"arrayList",
"as",
"$",
"value",
")",
"{",
"$",
"added",
"|=",
"$",
"this",
"->",
"add",
"(",
"$",
"value",
")",
";",
"}",
"return",
"$",
"added",
";",
"}"
] |
adds all elements of an array list to the list
@param ArrayList $arrayList
@return bool
|
[
"adds",
"all",
"elements",
"of",
"an",
"array",
"list",
"to",
"the",
"list"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Datastructure/Lists/ArrayLists/ArrayList.php#L367-L373
|
27,197
|
doganoo/PHPAlgorithms
|
src/Common/Util/MapUtil.php
|
MapUtil.stringToKey
|
public static function stringToKey(string $string): int {
$key = 0;
for ($i = 0; $i < \strlen($string); $i++) {
$char = $string[$i];
$key += \ord($char);
}
return $key;
}
|
php
|
public static function stringToKey(string $string): int {
$key = 0;
for ($i = 0; $i < \strlen($string); $i++) {
$char = $string[$i];
$key += \ord($char);
}
return $key;
}
|
[
"public",
"static",
"function",
"stringToKey",
"(",
"string",
"$",
"string",
")",
":",
"int",
"{",
"$",
"key",
"=",
"0",
";",
"for",
"(",
"$",
"i",
"=",
"0",
";",
"$",
"i",
"<",
"\\",
"strlen",
"(",
"$",
"string",
")",
";",
"$",
"i",
"++",
")",
"{",
"$",
"char",
"=",
"$",
"string",
"[",
"$",
"i",
"]",
";",
"$",
"key",
"+=",
"\\",
"ord",
"(",
"$",
"char",
")",
";",
"}",
"return",
"$",
"key",
";",
"}"
] |
converts a string to a hash map key by summing all
ASCII values of each character.
@param string $string
@return int
|
[
"converts",
"a",
"string",
"to",
"a",
"hash",
"map",
"key",
"by",
"summing",
"all",
"ASCII",
"values",
"of",
"each",
"character",
"."
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Common/Util/MapUtil.php#L96-L103
|
27,198
|
doganoo/PHPAlgorithms
|
src/Common/Util/MapUtil.php
|
MapUtil.objectToString
|
public static function objectToString($object): string {
//TODO do type hinting when possible
if (!\is_object($object)) {
throw new InvalidKeyTypeException("key has to be an object, " . \gettype($object) . "given");
}
return \serialize($object);
}
|
php
|
public static function objectToString($object): string {
//TODO do type hinting when possible
if (!\is_object($object)) {
throw new InvalidKeyTypeException("key has to be an object, " . \gettype($object) . "given");
}
return \serialize($object);
}
|
[
"public",
"static",
"function",
"objectToString",
"(",
"$",
"object",
")",
":",
"string",
"{",
"//TODO do type hinting when possible",
"if",
"(",
"!",
"\\",
"is_object",
"(",
"$",
"object",
")",
")",
"{",
"throw",
"new",
"InvalidKeyTypeException",
"(",
"\"key has to be an object, \"",
".",
"\\",
"gettype",
"(",
"$",
"object",
")",
".",
"\"given\"",
")",
";",
"}",
"return",
"\\",
"serialize",
"(",
"$",
"object",
")",
";",
"}"
] |
This methods converts an object to a string using serialization
@param $object
@return string
@throws InvalidKeyTypeException
|
[
"This",
"methods",
"converts",
"an",
"object",
"to",
"a",
"string",
"using",
"serialization"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Common/Util/MapUtil.php#L112-L118
|
27,199
|
doganoo/PHPAlgorithms
|
src/Common/Util/MapUtil.php
|
MapUtil.arrayToKey
|
public static function arrayToKey(array $array): string {
$result = "";
\array_walk_recursive($array, function ($key, $value) use (&$result) {
$result .= $key . $value;
});
return $result;
}
|
php
|
public static function arrayToKey(array $array): string {
$result = "";
\array_walk_recursive($array, function ($key, $value) use (&$result) {
$result .= $key . $value;
});
return $result;
}
|
[
"public",
"static",
"function",
"arrayToKey",
"(",
"array",
"$",
"array",
")",
":",
"string",
"{",
"$",
"result",
"=",
"\"\"",
";",
"\\",
"array_walk_recursive",
"(",
"$",
"array",
",",
"function",
"(",
"$",
"key",
",",
"$",
"value",
")",
"use",
"(",
"&",
"$",
"result",
")",
"{",
"$",
"result",
".=",
"$",
"key",
".",
"$",
"value",
";",
"}",
")",
";",
"return",
"$",
"result",
";",
"}"
] |
converts an array to a valid key for HashMap
@param array $array
@return string
|
[
"converts",
"an",
"array",
"to",
"a",
"valid",
"key",
"for",
"HashMap"
] |
7d07d4b1970958defda3c88188b7683ec999735f
|
https://github.com/doganoo/PHPAlgorithms/blob/7d07d4b1970958defda3c88188b7683ec999735f/src/Common/Util/MapUtil.php#L126-L132
|
Subsets and Splits
Yii Code Samples
Gathers all records from test, train, and validation sets that contain the word 'yii', providing a basic filtered view of the dataset relevant to Yii-related content.