repository_name
stringlengths
5
67
func_path_in_repository
stringlengths
4
234
func_name
stringlengths
0
314
whole_func_string
stringlengths
52
3.87M
language
stringclasses
6 values
func_code_string
stringlengths
52
3.87M
func_code_tokens
listlengths
15
672k
func_documentation_string
stringlengths
1
47.2k
func_documentation_tokens
listlengths
1
3.92k
split_name
stringclasses
1 value
func_code_url
stringlengths
85
339
Rct567/DomQuery
src/Rct567/DomQuery/DomQueryNodes.php
DomQueryNodes.getClonedNodes
protected function getClonedNodes() { $cloned_nodes = array(); foreach ($this->nodes as $node) { $cloned_node = $node->cloneNode(true); if ($cloned_node instanceof \DOMElement && $cloned_node->hasAttribute('dqn_tmp_id')) { $cloned_node->removeAttribute('dqn_tmp_id'); } $cloned_nodes[] = $cloned_node; } return $cloned_nodes; }
php
protected function getClonedNodes() { $cloned_nodes = array(); foreach ($this->nodes as $node) { $cloned_node = $node->cloneNode(true); if ($cloned_node instanceof \DOMElement && $cloned_node->hasAttribute('dqn_tmp_id')) { $cloned_node->removeAttribute('dqn_tmp_id'); } $cloned_nodes[] = $cloned_node; } return $cloned_nodes; }
[ "protected", "function", "getClonedNodes", "(", ")", "{", "$", "cloned_nodes", "=", "array", "(", ")", ";", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "$", "cloned_node", "=", "$", "node", "->", "cloneNode", "(", "true", ")", ";", "if", "(", "$", "cloned_node", "instanceof", "\\", "DOMElement", "&&", "$", "cloned_node", "->", "hasAttribute", "(", "'dqn_tmp_id'", ")", ")", "{", "$", "cloned_node", "->", "removeAttribute", "(", "'dqn_tmp_id'", ")", ";", "}", "$", "cloned_nodes", "[", "]", "=", "$", "cloned_node", ";", "}", "return", "$", "cloned_nodes", ";", "}" ]
Return array with cloned nodes @return \DOMNode[]
[ "Return", "array", "with", "cloned", "nodes" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQueryNodes.php#L541-L556
Rct567/DomQuery
src/Rct567/DomQuery/DomQueryNodes.php
DomQueryNodes.xpathQuery
public function xpathQuery(string $expression, \DOMNode $context_node=null) { if ($this->dom_xpath) { $node_list = $this->dom_xpath->query($expression, $context_node); if ($node_list instanceof \DOMNodeList) { return $node_list; } elseif ($node_list === false && $context_node) { throw new \Exception('Expression '.$expression.' is malformed or contextnode is invalid.'); } elseif ($node_list === false) { throw new \Exception('Expression '.$expression.' is malformed.'); } } return false; }
php
public function xpathQuery(string $expression, \DOMNode $context_node=null) { if ($this->dom_xpath) { $node_list = $this->dom_xpath->query($expression, $context_node); if ($node_list instanceof \DOMNodeList) { return $node_list; } elseif ($node_list === false && $context_node) { throw new \Exception('Expression '.$expression.' is malformed or contextnode is invalid.'); } elseif ($node_list === false) { throw new \Exception('Expression '.$expression.' is malformed.'); } } return false; }
[ "public", "function", "xpathQuery", "(", "string", "$", "expression", ",", "\\", "DOMNode", "$", "context_node", "=", "null", ")", "{", "if", "(", "$", "this", "->", "dom_xpath", ")", "{", "$", "node_list", "=", "$", "this", "->", "dom_xpath", "->", "query", "(", "$", "expression", ",", "$", "context_node", ")", ";", "if", "(", "$", "node_list", "instanceof", "\\", "DOMNodeList", ")", "{", "return", "$", "node_list", ";", "}", "elseif", "(", "$", "node_list", "===", "false", "&&", "$", "context_node", ")", "{", "throw", "new", "\\", "Exception", "(", "'Expression '", ".", "$", "expression", ".", "' is malformed or contextnode is invalid.'", ")", ";", "}", "elseif", "(", "$", "node_list", "===", "false", ")", "{", "throw", "new", "\\", "Exception", "(", "'Expression '", ".", "$", "expression", ".", "' is malformed.'", ")", ";", "}", "}", "return", "false", ";", "}" ]
Perform query via xpath expression (using DOMXPath::query) @param string $expression @param \DOMNode|null $context_node @return \DOMNodeList|false @throws \Exception
[ "Perform", "query", "via", "xpath", "expression", "(", "using", "DOMXPath", "::", "query", ")" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQueryNodes.php#L651-L666
Rct567/DomQuery
src/Rct567/DomQuery/DomQueryNodes.php
DomQueryNodes.createDomXpath
private function createDomXpath() { $xpath = new \DOMXPath($this->document); if ($this->xml_mode) { // register all name spaces foreach ($xpath->query('namespace::*') as $node) { if ($node->prefix !== 'xml') { $xpath->registerNamespace($node->prefix, $node->namespaceURI); } } } return $xpath; }
php
private function createDomXpath() { $xpath = new \DOMXPath($this->document); if ($this->xml_mode) { // register all name spaces foreach ($xpath->query('namespace::*') as $node) { if ($node->prefix !== 'xml') { $xpath->registerNamespace($node->prefix, $node->namespaceURI); } } } return $xpath; }
[ "private", "function", "createDomXpath", "(", ")", "{", "$", "xpath", "=", "new", "\\", "DOMXPath", "(", "$", "this", "->", "document", ")", ";", "if", "(", "$", "this", "->", "xml_mode", ")", "{", "// register all name spaces", "foreach", "(", "$", "xpath", "->", "query", "(", "'namespace::*'", ")", "as", "$", "node", ")", "{", "if", "(", "$", "node", "->", "prefix", "!==", "'xml'", ")", "{", "$", "xpath", "->", "registerNamespace", "(", "$", "node", "->", "prefix", ",", "$", "node", "->", "namespaceURI", ")", ";", "}", "}", "}", "return", "$", "xpath", ";", "}" ]
Create dom xpath instance @return \DOMXPath
[ "Create", "dom", "xpath", "instance" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQueryNodes.php#L673-L686
Rct567/DomQuery
src/Rct567/DomQuery/DomQueryNodes.php
DomQueryNodes.getOuterHtml
public function getOuterHtml() { $outer_html = ''; if ($this->xml_mode && $this->xml_print_pi) { $outer_html .= '<?xml version="'.$this->document->xmlVersion.'" encoding="'.$this->document->xmlEncoding.'"?>'; $outer_html .= "\n\n"; } foreach ($this->nodes as $node) { if (isset($this->document)) { if ($this->xml_mode) { $outer_html .= $this->document->saveXML($node); } else { $outer_html .= $this->document->saveHTML($node); } } } $outer_html = $this->handleHtmlResult($outer_html); return $outer_html; }
php
public function getOuterHtml() { $outer_html = ''; if ($this->xml_mode && $this->xml_print_pi) { $outer_html .= '<?xml version="'.$this->document->xmlVersion.'" encoding="'.$this->document->xmlEncoding.'"?>'; $outer_html .= "\n\n"; } foreach ($this->nodes as $node) { if (isset($this->document)) { if ($this->xml_mode) { $outer_html .= $this->document->saveXML($node); } else { $outer_html .= $this->document->saveHTML($node); } } } $outer_html = $this->handleHtmlResult($outer_html); return $outer_html; }
[ "public", "function", "getOuterHtml", "(", ")", "{", "$", "outer_html", "=", "''", ";", "if", "(", "$", "this", "->", "xml_mode", "&&", "$", "this", "->", "xml_print_pi", ")", "{", "$", "outer_html", ".=", "'<?xml version=\"'", ".", "$", "this", "->", "document", "->", "xmlVersion", ".", "'\" encoding=\"'", ".", "$", "this", "->", "document", "->", "xmlEncoding", ".", "'\"?>'", ";", "$", "outer_html", ".=", "\"\\n\\n\"", ";", "}", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "if", "(", "isset", "(", "$", "this", "->", "document", ")", ")", "{", "if", "(", "$", "this", "->", "xml_mode", ")", "{", "$", "outer_html", ".=", "$", "this", "->", "document", "->", "saveXML", "(", "$", "node", ")", ";", "}", "else", "{", "$", "outer_html", ".=", "$", "this", "->", "document", "->", "saveHTML", "(", "$", "node", ")", ";", "}", "}", "}", "$", "outer_html", "=", "$", "this", "->", "handleHtmlResult", "(", "$", "outer_html", ")", ";", "return", "$", "outer_html", ";", "}" ]
Return html of all nodes (HTML fragment describing all the elements, including their descendants) @return string
[ "Return", "html", "of", "all", "nodes", "(", "HTML", "fragment", "describing", "all", "the", "elements", "including", "their", "descendants", ")" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQueryNodes.php#L713-L735
Rct567/DomQuery
src/Rct567/DomQuery/DomQueryNodes.php
DomQueryNodes.getElementId
public static function getElementId(\DOMElement $node) { if ($node->hasAttribute('dqn_tmp_id')) { return $node->getAttribute('dqn_tmp_id'); } $node_id = md5(mt_rand()); $node->setAttribute('dqn_tmp_id', $node_id); return $node_id; }
php
public static function getElementId(\DOMElement $node) { if ($node->hasAttribute('dqn_tmp_id')) { return $node->getAttribute('dqn_tmp_id'); } $node_id = md5(mt_rand()); $node->setAttribute('dqn_tmp_id', $node_id); return $node_id; }
[ "public", "static", "function", "getElementId", "(", "\\", "DOMElement", "$", "node", ")", "{", "if", "(", "$", "node", "->", "hasAttribute", "(", "'dqn_tmp_id'", ")", ")", "{", "return", "$", "node", "->", "getAttribute", "(", "'dqn_tmp_id'", ")", ";", "}", "$", "node_id", "=", "md5", "(", "mt_rand", "(", ")", ")", ";", "$", "node", "->", "setAttribute", "(", "'dqn_tmp_id'", ",", "$", "node_id", ")", ";", "return", "$", "node_id", ";", "}" ]
Get id for node @param \DOMElement $node @return string $node_id
[ "Get", "id", "for", "node" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQueryNodes.php#L744-L753
Rct567/DomQuery
src/Rct567/DomQuery/DomQueryNodes.php
DomQueryNodes.handleHtmlResult
private function handleHtmlResult($html) { if ($this->preserve_no_newlines) { $html = str_replace("\n", '', $html); } if (stripos($html, 'dqn_tmp_id=') !== false) { $html = preg_replace('/ dqn_tmp_id="([a-z0-9]+)"/', '', $html); } return $html; }
php
private function handleHtmlResult($html) { if ($this->preserve_no_newlines) { $html = str_replace("\n", '', $html); } if (stripos($html, 'dqn_tmp_id=') !== false) { $html = preg_replace('/ dqn_tmp_id="([a-z0-9]+)"/', '', $html); } return $html; }
[ "private", "function", "handleHtmlResult", "(", "$", "html", ")", "{", "if", "(", "$", "this", "->", "preserve_no_newlines", ")", "{", "$", "html", "=", "str_replace", "(", "\"\\n\"", ",", "''", ",", "$", "html", ")", ";", "}", "if", "(", "stripos", "(", "$", "html", ",", "'dqn_tmp_id='", ")", "!==", "false", ")", "{", "$", "html", "=", "preg_replace", "(", "'/ dqn_tmp_id=\"([a-z0-9]+)\"/'", ",", "''", ",", "$", "html", ")", ";", "}", "return", "$", "html", ";", "}" ]
Handle html when resulting html is requested @param string $html @return string
[ "Handle", "html", "when", "resulting", "html", "is", "requested" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQueryNodes.php#L762-L772
Rct567/DomQuery
src/Rct567/DomQuery/DomQueryNodes.php
DomQueryNodes.getInnerHtml
public function getInnerHtml() { $html = ''; if ($content_node = $this->getFirstElmNode()) { $document = $content_node->ownerDocument; foreach ($content_node->childNodes as $node) { if ($this->xml_mode) { $html .= $document->saveXML($node); } else { $html .= $document->saveHTML($node); } } $html = $this->handleHtmlResult($html); } return $html; }
php
public function getInnerHtml() { $html = ''; if ($content_node = $this->getFirstElmNode()) { $document = $content_node->ownerDocument; foreach ($content_node->childNodes as $node) { if ($this->xml_mode) { $html .= $document->saveXML($node); } else { $html .= $document->saveHTML($node); } } $html = $this->handleHtmlResult($html); } return $html; }
[ "public", "function", "getInnerHtml", "(", ")", "{", "$", "html", "=", "''", ";", "if", "(", "$", "content_node", "=", "$", "this", "->", "getFirstElmNode", "(", ")", ")", "{", "$", "document", "=", "$", "content_node", "->", "ownerDocument", ";", "foreach", "(", "$", "content_node", "->", "childNodes", "as", "$", "node", ")", "{", "if", "(", "$", "this", "->", "xml_mode", ")", "{", "$", "html", ".=", "$", "document", "->", "saveXML", "(", "$", "node", ")", ";", "}", "else", "{", "$", "html", ".=", "$", "document", "->", "saveHTML", "(", "$", "node", ")", ";", "}", "}", "$", "html", "=", "$", "this", "->", "handleHtmlResult", "(", "$", "html", ")", ";", "}", "return", "$", "html", ";", "}" ]
Get the HTML contents of the first element in the set of matched elements. @return string
[ "Get", "the", "HTML", "contents", "of", "the", "first", "element", "in", "the", "set", "of", "matched", "elements", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQueryNodes.php#L779-L797
Rct567/DomQuery
src/Rct567/DomQuery/DomQueryNodes.php
DomQueryNodes.getIterator
public function getIterator() { $iteration_result = array(); if (\is_array($this->nodes)) { foreach ($this->nodes as $node) { $iteration_result[] = $this->createChildInstance($node); } } return new \ArrayIterator($iteration_result); }
php
public function getIterator() { $iteration_result = array(); if (\is_array($this->nodes)) { foreach ($this->nodes as $node) { $iteration_result[] = $this->createChildInstance($node); } } return new \ArrayIterator($iteration_result); }
[ "public", "function", "getIterator", "(", ")", "{", "$", "iteration_result", "=", "array", "(", ")", ";", "if", "(", "\\", "is_array", "(", "$", "this", "->", "nodes", ")", ")", "{", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "$", "iteration_result", "[", "]", "=", "$", "this", "->", "createChildInstance", "(", "$", "node", ")", ";", "}", "}", "return", "new", "\\", "ArrayIterator", "(", "$", "iteration_result", ")", ";", "}" ]
IteratorAggregate (note: using Iterator conflicts with next method in jquery) @return \ArrayIterator containing nodes as instances of DomQuery
[ "IteratorAggregate", "(", "note", ":", "using", "Iterator", "conflicts", "with", "next", "method", "in", "jquery", ")" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQueryNodes.php#L814-L824
Rct567/DomQuery
src/Rct567/DomQuery/DomQueryNodes.php
DomQueryNodes.offsetGet
public function offsetGet($key) { if (!\is_int($key)) { throw new \BadMethodCallException('Attempting to access node list with non-integer'); } if (isset($this->nodes[$key])) { return $this->createChildInstance($this->nodes[$key]); } return $this->createChildInstance(); }
php
public function offsetGet($key) { if (!\is_int($key)) { throw new \BadMethodCallException('Attempting to access node list with non-integer'); } if (isset($this->nodes[$key])) { return $this->createChildInstance($this->nodes[$key]); } return $this->createChildInstance(); }
[ "public", "function", "offsetGet", "(", "$", "key", ")", "{", "if", "(", "!", "\\", "is_int", "(", "$", "key", ")", ")", "{", "throw", "new", "\\", "BadMethodCallException", "(", "'Attempting to access node list with non-integer'", ")", ";", "}", "if", "(", "isset", "(", "$", "this", "->", "nodes", "[", "$", "key", "]", ")", ")", "{", "return", "$", "this", "->", "createChildInstance", "(", "$", "this", "->", "nodes", "[", "$", "key", "]", ")", ";", "}", "return", "$", "this", "->", "createChildInstance", "(", ")", ";", "}" ]
ArrayAccess: get offset @param mixed $key @return static
[ "ArrayAccess", ":", "get", "offset" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQueryNodes.php#L845-L856
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.text
public function text($val=null) { if ($val !== null) { // set node value for all nodes foreach ($this->nodes as $node) { $node->nodeValue = $val; } return $this; } if ($node = $this->getFirstElmNode()) { // get value for first node return $node->nodeValue; } }
php
public function text($val=null) { if ($val !== null) { // set node value for all nodes foreach ($this->nodes as $node) { $node->nodeValue = $val; } return $this; } if ($node = $this->getFirstElmNode()) { // get value for first node return $node->nodeValue; } }
[ "public", "function", "text", "(", "$", "val", "=", "null", ")", "{", "if", "(", "$", "val", "!==", "null", ")", "{", "// set node value for all nodes", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "$", "node", "->", "nodeValue", "=", "$", "val", ";", "}", "return", "$", "this", ";", "}", "if", "(", "$", "node", "=", "$", "this", "->", "getFirstElmNode", "(", ")", ")", "{", "// get value for first node", "return", "$", "node", "->", "nodeValue", ";", "}", "}" ]
Get the combined text contents of each element in the set of matched elements, including their descendants, or set the text contents of the matched elements. @param string|null $val @return $this|string|null
[ "Get", "the", "combined", "text", "contents", "of", "each", "element", "in", "the", "set", "of", "matched", "elements", "including", "their", "descendants", "or", "set", "the", "text", "contents", "of", "the", "matched", "elements", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L27-L39
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.html
public function html($html_string=null) { if ($html_string !== null) { // set html for all nodes foreach ($this as $node) { $node->get(0)->nodeValue = ''; $node->append($html_string); } return $this; } // get html for first node return $this->getInnerHtml(); }
php
public function html($html_string=null) { if ($html_string !== null) { // set html for all nodes foreach ($this as $node) { $node->get(0)->nodeValue = ''; $node->append($html_string); } return $this; } // get html for first node return $this->getInnerHtml(); }
[ "public", "function", "html", "(", "$", "html_string", "=", "null", ")", "{", "if", "(", "$", "html_string", "!==", "null", ")", "{", "// set html for all nodes", "foreach", "(", "$", "this", "as", "$", "node", ")", "{", "$", "node", "->", "get", "(", "0", ")", "->", "nodeValue", "=", "''", ";", "$", "node", "->", "append", "(", "$", "html_string", ")", ";", "}", "return", "$", "this", ";", "}", "// get html for first node", "return", "$", "this", "->", "getInnerHtml", "(", ")", ";", "}" ]
Get the HTML contents of the first element in the set of matched elements @param string|null $html_string @return $this|string
[ "Get", "the", "HTML", "contents", "of", "the", "first", "element", "in", "the", "set", "of", "matched", "elements" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L48-L60
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.attr
public function attr(string $name, $val=null) { if ($val !== null) { // set attribute for all nodes foreach ($this->getElements() as $node) { $node->setAttribute($name, $val); } return $this; } if ($node = $this->getFirstElmNode()) { // get attribute value for first element return $node->getAttribute($name); } }
php
public function attr(string $name, $val=null) { if ($val !== null) { // set attribute for all nodes foreach ($this->getElements() as $node) { $node->setAttribute($name, $val); } return $this; } if ($node = $this->getFirstElmNode()) { // get attribute value for first element return $node->getAttribute($name); } }
[ "public", "function", "attr", "(", "string", "$", "name", ",", "$", "val", "=", "null", ")", "{", "if", "(", "$", "val", "!==", "null", ")", "{", "// set attribute for all nodes", "foreach", "(", "$", "this", "->", "getElements", "(", ")", "as", "$", "node", ")", "{", "$", "node", "->", "setAttribute", "(", "$", "name", ",", "$", "val", ")", ";", "}", "return", "$", "this", ";", "}", "if", "(", "$", "node", "=", "$", "this", "->", "getFirstElmNode", "(", ")", ")", "{", "// get attribute value for first element", "return", "$", "node", "->", "getAttribute", "(", "$", "name", ")", ";", "}", "}" ]
Get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element. @param string $name @param string $val @return $this|string|null
[ "Get", "the", "value", "of", "an", "attribute", "for", "the", "first", "element", "in", "the", "set", "of", "matched", "elements", "or", "set", "one", "or", "more", "attributes", "for", "every", "matched", "element", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L71-L82
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.data
public function data(string $key=null, $val=null) { $doc_hash = spl_object_hash($this->document); if ($val !== null) { // set data for all nodes if (!isset(self::$node_data[$doc_hash])) { self::$node_data[$doc_hash] = array(); } foreach ($this->getElements() as $node) { if (!isset(self::$node_data[$doc_hash][self::getElementId($node)])) { self::$node_data[$doc_hash][self::getElementId($node)] = (object) array(); } self::$node_data[$doc_hash][self::getElementId($node)]->$key = $val; } return $this; } if ($node = $this->getFirstElmNode()) { // get data for first element if (isset(self::$node_data[$doc_hash]) && isset(self::$node_data[$doc_hash][self::getElementId($node)])) { if ($key === null) { return self::$node_data[$doc_hash][self::getElementId($node)]; } elseif (isset(self::$node_data[$doc_hash][self::getElementId($node)]->$key)) { return self::$node_data[$doc_hash][self::getElementId($node)]->$key; } } if ($key === null) { // object with all data $data = array(); foreach ($node->attributes as $attr) { if (strpos($attr->nodeName, 'data-') === 0) { $val = $attr->nodeValue[0] === '{' ? json_decode($attr->nodeValue) : $attr->nodeValue; $data[substr($attr->nodeName, 5)] = $val; } } return (object) $data; } if ($data = $node->getAttribute('data-'.$key)) { $val = $data[0] === '{' ? json_decode($data) : $data; return $val; } } }
php
public function data(string $key=null, $val=null) { $doc_hash = spl_object_hash($this->document); if ($val !== null) { // set data for all nodes if (!isset(self::$node_data[$doc_hash])) { self::$node_data[$doc_hash] = array(); } foreach ($this->getElements() as $node) { if (!isset(self::$node_data[$doc_hash][self::getElementId($node)])) { self::$node_data[$doc_hash][self::getElementId($node)] = (object) array(); } self::$node_data[$doc_hash][self::getElementId($node)]->$key = $val; } return $this; } if ($node = $this->getFirstElmNode()) { // get data for first element if (isset(self::$node_data[$doc_hash]) && isset(self::$node_data[$doc_hash][self::getElementId($node)])) { if ($key === null) { return self::$node_data[$doc_hash][self::getElementId($node)]; } elseif (isset(self::$node_data[$doc_hash][self::getElementId($node)]->$key)) { return self::$node_data[$doc_hash][self::getElementId($node)]->$key; } } if ($key === null) { // object with all data $data = array(); foreach ($node->attributes as $attr) { if (strpos($attr->nodeName, 'data-') === 0) { $val = $attr->nodeValue[0] === '{' ? json_decode($attr->nodeValue) : $attr->nodeValue; $data[substr($attr->nodeName, 5)] = $val; } } return (object) $data; } if ($data = $node->getAttribute('data-'.$key)) { $val = $data[0] === '{' ? json_decode($data) : $data; return $val; } } }
[ "public", "function", "data", "(", "string", "$", "key", "=", "null", ",", "$", "val", "=", "null", ")", "{", "$", "doc_hash", "=", "spl_object_hash", "(", "$", "this", "->", "document", ")", ";", "if", "(", "$", "val", "!==", "null", ")", "{", "// set data for all nodes", "if", "(", "!", "isset", "(", "self", "::", "$", "node_data", "[", "$", "doc_hash", "]", ")", ")", "{", "self", "::", "$", "node_data", "[", "$", "doc_hash", "]", "=", "array", "(", ")", ";", "}", "foreach", "(", "$", "this", "->", "getElements", "(", ")", "as", "$", "node", ")", "{", "if", "(", "!", "isset", "(", "self", "::", "$", "node_data", "[", "$", "doc_hash", "]", "[", "self", "::", "getElementId", "(", "$", "node", ")", "]", ")", ")", "{", "self", "::", "$", "node_data", "[", "$", "doc_hash", "]", "[", "self", "::", "getElementId", "(", "$", "node", ")", "]", "=", "(", "object", ")", "array", "(", ")", ";", "}", "self", "::", "$", "node_data", "[", "$", "doc_hash", "]", "[", "self", "::", "getElementId", "(", "$", "node", ")", "]", "->", "$", "key", "=", "$", "val", ";", "}", "return", "$", "this", ";", "}", "if", "(", "$", "node", "=", "$", "this", "->", "getFirstElmNode", "(", ")", ")", "{", "// get data for first element", "if", "(", "isset", "(", "self", "::", "$", "node_data", "[", "$", "doc_hash", "]", ")", "&&", "isset", "(", "self", "::", "$", "node_data", "[", "$", "doc_hash", "]", "[", "self", "::", "getElementId", "(", "$", "node", ")", "]", ")", ")", "{", "if", "(", "$", "key", "===", "null", ")", "{", "return", "self", "::", "$", "node_data", "[", "$", "doc_hash", "]", "[", "self", "::", "getElementId", "(", "$", "node", ")", "]", ";", "}", "elseif", "(", "isset", "(", "self", "::", "$", "node_data", "[", "$", "doc_hash", "]", "[", "self", "::", "getElementId", "(", "$", "node", ")", "]", "->", "$", "key", ")", ")", "{", "return", "self", "::", "$", "node_data", "[", "$", "doc_hash", "]", "[", "self", "::", "getElementId", "(", "$", "node", ")", "]", "->", "$", "key", ";", "}", "}", "if", "(", "$", "key", "===", "null", ")", "{", "// object with all data", "$", "data", "=", "array", "(", ")", ";", "foreach", "(", "$", "node", "->", "attributes", "as", "$", "attr", ")", "{", "if", "(", "strpos", "(", "$", "attr", "->", "nodeName", ",", "'data-'", ")", "===", "0", ")", "{", "$", "val", "=", "$", "attr", "->", "nodeValue", "[", "0", "]", "===", "'{'", "?", "json_decode", "(", "$", "attr", "->", "nodeValue", ")", ":", "$", "attr", "->", "nodeValue", ";", "$", "data", "[", "substr", "(", "$", "attr", "->", "nodeName", ",", "5", ")", "]", "=", "$", "val", ";", "}", "}", "return", "(", "object", ")", "$", "data", ";", "}", "if", "(", "$", "data", "=", "$", "node", "->", "getAttribute", "(", "'data-'", ".", "$", "key", ")", ")", "{", "$", "val", "=", "$", "data", "[", "0", "]", "===", "'{'", "?", "json_decode", "(", "$", "data", ")", ":", "$", "data", ";", "return", "$", "val", ";", "}", "}", "}" ]
Store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements. @param string $key @param $val @return $this|string|object
[ "Store", "arbitrary", "data", "associated", "with", "the", "matched", "elements", "or", "return", "the", "value", "at", "the", "named", "data", "store", "for", "the", "first", "element", "in", "the", "set", "of", "matched", "elements", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L93-L133
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.removeData
public function removeData($name=null) { $remove_names = \is_array($name) ? $name : explode(' ', $name); $doc_hash = spl_object_hash($this->document); if (!isset(self::$node_data[$doc_hash])) { return; } foreach ($this->getElements() as $node) { if (!$node->hasAttribute('dqn_tmp_id')) { continue; } $node_id = self::getElementId($node); if (isset(self::$node_data[$doc_hash][$node_id])) { if ($name === null) { self::$node_data[$doc_hash][$node_id] = null; } else { foreach ($remove_names as $remove_name) { if (isset(self::$node_data[$doc_hash][$node_id]->$remove_name)) { self::$node_data[$doc_hash][$node_id]->$remove_name = null; } } } } } }
php
public function removeData($name=null) { $remove_names = \is_array($name) ? $name : explode(' ', $name); $doc_hash = spl_object_hash($this->document); if (!isset(self::$node_data[$doc_hash])) { return; } foreach ($this->getElements() as $node) { if (!$node->hasAttribute('dqn_tmp_id')) { continue; } $node_id = self::getElementId($node); if (isset(self::$node_data[$doc_hash][$node_id])) { if ($name === null) { self::$node_data[$doc_hash][$node_id] = null; } else { foreach ($remove_names as $remove_name) { if (isset(self::$node_data[$doc_hash][$node_id]->$remove_name)) { self::$node_data[$doc_hash][$node_id]->$remove_name = null; } } } } } }
[ "public", "function", "removeData", "(", "$", "name", "=", "null", ")", "{", "$", "remove_names", "=", "\\", "is_array", "(", "$", "name", ")", "?", "$", "name", ":", "explode", "(", "' '", ",", "$", "name", ")", ";", "$", "doc_hash", "=", "spl_object_hash", "(", "$", "this", "->", "document", ")", ";", "if", "(", "!", "isset", "(", "self", "::", "$", "node_data", "[", "$", "doc_hash", "]", ")", ")", "{", "return", ";", "}", "foreach", "(", "$", "this", "->", "getElements", "(", ")", "as", "$", "node", ")", "{", "if", "(", "!", "$", "node", "->", "hasAttribute", "(", "'dqn_tmp_id'", ")", ")", "{", "continue", ";", "}", "$", "node_id", "=", "self", "::", "getElementId", "(", "$", "node", ")", ";", "if", "(", "isset", "(", "self", "::", "$", "node_data", "[", "$", "doc_hash", "]", "[", "$", "node_id", "]", ")", ")", "{", "if", "(", "$", "name", "===", "null", ")", "{", "self", "::", "$", "node_data", "[", "$", "doc_hash", "]", "[", "$", "node_id", "]", "=", "null", ";", "}", "else", "{", "foreach", "(", "$", "remove_names", "as", "$", "remove_name", ")", "{", "if", "(", "isset", "(", "self", "::", "$", "node_data", "[", "$", "doc_hash", "]", "[", "$", "node_id", "]", "->", "$", "remove_name", ")", ")", "{", "self", "::", "$", "node_data", "[", "$", "doc_hash", "]", "[", "$", "node_id", "]", "->", "$", "remove_name", "=", "null", ";", "}", "}", "}", "}", "}", "}" ]
Remove a previously-stored piece of data. @param string|string[] $name @return void
[ "Remove", "a", "previously", "-", "stored", "piece", "of", "data", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L142-L170
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.css
public function css(string $name, $val=null) { if ($val !== null) { // set css for all nodes foreach ($this->getElements() as $node) { $style = self::parseStyle($node->getAttribute('style')); $style[$name] = $val; $node->setAttribute('style', self::getStyle($style)); } return $this; } if ($node = $this->getFirstElmNode()) { // get css value for first element $style = self::parseStyle($node->getAttribute('style')); if (isset($style[$name])) { return $style[$name]; } } }
php
public function css(string $name, $val=null) { if ($val !== null) { // set css for all nodes foreach ($this->getElements() as $node) { $style = self::parseStyle($node->getAttribute('style')); $style[$name] = $val; $node->setAttribute('style', self::getStyle($style)); } return $this; } if ($node = $this->getFirstElmNode()) { // get css value for first element $style = self::parseStyle($node->getAttribute('style')); if (isset($style[$name])) { return $style[$name]; } } }
[ "public", "function", "css", "(", "string", "$", "name", ",", "$", "val", "=", "null", ")", "{", "if", "(", "$", "val", "!==", "null", ")", "{", "// set css for all nodes", "foreach", "(", "$", "this", "->", "getElements", "(", ")", "as", "$", "node", ")", "{", "$", "style", "=", "self", "::", "parseStyle", "(", "$", "node", "->", "getAttribute", "(", "'style'", ")", ")", ";", "$", "style", "[", "$", "name", "]", "=", "$", "val", ";", "$", "node", "->", "setAttribute", "(", "'style'", ",", "self", "::", "getStyle", "(", "$", "style", ")", ")", ";", "}", "return", "$", "this", ";", "}", "if", "(", "$", "node", "=", "$", "this", "->", "getFirstElmNode", "(", ")", ")", "{", "// get css value for first element", "$", "style", "=", "self", "::", "parseStyle", "(", "$", "node", "->", "getAttribute", "(", "'style'", ")", ")", ";", "if", "(", "isset", "(", "$", "style", "[", "$", "name", "]", ")", ")", "{", "return", "$", "style", "[", "$", "name", "]", ";", "}", "}", "}" ]
Get the value of a computed style property for the first element in the set of matched elements or set one or more CSS properties for every matched element. @param string $name @param string $val @return $this|string
[ "Get", "the", "value", "of", "a", "computed", "style", "property", "for", "the", "first", "element", "in", "the", "set", "of", "matched", "elements", "or", "set", "one", "or", "more", "CSS", "properties", "for", "every", "matched", "element", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L220-L236
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.removeAttr
public function removeAttr($name) { $remove_names = \is_array($name) ? $name : explode(' ', $name); foreach ($this->getElements() as $node) { foreach ($remove_names as $remove_name) { $node->removeAttribute($remove_name); } } return $this; }
php
public function removeAttr($name) { $remove_names = \is_array($name) ? $name : explode(' ', $name); foreach ($this->getElements() as $node) { foreach ($remove_names as $remove_name) { $node->removeAttribute($remove_name); } } return $this; }
[ "public", "function", "removeAttr", "(", "$", "name", ")", "{", "$", "remove_names", "=", "\\", "is_array", "(", "$", "name", ")", "?", "$", "name", ":", "explode", "(", "' '", ",", "$", "name", ")", ";", "foreach", "(", "$", "this", "->", "getElements", "(", ")", "as", "$", "node", ")", "{", "foreach", "(", "$", "remove_names", "as", "$", "remove_name", ")", "{", "$", "node", "->", "removeAttribute", "(", "$", "remove_name", ")", ";", "}", "}", "return", "$", "this", ";", "}" ]
Remove an attribute from each element in the set of matched elements. Name can be a space-separated list of attributes. @param string|string[] $name @return $this
[ "Remove", "an", "attribute", "from", "each", "element", "in", "the", "set", "of", "matched", "elements", ".", "Name", "can", "be", "a", "space", "-", "separated", "list", "of", "attributes", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L246-L257
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.addClass
public function addClass($class_name) { $add_names = \is_array($class_name) ? $class_name : explode(' ', $class_name); foreach ($this->getElements() as $node) { $node_classes = array(); if ($node_class_attr = $node->getAttribute('class')) { $node_classes = explode(' ', $node_class_attr); } foreach ($add_names as $add_name) { if (!\in_array($add_name, $node_classes, true)) { $node_classes[] = $add_name; } } if (\count($node_classes) > 0) { $node->setAttribute('class', implode(' ', $node_classes)); } } return $this; }
php
public function addClass($class_name) { $add_names = \is_array($class_name) ? $class_name : explode(' ', $class_name); foreach ($this->getElements() as $node) { $node_classes = array(); if ($node_class_attr = $node->getAttribute('class')) { $node_classes = explode(' ', $node_class_attr); } foreach ($add_names as $add_name) { if (!\in_array($add_name, $node_classes, true)) { $node_classes[] = $add_name; } } if (\count($node_classes) > 0) { $node->setAttribute('class', implode(' ', $node_classes)); } } return $this; }
[ "public", "function", "addClass", "(", "$", "class_name", ")", "{", "$", "add_names", "=", "\\", "is_array", "(", "$", "class_name", ")", "?", "$", "class_name", ":", "explode", "(", "' '", ",", "$", "class_name", ")", ";", "foreach", "(", "$", "this", "->", "getElements", "(", ")", "as", "$", "node", ")", "{", "$", "node_classes", "=", "array", "(", ")", ";", "if", "(", "$", "node_class_attr", "=", "$", "node", "->", "getAttribute", "(", "'class'", ")", ")", "{", "$", "node_classes", "=", "explode", "(", "' '", ",", "$", "node_class_attr", ")", ";", "}", "foreach", "(", "$", "add_names", "as", "$", "add_name", ")", "{", "if", "(", "!", "\\", "in_array", "(", "$", "add_name", ",", "$", "node_classes", ",", "true", ")", ")", "{", "$", "node_classes", "[", "]", "=", "$", "add_name", ";", "}", "}", "if", "(", "\\", "count", "(", "$", "node_classes", ")", ">", "0", ")", "{", "$", "node", "->", "setAttribute", "(", "'class'", ",", "implode", "(", "' '", ",", "$", "node_classes", ")", ")", ";", "}", "}", "return", "$", "this", ";", "}" ]
Adds the specified class(es) to each element in the set of matched elements. @param string|string[] $class_name class name(s) @return $this
[ "Adds", "the", "specified", "class", "(", "es", ")", "to", "each", "element", "in", "the", "set", "of", "matched", "elements", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L266-L286
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.hasClass
public function hasClass($class_name) { foreach ($this->nodes as $node) { if ($node instanceof \DOMElement && $node_class_attr = $node->getAttribute('class')) { $node_classes = explode(' ', $node_class_attr); if (\in_array($class_name, $node_classes, true)) { return true; } } } return false; }
php
public function hasClass($class_name) { foreach ($this->nodes as $node) { if ($node instanceof \DOMElement && $node_class_attr = $node->getAttribute('class')) { $node_classes = explode(' ', $node_class_attr); if (\in_array($class_name, $node_classes, true)) { return true; } } } return false; }
[ "public", "function", "hasClass", "(", "$", "class_name", ")", "{", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "if", "(", "$", "node", "instanceof", "\\", "DOMElement", "&&", "$", "node_class_attr", "=", "$", "node", "->", "getAttribute", "(", "'class'", ")", ")", "{", "$", "node_classes", "=", "explode", "(", "' '", ",", "$", "node_class_attr", ")", ";", "if", "(", "\\", "in_array", "(", "$", "class_name", ",", "$", "node_classes", ",", "true", ")", ")", "{", "return", "true", ";", "}", "}", "}", "return", "false", ";", "}" ]
Determine whether any of the matched elements are assigned the given class. @param string $class_name @return boolean
[ "Determine", "whether", "any", "of", "the", "matched", "elements", "are", "assigned", "the", "given", "class", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L295-L307
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.removeClass
public function removeClass($class_name='') { $remove_names = \is_array($class_name) ? $class_name : explode(' ', $class_name); foreach ($this->nodes as $node) { if ($node instanceof \DOMElement && $node->hasAttribute('class')) { $node_classes = preg_split('#\s+#s', $node->getAttribute('class')); $class_removed = false; if ($class_name === '') { // remove all $node_classes = array(); $class_removed = true; } else { foreach ($remove_names as $remove_name) { $key = array_search($remove_name, $node_classes, true); if ($key !== false) { unset($node_classes[$key]); $class_removed = true; } } } if ($class_removed) { $node->setAttribute('class', implode(' ', $node_classes)); } } } return $this; }
php
public function removeClass($class_name='') { $remove_names = \is_array($class_name) ? $class_name : explode(' ', $class_name); foreach ($this->nodes as $node) { if ($node instanceof \DOMElement && $node->hasAttribute('class')) { $node_classes = preg_split('#\s+#s', $node->getAttribute('class')); $class_removed = false; if ($class_name === '') { // remove all $node_classes = array(); $class_removed = true; } else { foreach ($remove_names as $remove_name) { $key = array_search($remove_name, $node_classes, true); if ($key !== false) { unset($node_classes[$key]); $class_removed = true; } } } if ($class_removed) { $node->setAttribute('class', implode(' ', $node_classes)); } } } return $this; }
[ "public", "function", "removeClass", "(", "$", "class_name", "=", "''", ")", "{", "$", "remove_names", "=", "\\", "is_array", "(", "$", "class_name", ")", "?", "$", "class_name", ":", "explode", "(", "' '", ",", "$", "class_name", ")", ";", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "if", "(", "$", "node", "instanceof", "\\", "DOMElement", "&&", "$", "node", "->", "hasAttribute", "(", "'class'", ")", ")", "{", "$", "node_classes", "=", "preg_split", "(", "'#\\s+#s'", ",", "$", "node", "->", "getAttribute", "(", "'class'", ")", ")", ";", "$", "class_removed", "=", "false", ";", "if", "(", "$", "class_name", "===", "''", ")", "{", "// remove all", "$", "node_classes", "=", "array", "(", ")", ";", "$", "class_removed", "=", "true", ";", "}", "else", "{", "foreach", "(", "$", "remove_names", "as", "$", "remove_name", ")", "{", "$", "key", "=", "array_search", "(", "$", "remove_name", ",", "$", "node_classes", ",", "true", ")", ";", "if", "(", "$", "key", "!==", "false", ")", "{", "unset", "(", "$", "node_classes", "[", "$", "key", "]", ")", ";", "$", "class_removed", "=", "true", ";", "}", "}", "}", "if", "(", "$", "class_removed", ")", "{", "$", "node", "->", "setAttribute", "(", "'class'", ",", "implode", "(", "' '", ",", "$", "node_classes", ")", ")", ";", "}", "}", "}", "return", "$", "this", ";", "}" ]
Remove a single class, multiple classes, or all classes from each element in the set of matched elements. @param string|string[] $class_name @return $this
[ "Remove", "a", "single", "class", "multiple", "classes", "or", "all", "classes", "from", "each", "element", "in", "the", "set", "of", "matched", "elements", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L316-L344
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.toggleClass
public function toggleClass($class_name='') { $toggle_names = \is_array($class_name) ? $class_name : explode(' ', $class_name); foreach ($this as $node) { foreach ($toggle_names as $toggle_class) { if (!$node->hasClass($toggle_class)) { $node->addClass($toggle_class); } else { $node->removeClass($toggle_class); } } } return $this; }
php
public function toggleClass($class_name='') { $toggle_names = \is_array($class_name) ? $class_name : explode(' ', $class_name); foreach ($this as $node) { foreach ($toggle_names as $toggle_class) { if (!$node->hasClass($toggle_class)) { $node->addClass($toggle_class); } else { $node->removeClass($toggle_class); } } } return $this; }
[ "public", "function", "toggleClass", "(", "$", "class_name", "=", "''", ")", "{", "$", "toggle_names", "=", "\\", "is_array", "(", "$", "class_name", ")", "?", "$", "class_name", ":", "explode", "(", "' '", ",", "$", "class_name", ")", ";", "foreach", "(", "$", "this", "as", "$", "node", ")", "{", "foreach", "(", "$", "toggle_names", "as", "$", "toggle_class", ")", "{", "if", "(", "!", "$", "node", "->", "hasClass", "(", "$", "toggle_class", ")", ")", "{", "$", "node", "->", "addClass", "(", "$", "toggle_class", ")", ";", "}", "else", "{", "$", "node", "->", "removeClass", "(", "$", "toggle_class", ")", ";", "}", "}", "}", "return", "$", "this", ";", "}" ]
Remove a single class, multiple classes, or all classes from each element in the set of matched elements. @param string|string[] $class_name @return $this
[ "Remove", "a", "single", "class", "multiple", "classes", "or", "all", "classes", "from", "each", "element", "in", "the", "set", "of", "matched", "elements", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L353-L368
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.prop
public function prop(string $name, $val=null) { if ($val !== null) { // set attribute for all nodes foreach ($this->nodes as $node) { $node->$name = $val; } return $this; } // get property value for first element if ($name === 'outerHTML') { return $this->getOuterHtml(); } if ($node = $this->getFirstElmNode()) { if (isset($node->$name)) { return $node->$name; } } }
php
public function prop(string $name, $val=null) { if ($val !== null) { // set attribute for all nodes foreach ($this->nodes as $node) { $node->$name = $val; } return $this; } // get property value for first element if ($name === 'outerHTML') { return $this->getOuterHtml(); } if ($node = $this->getFirstElmNode()) { if (isset($node->$name)) { return $node->$name; } } }
[ "public", "function", "prop", "(", "string", "$", "name", ",", "$", "val", "=", "null", ")", "{", "if", "(", "$", "val", "!==", "null", ")", "{", "// set attribute for all nodes", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "$", "node", "->", "$", "name", "=", "$", "val", ";", "}", "return", "$", "this", ";", "}", "// get property value for first element", "if", "(", "$", "name", "===", "'outerHTML'", ")", "{", "return", "$", "this", "->", "getOuterHtml", "(", ")", ";", "}", "if", "(", "$", "node", "=", "$", "this", "->", "getFirstElmNode", "(", ")", ")", "{", "if", "(", "isset", "(", "$", "node", "->", "$", "name", ")", ")", "{", "return", "$", "node", "->", "$", "name", ";", "}", "}", "}" ]
Get the value of a property for the first element in the set of matched elements or set one or more properties for every matched element. @param string $name @param string $val @return $this|mixed|null
[ "Get", "the", "value", "of", "a", "property", "for", "the", "first", "element", "in", "the", "set", "of", "matched", "elements", "or", "set", "one", "or", "more", "properties", "for", "every", "matched", "element", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L379-L397
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.children
public function children($selector=null) { $result = $this->createChildInstance(); if (isset($this->document) && $this->length > 0) { if (isset($this->root_instance) || $this->getXpathQuery()) { foreach ($this->nodes as $node) { if ($node->hasChildNodes()) { $result->loadDomNodeList($node->childNodes); } } } else { $result->loadDomNodeList($this->document->childNodes); } if ($selector !== false) { // filter out text nodes $filtered_elements = array(); foreach ($result->getElements() as $result_elm) { $filtered_elements[] = $result_elm; } $result->nodes = $filtered_elements; $result->length = \count($result->nodes); } if ($selector) { $result = $result->filter($selector); } } return $result; }
php
public function children($selector=null) { $result = $this->createChildInstance(); if (isset($this->document) && $this->length > 0) { if (isset($this->root_instance) || $this->getXpathQuery()) { foreach ($this->nodes as $node) { if ($node->hasChildNodes()) { $result->loadDomNodeList($node->childNodes); } } } else { $result->loadDomNodeList($this->document->childNodes); } if ($selector !== false) { // filter out text nodes $filtered_elements = array(); foreach ($result->getElements() as $result_elm) { $filtered_elements[] = $result_elm; } $result->nodes = $filtered_elements; $result->length = \count($result->nodes); } if ($selector) { $result = $result->filter($selector); } } return $result; }
[ "public", "function", "children", "(", "$", "selector", "=", "null", ")", "{", "$", "result", "=", "$", "this", "->", "createChildInstance", "(", ")", ";", "if", "(", "isset", "(", "$", "this", "->", "document", ")", "&&", "$", "this", "->", "length", ">", "0", ")", "{", "if", "(", "isset", "(", "$", "this", "->", "root_instance", ")", "||", "$", "this", "->", "getXpathQuery", "(", ")", ")", "{", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "if", "(", "$", "node", "->", "hasChildNodes", "(", ")", ")", "{", "$", "result", "->", "loadDomNodeList", "(", "$", "node", "->", "childNodes", ")", ";", "}", "}", "}", "else", "{", "$", "result", "->", "loadDomNodeList", "(", "$", "this", "->", "document", "->", "childNodes", ")", ";", "}", "if", "(", "$", "selector", "!==", "false", ")", "{", "// filter out text nodes", "$", "filtered_elements", "=", "array", "(", ")", ";", "foreach", "(", "$", "result", "->", "getElements", "(", ")", "as", "$", "result_elm", ")", "{", "$", "filtered_elements", "[", "]", "=", "$", "result_elm", ";", "}", "$", "result", "->", "nodes", "=", "$", "filtered_elements", ";", "$", "result", "->", "length", "=", "\\", "count", "(", "$", "result", "->", "nodes", ")", ";", "}", "if", "(", "$", "selector", ")", "{", "$", "result", "=", "$", "result", "->", "filter", "(", "$", "selector", ")", ";", "}", "}", "return", "$", "result", ";", "}" ]
Get the children of each element in the set of matched elements, optionally filtered by a selector. @param string|self|callable|\DOMNodeList|\DOMNode|false|null $selector expression that filters the set of matched elements @return self
[ "Get", "the", "children", "of", "each", "element", "in", "the", "set", "of", "matched", "elements", "optionally", "filtered", "by", "a", "selector", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L416-L446
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.siblings
public function siblings($selector=null) { $result = $this->createChildInstance(); if (isset($this->document) && $this->length > 0) { foreach ($this->nodes as $node) { if ($node->parentNode) { foreach ($node->parentNode->childNodes as $sibling) { if ($sibling instanceof \DOMElement && !$sibling->isSameNode($node)) { $result->addDomNode($sibling); } } } } if ($selector) { $result = $result->filter($selector); } } return $result; }
php
public function siblings($selector=null) { $result = $this->createChildInstance(); if (isset($this->document) && $this->length > 0) { foreach ($this->nodes as $node) { if ($node->parentNode) { foreach ($node->parentNode->childNodes as $sibling) { if ($sibling instanceof \DOMElement && !$sibling->isSameNode($node)) { $result->addDomNode($sibling); } } } } if ($selector) { $result = $result->filter($selector); } } return $result; }
[ "public", "function", "siblings", "(", "$", "selector", "=", "null", ")", "{", "$", "result", "=", "$", "this", "->", "createChildInstance", "(", ")", ";", "if", "(", "isset", "(", "$", "this", "->", "document", ")", "&&", "$", "this", "->", "length", ">", "0", ")", "{", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "if", "(", "$", "node", "->", "parentNode", ")", "{", "foreach", "(", "$", "node", "->", "parentNode", "->", "childNodes", "as", "$", "sibling", ")", "{", "if", "(", "$", "sibling", "instanceof", "\\", "DOMElement", "&&", "!", "$", "sibling", "->", "isSameNode", "(", "$", "node", ")", ")", "{", "$", "result", "->", "addDomNode", "(", "$", "sibling", ")", ";", "}", "}", "}", "}", "if", "(", "$", "selector", ")", "{", "$", "result", "=", "$", "result", "->", "filter", "(", "$", "selector", ")", ";", "}", "}", "return", "$", "result", ";", "}" ]
Get the siblings of each element in the set of matched elements, optionally filtered by a selector. @param string|self|callable|\DOMNodeList|\DOMNode|null $selector expression that filters the set of matched elements @return self
[ "Get", "the", "siblings", "of", "each", "element", "in", "the", "set", "of", "matched", "elements", "optionally", "filtered", "by", "a", "selector", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L455-L476
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.parent
public function parent($selector=null) { $result = $this->createChildInstance(); if (isset($this->document) && $this->length > 0) { foreach ($this->nodes as $node) { if ($node->parentNode) { $result->addDomNode($node->parentNode); } } if ($selector) { $result = $result->filter($selector); } } return $result; }
php
public function parent($selector=null) { $result = $this->createChildInstance(); if (isset($this->document) && $this->length > 0) { foreach ($this->nodes as $node) { if ($node->parentNode) { $result->addDomNode($node->parentNode); } } if ($selector) { $result = $result->filter($selector); } } return $result; }
[ "public", "function", "parent", "(", "$", "selector", "=", "null", ")", "{", "$", "result", "=", "$", "this", "->", "createChildInstance", "(", ")", ";", "if", "(", "isset", "(", "$", "this", "->", "document", ")", "&&", "$", "this", "->", "length", ">", "0", ")", "{", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "if", "(", "$", "node", "->", "parentNode", ")", "{", "$", "result", "->", "addDomNode", "(", "$", "node", "->", "parentNode", ")", ";", "}", "}", "if", "(", "$", "selector", ")", "{", "$", "result", "=", "$", "result", "->", "filter", "(", "$", "selector", ")", ";", "}", "}", "return", "$", "result", ";", "}" ]
Get the parent of each element in the current set of matched elements, optionally filtered by a selector @param string|self|callable|\DOMNodeList|\DOMNode|null $selector expression that filters the set of matched elements @return self
[ "Get", "the", "parent", "of", "each", "element", "in", "the", "current", "set", "of", "matched", "elements", "optionally", "filtered", "by", "a", "selector" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L485-L502
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.closest
public function closest($selector) { $result = $this->createChildInstance(); if (isset($this->document) && $this->length > 0) { foreach ($this->nodes as $node) { $current = $node; while ($current instanceof \DOMElement) { if (self::create($current)->is($selector)) { $result->addDomNode($current); break; } $current = $current->parentNode; } } } return $result; }
php
public function closest($selector) { $result = $this->createChildInstance(); if (isset($this->document) && $this->length > 0) { foreach ($this->nodes as $node) { $current = $node; while ($current instanceof \DOMElement) { if (self::create($current)->is($selector)) { $result->addDomNode($current); break; } $current = $current->parentNode; } } } return $result; }
[ "public", "function", "closest", "(", "$", "selector", ")", "{", "$", "result", "=", "$", "this", "->", "createChildInstance", "(", ")", ";", "if", "(", "isset", "(", "$", "this", "->", "document", ")", "&&", "$", "this", "->", "length", ">", "0", ")", "{", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "$", "current", "=", "$", "node", ";", "while", "(", "$", "current", "instanceof", "\\", "DOMElement", ")", "{", "if", "(", "self", "::", "create", "(", "$", "current", ")", "->", "is", "(", "$", "selector", ")", ")", "{", "$", "result", "->", "addDomNode", "(", "$", "current", ")", ";", "break", ";", "}", "$", "current", "=", "$", "current", "->", "parentNode", ";", "}", "}", "}", "return", "$", "result", ";", "}" ]
For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree. @param string|self|callable|\DOMNodeList|\DOMNode $selector selector expression to match elements against @return self
[ "For", "each", "element", "in", "the", "set", "get", "the", "first", "element", "that", "matches", "the", "selector", "by", "testing", "the", "element", "itself", "and", "traversing", "up", "through", "its", "ancestors", "in", "the", "DOM", "tree", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L512-L531
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.not
public function not($selector) { $result = $this->createChildInstance(); if ($this->length > 0) { if (\is_callable($selector)) { foreach ($this->nodes as $index => $node) { if (!$selector($node, $index)) { $result->addDomNode($node); } } } else { $selection = self::create($this->document)->find($selector); if ($selection->length > 0) { foreach ($this->nodes as $node) { $matched = false; foreach ($selection as $result_node) { if ($result_node->isSameNode($node)) { $matched = true; break 1; } } if (!$matched) { $result->addDomNode($node); } } } else { $result->addNodes($this->nodes); } } } return $result; }
php
public function not($selector) { $result = $this->createChildInstance(); if ($this->length > 0) { if (\is_callable($selector)) { foreach ($this->nodes as $index => $node) { if (!$selector($node, $index)) { $result->addDomNode($node); } } } else { $selection = self::create($this->document)->find($selector); if ($selection->length > 0) { foreach ($this->nodes as $node) { $matched = false; foreach ($selection as $result_node) { if ($result_node->isSameNode($node)) { $matched = true; break 1; } } if (!$matched) { $result->addDomNode($node); } } } else { $result->addNodes($this->nodes); } } } return $result; }
[ "public", "function", "not", "(", "$", "selector", ")", "{", "$", "result", "=", "$", "this", "->", "createChildInstance", "(", ")", ";", "if", "(", "$", "this", "->", "length", ">", "0", ")", "{", "if", "(", "\\", "is_callable", "(", "$", "selector", ")", ")", "{", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "index", "=>", "$", "node", ")", "{", "if", "(", "!", "$", "selector", "(", "$", "node", ",", "$", "index", ")", ")", "{", "$", "result", "->", "addDomNode", "(", "$", "node", ")", ";", "}", "}", "}", "else", "{", "$", "selection", "=", "self", "::", "create", "(", "$", "this", "->", "document", ")", "->", "find", "(", "$", "selector", ")", ";", "if", "(", "$", "selection", "->", "length", ">", "0", ")", "{", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "$", "matched", "=", "false", ";", "foreach", "(", "$", "selection", "as", "$", "result_node", ")", "{", "if", "(", "$", "result_node", "->", "isSameNode", "(", "$", "node", ")", ")", "{", "$", "matched", "=", "true", ";", "break", "1", ";", "}", "}", "if", "(", "!", "$", "matched", ")", "{", "$", "result", "->", "addDomNode", "(", "$", "node", ")", ";", "}", "}", "}", "else", "{", "$", "result", "->", "addNodes", "(", "$", "this", "->", "nodes", ")", ";", "}", "}", "}", "return", "$", "result", ";", "}" ]
Remove elements from the set of matched elements. @param string|self|callable|\DOMNodeList|\DOMNode $selector @return self
[ "Remove", "elements", "from", "the", "set", "of", "matched", "elements", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L540-L574
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.add
public function add($selector, $context=null) { $result = $this->createChildInstance(); $result->nodes = $this->nodes; $selection = $this->getTargetResult($selector, $context); foreach ($selection as $selection_node) { if (!$result->is($selection_node)) { if ($result->document === $selection_node->document) { $new_node = $selection_node->get(0); } else { $new_node = $this->document->importNode($selection_node->get(0), true); } $result->addDomNode($new_node); } } return $result; }
php
public function add($selector, $context=null) { $result = $this->createChildInstance(); $result->nodes = $this->nodes; $selection = $this->getTargetResult($selector, $context); foreach ($selection as $selection_node) { if (!$result->is($selection_node)) { if ($result->document === $selection_node->document) { $new_node = $selection_node->get(0); } else { $new_node = $this->document->importNode($selection_node->get(0), true); } $result->addDomNode($new_node); } } return $result; }
[ "public", "function", "add", "(", "$", "selector", ",", "$", "context", "=", "null", ")", "{", "$", "result", "=", "$", "this", "->", "createChildInstance", "(", ")", ";", "$", "result", "->", "nodes", "=", "$", "this", "->", "nodes", ";", "$", "selection", "=", "$", "this", "->", "getTargetResult", "(", "$", "selector", ",", "$", "context", ")", ";", "foreach", "(", "$", "selection", "as", "$", "selection_node", ")", "{", "if", "(", "!", "$", "result", "->", "is", "(", "$", "selection_node", ")", ")", "{", "if", "(", "$", "result", "->", "document", "===", "$", "selection_node", "->", "document", ")", "{", "$", "new_node", "=", "$", "selection_node", "->", "get", "(", "0", ")", ";", "}", "else", "{", "$", "new_node", "=", "$", "this", "->", "document", "->", "importNode", "(", "$", "selection_node", "->", "get", "(", "0", ")", ",", "true", ")", ";", "}", "$", "result", "->", "addDomNode", "(", "$", "new_node", ")", ";", "}", "}", "return", "$", "result", ";", "}" ]
Reduce the set of matched elements to those that match the selector @param string|self|callable|\DOMNodeList|\DOMNode $selector @param string|self|\DOMNodeList|\DOMNode|\DOMDocument $context @return self
[ "Reduce", "the", "set", "of", "matched", "elements", "to", "those", "that", "match", "the", "selector" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L584-L604
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.filter
public function filter($selector) { $result = $this->createChildInstance(); if ($this->length > 0) { if (\is_callable($selector)) { foreach ($this->nodes as $index => $node) { if ($selector($node, $index)) { $result->addDomNode($node); } } } else { $selection = self::create($this->document)->find($selector); foreach ($selection as $result_node) { foreach ($this->nodes as $node) { if ($result_node->isSameNode($node)) { $result->addDomNode($node); break 1; } } } } } return $result; }
php
public function filter($selector) { $result = $this->createChildInstance(); if ($this->length > 0) { if (\is_callable($selector)) { foreach ($this->nodes as $index => $node) { if ($selector($node, $index)) { $result->addDomNode($node); } } } else { $selection = self::create($this->document)->find($selector); foreach ($selection as $result_node) { foreach ($this->nodes as $node) { if ($result_node->isSameNode($node)) { $result->addDomNode($node); break 1; } } } } } return $result; }
[ "public", "function", "filter", "(", "$", "selector", ")", "{", "$", "result", "=", "$", "this", "->", "createChildInstance", "(", ")", ";", "if", "(", "$", "this", "->", "length", ">", "0", ")", "{", "if", "(", "\\", "is_callable", "(", "$", "selector", ")", ")", "{", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "index", "=>", "$", "node", ")", "{", "if", "(", "$", "selector", "(", "$", "node", ",", "$", "index", ")", ")", "{", "$", "result", "->", "addDomNode", "(", "$", "node", ")", ";", "}", "}", "}", "else", "{", "$", "selection", "=", "self", "::", "create", "(", "$", "this", "->", "document", ")", "->", "find", "(", "$", "selector", ")", ";", "foreach", "(", "$", "selection", "as", "$", "result_node", ")", "{", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "if", "(", "$", "result_node", "->", "isSameNode", "(", "$", "node", ")", ")", "{", "$", "result", "->", "addDomNode", "(", "$", "node", ")", ";", "break", "1", ";", "}", "}", "}", "}", "}", "return", "$", "result", ";", "}" ]
Reduce the set of matched elements to those that match the selector @param string|self|callable|\DOMNodeList|\DOMNode $selector @return self
[ "Reduce", "the", "set", "of", "matched", "elements", "to", "those", "that", "match", "the", "selector" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L613-L639
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.index
public function index($selector=null) { if ($selector === null) { if ($node = $this->getFirstElmNode()) { $position = 0; while ($node && ($node = $node->previousSibling)) { if ($node instanceof \DOMElement) { $position++; } else { break; } } return $position; } } else { foreach ($this as $key => $node) { if ($node->is($selector)) { return $key; } } } return -1; }
php
public function index($selector=null) { if ($selector === null) { if ($node = $this->getFirstElmNode()) { $position = 0; while ($node && ($node = $node->previousSibling)) { if ($node instanceof \DOMElement) { $position++; } else { break; } } return $position; } } else { foreach ($this as $key => $node) { if ($node->is($selector)) { return $key; } } } return -1; }
[ "public", "function", "index", "(", "$", "selector", "=", "null", ")", "{", "if", "(", "$", "selector", "===", "null", ")", "{", "if", "(", "$", "node", "=", "$", "this", "->", "getFirstElmNode", "(", ")", ")", "{", "$", "position", "=", "0", ";", "while", "(", "$", "node", "&&", "(", "$", "node", "=", "$", "node", "->", "previousSibling", ")", ")", "{", "if", "(", "$", "node", "instanceof", "\\", "DOMElement", ")", "{", "$", "position", "++", ";", "}", "else", "{", "break", ";", "}", "}", "return", "$", "position", ";", "}", "}", "else", "{", "foreach", "(", "$", "this", "as", "$", "key", "=>", "$", "node", ")", "{", "if", "(", "$", "node", "->", "is", "(", "$", "selector", ")", ")", "{", "return", "$", "key", ";", "}", "}", "}", "return", "-", "1", ";", "}" ]
Get the position of the first element within the DOM, relative to its sibling elements. Or get the position of the first node in the result that matches the selector. @param string|self|callable|\DOMNodeList|\DOMNode $selector @return int $position
[ "Get", "the", "position", "of", "the", "first", "element", "within", "the", "DOM", "relative", "to", "its", "sibling", "elements", ".", "Or", "get", "the", "position", "of", "the", "first", "node", "in", "the", "result", "that", "matches", "the", "selector", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L659-L682
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.is
public function is($selector) { if ($this->length > 0) { if (\is_callable($selector)) { foreach ($this->nodes as $index => $node) { if ($selector($node, $index)) { return true; } } } else { $selection = self::create($this->document)->find($selector); foreach ($selection->nodes as $result_node) { foreach ($this->nodes as $node) { if ($result_node->isSameNode($node)) { return true; } } } } } return false; }
php
public function is($selector) { if ($this->length > 0) { if (\is_callable($selector)) { foreach ($this->nodes as $index => $node) { if ($selector($node, $index)) { return true; } } } else { $selection = self::create($this->document)->find($selector); foreach ($selection->nodes as $result_node) { foreach ($this->nodes as $node) { if ($result_node->isSameNode($node)) { return true; } } } } } return false; }
[ "public", "function", "is", "(", "$", "selector", ")", "{", "if", "(", "$", "this", "->", "length", ">", "0", ")", "{", "if", "(", "\\", "is_callable", "(", "$", "selector", ")", ")", "{", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "index", "=>", "$", "node", ")", "{", "if", "(", "$", "selector", "(", "$", "node", ",", "$", "index", ")", ")", "{", "return", "true", ";", "}", "}", "}", "else", "{", "$", "selection", "=", "self", "::", "create", "(", "$", "this", "->", "document", ")", "->", "find", "(", "$", "selector", ")", ";", "foreach", "(", "$", "selection", "->", "nodes", "as", "$", "result_node", ")", "{", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "if", "(", "$", "result_node", "->", "isSameNode", "(", "$", "node", ")", ")", "{", "return", "true", ";", "}", "}", "}", "}", "}", "return", "false", ";", "}" ]
Check if any node matches the selector @param string|self|callable|\DOMNodeList|\DOMNode $selector @return boolean
[ "Check", "if", "any", "node", "matches", "the", "selector" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L691-L714
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.has
public function has($selector) { $result = $this->createChildInstance(); if ($this->length > 0) { foreach ($this as $node) { if ($node->find($selector)->length > 0) { $result->addDomNode($node->get(0)); } } } return $result; }
php
public function has($selector) { $result = $this->createChildInstance(); if ($this->length > 0) { foreach ($this as $node) { if ($node->find($selector)->length > 0) { $result->addDomNode($node->get(0)); } } } return $result; }
[ "public", "function", "has", "(", "$", "selector", ")", "{", "$", "result", "=", "$", "this", "->", "createChildInstance", "(", ")", ";", "if", "(", "$", "this", "->", "length", ">", "0", ")", "{", "foreach", "(", "$", "this", "as", "$", "node", ")", "{", "if", "(", "$", "node", "->", "find", "(", "$", "selector", ")", "->", "length", ">", "0", ")", "{", "$", "result", "->", "addDomNode", "(", "$", "node", "->", "get", "(", "0", ")", ")", ";", "}", "}", "}", "return", "$", "result", ";", "}" ]
Reduce the set of matched elements to those that have a descendant that matches the selector @param string|self|callable|\DOMNodeList|\DOMNode $selector @return self
[ "Reduce", "the", "set", "of", "matched", "elements", "to", "those", "that", "have", "a", "descendant", "that", "matches", "the", "selector" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L723-L736
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.slice
public function slice($offset=0, $length=null) { $result = $this->createChildInstance(); $result->nodes = \array_slice($this->nodes, $offset, $length); $result->length = \count($result->nodes); return $result; }
php
public function slice($offset=0, $length=null) { $result = $this->createChildInstance(); $result->nodes = \array_slice($this->nodes, $offset, $length); $result->length = \count($result->nodes); return $result; }
[ "public", "function", "slice", "(", "$", "offset", "=", "0", ",", "$", "length", "=", "null", ")", "{", "$", "result", "=", "$", "this", "->", "createChildInstance", "(", ")", ";", "$", "result", "->", "nodes", "=", "\\", "array_slice", "(", "$", "this", "->", "nodes", ",", "$", "offset", ",", "$", "length", ")", ";", "$", "result", "->", "length", "=", "\\", "count", "(", "$", "result", "->", "nodes", ")", ";", "return", "$", "result", ";", "}" ]
Reduce the set of matched elements to a subset specified by the offset and length (php like) @param integer $offset @param integer $length @return self
[ "Reduce", "the", "set", "of", "matched", "elements", "to", "a", "subset", "specified", "by", "the", "offset", "and", "length", "(", "php", "like", ")" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L746-L752
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.first
public function first($selector=null) { $result = $this[0]; if ($selector) { $result = $result->filter($selector); } return $result; }
php
public function first($selector=null) { $result = $this[0]; if ($selector) { $result = $result->filter($selector); } return $result; }
[ "public", "function", "first", "(", "$", "selector", "=", "null", ")", "{", "$", "result", "=", "$", "this", "[", "0", "]", ";", "if", "(", "$", "selector", ")", "{", "$", "result", "=", "$", "result", "->", "filter", "(", "$", "selector", ")", ";", "}", "return", "$", "result", ";", "}" ]
Returns DomQuery with first node @param string|self|callable|\DOMNodeList|\DOMNode|null $selector expression that filters the set of matched elements @return self
[ "Returns", "DomQuery", "with", "first", "node" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L773-L780
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.last
public function last($selector=null) { $result = $this[$this->length-1]; if ($selector) { $result = $result->filter($selector); } return $result; }
php
public function last($selector=null) { $result = $this[$this->length-1]; if ($selector) { $result = $result->filter($selector); } return $result; }
[ "public", "function", "last", "(", "$", "selector", "=", "null", ")", "{", "$", "result", "=", "$", "this", "[", "$", "this", "->", "length", "-", "1", "]", ";", "if", "(", "$", "selector", ")", "{", "$", "result", "=", "$", "result", "->", "filter", "(", "$", "selector", ")", ";", "}", "return", "$", "result", ";", "}" ]
Returns DomQuery with last node @param string|self|callable|\DOMNodeList|\DOMNode|null $selector expression that filters the set of matched elements @return self
[ "Returns", "DomQuery", "with", "last", "node" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L789-L796
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.next
public function next($selector=null) { $result = $this->createChildInstance(); if (isset($this->document) && $this->length > 0) { foreach ($this->nodes as $node) { if ($next = self::getNextElement($node)) { $result->addDomNode($next); } } if ($selector) { $result = $result->filter($selector); } } return $result; }
php
public function next($selector=null) { $result = $this->createChildInstance(); if (isset($this->document) && $this->length > 0) { foreach ($this->nodes as $node) { if ($next = self::getNextElement($node)) { $result->addDomNode($next); } } if ($selector) { $result = $result->filter($selector); } } return $result; }
[ "public", "function", "next", "(", "$", "selector", "=", "null", ")", "{", "$", "result", "=", "$", "this", "->", "createChildInstance", "(", ")", ";", "if", "(", "isset", "(", "$", "this", "->", "document", ")", "&&", "$", "this", "->", "length", ">", "0", ")", "{", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "if", "(", "$", "next", "=", "self", "::", "getNextElement", "(", "$", "node", ")", ")", "{", "$", "result", "->", "addDomNode", "(", "$", "next", ")", ";", "}", "}", "if", "(", "$", "selector", ")", "{", "$", "result", "=", "$", "result", "->", "filter", "(", "$", "selector", ")", ";", "}", "}", "return", "$", "result", ";", "}" ]
Returns DomQuery with immediately following sibling of all nodes @param string|self|callable|\DOMNodeList|\DOMNode|null $selector expression that filters the set of matched elements @return self
[ "Returns", "DomQuery", "with", "immediately", "following", "sibling", "of", "all", "nodes" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L805-L822
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.prev
public function prev($selector=null) { $result = $this->createChildInstance(); if (isset($this->document) && $this->length > 0) { foreach ($this->nodes as $node) { // get all previous sibling of all nodes if ($prev = self::getPreviousElement($node)) { $result->addDomNode($prev); } } if ($selector) { $result = $result->filter($selector); } } return $result; }
php
public function prev($selector=null) { $result = $this->createChildInstance(); if (isset($this->document) && $this->length > 0) { foreach ($this->nodes as $node) { // get all previous sibling of all nodes if ($prev = self::getPreviousElement($node)) { $result->addDomNode($prev); } } if ($selector) { $result = $result->filter($selector); } } return $result; }
[ "public", "function", "prev", "(", "$", "selector", "=", "null", ")", "{", "$", "result", "=", "$", "this", "->", "createChildInstance", "(", ")", ";", "if", "(", "isset", "(", "$", "this", "->", "document", ")", "&&", "$", "this", "->", "length", ">", "0", ")", "{", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "// get all previous sibling of all nodes", "if", "(", "$", "prev", "=", "self", "::", "getPreviousElement", "(", "$", "node", ")", ")", "{", "$", "result", "->", "addDomNode", "(", "$", "prev", ")", ";", "}", "}", "if", "(", "$", "selector", ")", "{", "$", "result", "=", "$", "result", "->", "filter", "(", "$", "selector", ")", ";", "}", "}", "return", "$", "result", ";", "}" ]
Returns DomQuery with immediately preceding sibling of all nodes @param string|self|callable|\DOMNodeList|\DOMNode|null $selector expression that filters the set of matched elements @return self
[ "Returns", "DomQuery", "with", "immediately", "preceding", "sibling", "of", "all", "nodes" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L859-L876
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.remove
public function remove($selector=null) { $result = $this; if ($selector) { $result = $result->filter($selector); } foreach ($result->nodes as $node) { if ($node->parentNode) { $node->parentNode->removeChild($node); } } $result->nodes = array(); $result->length = 0; return $result; }
php
public function remove($selector=null) { $result = $this; if ($selector) { $result = $result->filter($selector); } foreach ($result->nodes as $node) { if ($node->parentNode) { $node->parentNode->removeChild($node); } } $result->nodes = array(); $result->length = 0; return $result; }
[ "public", "function", "remove", "(", "$", "selector", "=", "null", ")", "{", "$", "result", "=", "$", "this", ";", "if", "(", "$", "selector", ")", "{", "$", "result", "=", "$", "result", "->", "filter", "(", "$", "selector", ")", ";", "}", "foreach", "(", "$", "result", "->", "nodes", "as", "$", "node", ")", "{", "if", "(", "$", "node", "->", "parentNode", ")", "{", "$", "node", "->", "parentNode", "->", "removeChild", "(", "$", "node", ")", ";", "}", "}", "$", "result", "->", "nodes", "=", "array", "(", ")", ";", "$", "result", "->", "length", "=", "0", ";", "return", "$", "result", ";", "}" ]
Remove the set of matched elements @param string|self|callable|\DOMNodeList|\DOMNode|null $selector expression that filters the set of matched elements to be removed @return self
[ "Remove", "the", "set", "of", "matched", "elements" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L914-L930
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.importNodes
private function importNodes($content, callable $import_function) { if (\is_array($content)) { foreach ($content as $item) { $this->importNodes($item, $import_function); } } else { if (\is_string($content) && strpos($content, "\n") !== false) { $this->preserve_no_newlines = false; if (isset($this->root_instance)) { $this->root_instance->preserve_no_newlines = false; } } if (!($content instanceof self)) { $content = new self($content); } foreach ($this->nodes as $node) { foreach ($content->getNodes() as $content_node) { if ($content_node->ownerDocument === $node->ownerDocument) { $imported_node = $content_node->cloneNode(true); } else { $imported_node = $this->document->importNode($content_node, true); } $import_function($node, $imported_node); } } } }
php
private function importNodes($content, callable $import_function) { if (\is_array($content)) { foreach ($content as $item) { $this->importNodes($item, $import_function); } } else { if (\is_string($content) && strpos($content, "\n") !== false) { $this->preserve_no_newlines = false; if (isset($this->root_instance)) { $this->root_instance->preserve_no_newlines = false; } } if (!($content instanceof self)) { $content = new self($content); } foreach ($this->nodes as $node) { foreach ($content->getNodes() as $content_node) { if ($content_node->ownerDocument === $node->ownerDocument) { $imported_node = $content_node->cloneNode(true); } else { $imported_node = $this->document->importNode($content_node, true); } $import_function($node, $imported_node); } } } }
[ "private", "function", "importNodes", "(", "$", "content", ",", "callable", "$", "import_function", ")", "{", "if", "(", "\\", "is_array", "(", "$", "content", ")", ")", "{", "foreach", "(", "$", "content", "as", "$", "item", ")", "{", "$", "this", "->", "importNodes", "(", "$", "item", ",", "$", "import_function", ")", ";", "}", "}", "else", "{", "if", "(", "\\", "is_string", "(", "$", "content", ")", "&&", "strpos", "(", "$", "content", ",", "\"\\n\"", ")", "!==", "false", ")", "{", "$", "this", "->", "preserve_no_newlines", "=", "false", ";", "if", "(", "isset", "(", "$", "this", "->", "root_instance", ")", ")", "{", "$", "this", "->", "root_instance", "->", "preserve_no_newlines", "=", "false", ";", "}", "}", "if", "(", "!", "(", "$", "content", "instanceof", "self", ")", ")", "{", "$", "content", "=", "new", "self", "(", "$", "content", ")", ";", "}", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "foreach", "(", "$", "content", "->", "getNodes", "(", ")", "as", "$", "content_node", ")", "{", "if", "(", "$", "content_node", "->", "ownerDocument", "===", "$", "node", "->", "ownerDocument", ")", "{", "$", "imported_node", "=", "$", "content_node", "->", "cloneNode", "(", "true", ")", ";", "}", "else", "{", "$", "imported_node", "=", "$", "this", "->", "document", "->", "importNode", "(", "$", "content_node", ",", "true", ")", ";", "}", "$", "import_function", "(", "$", "node", ",", "$", "imported_node", ")", ";", "}", "}", "}", "}" ]
Import nodes and insert or append them via callback function @param string|self|array $content @param callable $import_function @return void
[ "Import", "nodes", "and", "insert", "or", "append", "them", "via", "callback", "function" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L940-L969
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.getTargetResult
private function getTargetResult($target, $context=null) { if ($context===null && \is_string($target) && strpos($target, '<') === false) { $context = $this->document; } return $context === null ? self::create($target) : self::create($target, $context); }
php
private function getTargetResult($target, $context=null) { if ($context===null && \is_string($target) && strpos($target, '<') === false) { $context = $this->document; } return $context === null ? self::create($target) : self::create($target, $context); }
[ "private", "function", "getTargetResult", "(", "$", "target", ",", "$", "context", "=", "null", ")", "{", "if", "(", "$", "context", "===", "null", "&&", "\\", "is_string", "(", "$", "target", ")", "&&", "strpos", "(", "$", "target", ",", "'<'", ")", "===", "false", ")", "{", "$", "context", "=", "$", "this", "->", "document", ";", "}", "return", "$", "context", "===", "null", "?", "self", "::", "create", "(", "$", "target", ")", ":", "self", "::", "create", "(", "$", "target", ",", "$", "context", ")", ";", "}" ]
Get target result using selector or instance of self @param string|self $target @param string|self|\DOMNodeList|\DOMNode|\DOMDocument $context @return self
[ "Get", "target", "result", "using", "selector", "or", "instance", "of", "self" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L979-L986
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.append
public function append() { $this->importNodes(\func_get_args(), function ($node, $imported_node) { $node->appendChild($imported_node); }); return $this; }
php
public function append() { $this->importNodes(\func_get_args(), function ($node, $imported_node) { $node->appendChild($imported_node); }); return $this; }
[ "public", "function", "append", "(", ")", "{", "$", "this", "->", "importNodes", "(", "\\", "func_get_args", "(", ")", ",", "function", "(", "$", "node", ",", "$", "imported_node", ")", "{", "$", "node", "->", "appendChild", "(", "$", "imported_node", ")", ";", "}", ")", ";", "return", "$", "this", ";", "}" ]
Insert content to the end of each element in the set of matched elements. @param string|self $content,... @return $this
[ "Insert", "content", "to", "the", "end", "of", "each", "element", "in", "the", "set", "of", "matched", "elements", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L995-L1002
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.appendTo
public function appendTo($target) { $target_result = $this->getTargetResult($target); $target_result->append($this); $this->remove(); return $target_result; }
php
public function appendTo($target) { $target_result = $this->getTargetResult($target); $target_result->append($this); $this->remove(); return $target_result; }
[ "public", "function", "appendTo", "(", "$", "target", ")", "{", "$", "target_result", "=", "$", "this", "->", "getTargetResult", "(", "$", "target", ")", ";", "$", "target_result", "->", "append", "(", "$", "this", ")", ";", "$", "this", "->", "remove", "(", ")", ";", "return", "$", "target_result", ";", "}" ]
Insert every element in the set of matched elements to the end of the target. @param string|self $target @return self
[ "Insert", "every", "element", "in", "the", "set", "of", "matched", "elements", "to", "the", "end", "of", "the", "target", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L1011-L1019
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.prepend
public function prepend() { $this->importNodes(\func_get_args(), function ($node, $imported_node) { $node->insertBefore($imported_node, $node->childNodes->item(0)); }); return $this; }
php
public function prepend() { $this->importNodes(\func_get_args(), function ($node, $imported_node) { $node->insertBefore($imported_node, $node->childNodes->item(0)); }); return $this; }
[ "public", "function", "prepend", "(", ")", "{", "$", "this", "->", "importNodes", "(", "\\", "func_get_args", "(", ")", ",", "function", "(", "$", "node", ",", "$", "imported_node", ")", "{", "$", "node", "->", "insertBefore", "(", "$", "imported_node", ",", "$", "node", "->", "childNodes", "->", "item", "(", "0", ")", ")", ";", "}", ")", ";", "return", "$", "this", ";", "}" ]
Insert content to the beginning of each element in the set of matched elements @param string|self $content,... @return $this
[ "Insert", "content", "to", "the", "beginning", "of", "each", "element", "in", "the", "set", "of", "matched", "elements" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L1028-L1035
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.prependTo
public function prependTo($target) { $target_result = $this->getTargetResult($target); $target_result->prepend($this); $this->remove(); return $target_result; }
php
public function prependTo($target) { $target_result = $this->getTargetResult($target); $target_result->prepend($this); $this->remove(); return $target_result; }
[ "public", "function", "prependTo", "(", "$", "target", ")", "{", "$", "target_result", "=", "$", "this", "->", "getTargetResult", "(", "$", "target", ")", ";", "$", "target_result", "->", "prepend", "(", "$", "this", ")", ";", "$", "this", "->", "remove", "(", ")", ";", "return", "$", "target_result", ";", "}" ]
Insert every element in the set of matched elements to the beginning of the target. @param string|self $target @return self
[ "Insert", "every", "element", "in", "the", "set", "of", "matched", "elements", "to", "the", "beginning", "of", "the", "target", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L1044-L1052
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.before
public function before() { $this->importNodes(\func_get_args(), function ($node, $imported_node) { if ($node->parentNode instanceof \DOMDocument) { throw new \Exception('Can not set before root element '.$node->tagName.' of document'); } $node->parentNode->insertBefore($imported_node, $node); }); return $this; }
php
public function before() { $this->importNodes(\func_get_args(), function ($node, $imported_node) { if ($node->parentNode instanceof \DOMDocument) { throw new \Exception('Can not set before root element '.$node->tagName.' of document'); } $node->parentNode->insertBefore($imported_node, $node); }); return $this; }
[ "public", "function", "before", "(", ")", "{", "$", "this", "->", "importNodes", "(", "\\", "func_get_args", "(", ")", ",", "function", "(", "$", "node", ",", "$", "imported_node", ")", "{", "if", "(", "$", "node", "->", "parentNode", "instanceof", "\\", "DOMDocument", ")", "{", "throw", "new", "\\", "Exception", "(", "'Can not set before root element '", ".", "$", "node", "->", "tagName", ".", "' of document'", ")", ";", "}", "$", "node", "->", "parentNode", "->", "insertBefore", "(", "$", "imported_node", ",", "$", "node", ")", ";", "}", ")", ";", "return", "$", "this", ";", "}" ]
Insert content before each element in the set of matched elements. @param string|self $content,... @return $this
[ "Insert", "content", "before", "each", "element", "in", "the", "set", "of", "matched", "elements", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L1061-L1072
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.after
public function after() { $this->importNodes(\func_get_args(), function ($node, $imported_node) { if ($node->nextSibling) { $node->parentNode->insertBefore($imported_node, $node->nextSibling); } else { // node is last, so there is no next sibling to insert before $node->parentNode->appendChild($imported_node); } }); return $this; }
php
public function after() { $this->importNodes(\func_get_args(), function ($node, $imported_node) { if ($node->nextSibling) { $node->parentNode->insertBefore($imported_node, $node->nextSibling); } else { // node is last, so there is no next sibling to insert before $node->parentNode->appendChild($imported_node); } }); return $this; }
[ "public", "function", "after", "(", ")", "{", "$", "this", "->", "importNodes", "(", "\\", "func_get_args", "(", ")", ",", "function", "(", "$", "node", ",", "$", "imported_node", ")", "{", "if", "(", "$", "node", "->", "nextSibling", ")", "{", "$", "node", "->", "parentNode", "->", "insertBefore", "(", "$", "imported_node", ",", "$", "node", "->", "nextSibling", ")", ";", "}", "else", "{", "// node is last, so there is no next sibling to insert before", "$", "node", "->", "parentNode", "->", "appendChild", "(", "$", "imported_node", ")", ";", "}", "}", ")", ";", "return", "$", "this", ";", "}" ]
Insert content after each element in the set of matched elements. @param string|self $content,... @return $this
[ "Insert", "content", "after", "each", "element", "in", "the", "set", "of", "matched", "elements", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L1081-L1092
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.replaceWith
public function replaceWith() { $removed_nodes = new self(); $this->importNodes(\func_get_args(), function ($node, $imported_node) use (&$removed_nodes) { if ($node->nextSibling) { $node->parentNode->insertBefore($imported_node, $node->nextSibling); } else { // node is last, so there is no next sibling to insert before $node->parentNode->appendChild($imported_node); } $removed_nodes->addDomNode($node); $node->parentNode->removeChild($node); }); foreach (\func_get_args() as $new_content) { if (!\is_string($new_content)) { self::create($new_content)->remove(); } } return $removed_nodes; }
php
public function replaceWith() { $removed_nodes = new self(); $this->importNodes(\func_get_args(), function ($node, $imported_node) use (&$removed_nodes) { if ($node->nextSibling) { $node->parentNode->insertBefore($imported_node, $node->nextSibling); } else { // node is last, so there is no next sibling to insert before $node->parentNode->appendChild($imported_node); } $removed_nodes->addDomNode($node); $node->parentNode->removeChild($node); }); foreach (\func_get_args() as $new_content) { if (!\is_string($new_content)) { self::create($new_content)->remove(); } } return $removed_nodes; }
[ "public", "function", "replaceWith", "(", ")", "{", "$", "removed_nodes", "=", "new", "self", "(", ")", ";", "$", "this", "->", "importNodes", "(", "\\", "func_get_args", "(", ")", ",", "function", "(", "$", "node", ",", "$", "imported_node", ")", "use", "(", "&", "$", "removed_nodes", ")", "{", "if", "(", "$", "node", "->", "nextSibling", ")", "{", "$", "node", "->", "parentNode", "->", "insertBefore", "(", "$", "imported_node", ",", "$", "node", "->", "nextSibling", ")", ";", "}", "else", "{", "// node is last, so there is no next sibling to insert before", "$", "node", "->", "parentNode", "->", "appendChild", "(", "$", "imported_node", ")", ";", "}", "$", "removed_nodes", "->", "addDomNode", "(", "$", "node", ")", ";", "$", "node", "->", "parentNode", "->", "removeChild", "(", "$", "node", ")", ";", "}", ")", ";", "foreach", "(", "\\", "func_get_args", "(", ")", "as", "$", "new_content", ")", "{", "if", "(", "!", "\\", "is_string", "(", "$", "new_content", ")", ")", "{", "self", "::", "create", "(", "$", "new_content", ")", "->", "remove", "(", ")", ";", "}", "}", "return", "$", "removed_nodes", ";", "}" ]
Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed. @param string|self $new_content,... @return self
[ "Replace", "each", "element", "in", "the", "set", "of", "matched", "elements", "with", "the", "provided", "new", "content", "and", "return", "the", "set", "of", "elements", "that", "was", "removed", "." ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L1102-L1123
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.wrap
public function wrap() { $this->importNodes(\func_get_args(), function ($node, $imported_node) { if ($node->parentNode instanceof \DOMDocument) { throw new \Exception('Can not wrap inside root element '.$node->tagName.' of document'); } // replace node with imported wrapper $old = $node->parentNode->replaceChild($imported_node, $node); // old node goes inside the most inner child of wrapper $target = $imported_node; while ($target->hasChildNodes()) { $target = $target->childNodes[0]; } $target->appendChild($old); }); return $this; }
php
public function wrap() { $this->importNodes(\func_get_args(), function ($node, $imported_node) { if ($node->parentNode instanceof \DOMDocument) { throw new \Exception('Can not wrap inside root element '.$node->tagName.' of document'); } // replace node with imported wrapper $old = $node->parentNode->replaceChild($imported_node, $node); // old node goes inside the most inner child of wrapper $target = $imported_node; while ($target->hasChildNodes()) { $target = $target->childNodes[0]; } $target->appendChild($old); }); return $this; }
[ "public", "function", "wrap", "(", ")", "{", "$", "this", "->", "importNodes", "(", "\\", "func_get_args", "(", ")", ",", "function", "(", "$", "node", ",", "$", "imported_node", ")", "{", "if", "(", "$", "node", "->", "parentNode", "instanceof", "\\", "DOMDocument", ")", "{", "throw", "new", "\\", "Exception", "(", "'Can not wrap inside root element '", ".", "$", "node", "->", "tagName", ".", "' of document'", ")", ";", "}", "// replace node with imported wrapper", "$", "old", "=", "$", "node", "->", "parentNode", "->", "replaceChild", "(", "$", "imported_node", ",", "$", "node", ")", ";", "// old node goes inside the most inner child of wrapper", "$", "target", "=", "$", "imported_node", ";", "while", "(", "$", "target", "->", "hasChildNodes", "(", ")", ")", "{", "$", "target", "=", "$", "target", "->", "childNodes", "[", "0", "]", ";", "}", "$", "target", "->", "appendChild", "(", "$", "old", ")", ";", "}", ")", ";", "return", "$", "this", ";", "}" ]
Wrap an HTML structure around each element in the set of matched elements @param string|self $content,... @return $this
[ "Wrap", "an", "HTML", "structure", "around", "each", "element", "in", "the", "set", "of", "matched", "elements" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L1132-L1150
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.wrapAll
public function wrapAll() { $wrapper_node = null; // node given as wrapper $wrap_target_node = null; // node that wil be parent of content to be wrapped $this->importNodes(\func_get_args(), function ($node, $imported_node) use (&$wrapper_node, &$wrap_target_node) { if ($node->parentNode instanceof \DOMDocument) { throw new \Exception('Can not wrap inside root element '.$node->tagName.' of document'); } if ($wrapper_node && $wrap_target_node) { // already wrapped before $old = $node->parentNode->removeChild($node); $wrap_target_node->appendChild($old); } else { $wrapper_node = $imported_node; // replace node with (imported) wrapper $old = $node->parentNode->replaceChild($imported_node, $node); // old node goes inside the most inner child of wrapper $target = $imported_node; while ($target->hasChildNodes()) { $target = $target->childNodes[0]; } $target->appendChild($old); $wrap_target_node = $target; // save for next round } }); return $this; }
php
public function wrapAll() { $wrapper_node = null; // node given as wrapper $wrap_target_node = null; // node that wil be parent of content to be wrapped $this->importNodes(\func_get_args(), function ($node, $imported_node) use (&$wrapper_node, &$wrap_target_node) { if ($node->parentNode instanceof \DOMDocument) { throw new \Exception('Can not wrap inside root element '.$node->tagName.' of document'); } if ($wrapper_node && $wrap_target_node) { // already wrapped before $old = $node->parentNode->removeChild($node); $wrap_target_node->appendChild($old); } else { $wrapper_node = $imported_node; // replace node with (imported) wrapper $old = $node->parentNode->replaceChild($imported_node, $node); // old node goes inside the most inner child of wrapper $target = $imported_node; while ($target->hasChildNodes()) { $target = $target->childNodes[0]; } $target->appendChild($old); $wrap_target_node = $target; // save for next round } }); return $this; }
[ "public", "function", "wrapAll", "(", ")", "{", "$", "wrapper_node", "=", "null", ";", "// node given as wrapper", "$", "wrap_target_node", "=", "null", ";", "// node that wil be parent of content to be wrapped", "$", "this", "->", "importNodes", "(", "\\", "func_get_args", "(", ")", ",", "function", "(", "$", "node", ",", "$", "imported_node", ")", "use", "(", "&", "$", "wrapper_node", ",", "&", "$", "wrap_target_node", ")", "{", "if", "(", "$", "node", "->", "parentNode", "instanceof", "\\", "DOMDocument", ")", "{", "throw", "new", "\\", "Exception", "(", "'Can not wrap inside root element '", ".", "$", "node", "->", "tagName", ".", "' of document'", ")", ";", "}", "if", "(", "$", "wrapper_node", "&&", "$", "wrap_target_node", ")", "{", "// already wrapped before", "$", "old", "=", "$", "node", "->", "parentNode", "->", "removeChild", "(", "$", "node", ")", ";", "$", "wrap_target_node", "->", "appendChild", "(", "$", "old", ")", ";", "}", "else", "{", "$", "wrapper_node", "=", "$", "imported_node", ";", "// replace node with (imported) wrapper", "$", "old", "=", "$", "node", "->", "parentNode", "->", "replaceChild", "(", "$", "imported_node", ",", "$", "node", ")", ";", "// old node goes inside the most inner child of wrapper", "$", "target", "=", "$", "imported_node", ";", "while", "(", "$", "target", "->", "hasChildNodes", "(", ")", ")", "{", "$", "target", "=", "$", "target", "->", "childNodes", "[", "0", "]", ";", "}", "$", "target", "->", "appendChild", "(", "$", "old", ")", ";", "$", "wrap_target_node", "=", "$", "target", ";", "// save for next round", "}", "}", ")", ";", "return", "$", "this", ";", "}" ]
Wrap an HTML structure around all elements in the set of matched elements @param string|self $content,... @return $this
[ "Wrap", "an", "HTML", "structure", "around", "all", "elements", "in", "the", "set", "of", "matched", "elements" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L1159-L1187
Rct567/DomQuery
src/Rct567/DomQuery/DomQuery.php
DomQuery.wrapInner
public function wrapInner() { foreach ($this->nodes as $node) { self::create($node->childNodes)->wrapAll(\func_get_args()); } return $this; }
php
public function wrapInner() { foreach ($this->nodes as $node) { self::create($node->childNodes)->wrapAll(\func_get_args()); } return $this; }
[ "public", "function", "wrapInner", "(", ")", "{", "foreach", "(", "$", "this", "->", "nodes", "as", "$", "node", ")", "{", "self", "::", "create", "(", "$", "node", "->", "childNodes", ")", "->", "wrapAll", "(", "\\", "func_get_args", "(", ")", ")", ";", "}", "return", "$", "this", ";", "}" ]
Wrap an HTML structure around the content of each element in the set of matched elements @param string|self $content,... @return $this
[ "Wrap", "an", "HTML", "structure", "around", "the", "content", "of", "each", "element", "in", "the", "set", "of", "matched", "elements" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/DomQuery.php#L1196-L1203
Rct567/DomQuery
src/Rct567/DomQuery/CssToXpath.php
CssToXpath.transform
public static function transform(string $path) { if (isset(self::$xpath_cache[$path])) { return self::$xpath_cache[$path]; } $tmp_path = self::replaceCharInsideEnclosure($path, ','); if (strpos($tmp_path, ',') !== false) { $paths = explode(',', $tmp_path); $expressions = array(); foreach ($paths as $single_path) { $single_path = str_replace("\0", ',', $single_path); // restore commas $xpath = self::transform(trim($single_path)); $expressions[] = $xpath; } return implode('|', $expressions); } // replace spaces inside (), to correctly create tokens (restore later) $path_escaped = self::replaceCharInsideEnclosure($path, ' '); // create tokens and analyze to create segments $tokens = preg_split('/\s+/', $path_escaped); $segments = array(); foreach ($tokens as $key => $token) { $token = str_replace("\0", ' ', $token); // restore spaces if ($segment = self::getSegmentFromToken($token, $key, $tokens)) { $segments[] = $segment; } } // use segments to create array with transformed tokens $new_path_tokens = self::transformCssSegments($segments); // result tokens to xpath $xpath_result = implode('', $new_path_tokens); self::$xpath_cache[$path] = $xpath_result; return $xpath_result; }
php
public static function transform(string $path) { if (isset(self::$xpath_cache[$path])) { return self::$xpath_cache[$path]; } $tmp_path = self::replaceCharInsideEnclosure($path, ','); if (strpos($tmp_path, ',') !== false) { $paths = explode(',', $tmp_path); $expressions = array(); foreach ($paths as $single_path) { $single_path = str_replace("\0", ',', $single_path); // restore commas $xpath = self::transform(trim($single_path)); $expressions[] = $xpath; } return implode('|', $expressions); } // replace spaces inside (), to correctly create tokens (restore later) $path_escaped = self::replaceCharInsideEnclosure($path, ' '); // create tokens and analyze to create segments $tokens = preg_split('/\s+/', $path_escaped); $segments = array(); foreach ($tokens as $key => $token) { $token = str_replace("\0", ' ', $token); // restore spaces if ($segment = self::getSegmentFromToken($token, $key, $tokens)) { $segments[] = $segment; } } // use segments to create array with transformed tokens $new_path_tokens = self::transformCssSegments($segments); // result tokens to xpath $xpath_result = implode('', $new_path_tokens); self::$xpath_cache[$path] = $xpath_result; return $xpath_result; }
[ "public", "static", "function", "transform", "(", "string", "$", "path", ")", "{", "if", "(", "isset", "(", "self", "::", "$", "xpath_cache", "[", "$", "path", "]", ")", ")", "{", "return", "self", "::", "$", "xpath_cache", "[", "$", "path", "]", ";", "}", "$", "tmp_path", "=", "self", "::", "replaceCharInsideEnclosure", "(", "$", "path", ",", "','", ")", ";", "if", "(", "strpos", "(", "$", "tmp_path", ",", "','", ")", "!==", "false", ")", "{", "$", "paths", "=", "explode", "(", "','", ",", "$", "tmp_path", ")", ";", "$", "expressions", "=", "array", "(", ")", ";", "foreach", "(", "$", "paths", "as", "$", "single_path", ")", "{", "$", "single_path", "=", "str_replace", "(", "\"\\0\"", ",", "','", ",", "$", "single_path", ")", ";", "// restore commas", "$", "xpath", "=", "self", "::", "transform", "(", "trim", "(", "$", "single_path", ")", ")", ";", "$", "expressions", "[", "]", "=", "$", "xpath", ";", "}", "return", "implode", "(", "'|'", ",", "$", "expressions", ")", ";", "}", "// replace spaces inside (), to correctly create tokens (restore later)", "$", "path_escaped", "=", "self", "::", "replaceCharInsideEnclosure", "(", "$", "path", ",", "' '", ")", ";", "// create tokens and analyze to create segments", "$", "tokens", "=", "preg_split", "(", "'/\\s+/'", ",", "$", "path_escaped", ")", ";", "$", "segments", "=", "array", "(", ")", ";", "foreach", "(", "$", "tokens", "as", "$", "key", "=>", "$", "token", ")", "{", "$", "token", "=", "str_replace", "(", "\"\\0\"", ",", "' '", ",", "$", "token", ")", ";", "// restore spaces", "if", "(", "$", "segment", "=", "self", "::", "getSegmentFromToken", "(", "$", "token", ",", "$", "key", ",", "$", "tokens", ")", ")", "{", "$", "segments", "[", "]", "=", "$", "segment", ";", "}", "}", "// use segments to create array with transformed tokens", "$", "new_path_tokens", "=", "self", "::", "transformCssSegments", "(", "$", "segments", ")", ";", "// result tokens to xpath", "$", "xpath_result", "=", "implode", "(", "''", ",", "$", "new_path_tokens", ")", ";", "self", "::", "$", "xpath_cache", "[", "$", "path", "]", "=", "$", "xpath_result", ";", "return", "$", "xpath_result", ";", "}" ]
Transform CSS selector expression to XPath @param string $path css selector expression @return string xpath expression
[ "Transform", "CSS", "selector", "expression", "to", "XPath" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/CssToXpath.php#L25-L74
Rct567/DomQuery
src/Rct567/DomQuery/CssToXpath.php
CssToXpath.replaceCharInsideEnclosure
private static function replaceCharInsideEnclosure($str, $search_char, $enclosure_open='(', $enclosure_close=')') { if ($str === '' || strpos($str, $search_char) === false || strpos($str, $enclosure_open) === false) { return $str; } for ($i = 0, $str_length = \strlen($str); $i < $str_length; $i++) { if ($i > 0 && $str[$i] === $search_char) { // check if enclosure is open by counting char before position $enclosure_is_open = substr_count($str, $enclosure_open, 0, $i) !== substr_count($str, $enclosure_close, 0, $i); if ($enclosure_is_open) { $str[$i] = "\0"; } } } return $str; }
php
private static function replaceCharInsideEnclosure($str, $search_char, $enclosure_open='(', $enclosure_close=')') { if ($str === '' || strpos($str, $search_char) === false || strpos($str, $enclosure_open) === false) { return $str; } for ($i = 0, $str_length = \strlen($str); $i < $str_length; $i++) { if ($i > 0 && $str[$i] === $search_char) { // check if enclosure is open by counting char before position $enclosure_is_open = substr_count($str, $enclosure_open, 0, $i) !== substr_count($str, $enclosure_close, 0, $i); if ($enclosure_is_open) { $str[$i] = "\0"; } } } return $str; }
[ "private", "static", "function", "replaceCharInsideEnclosure", "(", "$", "str", ",", "$", "search_char", ",", "$", "enclosure_open", "=", "'('", ",", "$", "enclosure_close", "=", "')'", ")", "{", "if", "(", "$", "str", "===", "''", "||", "strpos", "(", "$", "str", ",", "$", "search_char", ")", "===", "false", "||", "strpos", "(", "$", "str", ",", "$", "enclosure_open", ")", "===", "false", ")", "{", "return", "$", "str", ";", "}", "for", "(", "$", "i", "=", "0", ",", "$", "str_length", "=", "\\", "strlen", "(", "$", "str", ")", ";", "$", "i", "<", "$", "str_length", ";", "$", "i", "++", ")", "{", "if", "(", "$", "i", ">", "0", "&&", "$", "str", "[", "$", "i", "]", "===", "$", "search_char", ")", "{", "// check if enclosure is open by counting char before position", "$", "enclosure_is_open", "=", "substr_count", "(", "$", "str", ",", "$", "enclosure_open", ",", "0", ",", "$", "i", ")", "!==", "substr_count", "(", "$", "str", ",", "$", "enclosure_close", ",", "0", ",", "$", "i", ")", ";", "if", "(", "$", "enclosure_is_open", ")", "{", "$", "str", "[", "$", "i", "]", "=", "\"\\0\"", ";", "}", "}", "}", "return", "$", "str", ";", "}" ]
Replace char with null bytes inside (optionally specified) enclosure @param string $str @param string $search_char @param string $enclosure_open @param string $enclosure_close @return string $str
[ "Replace", "char", "with", "null", "bytes", "inside", "(", "optionally", "specified", ")", "enclosure" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/CssToXpath.php#L86-L103
Rct567/DomQuery
src/Rct567/DomQuery/CssToXpath.php
CssToXpath.getSegmentFromToken
private static function getSegmentFromToken($token, $key, array $tokens) { $relation_tokens = array('>', '~', '+'); if (\in_array($token, $relation_tokens, true)) { // not a segment return false; } $segment = (object) array( 'selector' => '', 'relation_token' => false, 'attribute_filters' => array(), 'pseudo_filters' => array() ); if (isset($tokens[$key-1]) && \in_array($tokens[$key-1], $relation_tokens, true)) { // get relationship token $segment->relation_token = $tokens[$key-1]; } if (ctype_alpha($token)) { // simple element selector $segment->selector = $token; return $segment; } $char_tmp_replaced = false; if (strpos($token, '\\') !== false) { $token = preg_replace_callback( // temporary replace escaped characters '#(\\\\)(.{1})#', function ($matches) { return 'ESCAPED'. \ord($matches[2]); }, $token ); $char_tmp_replaced = true; } $segment->selector = $token; while (preg_match('/(.*)\:(not|contains|has)\((.+)\)$/i', $segment->selector, $matches)) { // pseudo selector $segment->selector = $matches[1]; $segment->pseudo_filters[] = $matches[2].'('.$matches[3].')'; } while (preg_match('/(.*)\:([a-z][a-z\-]+)$/i', $segment->selector, $matches)) { // pseudo selector $segment->selector = $matches[1]; $segment->pseudo_filters[] = $matches[2]; } while (preg_match('/(.*)\[([^]]+)\]$/', $segment->selector, $matches)) { // attribute selector $segment->selector = $matches[1]; $segment->attribute_filters[] = $matches[2]; } while (preg_match('/(.*)\.([^\.\#]+)$/i', $segment->selector, $matches)) { // class selector $segment->selector = $matches[1]; $segment->attribute_filters[] = 'class~="'.$matches[2].'"'; } while (preg_match('/(.*)\#([^\.\#]+)$/i', $segment->selector, $matches)) { // id selector $segment->selector = $matches[1]; $segment->attribute_filters[] = 'id="'.$matches[2].'"'; } if ($char_tmp_replaced) { // restore temporary replaced characters $set_escape_back = function (string $str) { return preg_replace_callback( '#(ESCAPED)([0-9]{1,3})#', function ($matches) { return \chr($matches[2]); }, $str ); }; $segment->selector = $set_escape_back($segment->selector); foreach ($segment->attribute_filters as &$attr_filter) { $attr_filter = $set_escape_back($attr_filter); } } return $segment; }
php
private static function getSegmentFromToken($token, $key, array $tokens) { $relation_tokens = array('>', '~', '+'); if (\in_array($token, $relation_tokens, true)) { // not a segment return false; } $segment = (object) array( 'selector' => '', 'relation_token' => false, 'attribute_filters' => array(), 'pseudo_filters' => array() ); if (isset($tokens[$key-1]) && \in_array($tokens[$key-1], $relation_tokens, true)) { // get relationship token $segment->relation_token = $tokens[$key-1]; } if (ctype_alpha($token)) { // simple element selector $segment->selector = $token; return $segment; } $char_tmp_replaced = false; if (strpos($token, '\\') !== false) { $token = preg_replace_callback( // temporary replace escaped characters '#(\\\\)(.{1})#', function ($matches) { return 'ESCAPED'. \ord($matches[2]); }, $token ); $char_tmp_replaced = true; } $segment->selector = $token; while (preg_match('/(.*)\:(not|contains|has)\((.+)\)$/i', $segment->selector, $matches)) { // pseudo selector $segment->selector = $matches[1]; $segment->pseudo_filters[] = $matches[2].'('.$matches[3].')'; } while (preg_match('/(.*)\:([a-z][a-z\-]+)$/i', $segment->selector, $matches)) { // pseudo selector $segment->selector = $matches[1]; $segment->pseudo_filters[] = $matches[2]; } while (preg_match('/(.*)\[([^]]+)\]$/', $segment->selector, $matches)) { // attribute selector $segment->selector = $matches[1]; $segment->attribute_filters[] = $matches[2]; } while (preg_match('/(.*)\.([^\.\#]+)$/i', $segment->selector, $matches)) { // class selector $segment->selector = $matches[1]; $segment->attribute_filters[] = 'class~="'.$matches[2].'"'; } while (preg_match('/(.*)\#([^\.\#]+)$/i', $segment->selector, $matches)) { // id selector $segment->selector = $matches[1]; $segment->attribute_filters[] = 'id="'.$matches[2].'"'; } if ($char_tmp_replaced) { // restore temporary replaced characters $set_escape_back = function (string $str) { return preg_replace_callback( '#(ESCAPED)([0-9]{1,3})#', function ($matches) { return \chr($matches[2]); }, $str ); }; $segment->selector = $set_escape_back($segment->selector); foreach ($segment->attribute_filters as &$attr_filter) { $attr_filter = $set_escape_back($attr_filter); } } return $segment; }
[ "private", "static", "function", "getSegmentFromToken", "(", "$", "token", ",", "$", "key", ",", "array", "$", "tokens", ")", "{", "$", "relation_tokens", "=", "array", "(", "'>'", ",", "'~'", ",", "'+'", ")", ";", "if", "(", "\\", "in_array", "(", "$", "token", ",", "$", "relation_tokens", ",", "true", ")", ")", "{", "// not a segment", "return", "false", ";", "}", "$", "segment", "=", "(", "object", ")", "array", "(", "'selector'", "=>", "''", ",", "'relation_token'", "=>", "false", ",", "'attribute_filters'", "=>", "array", "(", ")", ",", "'pseudo_filters'", "=>", "array", "(", ")", ")", ";", "if", "(", "isset", "(", "$", "tokens", "[", "$", "key", "-", "1", "]", ")", "&&", "\\", "in_array", "(", "$", "tokens", "[", "$", "key", "-", "1", "]", ",", "$", "relation_tokens", ",", "true", ")", ")", "{", "// get relationship token", "$", "segment", "->", "relation_token", "=", "$", "tokens", "[", "$", "key", "-", "1", "]", ";", "}", "if", "(", "ctype_alpha", "(", "$", "token", ")", ")", "{", "// simple element selector", "$", "segment", "->", "selector", "=", "$", "token", ";", "return", "$", "segment", ";", "}", "$", "char_tmp_replaced", "=", "false", ";", "if", "(", "strpos", "(", "$", "token", ",", "'\\\\'", ")", "!==", "false", ")", "{", "$", "token", "=", "preg_replace_callback", "(", "// temporary replace escaped characters", "'#(\\\\\\\\)(.{1})#'", ",", "function", "(", "$", "matches", ")", "{", "return", "'ESCAPED'", ".", "\\", "ord", "(", "$", "matches", "[", "2", "]", ")", ";", "}", ",", "$", "token", ")", ";", "$", "char_tmp_replaced", "=", "true", ";", "}", "$", "segment", "->", "selector", "=", "$", "token", ";", "while", "(", "preg_match", "(", "'/(.*)\\:(not|contains|has)\\((.+)\\)$/i'", ",", "$", "segment", "->", "selector", ",", "$", "matches", ")", ")", "{", "// pseudo selector", "$", "segment", "->", "selector", "=", "$", "matches", "[", "1", "]", ";", "$", "segment", "->", "pseudo_filters", "[", "]", "=", "$", "matches", "[", "2", "]", ".", "'('", ".", "$", "matches", "[", "3", "]", ".", "')'", ";", "}", "while", "(", "preg_match", "(", "'/(.*)\\:([a-z][a-z\\-]+)$/i'", ",", "$", "segment", "->", "selector", ",", "$", "matches", ")", ")", "{", "// pseudo selector", "$", "segment", "->", "selector", "=", "$", "matches", "[", "1", "]", ";", "$", "segment", "->", "pseudo_filters", "[", "]", "=", "$", "matches", "[", "2", "]", ";", "}", "while", "(", "preg_match", "(", "'/(.*)\\[([^]]+)\\]$/'", ",", "$", "segment", "->", "selector", ",", "$", "matches", ")", ")", "{", "// attribute selector", "$", "segment", "->", "selector", "=", "$", "matches", "[", "1", "]", ";", "$", "segment", "->", "attribute_filters", "[", "]", "=", "$", "matches", "[", "2", "]", ";", "}", "while", "(", "preg_match", "(", "'/(.*)\\.([^\\.\\#]+)$/i'", ",", "$", "segment", "->", "selector", ",", "$", "matches", ")", ")", "{", "// class selector", "$", "segment", "->", "selector", "=", "$", "matches", "[", "1", "]", ";", "$", "segment", "->", "attribute_filters", "[", "]", "=", "'class~=\"'", ".", "$", "matches", "[", "2", "]", ".", "'\"'", ";", "}", "while", "(", "preg_match", "(", "'/(.*)\\#([^\\.\\#]+)$/i'", ",", "$", "segment", "->", "selector", ",", "$", "matches", ")", ")", "{", "// id selector", "$", "segment", "->", "selector", "=", "$", "matches", "[", "1", "]", ";", "$", "segment", "->", "attribute_filters", "[", "]", "=", "'id=\"'", ".", "$", "matches", "[", "2", "]", ".", "'\"'", ";", "}", "if", "(", "$", "char_tmp_replaced", ")", "{", "// restore temporary replaced characters", "$", "set_escape_back", "=", "function", "(", "string", "$", "str", ")", "{", "return", "preg_replace_callback", "(", "'#(ESCAPED)([0-9]{1,3})#'", ",", "function", "(", "$", "matches", ")", "{", "return", "\\", "chr", "(", "$", "matches", "[", "2", "]", ")", ";", "}", ",", "$", "str", ")", ";", "}", ";", "$", "segment", "->", "selector", "=", "$", "set_escape_back", "(", "$", "segment", "->", "selector", ")", ";", "foreach", "(", "$", "segment", "->", "attribute_filters", "as", "&", "$", "attr_filter", ")", "{", "$", "attr_filter", "=", "$", "set_escape_back", "(", "$", "attr_filter", ")", ";", "}", "}", "return", "$", "segment", ";", "}" ]
Get segment data from token (css selector delimited by space and commas) @param string $token @param integer $key @param string[] $tokens @return object|false $segment
[ "Get", "segment", "data", "from", "token", "(", "css", "selector", "delimited", "by", "space", "and", "commas", ")" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/CssToXpath.php#L114-L196
Rct567/DomQuery
src/Rct567/DomQuery/CssToXpath.php
CssToXpath.transformCssSegments
private static function transformCssSegments(array $segments) { $new_path_tokens = array(); foreach ($segments as $num => $segment) { if ($segment->relation_token === '>') { $new_path_tokens[] = '/'; } elseif ($segment->relation_token === '~' || $segment->relation_token === '+') { $new_path_tokens[] = '/following-sibling::'; } else { $new_path_tokens[] = '//'; } if ($segment->selector !== '') { $new_path_tokens[] = $segment->selector; // specific tagname } else { $new_path_tokens[] = '*'; // any tagname } if ($segment->relation_token === '+' && isset($segments[$num-1])) { // add adjacent filter $prev_selector = implode('', self::transformCssSegments([$segments[$num-1]])); $new_path_tokens[] = '[preceding-sibling::*[1][self::'.ltrim($prev_selector, '/').']]'; } foreach (array_reverse($segment->attribute_filters) as $attr) { $new_path_tokens[] = self::transformAttrSelector($attr); } foreach (array_reverse($segment->pseudo_filters) as $attr) { $new_path_tokens[] = self::transformCssPseudoSelector($attr, $new_path_tokens); } } return $new_path_tokens; }
php
private static function transformCssSegments(array $segments) { $new_path_tokens = array(); foreach ($segments as $num => $segment) { if ($segment->relation_token === '>') { $new_path_tokens[] = '/'; } elseif ($segment->relation_token === '~' || $segment->relation_token === '+') { $new_path_tokens[] = '/following-sibling::'; } else { $new_path_tokens[] = '//'; } if ($segment->selector !== '') { $new_path_tokens[] = $segment->selector; // specific tagname } else { $new_path_tokens[] = '*'; // any tagname } if ($segment->relation_token === '+' && isset($segments[$num-1])) { // add adjacent filter $prev_selector = implode('', self::transformCssSegments([$segments[$num-1]])); $new_path_tokens[] = '[preceding-sibling::*[1][self::'.ltrim($prev_selector, '/').']]'; } foreach (array_reverse($segment->attribute_filters) as $attr) { $new_path_tokens[] = self::transformAttrSelector($attr); } foreach (array_reverse($segment->pseudo_filters) as $attr) { $new_path_tokens[] = self::transformCssPseudoSelector($attr, $new_path_tokens); } } return $new_path_tokens; }
[ "private", "static", "function", "transformCssSegments", "(", "array", "$", "segments", ")", "{", "$", "new_path_tokens", "=", "array", "(", ")", ";", "foreach", "(", "$", "segments", "as", "$", "num", "=>", "$", "segment", ")", "{", "if", "(", "$", "segment", "->", "relation_token", "===", "'>'", ")", "{", "$", "new_path_tokens", "[", "]", "=", "'/'", ";", "}", "elseif", "(", "$", "segment", "->", "relation_token", "===", "'~'", "||", "$", "segment", "->", "relation_token", "===", "'+'", ")", "{", "$", "new_path_tokens", "[", "]", "=", "'/following-sibling::'", ";", "}", "else", "{", "$", "new_path_tokens", "[", "]", "=", "'//'", ";", "}", "if", "(", "$", "segment", "->", "selector", "!==", "''", ")", "{", "$", "new_path_tokens", "[", "]", "=", "$", "segment", "->", "selector", ";", "// specific tagname", "}", "else", "{", "$", "new_path_tokens", "[", "]", "=", "'*'", ";", "// any tagname", "}", "if", "(", "$", "segment", "->", "relation_token", "===", "'+'", "&&", "isset", "(", "$", "segments", "[", "$", "num", "-", "1", "]", ")", ")", "{", "// add adjacent filter", "$", "prev_selector", "=", "implode", "(", "''", ",", "self", "::", "transformCssSegments", "(", "[", "$", "segments", "[", "$", "num", "-", "1", "]", "]", ")", ")", ";", "$", "new_path_tokens", "[", "]", "=", "'[preceding-sibling::*[1][self::'", ".", "ltrim", "(", "$", "prev_selector", ",", "'/'", ")", ".", "']]'", ";", "}", "foreach", "(", "array_reverse", "(", "$", "segment", "->", "attribute_filters", ")", "as", "$", "attr", ")", "{", "$", "new_path_tokens", "[", "]", "=", "self", "::", "transformAttrSelector", "(", "$", "attr", ")", ";", "}", "foreach", "(", "array_reverse", "(", "$", "segment", "->", "pseudo_filters", ")", "as", "$", "attr", ")", "{", "$", "new_path_tokens", "[", "]", "=", "self", "::", "transformCssPseudoSelector", "(", "$", "attr", ",", "$", "new_path_tokens", ")", ";", "}", "}", "return", "$", "new_path_tokens", ";", "}" ]
Transform css segments to xpath @param object[] $segments @return string[] $new_path_tokens
[ "Transform", "css", "segments", "to", "xpath" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/CssToXpath.php#L205-L239
Rct567/DomQuery
src/Rct567/DomQuery/CssToXpath.php
CssToXpath.transformCssPseudoSelector
private static function transformCssPseudoSelector($expression, array &$new_path_tokens) { if (preg_match('|not\((.+)\)|i', $expression, $matches)) { $parts = explode(',', $matches[1]); foreach ($parts as &$part) { $part = trim($part); $part = 'self::'.ltrim(self::transform($part), '/'); } $not_selector = implode(' or ', $parts); return '[not('.$not_selector.')]'; } elseif (preg_match('|contains\((.+)\)|i', $expression, $matches)) { return '[text()[contains(.,\''.$matches[1].'\')]]'; // contain the specified text } elseif (preg_match('|has\((.+)\)|i', $expression, $matches)) { if (strpos($matches[1], '> ') === 0) { return '[child::' . ltrim(self::transform($matches[1]), '/') .']'; } else { return '[descendant::' . ltrim(self::transform($matches[1]), '/') .']'; } } elseif ($expression === 'first' || $expression === 'last') { // new path inside selection array_unshift($new_path_tokens, '('); $new_path_tokens[] = ')'; } // static replacement $pseudo_class_selectors = array( 'disabled' => '[@disabled]', 'first-child' => '[not(preceding-sibling::*)]', 'last-child' => '[not(following-sibling::*)]', 'only-child' => '[not(preceding-sibling::*) and not(following-sibling::*)]', 'empty' => '[count(*) = 0 and string-length() = 0]', 'not-empty' => '[count(*) > 0 or string-length() > 0]', 'parent' => '[count(*) > 0]', 'header' => '[self::h1 or self::h2 or self::h3 or self::h5 or self::h5 or self::h6]', 'odd' => '[position() mod 2 = 0]', 'even' => '[position() mod 2 = 1]', 'first' => '[1]', 'last' => '[last()]', 'root' => '[not(parent::*)]' ); if (!isset($pseudo_class_selectors[$expression])) { throw new \Exception('Pseudo class '.$expression.' unknown'); } return $pseudo_class_selectors[$expression]; }
php
private static function transformCssPseudoSelector($expression, array &$new_path_tokens) { if (preg_match('|not\((.+)\)|i', $expression, $matches)) { $parts = explode(',', $matches[1]); foreach ($parts as &$part) { $part = trim($part); $part = 'self::'.ltrim(self::transform($part), '/'); } $not_selector = implode(' or ', $parts); return '[not('.$not_selector.')]'; } elseif (preg_match('|contains\((.+)\)|i', $expression, $matches)) { return '[text()[contains(.,\''.$matches[1].'\')]]'; // contain the specified text } elseif (preg_match('|has\((.+)\)|i', $expression, $matches)) { if (strpos($matches[1], '> ') === 0) { return '[child::' . ltrim(self::transform($matches[1]), '/') .']'; } else { return '[descendant::' . ltrim(self::transform($matches[1]), '/') .']'; } } elseif ($expression === 'first' || $expression === 'last') { // new path inside selection array_unshift($new_path_tokens, '('); $new_path_tokens[] = ')'; } // static replacement $pseudo_class_selectors = array( 'disabled' => '[@disabled]', 'first-child' => '[not(preceding-sibling::*)]', 'last-child' => '[not(following-sibling::*)]', 'only-child' => '[not(preceding-sibling::*) and not(following-sibling::*)]', 'empty' => '[count(*) = 0 and string-length() = 0]', 'not-empty' => '[count(*) > 0 or string-length() > 0]', 'parent' => '[count(*) > 0]', 'header' => '[self::h1 or self::h2 or self::h3 or self::h5 or self::h5 or self::h6]', 'odd' => '[position() mod 2 = 0]', 'even' => '[position() mod 2 = 1]', 'first' => '[1]', 'last' => '[last()]', 'root' => '[not(parent::*)]' ); if (!isset($pseudo_class_selectors[$expression])) { throw new \Exception('Pseudo class '.$expression.' unknown'); } return $pseudo_class_selectors[$expression]; }
[ "private", "static", "function", "transformCssPseudoSelector", "(", "$", "expression", ",", "array", "&", "$", "new_path_tokens", ")", "{", "if", "(", "preg_match", "(", "'|not\\((.+)\\)|i'", ",", "$", "expression", ",", "$", "matches", ")", ")", "{", "$", "parts", "=", "explode", "(", "','", ",", "$", "matches", "[", "1", "]", ")", ";", "foreach", "(", "$", "parts", "as", "&", "$", "part", ")", "{", "$", "part", "=", "trim", "(", "$", "part", ")", ";", "$", "part", "=", "'self::'", ".", "ltrim", "(", "self", "::", "transform", "(", "$", "part", ")", ",", "'/'", ")", ";", "}", "$", "not_selector", "=", "implode", "(", "' or '", ",", "$", "parts", ")", ";", "return", "'[not('", ".", "$", "not_selector", ".", "')]'", ";", "}", "elseif", "(", "preg_match", "(", "'|contains\\((.+)\\)|i'", ",", "$", "expression", ",", "$", "matches", ")", ")", "{", "return", "'[text()[contains(.,\\''", ".", "$", "matches", "[", "1", "]", ".", "'\\')]]'", ";", "// contain the specified text", "}", "elseif", "(", "preg_match", "(", "'|has\\((.+)\\)|i'", ",", "$", "expression", ",", "$", "matches", ")", ")", "{", "if", "(", "strpos", "(", "$", "matches", "[", "1", "]", ",", "'> '", ")", "===", "0", ")", "{", "return", "'[child::'", ".", "ltrim", "(", "self", "::", "transform", "(", "$", "matches", "[", "1", "]", ")", ",", "'/'", ")", ".", "']'", ";", "}", "else", "{", "return", "'[descendant::'", ".", "ltrim", "(", "self", "::", "transform", "(", "$", "matches", "[", "1", "]", ")", ",", "'/'", ")", ".", "']'", ";", "}", "}", "elseif", "(", "$", "expression", "===", "'first'", "||", "$", "expression", "===", "'last'", ")", "{", "// new path inside selection", "array_unshift", "(", "$", "new_path_tokens", ",", "'('", ")", ";", "$", "new_path_tokens", "[", "]", "=", "')'", ";", "}", "// static replacement", "$", "pseudo_class_selectors", "=", "array", "(", "'disabled'", "=>", "'[@disabled]'", ",", "'first-child'", "=>", "'[not(preceding-sibling::*)]'", ",", "'last-child'", "=>", "'[not(following-sibling::*)]'", ",", "'only-child'", "=>", "'[not(preceding-sibling::*) and not(following-sibling::*)]'", ",", "'empty'", "=>", "'[count(*) = 0 and string-length() = 0]'", ",", "'not-empty'", "=>", "'[count(*) > 0 or string-length() > 0]'", ",", "'parent'", "=>", "'[count(*) > 0]'", ",", "'header'", "=>", "'[self::h1 or self::h2 or self::h3 or self::h5 or self::h5 or self::h6]'", ",", "'odd'", "=>", "'[position() mod 2 = 0]'", ",", "'even'", "=>", "'[position() mod 2 = 1]'", ",", "'first'", "=>", "'[1]'", ",", "'last'", "=>", "'[last()]'", ",", "'root'", "=>", "'[not(parent::*)]'", ")", ";", "if", "(", "!", "isset", "(", "$", "pseudo_class_selectors", "[", "$", "expression", "]", ")", ")", "{", "throw", "new", "\\", "Exception", "(", "'Pseudo class '", ".", "$", "expression", ".", "' unknown'", ")", ";", "}", "return", "$", "pseudo_class_selectors", "[", "$", "expression", "]", ";", "}" ]
Transform 'css pseudo selector' expression to xpath expression @param string $expression @param string[] $new_path_tokens @return string transformed expression (xpath) @throws \Exception
[ "Transform", "css", "pseudo", "selector", "expression", "to", "xpath", "expression" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/CssToXpath.php#L250-L296
Rct567/DomQuery
src/Rct567/DomQuery/CssToXpath.php
CssToXpath.transformAttrSelector
private static function transformAttrSelector($expression) { if (preg_match('|@?([a-z0-9_]+)(([\!\*\^\$\~\|]{0,1})=)[\'"]([^\'"]+)[\'"]|i', $expression, $matches)) { if ($matches[3] === '') { // arbitrary attribute strict value equality return '[@' . strtolower($matches[1]) . "='" . $matches[4] . "']"; } elseif ($matches[3] === '!') { // arbitrary attribute negation strict value return '[@' . strtolower($matches[1]) . "!='" . $matches[4] . "']"; } elseif ($matches[3] === '~') { // arbitrary attribute value contains full word return "[contains(concat(' ', normalize-space(@" . strtolower($matches[1]) . "), ' '), ' ". $matches[4] . " ')]"; } elseif ($matches[3] === '*') { // arbitrary attribute value contains specified content return "[contains(@" . strtolower($matches[1]) . ", '" . $matches[4] . "')]"; } elseif ($matches[3] === '^') { // attribute value starts with specified content return "[starts-with(@" . strtolower($matches[1]) . ", '" . $matches[4] . "')]"; } elseif ($matches[3] === '$') { // attribute value ends with specified content return "[@".$matches[1]." and substring(@".$matches[1].", string-length(@".$matches[1].")-". (\strlen($matches[4])-1).") = '".$matches[4]."']"; } elseif ($matches[3] === '|') { // attribute has prefix selector return '[@' . strtolower($matches[1]) . "='" . $matches[4] . "' or starts-with(@". strtolower($matches[1]) . ", '" . $matches[4].'-' . "')]"; } } // attribute without value if (preg_match('|([a-z0-9_]{1})([a-z0-9_-]*)|i', $expression, $matches)) { return "[@" . $matches[1].$matches[2] . "]"; } throw new \Exception('Attribute selector is malformed or contains unsupported characters.'); }
php
private static function transformAttrSelector($expression) { if (preg_match('|@?([a-z0-9_]+)(([\!\*\^\$\~\|]{0,1})=)[\'"]([^\'"]+)[\'"]|i', $expression, $matches)) { if ($matches[3] === '') { // arbitrary attribute strict value equality return '[@' . strtolower($matches[1]) . "='" . $matches[4] . "']"; } elseif ($matches[3] === '!') { // arbitrary attribute negation strict value return '[@' . strtolower($matches[1]) . "!='" . $matches[4] . "']"; } elseif ($matches[3] === '~') { // arbitrary attribute value contains full word return "[contains(concat(' ', normalize-space(@" . strtolower($matches[1]) . "), ' '), ' ". $matches[4] . " ')]"; } elseif ($matches[3] === '*') { // arbitrary attribute value contains specified content return "[contains(@" . strtolower($matches[1]) . ", '" . $matches[4] . "')]"; } elseif ($matches[3] === '^') { // attribute value starts with specified content return "[starts-with(@" . strtolower($matches[1]) . ", '" . $matches[4] . "')]"; } elseif ($matches[3] === '$') { // attribute value ends with specified content return "[@".$matches[1]." and substring(@".$matches[1].", string-length(@".$matches[1].")-". (\strlen($matches[4])-1).") = '".$matches[4]."']"; } elseif ($matches[3] === '|') { // attribute has prefix selector return '[@' . strtolower($matches[1]) . "='" . $matches[4] . "' or starts-with(@". strtolower($matches[1]) . ", '" . $matches[4].'-' . "')]"; } } // attribute without value if (preg_match('|([a-z0-9_]{1})([a-z0-9_-]*)|i', $expression, $matches)) { return "[@" . $matches[1].$matches[2] . "]"; } throw new \Exception('Attribute selector is malformed or contains unsupported characters.'); }
[ "private", "static", "function", "transformAttrSelector", "(", "$", "expression", ")", "{", "if", "(", "preg_match", "(", "'|@?([a-z0-9_]+)(([\\!\\*\\^\\$\\~\\|]{0,1})=)[\\'\"]([^\\'\"]+)[\\'\"]|i'", ",", "$", "expression", ",", "$", "matches", ")", ")", "{", "if", "(", "$", "matches", "[", "3", "]", "===", "''", ")", "{", "// arbitrary attribute strict value equality", "return", "'[@'", ".", "strtolower", "(", "$", "matches", "[", "1", "]", ")", ".", "\"='\"", ".", "$", "matches", "[", "4", "]", ".", "\"']\"", ";", "}", "elseif", "(", "$", "matches", "[", "3", "]", "===", "'!'", ")", "{", "// arbitrary attribute negation strict value", "return", "'[@'", ".", "strtolower", "(", "$", "matches", "[", "1", "]", ")", ".", "\"!='\"", ".", "$", "matches", "[", "4", "]", ".", "\"']\"", ";", "}", "elseif", "(", "$", "matches", "[", "3", "]", "===", "'~'", ")", "{", "// arbitrary attribute value contains full word", "return", "\"[contains(concat(' ', normalize-space(@\"", ".", "strtolower", "(", "$", "matches", "[", "1", "]", ")", ".", "\"), ' '), ' \"", ".", "$", "matches", "[", "4", "]", ".", "\" ')]\"", ";", "}", "elseif", "(", "$", "matches", "[", "3", "]", "===", "'*'", ")", "{", "// arbitrary attribute value contains specified content", "return", "\"[contains(@\"", ".", "strtolower", "(", "$", "matches", "[", "1", "]", ")", ".", "\", '\"", ".", "$", "matches", "[", "4", "]", ".", "\"')]\"", ";", "}", "elseif", "(", "$", "matches", "[", "3", "]", "===", "'^'", ")", "{", "// attribute value starts with specified content", "return", "\"[starts-with(@\"", ".", "strtolower", "(", "$", "matches", "[", "1", "]", ")", ".", "\", '\"", ".", "$", "matches", "[", "4", "]", ".", "\"')]\"", ";", "}", "elseif", "(", "$", "matches", "[", "3", "]", "===", "'$'", ")", "{", "// attribute value ends with specified content", "return", "\"[@\"", ".", "$", "matches", "[", "1", "]", ".", "\" and substring(@\"", ".", "$", "matches", "[", "1", "]", ".", "\", string-length(@\"", ".", "$", "matches", "[", "1", "]", ".", "\")-\"", ".", "(", "\\", "strlen", "(", "$", "matches", "[", "4", "]", ")", "-", "1", ")", ".", "\") = '\"", ".", "$", "matches", "[", "4", "]", ".", "\"']\"", ";", "}", "elseif", "(", "$", "matches", "[", "3", "]", "===", "'|'", ")", "{", "// attribute has prefix selector", "return", "'[@'", ".", "strtolower", "(", "$", "matches", "[", "1", "]", ")", ".", "\"='\"", ".", "$", "matches", "[", "4", "]", ".", "\"' or starts-with(@\"", ".", "strtolower", "(", "$", "matches", "[", "1", "]", ")", ".", "\", '\"", ".", "$", "matches", "[", "4", "]", ".", "'-'", ".", "\"')]\"", ";", "}", "}", "// attribute without value", "if", "(", "preg_match", "(", "'|([a-z0-9_]{1})([a-z0-9_-]*)|i'", ",", "$", "expression", ",", "$", "matches", ")", ")", "{", "return", "\"[@\"", ".", "$", "matches", "[", "1", "]", ".", "$", "matches", "[", "2", "]", ".", "\"]\"", ";", "}", "throw", "new", "\\", "Exception", "(", "'Attribute selector is malformed or contains unsupported characters.'", ")", ";", "}" ]
Transform 'css attribute selector' expression to xpath expression @param string $expression @return string transformed expression (xpath)
[ "Transform", "css", "attribute", "selector", "expression", "to", "xpath", "expression" ]
train
https://github.com/Rct567/DomQuery/blob/40ff02b4337e704c5816a6ecae11182dd705f136/src/Rct567/DomQuery/CssToXpath.php#L305-L333
laravel/spark-installer
src/Installation/RunSparkInstall.php
RunSparkInstall.install
public function install() { $process = new Process($this->command(), $this->command->path); if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) { $process->setTty(true); } $process->run(function ($type, $line) { $this->command->output->write($line); }); }
php
public function install() { $process = new Process($this->command(), $this->command->path); if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) { $process->setTty(true); } $process->run(function ($type, $line) { $this->command->output->write($line); }); }
[ "public", "function", "install", "(", ")", "{", "$", "process", "=", "new", "Process", "(", "$", "this", "->", "command", "(", ")", ",", "$", "this", "->", "command", "->", "path", ")", ";", "if", "(", "'\\\\'", "!==", "DIRECTORY_SEPARATOR", "&&", "file_exists", "(", "'/dev/tty'", ")", "&&", "is_readable", "(", "'/dev/tty'", ")", ")", "{", "$", "process", "->", "setTty", "(", "true", ")", ";", "}", "$", "process", "->", "run", "(", "function", "(", "$", "type", ",", "$", "line", ")", "{", "$", "this", "->", "command", "->", "output", "->", "write", "(", "$", "line", ")", ";", "}", ")", ";", "}" ]
Run the installation helper. @return void
[ "Run", "the", "installation", "helper", "." ]
train
https://github.com/laravel/spark-installer/blob/2aa18947b9d8cdcf83aaaa273bc7b76426404507/src/Installation/RunSparkInstall.php#L28-L39
laravel/spark-installer
src/Installation/RunSparkInstall.php
RunSparkInstall.command
protected function command() { $command = 'php artisan spark:install --force'; if ($this->command->input->getOption('braintree')) { $command .= ' --braintree'; } if ($this->command->input->getOption('team-billing')) { $command .= ' --team-billing'; } return $command; }
php
protected function command() { $command = 'php artisan spark:install --force'; if ($this->command->input->getOption('braintree')) { $command .= ' --braintree'; } if ($this->command->input->getOption('team-billing')) { $command .= ' --team-billing'; } return $command; }
[ "protected", "function", "command", "(", ")", "{", "$", "command", "=", "'php artisan spark:install --force'", ";", "if", "(", "$", "this", "->", "command", "->", "input", "->", "getOption", "(", "'braintree'", ")", ")", "{", "$", "command", ".=", "' --braintree'", ";", "}", "if", "(", "$", "this", "->", "command", "->", "input", "->", "getOption", "(", "'team-billing'", ")", ")", "{", "$", "command", ".=", "' --team-billing'", ";", "}", "return", "$", "command", ";", "}" ]
Get the proper Spark installation command. @return string
[ "Get", "the", "proper", "Spark", "installation", "command", "." ]
train
https://github.com/laravel/spark-installer/blob/2aa18947b9d8cdcf83aaaa273bc7b76426404507/src/Installation/RunSparkInstall.php#L46-L59
laravel/spark-installer
src/NewCommand.php
NewCommand.execute
public function execute(InputInterface $input, OutputInterface $output) { $this->input = $input; $this->output = new SymfonyStyle($input, $output); $this->path = getcwd().'/'.$input->getArgument('name'); $installers = [ Installation\CreateLaravelProject::class, Installation\DownloadSpark::class, Installation\UpdateComposerFile::class, Installation\ComposerUpdate::class, Installation\AddCoreProviderToConfiguration::class, Installation\RunSparkInstall::class, Installation\AddAppProviderToConfiguration::class, Installation\RunNpmInstall::class, Installation\CompileAssets::class, ]; foreach ($installers as $installer) { (new $installer($this, $input->getArgument('name')))->install(); } }
php
public function execute(InputInterface $input, OutputInterface $output) { $this->input = $input; $this->output = new SymfonyStyle($input, $output); $this->path = getcwd().'/'.$input->getArgument('name'); $installers = [ Installation\CreateLaravelProject::class, Installation\DownloadSpark::class, Installation\UpdateComposerFile::class, Installation\ComposerUpdate::class, Installation\AddCoreProviderToConfiguration::class, Installation\RunSparkInstall::class, Installation\AddAppProviderToConfiguration::class, Installation\RunNpmInstall::class, Installation\CompileAssets::class, ]; foreach ($installers as $installer) { (new $installer($this, $input->getArgument('name')))->install(); } }
[ "public", "function", "execute", "(", "InputInterface", "$", "input", ",", "OutputInterface", "$", "output", ")", "{", "$", "this", "->", "input", "=", "$", "input", ";", "$", "this", "->", "output", "=", "new", "SymfonyStyle", "(", "$", "input", ",", "$", "output", ")", ";", "$", "this", "->", "path", "=", "getcwd", "(", ")", ".", "'/'", ".", "$", "input", "->", "getArgument", "(", "'name'", ")", ";", "$", "installers", "=", "[", "Installation", "\\", "CreateLaravelProject", "::", "class", ",", "Installation", "\\", "DownloadSpark", "::", "class", ",", "Installation", "\\", "UpdateComposerFile", "::", "class", ",", "Installation", "\\", "ComposerUpdate", "::", "class", ",", "Installation", "\\", "AddCoreProviderToConfiguration", "::", "class", ",", "Installation", "\\", "RunSparkInstall", "::", "class", ",", "Installation", "\\", "AddAppProviderToConfiguration", "::", "class", ",", "Installation", "\\", "RunNpmInstall", "::", "class", ",", "Installation", "\\", "CompileAssets", "::", "class", ",", "]", ";", "foreach", "(", "$", "installers", "as", "$", "installer", ")", "{", "(", "new", "$", "installer", "(", "$", "this", ",", "$", "input", "->", "getArgument", "(", "'name'", ")", ")", ")", "->", "install", "(", ")", ";", "}", "}" ]
Execute the command. @param InputInterface $input @param OutputInterface $output @return void
[ "Execute", "the", "command", "." ]
train
https://github.com/laravel/spark-installer/blob/2aa18947b9d8cdcf83aaaa273bc7b76426404507/src/NewCommand.php#L57-L79
laravel/spark-installer
src/Installation/DownloadSpark.php
DownloadSpark.install
public function install() { $this->extractZip($this->downloadZip()); rename($this->sparkPath(), $this->command->path.'/spark'); (new Filesystem)->deleteDirectory( $this->command->path.'/spark-new' ); }
php
public function install() { $this->extractZip($this->downloadZip()); rename($this->sparkPath(), $this->command->path.'/spark'); (new Filesystem)->deleteDirectory( $this->command->path.'/spark-new' ); }
[ "public", "function", "install", "(", ")", "{", "$", "this", "->", "extractZip", "(", "$", "this", "->", "downloadZip", "(", ")", ")", ";", "rename", "(", "$", "this", "->", "sparkPath", "(", ")", ",", "$", "this", "->", "command", "->", "path", ".", "'/spark'", ")", ";", "(", "new", "Filesystem", ")", "->", "deleteDirectory", "(", "$", "this", "->", "command", "->", "path", ".", "'/spark-new'", ")", ";", "}" ]
Run the installation helper. @return void
[ "Run", "the", "installation", "helper", "." ]
train
https://github.com/laravel/spark-installer/blob/2aa18947b9d8cdcf83aaaa273bc7b76426404507/src/Installation/DownloadSpark.php#L36-L45
laravel/spark-installer
src/Installation/DownloadSpark.php
DownloadSpark.downloadZip
protected function downloadZip() { $this->command->output->writeln( '<info>Downloading Spark...</info>' ); file_put_contents( $zipPath = $this->command->path.'/spark-archive.zip', $this->zipResponse() ); return $zipPath; }
php
protected function downloadZip() { $this->command->output->writeln( '<info>Downloading Spark...</info>' ); file_put_contents( $zipPath = $this->command->path.'/spark-archive.zip', $this->zipResponse() ); return $zipPath; }
[ "protected", "function", "downloadZip", "(", ")", "{", "$", "this", "->", "command", "->", "output", "->", "writeln", "(", "'<info>Downloading Spark...</info>'", ")", ";", "file_put_contents", "(", "$", "zipPath", "=", "$", "this", "->", "command", "->", "path", ".", "'/spark-archive.zip'", ",", "$", "this", "->", "zipResponse", "(", ")", ")", ";", "return", "$", "zipPath", ";", "}" ]
Download the latest Spark release. @return void
[ "Download", "the", "latest", "Spark", "release", "." ]
train
https://github.com/laravel/spark-installer/blob/2aa18947b9d8cdcf83aaaa273bc7b76426404507/src/Installation/DownloadSpark.php#L52-L63
laravel/spark-installer
src/Installation/DownloadSpark.php
DownloadSpark.zipResponse
protected function zipResponse() { $release = $this->latestSparkRelease(); try { return (string) (new HttpClient)->get( $this->sparkUrl.'/api/releases/'.$release.'/download?api_token='.$this->readToken(), [ 'headers' => [ 'X-Requested-With' => 'XMLHttpRequest', ], 'verify' => __DIR__.'/../cacert.pem', ] )->getBody(); } catch (ClientException $e) { if ($e->getResponse()->getStatusCode() === 401) { $this->invalidLicense($release); } throw $e; } }
php
protected function zipResponse() { $release = $this->latestSparkRelease(); try { return (string) (new HttpClient)->get( $this->sparkUrl.'/api/releases/'.$release.'/download?api_token='.$this->readToken(), [ 'headers' => [ 'X-Requested-With' => 'XMLHttpRequest', ], 'verify' => __DIR__.'/../cacert.pem', ] )->getBody(); } catch (ClientException $e) { if ($e->getResponse()->getStatusCode() === 401) { $this->invalidLicense($release); } throw $e; } }
[ "protected", "function", "zipResponse", "(", ")", "{", "$", "release", "=", "$", "this", "->", "latestSparkRelease", "(", ")", ";", "try", "{", "return", "(", "string", ")", "(", "new", "HttpClient", ")", "->", "get", "(", "$", "this", "->", "sparkUrl", ".", "'/api/releases/'", ".", "$", "release", ".", "'/download?api_token='", ".", "$", "this", "->", "readToken", "(", ")", ",", "[", "'headers'", "=>", "[", "'X-Requested-With'", "=>", "'XMLHttpRequest'", ",", "]", ",", "'verify'", "=>", "__DIR__", ".", "'/../cacert.pem'", ",", "]", ")", "->", "getBody", "(", ")", ";", "}", "catch", "(", "ClientException", "$", "e", ")", "{", "if", "(", "$", "e", "->", "getResponse", "(", ")", "->", "getStatusCode", "(", ")", "===", "401", ")", "{", "$", "this", "->", "invalidLicense", "(", "$", "release", ")", ";", "}", "throw", "$", "e", ";", "}", "}" ]
Get the raw Zip response for a Spark download. @return string
[ "Get", "the", "raw", "Zip", "response", "for", "a", "Spark", "download", "." ]
train
https://github.com/laravel/spark-installer/blob/2aa18947b9d8cdcf83aaaa273bc7b76426404507/src/Installation/DownloadSpark.php#L70-L91
laravel/spark-installer
src/Installation/DownloadSpark.php
DownloadSpark.extractZip
protected function extractZip($zipPath) { $archive = new ZipArchive; $archive->open($zipPath); $archive->extractTo($this->command->path.'/spark-new'); $archive->close(); @unlink($zipPath); }
php
protected function extractZip($zipPath) { $archive = new ZipArchive; $archive->open($zipPath); $archive->extractTo($this->command->path.'/spark-new'); $archive->close(); @unlink($zipPath); }
[ "protected", "function", "extractZip", "(", "$", "zipPath", ")", "{", "$", "archive", "=", "new", "ZipArchive", ";", "$", "archive", "->", "open", "(", "$", "zipPath", ")", ";", "$", "archive", "->", "extractTo", "(", "$", "this", "->", "command", "->", "path", ".", "'/spark-new'", ")", ";", "$", "archive", "->", "close", "(", ")", ";", "@", "unlink", "(", "$", "zipPath", ")", ";", "}" ]
Extract the Spark Zip archive. @param string $zipPath @return void
[ "Extract", "the", "Spark", "Zip", "archive", "." ]
train
https://github.com/laravel/spark-installer/blob/2aa18947b9d8cdcf83aaaa273bc7b76426404507/src/Installation/DownloadSpark.php#L99-L110
laravel/spark-installer
src/Installation/DownloadSpark.php
DownloadSpark.sparkPath
protected function sparkPath() { return $this->command->path.'/spark-new/'.basename( (new Filesystem)->directories($this->command->path.'/spark-new')[0] ); }
php
protected function sparkPath() { return $this->command->path.'/spark-new/'.basename( (new Filesystem)->directories($this->command->path.'/spark-new')[0] ); }
[ "protected", "function", "sparkPath", "(", ")", "{", "return", "$", "this", "->", "command", "->", "path", ".", "'/spark-new/'", ".", "basename", "(", "(", "new", "Filesystem", ")", "->", "directories", "(", "$", "this", "->", "command", "->", "path", ".", "'/spark-new'", ")", "[", "0", "]", ")", ";", "}" ]
Get the release directory. @return string
[ "Get", "the", "release", "directory", "." ]
train
https://github.com/laravel/spark-installer/blob/2aa18947b9d8cdcf83aaaa273bc7b76426404507/src/Installation/DownloadSpark.php#L117-L122
laravel/spark-installer
src/InteractsWithSparkConfiguration.php
InteractsWithSparkConfiguration.homePath
protected function homePath() { if (! empty($_SERVER['HOME'])) { return $_SERVER['HOME']; } elseif (! empty($_SERVER['HOMEDRIVE']) && ! empty($_SERVER['HOMEPATH'])) { return $_SERVER['HOMEDRIVE'].$_SERVER['HOMEPATH']; } else { throw new Exception('Cannot determine home directory.'); } }
php
protected function homePath() { if (! empty($_SERVER['HOME'])) { return $_SERVER['HOME']; } elseif (! empty($_SERVER['HOMEDRIVE']) && ! empty($_SERVER['HOMEPATH'])) { return $_SERVER['HOMEDRIVE'].$_SERVER['HOMEPATH']; } else { throw new Exception('Cannot determine home directory.'); } }
[ "protected", "function", "homePath", "(", ")", "{", "if", "(", "!", "empty", "(", "$", "_SERVER", "[", "'HOME'", "]", ")", ")", "{", "return", "$", "_SERVER", "[", "'HOME'", "]", ";", "}", "elseif", "(", "!", "empty", "(", "$", "_SERVER", "[", "'HOMEDRIVE'", "]", ")", "&&", "!", "empty", "(", "$", "_SERVER", "[", "'HOMEPATH'", "]", ")", ")", "{", "return", "$", "_SERVER", "[", "'HOMEDRIVE'", "]", ".", "$", "_SERVER", "[", "'HOMEPATH'", "]", ";", "}", "else", "{", "throw", "new", "Exception", "(", "'Cannot determine home directory.'", ")", ";", "}", "}" ]
Get the User's home path. @return string @throws Exception
[ "Get", "the", "User", "s", "home", "path", "." ]
train
https://github.com/laravel/spark-installer/blob/2aa18947b9d8cdcf83aaaa273bc7b76426404507/src/InteractsWithSparkConfiguration.php#L65-L74
laravel/spark-installer
src/RegisterCommand.php
RegisterCommand.execute
public function execute(InputInterface $input, OutputInterface $output) { if (! $this->valid($input->getArgument('token'))) { return $this->tokenIsInvalid($output); } if (! $this->configExists()) { mkdir($this->homePath().'/.spark'); } $this->storeToken($input->getArgument('token')); $this->tokenIsValid($output); }
php
public function execute(InputInterface $input, OutputInterface $output) { if (! $this->valid($input->getArgument('token'))) { return $this->tokenIsInvalid($output); } if (! $this->configExists()) { mkdir($this->homePath().'/.spark'); } $this->storeToken($input->getArgument('token')); $this->tokenIsValid($output); }
[ "public", "function", "execute", "(", "InputInterface", "$", "input", ",", "OutputInterface", "$", "output", ")", "{", "if", "(", "!", "$", "this", "->", "valid", "(", "$", "input", "->", "getArgument", "(", "'token'", ")", ")", ")", "{", "return", "$", "this", "->", "tokenIsInvalid", "(", "$", "output", ")", ";", "}", "if", "(", "!", "$", "this", "->", "configExists", "(", ")", ")", "{", "mkdir", "(", "$", "this", "->", "homePath", "(", ")", ".", "'/.spark'", ")", ";", "}", "$", "this", "->", "storeToken", "(", "$", "input", "->", "getArgument", "(", "'token'", ")", ")", ";", "$", "this", "->", "tokenIsValid", "(", "$", "output", ")", ";", "}" ]
Execute the command. @param InputInterface $input @param OutputInterface $output @return void
[ "Execute", "the", "command", "." ]
train
https://github.com/laravel/spark-installer/blob/2aa18947b9d8cdcf83aaaa273bc7b76426404507/src/RegisterCommand.php#L37-L50
laravel/spark-installer
src/RegisterCommand.php
RegisterCommand.valid
protected function valid($token) { try { (new HttpClient)->get( $this->sparkUrl.'/api/token/'.$token.'/validate', ['verify' => __DIR__.'/cacert.pem'] ); return true; } catch (Exception $e) { var_dump($e->getMessage()); return false; } }
php
protected function valid($token) { try { (new HttpClient)->get( $this->sparkUrl.'/api/token/'.$token.'/validate', ['verify' => __DIR__.'/cacert.pem'] ); return true; } catch (Exception $e) { var_dump($e->getMessage()); return false; } }
[ "protected", "function", "valid", "(", "$", "token", ")", "{", "try", "{", "(", "new", "HttpClient", ")", "->", "get", "(", "$", "this", "->", "sparkUrl", ".", "'/api/token/'", ".", "$", "token", ".", "'/validate'", ",", "[", "'verify'", "=>", "__DIR__", ".", "'/cacert.pem'", "]", ")", ";", "return", "true", ";", "}", "catch", "(", "Exception", "$", "e", ")", "{", "var_dump", "(", "$", "e", "->", "getMessage", "(", ")", ")", ";", "return", "false", ";", "}", "}" ]
Determine if the given token is valid. @param string $token @return bool
[ "Determine", "if", "the", "given", "token", "is", "valid", "." ]
train
https://github.com/laravel/spark-installer/blob/2aa18947b9d8cdcf83aaaa273bc7b76426404507/src/RegisterCommand.php#L58-L72
laravel/spark-installer
src/Installation/UpdateComposerFile.php
UpdateComposerFile.install
public function install() { $composer = $this->getComposerConfiguration(); // Next we will add the Spark and Cashier dependencies to the Composer array as // well as add the Spark "repository" to the configuration so Composer knows // where Spark is located. Spark will get installed using the path option. $composer = $this->addRepository( $this->addSparkDependency( $this->addCashierDependency($composer) ) ); $this->writeComposerFile($composer); }
php
public function install() { $composer = $this->getComposerConfiguration(); // Next we will add the Spark and Cashier dependencies to the Composer array as // well as add the Spark "repository" to the configuration so Composer knows // where Spark is located. Spark will get installed using the path option. $composer = $this->addRepository( $this->addSparkDependency( $this->addCashierDependency($composer) ) ); $this->writeComposerFile($composer); }
[ "public", "function", "install", "(", ")", "{", "$", "composer", "=", "$", "this", "->", "getComposerConfiguration", "(", ")", ";", "// Next we will add the Spark and Cashier dependencies to the Composer array as", "// well as add the Spark \"repository\" to the configuration so Composer knows", "// where Spark is located. Spark will get installed using the path option.", "$", "composer", "=", "$", "this", "->", "addRepository", "(", "$", "this", "->", "addSparkDependency", "(", "$", "this", "->", "addCashierDependency", "(", "$", "composer", ")", ")", ")", ";", "$", "this", "->", "writeComposerFile", "(", "$", "composer", ")", ";", "}" ]
Run the installation helper. @return void
[ "Run", "the", "installation", "helper", "." ]
train
https://github.com/laravel/spark-installer/blob/2aa18947b9d8cdcf83aaaa273bc7b76426404507/src/Installation/UpdateComposerFile.php#L30-L44
laravel/spark-installer
src/Installation/UpdateComposerFile.php
UpdateComposerFile.writeComposerFile
protected function writeComposerFile($composer) { file_put_contents( $this->command->path.'/composer.json', json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) ); }
php
protected function writeComposerFile($composer) { file_put_contents( $this->command->path.'/composer.json', json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) ); }
[ "protected", "function", "writeComposerFile", "(", "$", "composer", ")", "{", "file_put_contents", "(", "$", "this", "->", "command", "->", "path", ".", "'/composer.json'", ",", "json_encode", "(", "$", "composer", ",", "JSON_PRETTY_PRINT", "|", "JSON_UNESCAPED_SLASHES", ")", ")", ";", "}" ]
Write the given Composer configuration back to disk. @param array $composer @return void
[ "Write", "the", "given", "Composer", "configuration", "back", "to", "disk", "." ]
train
https://github.com/laravel/spark-installer/blob/2aa18947b9d8cdcf83aaaa273bc7b76426404507/src/Installation/UpdateComposerFile.php#L110-L116
laravel/spark-installer
src/Installation/AddCoreProviderToConfiguration.php
AddCoreProviderToConfiguration.install
public function install() { $path = $this->command->path.'/config/app.php'; $contents = file_get_contents($path); $contents = str_replace( ' App\\Providers\\AppServiceProvider::class,', " Laravel\Spark\Providers\SparkServiceProvider::class,\n App\Providers\AppServiceProvider::class,", $contents ); file_put_contents($path, $contents); }
php
public function install() { $path = $this->command->path.'/config/app.php'; $contents = file_get_contents($path); $contents = str_replace( ' App\\Providers\\AppServiceProvider::class,', " Laravel\Spark\Providers\SparkServiceProvider::class,\n App\Providers\AppServiceProvider::class,", $contents ); file_put_contents($path, $contents); }
[ "public", "function", "install", "(", ")", "{", "$", "path", "=", "$", "this", "->", "command", "->", "path", ".", "'/config/app.php'", ";", "$", "contents", "=", "file_get_contents", "(", "$", "path", ")", ";", "$", "contents", "=", "str_replace", "(", "' App\\\\Providers\\\\AppServiceProvider::class,'", ",", "\" Laravel\\Spark\\Providers\\SparkServiceProvider::class,\\n App\\Providers\\AppServiceProvider::class,\"", ",", "$", "contents", ")", ";", "file_put_contents", "(", "$", "path", ",", "$", "contents", ")", ";", "}" ]
Run the installation helper. @return void
[ "Run", "the", "installation", "helper", "." ]
train
https://github.com/laravel/spark-installer/blob/2aa18947b9d8cdcf83aaaa273bc7b76426404507/src/Installation/AddCoreProviderToConfiguration.php#L27-L40
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.set_user_agent
private function set_user_agent() { if (!empty($this->config['user_agent'])) return; $ssl = ($this->config['curl_ssl_verifyhost'] && $this->config['curl_ssl_verifypeer'] && $this->config['use_ssl']) ? '+' : '-'; $ua = 'tmhOAuth ' . self::VERSION . $ssl . 'SSL - //github.com/themattharris/tmhOAuth'; $this->config['user_agent'] = $ua; }
php
private function set_user_agent() { if (!empty($this->config['user_agent'])) return; $ssl = ($this->config['curl_ssl_verifyhost'] && $this->config['curl_ssl_verifypeer'] && $this->config['use_ssl']) ? '+' : '-'; $ua = 'tmhOAuth ' . self::VERSION . $ssl . 'SSL - //github.com/themattharris/tmhOAuth'; $this->config['user_agent'] = $ua; }
[ "private", "function", "set_user_agent", "(", ")", "{", "if", "(", "!", "empty", "(", "$", "this", "->", "config", "[", "'user_agent'", "]", ")", ")", "return", ";", "$", "ssl", "=", "(", "$", "this", "->", "config", "[", "'curl_ssl_verifyhost'", "]", "&&", "$", "this", "->", "config", "[", "'curl_ssl_verifypeer'", "]", "&&", "$", "this", "->", "config", "[", "'use_ssl'", "]", ")", "?", "'+'", ":", "'-'", ";", "$", "ua", "=", "'tmhOAuth '", ".", "self", "::", "VERSION", ".", "$", "ssl", ".", "'SSL - //github.com/themattharris/tmhOAuth'", ";", "$", "this", "->", "config", "[", "'user_agent'", "]", "=", "$", "ua", ";", "}" ]
Sets the useragent for PHP to use If '$this->config['user_agent']' already has a value it is used instead of one being generated. @return void value is stored to the config array class variable
[ "Sets", "the", "useragent", "for", "PHP", "to", "use", "If", "$this", "-", ">", "config", "[", "user_agent", "]", "already", "has", "a", "value", "it", "is", "used", "instead", "of", "one", "being", "generated", "." ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L119-L126
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.nonce
private function nonce($length=12, $include_time=true) { if ($this->config['force_nonce'] === false) { $prefix = $include_time ? microtime() : ''; return md5(substr($prefix . uniqid(), 0, $length)); } else { return $this->config['force_nonce']; } }
php
private function nonce($length=12, $include_time=true) { if ($this->config['force_nonce'] === false) { $prefix = $include_time ? microtime() : ''; return md5(substr($prefix . uniqid(), 0, $length)); } else { return $this->config['force_nonce']; } }
[ "private", "function", "nonce", "(", "$", "length", "=", "12", ",", "$", "include_time", "=", "true", ")", "{", "if", "(", "$", "this", "->", "config", "[", "'force_nonce'", "]", "===", "false", ")", "{", "$", "prefix", "=", "$", "include_time", "?", "microtime", "(", ")", ":", "''", ";", "return", "md5", "(", "substr", "(", "$", "prefix", ".", "uniqid", "(", ")", ",", "0", ",", "$", "length", ")", ")", ";", "}", "else", "{", "return", "$", "this", "->", "config", "[", "'force_nonce'", "]", ";", "}", "}" ]
Generates a random OAuth nonce. If 'force_nonce' is false a nonce will be generated, otherwise the value of '$this->config['force_nonce']' will be used. @param string $length how many characters the nonce should be before MD5 hashing. default 12 @param string $include_time whether to include time at the beginning of the nonce. default true @return $nonce as a string
[ "Generates", "a", "random", "OAuth", "nonce", ".", "If", "force_nonce", "is", "false", "a", "nonce", "will", "be", "generated", "otherwise", "the", "value", "of", "$this", "-", ">", "config", "[", "force_nonce", "]", "will", "be", "used", "." ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L136-L143
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.timestamp
private function timestamp() { if ($this->config['force_timestamp'] === false) { $time = time(); } else { $time = $this->config['force_timestamp']; } return (string) $time; }
php
private function timestamp() { if ($this->config['force_timestamp'] === false) { $time = time(); } else { $time = $this->config['force_timestamp']; } return (string) $time; }
[ "private", "function", "timestamp", "(", ")", "{", "if", "(", "$", "this", "->", "config", "[", "'force_timestamp'", "]", "===", "false", ")", "{", "$", "time", "=", "time", "(", ")", ";", "}", "else", "{", "$", "time", "=", "$", "this", "->", "config", "[", "'force_timestamp'", "]", ";", "}", "return", "(", "string", ")", "$", "time", ";", "}" ]
Generates a timestamp. If 'force_timestamp' is false a timestamp will be generated, otherwise the value of '$this->config['force_timestamp']' will be used. @return $time as a string
[ "Generates", "a", "timestamp", ".", "If", "force_timestamp", "is", "false", "a", "timestamp", "will", "be", "generated", "otherwise", "the", "value", "of", "$this", "-", ">", "config", "[", "force_timestamp", "]", "will", "be", "used", "." ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L151-L158
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.safe_encode
private function safe_encode($data) { if (is_array($data)) { return array_map(array($this, 'safe_encode'), $data); } else if (is_scalar($data)) { return str_ireplace( array('+', '%7E'), array(' ', '~'), rawurlencode($data) ); } else { return ''; } }
php
private function safe_encode($data) { if (is_array($data)) { return array_map(array($this, 'safe_encode'), $data); } else if (is_scalar($data)) { return str_ireplace( array('+', '%7E'), array(' ', '~'), rawurlencode($data) ); } else { return ''; } }
[ "private", "function", "safe_encode", "(", "$", "data", ")", "{", "if", "(", "is_array", "(", "$", "data", ")", ")", "{", "return", "array_map", "(", "array", "(", "$", "this", ",", "'safe_encode'", ")", ",", "$", "data", ")", ";", "}", "else", "if", "(", "is_scalar", "(", "$", "data", ")", ")", "{", "return", "str_ireplace", "(", "array", "(", "'+'", ",", "'%7E'", ")", ",", "array", "(", "' '", ",", "'~'", ")", ",", "rawurlencode", "(", "$", "data", ")", ")", ";", "}", "else", "{", "return", "''", ";", "}", "}" ]
Encodes the string or array passed in a way compatible with OAuth. If an array is passed each array value will will be encoded. @param mixed $data the scalar or array to encode @return $data encoded in a way compatible with OAuth
[ "Encodes", "the", "string", "or", "array", "passed", "in", "a", "way", "compatible", "with", "OAuth", ".", "If", "an", "array", "is", "passed", "each", "array", "value", "will", "will", "be", "encoded", "." ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L167-L179
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.safe_decode
private function safe_decode($data) { if (is_array($data)) { return array_map(array($this, 'safe_decode'), $data); } else if (is_scalar($data)) { return rawurldecode($data); } else { return ''; } }
php
private function safe_decode($data) { if (is_array($data)) { return array_map(array($this, 'safe_decode'), $data); } else if (is_scalar($data)) { return rawurldecode($data); } else { return ''; } }
[ "private", "function", "safe_decode", "(", "$", "data", ")", "{", "if", "(", "is_array", "(", "$", "data", ")", ")", "{", "return", "array_map", "(", "array", "(", "$", "this", ",", "'safe_decode'", ")", ",", "$", "data", ")", ";", "}", "else", "if", "(", "is_scalar", "(", "$", "data", ")", ")", "{", "return", "rawurldecode", "(", "$", "data", ")", ";", "}", "else", "{", "return", "''", ";", "}", "}" ]
Decodes the string or array from it's URL encoded form If an array is passed each array value will will be decoded. @param mixed $data the scalar or array to decode @return string $data decoded from the URL encoded form
[ "Decodes", "the", "string", "or", "array", "from", "it", "s", "URL", "encoded", "form", "If", "an", "array", "is", "passed", "each", "array", "value", "will", "will", "be", "decoded", "." ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L188-L196
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.prepare_oauth1_params
private function prepare_oauth1_params() { $defaults = array( 'oauth_nonce' => $this->nonce(), 'oauth_timestamp' => $this->timestamp(), 'oauth_version' => $this->config['oauth_version'], 'oauth_consumer_key' => $this->config['consumer_key'], 'oauth_signature_method' => $this->config['oauth_signature_method'], ); // include the user token if it exists if ( $oauth_token = $this->token() ) $defaults['oauth_token'] = $oauth_token; $this->request_settings['oauth1_params'] = array(); // safely encode foreach ($defaults as $k => $v) { $this->request_settings['oauth1_params'][$this->safe_encode($k)] = $this->safe_encode($v); } }
php
private function prepare_oauth1_params() { $defaults = array( 'oauth_nonce' => $this->nonce(), 'oauth_timestamp' => $this->timestamp(), 'oauth_version' => $this->config['oauth_version'], 'oauth_consumer_key' => $this->config['consumer_key'], 'oauth_signature_method' => $this->config['oauth_signature_method'], ); // include the user token if it exists if ( $oauth_token = $this->token() ) $defaults['oauth_token'] = $oauth_token; $this->request_settings['oauth1_params'] = array(); // safely encode foreach ($defaults as $k => $v) { $this->request_settings['oauth1_params'][$this->safe_encode($k)] = $this->safe_encode($v); } }
[ "private", "function", "prepare_oauth1_params", "(", ")", "{", "$", "defaults", "=", "array", "(", "'oauth_nonce'", "=>", "$", "this", "->", "nonce", "(", ")", ",", "'oauth_timestamp'", "=>", "$", "this", "->", "timestamp", "(", ")", ",", "'oauth_version'", "=>", "$", "this", "->", "config", "[", "'oauth_version'", "]", ",", "'oauth_consumer_key'", "=>", "$", "this", "->", "config", "[", "'consumer_key'", "]", ",", "'oauth_signature_method'", "=>", "$", "this", "->", "config", "[", "'oauth_signature_method'", "]", ",", ")", ";", "// include the user token if it exists", "if", "(", "$", "oauth_token", "=", "$", "this", "->", "token", "(", ")", ")", "$", "defaults", "[", "'oauth_token'", "]", "=", "$", "oauth_token", ";", "$", "this", "->", "request_settings", "[", "'oauth1_params'", "]", "=", "array", "(", ")", ";", "// safely encode", "foreach", "(", "$", "defaults", "as", "$", "k", "=>", "$", "v", ")", "{", "$", "this", "->", "request_settings", "[", "'oauth1_params'", "]", "[", "$", "this", "->", "safe_encode", "(", "$", "k", ")", "]", "=", "$", "this", "->", "safe_encode", "(", "$", "v", ")", ";", "}", "}" ]
Prepares OAuth1 signing parameters. @return void all required OAuth parameters, safely encoded, are stored to the class variable '$this->request_settings['oauth1_params']'
[ "Prepares", "OAuth1", "signing", "parameters", "." ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L203-L222
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.prepare_url
private function prepare_url() { $parts = parse_url($this->request_settings['url']); $port = isset($parts['port']) ? $parts['port'] : false; $scheme = $parts['scheme']; $host = $parts['host']; $path = isset($parts['path']) ? $parts['path'] : false; $port or $port = ($scheme == 'https') ? '443' : '80'; if (($scheme == 'https' && $port != '443') || ($scheme == 'http' && $port != '80')) { $host = "$host:$port"; } // the scheme and host MUST be lowercase $this->request_settings['url'] = strtolower("$scheme://$host"); // but not the path $this->request_settings['url'] .= $path; }
php
private function prepare_url() { $parts = parse_url($this->request_settings['url']); $port = isset($parts['port']) ? $parts['port'] : false; $scheme = $parts['scheme']; $host = $parts['host']; $path = isset($parts['path']) ? $parts['path'] : false; $port or $port = ($scheme == 'https') ? '443' : '80'; if (($scheme == 'https' && $port != '443') || ($scheme == 'http' && $port != '80')) { $host = "$host:$port"; } // the scheme and host MUST be lowercase $this->request_settings['url'] = strtolower("$scheme://$host"); // but not the path $this->request_settings['url'] .= $path; }
[ "private", "function", "prepare_url", "(", ")", "{", "$", "parts", "=", "parse_url", "(", "$", "this", "->", "request_settings", "[", "'url'", "]", ")", ";", "$", "port", "=", "isset", "(", "$", "parts", "[", "'port'", "]", ")", "?", "$", "parts", "[", "'port'", "]", ":", "false", ";", "$", "scheme", "=", "$", "parts", "[", "'scheme'", "]", ";", "$", "host", "=", "$", "parts", "[", "'host'", "]", ";", "$", "path", "=", "isset", "(", "$", "parts", "[", "'path'", "]", ")", "?", "$", "parts", "[", "'path'", "]", ":", "false", ";", "$", "port", "or", "$", "port", "=", "(", "$", "scheme", "==", "'https'", ")", "?", "'443'", ":", "'80'", ";", "if", "(", "(", "$", "scheme", "==", "'https'", "&&", "$", "port", "!=", "'443'", ")", "||", "(", "$", "scheme", "==", "'http'", "&&", "$", "port", "!=", "'80'", ")", ")", "{", "$", "host", "=", "\"$host:$port\"", ";", "}", "// the scheme and host MUST be lowercase", "$", "this", "->", "request_settings", "[", "'url'", "]", "=", "strtolower", "(", "\"$scheme://$host\"", ")", ";", "// but not the path", "$", "this", "->", "request_settings", "[", "'url'", "]", ".=", "$", "path", ";", "}" ]
Prepares the URL for use in the base string by ripping it apart and reconstructing it. Ref: 3.4.1.2 @return void value is stored to the class array variable '$this->request_settings['url']'
[ "Prepares", "the", "URL", "for", "use", "in", "the", "base", "string", "by", "ripping", "it", "apart", "and", "reconstructing", "it", "." ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L276-L294
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.multipart_escape
private function multipart_escape($value) { if (!$this->request_settings['multipart'] || strpos($value, '@') !== 0) return $value; // see if the parameter is a file. // we split on the semi-colon as it's the delimiter used on media uploads // for fields with semi-colons this will return the original string list($file) = explode(';', substr($value, 1), 2); if (file_exists($file)) return $value; return " $value"; }
php
private function multipart_escape($value) { if (!$this->request_settings['multipart'] || strpos($value, '@') !== 0) return $value; // see if the parameter is a file. // we split on the semi-colon as it's the delimiter used on media uploads // for fields with semi-colons this will return the original string list($file) = explode(';', substr($value, 1), 2); if (file_exists($file)) return $value; return " $value"; }
[ "private", "function", "multipart_escape", "(", "$", "value", ")", "{", "if", "(", "!", "$", "this", "->", "request_settings", "[", "'multipart'", "]", "||", "strpos", "(", "$", "value", ",", "'@'", ")", "!==", "0", ")", "return", "$", "value", ";", "// see if the parameter is a file.", "// we split on the semi-colon as it's the delimiter used on media uploads", "// for fields with semi-colons this will return the original string", "list", "(", "$", "file", ")", "=", "explode", "(", "';'", ",", "substr", "(", "$", "value", ",", "1", ")", ",", "2", ")", ";", "if", "(", "file_exists", "(", "$", "file", ")", ")", "return", "$", "value", ";", "return", "\" $value\"", ";", "}" ]
If the request uses multipart, and the parameter isn't a file path, prepend a space otherwise return the original value. we chose a space here as twitter whitespace trims from the beginning of the tweet. we don't use \0 here because it's the character for string termination. @param the parameter value @return string the original or modified string, depending on the request and the input parameter
[ "If", "the", "request", "uses", "multipart", "and", "the", "parameter", "isn", "t", "a", "file", "path", "prepend", "a", "space", "otherwise", "return", "the", "original", "value", ".", "we", "chose", "a", "space", "here", "as", "twitter", "whitespace", "trims", "from", "the", "beginning", "of", "the", "tweet", ".", "we", "don", "t", "use", "\\", "0", "here", "because", "it", "s", "the", "character", "for", "string", "termination", "." ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L305-L317
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.prepare_params
private function prepare_params() { $doing_oauth1 = false; $this->request_settings['prepared_params'] = array(); $prepared = &$this->request_settings['prepared_params']; $prepared_pairs = array(); $prepared_pairs_with_oauth = array(); if (isset($this->request_settings['oauth1_params'])) { $oauth1 = &$this->request_settings['oauth1_params']; $doing_oauth1 = true; $params = array_merge($oauth1, $this->request_settings['params']); // Remove oauth_signature if present // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.") unset($params['oauth_signature']); // empty the oauth1 array. we reset these values later in this method $oauth1 = array(); } else { $params = $this->request_settings['params']; } // Parameters are sorted by name, using lexicographical byte value ordering. // Ref: Spec: 9.1.1 (1) uksort($params, 'strcmp'); // set this now so we're not doing it on every parameter $supports_curl_file = class_exists('CurlFile', false); // encode params unless we're doing multipart foreach ($params as $k => $v) { $k = $this->request_settings['multipart'] ? $k : $this->safe_encode($k); if (is_array($v)) $v = implode(',', $v); // we don't need to do the multipart escaping if we support curlfile if ($supports_curl_file && ($v instanceof CurlFile)) { // leave $v alone } elseif ($this->request_settings['multipart']) { $v = $this->multipart_escape($v); } else { $v = $this->safe_encode($v); } // split parameters for the basestring and authorization header, and recreate the oauth1 array if ($doing_oauth1) { // if we're doing multipart, only store the oauth_* params, ignore the users request params if ((strpos($k, 'oauth') === 0) || !$this->request_settings['multipart']) $prepared_pairs_with_oauth[] = "{$k}={$v}"; if (strpos($k, 'oauth') === 0) { $oauth1[$k] = $v; continue; } } $prepared[$k] = $v; if (!$this->request_settings['multipart']) $prepared_pairs[] = "{$k}={$v}"; } if ($doing_oauth1) { $this->request_settings['basestring_params'] = implode('&', $prepared_pairs_with_oauth); } // setup params for GET/POST/PUT method handling if (!empty($prepared)) { $content = implode('&', $prepared_pairs); switch ($this->request_settings['method']) { case 'PUT': // fall through to POST as PUT should be treated the same case 'POST': $this->request_settings['postfields'] = $this->request_settings['multipart'] ? $prepared : $content; break; default: $this->request_settings['querystring'] = $content; break; } } }
php
private function prepare_params() { $doing_oauth1 = false; $this->request_settings['prepared_params'] = array(); $prepared = &$this->request_settings['prepared_params']; $prepared_pairs = array(); $prepared_pairs_with_oauth = array(); if (isset($this->request_settings['oauth1_params'])) { $oauth1 = &$this->request_settings['oauth1_params']; $doing_oauth1 = true; $params = array_merge($oauth1, $this->request_settings['params']); // Remove oauth_signature if present // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.") unset($params['oauth_signature']); // empty the oauth1 array. we reset these values later in this method $oauth1 = array(); } else { $params = $this->request_settings['params']; } // Parameters are sorted by name, using lexicographical byte value ordering. // Ref: Spec: 9.1.1 (1) uksort($params, 'strcmp'); // set this now so we're not doing it on every parameter $supports_curl_file = class_exists('CurlFile', false); // encode params unless we're doing multipart foreach ($params as $k => $v) { $k = $this->request_settings['multipart'] ? $k : $this->safe_encode($k); if (is_array($v)) $v = implode(',', $v); // we don't need to do the multipart escaping if we support curlfile if ($supports_curl_file && ($v instanceof CurlFile)) { // leave $v alone } elseif ($this->request_settings['multipart']) { $v = $this->multipart_escape($v); } else { $v = $this->safe_encode($v); } // split parameters for the basestring and authorization header, and recreate the oauth1 array if ($doing_oauth1) { // if we're doing multipart, only store the oauth_* params, ignore the users request params if ((strpos($k, 'oauth') === 0) || !$this->request_settings['multipart']) $prepared_pairs_with_oauth[] = "{$k}={$v}"; if (strpos($k, 'oauth') === 0) { $oauth1[$k] = $v; continue; } } $prepared[$k] = $v; if (!$this->request_settings['multipart']) $prepared_pairs[] = "{$k}={$v}"; } if ($doing_oauth1) { $this->request_settings['basestring_params'] = implode('&', $prepared_pairs_with_oauth); } // setup params for GET/POST/PUT method handling if (!empty($prepared)) { $content = implode('&', $prepared_pairs); switch ($this->request_settings['method']) { case 'PUT': // fall through to POST as PUT should be treated the same case 'POST': $this->request_settings['postfields'] = $this->request_settings['multipart'] ? $prepared : $content; break; default: $this->request_settings['querystring'] = $content; break; } } }
[ "private", "function", "prepare_params", "(", ")", "{", "$", "doing_oauth1", "=", "false", ";", "$", "this", "->", "request_settings", "[", "'prepared_params'", "]", "=", "array", "(", ")", ";", "$", "prepared", "=", "&", "$", "this", "->", "request_settings", "[", "'prepared_params'", "]", ";", "$", "prepared_pairs", "=", "array", "(", ")", ";", "$", "prepared_pairs_with_oauth", "=", "array", "(", ")", ";", "if", "(", "isset", "(", "$", "this", "->", "request_settings", "[", "'oauth1_params'", "]", ")", ")", "{", "$", "oauth1", "=", "&", "$", "this", "->", "request_settings", "[", "'oauth1_params'", "]", ";", "$", "doing_oauth1", "=", "true", ";", "$", "params", "=", "array_merge", "(", "$", "oauth1", ",", "$", "this", "->", "request_settings", "[", "'params'", "]", ")", ";", "// Remove oauth_signature if present", "// Ref: Spec: 9.1.1 (\"The oauth_signature parameter MUST be excluded.\")", "unset", "(", "$", "params", "[", "'oauth_signature'", "]", ")", ";", "// empty the oauth1 array. we reset these values later in this method", "$", "oauth1", "=", "array", "(", ")", ";", "}", "else", "{", "$", "params", "=", "$", "this", "->", "request_settings", "[", "'params'", "]", ";", "}", "// Parameters are sorted by name, using lexicographical byte value ordering.", "// Ref: Spec: 9.1.1 (1)", "uksort", "(", "$", "params", ",", "'strcmp'", ")", ";", "// set this now so we're not doing it on every parameter", "$", "supports_curl_file", "=", "class_exists", "(", "'CurlFile'", ",", "false", ")", ";", "// encode params unless we're doing multipart", "foreach", "(", "$", "params", "as", "$", "k", "=>", "$", "v", ")", "{", "$", "k", "=", "$", "this", "->", "request_settings", "[", "'multipart'", "]", "?", "$", "k", ":", "$", "this", "->", "safe_encode", "(", "$", "k", ")", ";", "if", "(", "is_array", "(", "$", "v", ")", ")", "$", "v", "=", "implode", "(", "','", ",", "$", "v", ")", ";", "// we don't need to do the multipart escaping if we support curlfile", "if", "(", "$", "supports_curl_file", "&&", "(", "$", "v", "instanceof", "CurlFile", ")", ")", "{", "// leave $v alone", "}", "elseif", "(", "$", "this", "->", "request_settings", "[", "'multipart'", "]", ")", "{", "$", "v", "=", "$", "this", "->", "multipart_escape", "(", "$", "v", ")", ";", "}", "else", "{", "$", "v", "=", "$", "this", "->", "safe_encode", "(", "$", "v", ")", ";", "}", "// split parameters for the basestring and authorization header, and recreate the oauth1 array", "if", "(", "$", "doing_oauth1", ")", "{", "// if we're doing multipart, only store the oauth_* params, ignore the users request params", "if", "(", "(", "strpos", "(", "$", "k", ",", "'oauth'", ")", "===", "0", ")", "||", "!", "$", "this", "->", "request_settings", "[", "'multipart'", "]", ")", "$", "prepared_pairs_with_oauth", "[", "]", "=", "\"{$k}={$v}\"", ";", "if", "(", "strpos", "(", "$", "k", ",", "'oauth'", ")", "===", "0", ")", "{", "$", "oauth1", "[", "$", "k", "]", "=", "$", "v", ";", "continue", ";", "}", "}", "$", "prepared", "[", "$", "k", "]", "=", "$", "v", ";", "if", "(", "!", "$", "this", "->", "request_settings", "[", "'multipart'", "]", ")", "$", "prepared_pairs", "[", "]", "=", "\"{$k}={$v}\"", ";", "}", "if", "(", "$", "doing_oauth1", ")", "{", "$", "this", "->", "request_settings", "[", "'basestring_params'", "]", "=", "implode", "(", "'&'", ",", "$", "prepared_pairs_with_oauth", ")", ";", "}", "// setup params for GET/POST/PUT method handling", "if", "(", "!", "empty", "(", "$", "prepared", ")", ")", "{", "$", "content", "=", "implode", "(", "'&'", ",", "$", "prepared_pairs", ")", ";", "switch", "(", "$", "this", "->", "request_settings", "[", "'method'", "]", ")", "{", "case", "'PUT'", ":", "// fall through to POST as PUT should be treated the same", "case", "'POST'", ":", "$", "this", "->", "request_settings", "[", "'postfields'", "]", "=", "$", "this", "->", "request_settings", "[", "'multipart'", "]", "?", "$", "prepared", ":", "$", "content", ";", "break", ";", "default", ":", "$", "this", "->", "request_settings", "[", "'querystring'", "]", "=", "$", "content", ";", "break", ";", "}", "}", "}" ]
Prepares all parameters for the base string and request. Multipart parameters are ignored as they are not defined in the specification, all other types of parameter are encoded for compatibility with OAuth. @param array $params the parameters for the request @return void prepared values are stored in the class array variable '$this->request_settings'
[ "Prepares", "all", "parameters", "for", "the", "base", "string", "and", "request", ".", "Multipart", "parameters", "are", "ignored", "as", "they", "are", "not", "defined", "in", "the", "specification", "all", "other", "types", "of", "parameter", "are", "encoded", "for", "compatibility", "with", "OAuth", "." ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L328-L409
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.prepare_signing_key
private function prepare_signing_key() { $left = $this->safe_encode($this->config['consumer_secret']); $right = $this->safe_encode($this->secret()); $this->request_settings['signing_key'] = $left . '&' . $right; }
php
private function prepare_signing_key() { $left = $this->safe_encode($this->config['consumer_secret']); $right = $this->safe_encode($this->secret()); $this->request_settings['signing_key'] = $left . '&' . $right; }
[ "private", "function", "prepare_signing_key", "(", ")", "{", "$", "left", "=", "$", "this", "->", "safe_encode", "(", "$", "this", "->", "config", "[", "'consumer_secret'", "]", ")", ";", "$", "right", "=", "$", "this", "->", "safe_encode", "(", "$", "this", "->", "secret", "(", ")", ")", ";", "$", "this", "->", "request_settings", "[", "'signing_key'", "]", "=", "$", "left", ".", "'&'", ".", "$", "right", ";", "}" ]
Prepares the OAuth signing key @return void prepared signing key is stored in the class variable 'signing_key'
[ "Prepares", "the", "OAuth", "signing", "key" ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L416-L420
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.prepare_base_string
private function prepare_base_string() { $url = $this->request_settings['url']; # if the host header is set we need to rewrite the basestring to use # that, instead of the request host. otherwise the signature won't match # on the server side if (!empty($this->request_settings['headers']['Host'])) { $url = str_ireplace( $this->config['host'], $this->request_settings['headers']['Host'], $url ); } $base = array( $this->request_settings['method'], $url, $this->request_settings['basestring_params'] ); $this->request_settings['basestring'] = implode('&', $this->safe_encode($base)); }
php
private function prepare_base_string() { $url = $this->request_settings['url']; # if the host header is set we need to rewrite the basestring to use # that, instead of the request host. otherwise the signature won't match # on the server side if (!empty($this->request_settings['headers']['Host'])) { $url = str_ireplace( $this->config['host'], $this->request_settings['headers']['Host'], $url ); } $base = array( $this->request_settings['method'], $url, $this->request_settings['basestring_params'] ); $this->request_settings['basestring'] = implode('&', $this->safe_encode($base)); }
[ "private", "function", "prepare_base_string", "(", ")", "{", "$", "url", "=", "$", "this", "->", "request_settings", "[", "'url'", "]", ";", "# if the host header is set we need to rewrite the basestring to use", "# that, instead of the request host. otherwise the signature won't match", "# on the server side", "if", "(", "!", "empty", "(", "$", "this", "->", "request_settings", "[", "'headers'", "]", "[", "'Host'", "]", ")", ")", "{", "$", "url", "=", "str_ireplace", "(", "$", "this", "->", "config", "[", "'host'", "]", ",", "$", "this", "->", "request_settings", "[", "'headers'", "]", "[", "'Host'", "]", ",", "$", "url", ")", ";", "}", "$", "base", "=", "array", "(", "$", "this", "->", "request_settings", "[", "'method'", "]", ",", "$", "url", ",", "$", "this", "->", "request_settings", "[", "'basestring_params'", "]", ")", ";", "$", "this", "->", "request_settings", "[", "'basestring'", "]", "=", "implode", "(", "'&'", ",", "$", "this", "->", "safe_encode", "(", "$", "base", ")", ")", ";", "}" ]
Prepare the base string. Ref: Spec: 9.1.3 ("Concatenate Request Elements") @return void prepared base string is stored in the class variable 'base_string'
[ "Prepare", "the", "base", "string", ".", "Ref", ":", "Spec", ":", "9", ".", "1", ".", "3", "(", "Concatenate", "Request", "Elements", ")" ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L428-L448
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.prepare_oauth_signature
private function prepare_oauth_signature() { $this->request_settings['oauth1_params']['oauth_signature'] = $this->safe_encode( base64_encode( hash_hmac( 'sha1', $this->request_settings['basestring'], $this->request_settings['signing_key'], true ))); }
php
private function prepare_oauth_signature() { $this->request_settings['oauth1_params']['oauth_signature'] = $this->safe_encode( base64_encode( hash_hmac( 'sha1', $this->request_settings['basestring'], $this->request_settings['signing_key'], true ))); }
[ "private", "function", "prepare_oauth_signature", "(", ")", "{", "$", "this", "->", "request_settings", "[", "'oauth1_params'", "]", "[", "'oauth_signature'", "]", "=", "$", "this", "->", "safe_encode", "(", "base64_encode", "(", "hash_hmac", "(", "'sha1'", ",", "$", "this", "->", "request_settings", "[", "'basestring'", "]", ",", "$", "this", "->", "request_settings", "[", "'signing_key'", "]", ",", "true", ")", ")", ")", ";", "}" ]
Signs the OAuth 1 request @return void oauth_signature is added to the parameters in the class array variable '$this->request_settings'
[ "Signs", "the", "OAuth", "1", "request" ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L455-L461
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.prepare_auth_header
private function prepare_auth_header() { if (!$this->config['as_header']) return; // oauth1 if (isset($this->request_settings['oauth1_params'])) { // sort again as oauth_signature was added post param preparation uksort($this->request_settings['oauth1_params'], 'strcmp'); $encoded_quoted_pairs = array(); foreach ($this->request_settings['oauth1_params'] as $k => $v) { $encoded_quoted_pairs[] = "{$k}=\"{$v}\""; } $header = 'OAuth ' . implode(', ', $encoded_quoted_pairs); } elseif (!empty($this->config['bearer'])) { $header = 'Bearer ' . $this->config['bearer']; } if (isset($header)) $this->request_settings['headers']['Authorization'] = $header; }
php
private function prepare_auth_header() { if (!$this->config['as_header']) return; // oauth1 if (isset($this->request_settings['oauth1_params'])) { // sort again as oauth_signature was added post param preparation uksort($this->request_settings['oauth1_params'], 'strcmp'); $encoded_quoted_pairs = array(); foreach ($this->request_settings['oauth1_params'] as $k => $v) { $encoded_quoted_pairs[] = "{$k}=\"{$v}\""; } $header = 'OAuth ' . implode(', ', $encoded_quoted_pairs); } elseif (!empty($this->config['bearer'])) { $header = 'Bearer ' . $this->config['bearer']; } if (isset($header)) $this->request_settings['headers']['Authorization'] = $header; }
[ "private", "function", "prepare_auth_header", "(", ")", "{", "if", "(", "!", "$", "this", "->", "config", "[", "'as_header'", "]", ")", "return", ";", "// oauth1", "if", "(", "isset", "(", "$", "this", "->", "request_settings", "[", "'oauth1_params'", "]", ")", ")", "{", "// sort again as oauth_signature was added post param preparation", "uksort", "(", "$", "this", "->", "request_settings", "[", "'oauth1_params'", "]", ",", "'strcmp'", ")", ";", "$", "encoded_quoted_pairs", "=", "array", "(", ")", ";", "foreach", "(", "$", "this", "->", "request_settings", "[", "'oauth1_params'", "]", "as", "$", "k", "=>", "$", "v", ")", "{", "$", "encoded_quoted_pairs", "[", "]", "=", "\"{$k}=\\\"{$v}\\\"\"", ";", "}", "$", "header", "=", "'OAuth '", ".", "implode", "(", "', '", ",", "$", "encoded_quoted_pairs", ")", ";", "}", "elseif", "(", "!", "empty", "(", "$", "this", "->", "config", "[", "'bearer'", "]", ")", ")", "{", "$", "header", "=", "'Bearer '", ".", "$", "this", "->", "config", "[", "'bearer'", "]", ";", "}", "if", "(", "isset", "(", "$", "header", ")", ")", "$", "this", "->", "request_settings", "[", "'headers'", "]", "[", "'Authorization'", "]", "=", "$", "header", ";", "}" ]
Prepares the Authorization header @return void prepared authorization header is stored in the class variable headers['Authorization']
[ "Prepares", "the", "Authorization", "header" ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L468-L487
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.bearer_token_credentials
public function bearer_token_credentials() { $credentials = implode(':', array( $this->safe_encode($this->config['consumer_key']), $this->safe_encode($this->config['consumer_secret']) )); return base64_encode($credentials); }
php
public function bearer_token_credentials() { $credentials = implode(':', array( $this->safe_encode($this->config['consumer_key']), $this->safe_encode($this->config['consumer_secret']) )); return base64_encode($credentials); }
[ "public", "function", "bearer_token_credentials", "(", ")", "{", "$", "credentials", "=", "implode", "(", "':'", ",", "array", "(", "$", "this", "->", "safe_encode", "(", "$", "this", "->", "config", "[", "'consumer_key'", "]", ")", ",", "$", "this", "->", "safe_encode", "(", "$", "this", "->", "config", "[", "'consumer_secret'", "]", ")", ")", ")", ";", "return", "base64_encode", "(", "$", "credentials", ")", ";", "}" ]
Create the bearer token for OAuth2 requests from the consumer_key and consumer_secret. @return string the bearer token
[ "Create", "the", "bearer", "token", "for", "OAuth2", "requests", "from", "the", "consumer_key", "and", "consumer_secret", "." ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L494-L500
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.request
public function request($method, $url, $params=array(), $useauth=true, $multipart=false, $headers=array()) { $options = array( 'method' => $method, 'url' => $url, 'params' => $params, 'with_user' => true, 'multipart' => $multipart, 'headers' => $headers ); $options = array_merge($this->default_options(), $options); if ($useauth) { return $this->user_request($options); } else { return $this->unauthenticated_request($options); } }
php
public function request($method, $url, $params=array(), $useauth=true, $multipart=false, $headers=array()) { $options = array( 'method' => $method, 'url' => $url, 'params' => $params, 'with_user' => true, 'multipart' => $multipart, 'headers' => $headers ); $options = array_merge($this->default_options(), $options); if ($useauth) { return $this->user_request($options); } else { return $this->unauthenticated_request($options); } }
[ "public", "function", "request", "(", "$", "method", ",", "$", "url", ",", "$", "params", "=", "array", "(", ")", ",", "$", "useauth", "=", "true", ",", "$", "multipart", "=", "false", ",", "$", "headers", "=", "array", "(", ")", ")", "{", "$", "options", "=", "array", "(", "'method'", "=>", "$", "method", ",", "'url'", "=>", "$", "url", ",", "'params'", "=>", "$", "params", ",", "'with_user'", "=>", "true", ",", "'multipart'", "=>", "$", "multipart", ",", "'headers'", "=>", "$", "headers", ")", ";", "$", "options", "=", "array_merge", "(", "$", "this", "->", "default_options", "(", ")", ",", "$", "options", ")", ";", "if", "(", "$", "useauth", ")", "{", "return", "$", "this", "->", "user_request", "(", "$", "options", ")", ";", "}", "else", "{", "return", "$", "this", "->", "unauthenticated_request", "(", "$", "options", ")", ";", "}", "}" ]
Make an HTTP request using this library. This method doesn't return anything. Instead the response should be inspected directly. @param string $method the HTTP method being used. e.g. POST, GET, HEAD etc @param string $url the request URL without query string parameters @param array $params the request parameters as an array of key=value pairs. Default empty array @param string $useauth whether to use authentication when making the request. Default true @param string $multipart whether this request contains multipart data. Default false @param array $headers any custom headers to send with the request. Default empty array @return int the http response code for the request. 0 is returned if a connection could not be made
[ "Make", "an", "HTTP", "request", "using", "this", "library", ".", "This", "method", "doesn", "t", "return", "anything", ".", "Instead", "the", "response", "should", "be", "inspected", "directly", "." ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L514-L530
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.oauth1_request
private function oauth1_request() { $this->prepare_oauth1_params(); $this->prepare_method(); $this->prepare_url(); $this->prepare_params(); $this->prepare_base_string(); $this->prepare_signing_key(); $this->prepare_oauth_signature(); $this->prepare_auth_header(); return $this->curlit(); }
php
private function oauth1_request() { $this->prepare_oauth1_params(); $this->prepare_method(); $this->prepare_url(); $this->prepare_params(); $this->prepare_base_string(); $this->prepare_signing_key(); $this->prepare_oauth_signature(); $this->prepare_auth_header(); return $this->curlit(); }
[ "private", "function", "oauth1_request", "(", ")", "{", "$", "this", "->", "prepare_oauth1_params", "(", ")", ";", "$", "this", "->", "prepare_method", "(", ")", ";", "$", "this", "->", "prepare_url", "(", ")", ";", "$", "this", "->", "prepare_params", "(", ")", ";", "$", "this", "->", "prepare_base_string", "(", ")", ";", "$", "this", "->", "prepare_signing_key", "(", ")", ";", "$", "this", "->", "prepare_oauth_signature", "(", ")", ";", "$", "this", "->", "prepare_auth_header", "(", ")", ";", "return", "$", "this", "->", "curlit", "(", ")", ";", "}" ]
Signs the request and adds the OAuth signature. This runs all the request parameter preparation methods. @param string $method the HTTP method being used. e.g. POST, GET, HEAD etc @param string $url the request URL without query string parameters @param array $params the request parameters as an array of key=value pairs @param boolean $with_user whether to include the user credentials when making the request. @return void
[ "Signs", "the", "request", "and", "adds", "the", "OAuth", "signature", ".", "This", "runs", "all", "the", "request", "parameter", "preparation", "methods", "." ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L577-L587
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.streaming_request
public function streaming_request($method, $url, $params=array(), $callback='') { if ( ! empty($callback) ) { if ( ! is_callable($callback) ) { return false; } $this->config['streaming_callback'] = $callback; } $this->metrics['start'] = time(); $this->metrics['interval_start'] = $this->metrics['start']; $this->metrics['messages'] = 0; $this->metrics['last_messages'] = 0; $this->metrics['bytes'] = 0; $this->metrics['last_bytes'] = 0; $this->config['is_streaming'] = true; $this->request($method, $url, $params); }
php
public function streaming_request($method, $url, $params=array(), $callback='') { if ( ! empty($callback) ) { if ( ! is_callable($callback) ) { return false; } $this->config['streaming_callback'] = $callback; } $this->metrics['start'] = time(); $this->metrics['interval_start'] = $this->metrics['start']; $this->metrics['messages'] = 0; $this->metrics['last_messages'] = 0; $this->metrics['bytes'] = 0; $this->metrics['last_bytes'] = 0; $this->config['is_streaming'] = true; $this->request($method, $url, $params); }
[ "public", "function", "streaming_request", "(", "$", "method", ",", "$", "url", ",", "$", "params", "=", "array", "(", ")", ",", "$", "callback", "=", "''", ")", "{", "if", "(", "!", "empty", "(", "$", "callback", ")", ")", "{", "if", "(", "!", "is_callable", "(", "$", "callback", ")", ")", "{", "return", "false", ";", "}", "$", "this", "->", "config", "[", "'streaming_callback'", "]", "=", "$", "callback", ";", "}", "$", "this", "->", "metrics", "[", "'start'", "]", "=", "time", "(", ")", ";", "$", "this", "->", "metrics", "[", "'interval_start'", "]", "=", "$", "this", "->", "metrics", "[", "'start'", "]", ";", "$", "this", "->", "metrics", "[", "'messages'", "]", "=", "0", ";", "$", "this", "->", "metrics", "[", "'last_messages'", "]", "=", "0", ";", "$", "this", "->", "metrics", "[", "'bytes'", "]", "=", "0", ";", "$", "this", "->", "metrics", "[", "'last_bytes'", "]", "=", "0", ";", "$", "this", "->", "config", "[", "'is_streaming'", "]", "=", "true", ";", "$", "this", "->", "request", "(", "$", "method", ",", "$", "url", ",", "$", "params", ")", ";", "}" ]
Make a long poll HTTP request using this library. This method is different to the other request methods as it isn't supposed to disconnect Using this method expects a callback which will receive the streaming responses. @param string $method the HTTP method being used. e.g. POST, GET, HEAD etc @param string $url the request URL without query string parameters @param array $params the request parameters as an array of key=value pairs @param string $callback the callback function to stream the buffer to. @return void
[ "Make", "a", "long", "poll", "HTTP", "request", "using", "this", "library", ".", "This", "method", "is", "different", "to", "the", "other", "request", "methods", "as", "it", "isn", "t", "supposed", "to", "disconnect" ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L613-L628
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.update_metrics
private function update_metrics() { $now = time(); if (($this->metrics['interval_start'] + $this->config['streaming_metrics_interval']) > $now) return null; $this->metrics['mps'] = round( ($this->metrics['messages'] - $this->metrics['last_messages']) / $this->config['streaming_metrics_interval'], 2); $this->metrics['bps'] = round( ($this->metrics['bytes'] - $this->metrics['last_bytes']) / $this->config['streaming_metrics_interval'], 2); $this->metrics['last_bytes'] = $this->metrics['bytes']; $this->metrics['last_messages'] = $this->metrics['messages']; $this->metrics['interval_start'] = $now; return $this->metrics; }
php
private function update_metrics() { $now = time(); if (($this->metrics['interval_start'] + $this->config['streaming_metrics_interval']) > $now) return null; $this->metrics['mps'] = round( ($this->metrics['messages'] - $this->metrics['last_messages']) / $this->config['streaming_metrics_interval'], 2); $this->metrics['bps'] = round( ($this->metrics['bytes'] - $this->metrics['last_bytes']) / $this->config['streaming_metrics_interval'], 2); $this->metrics['last_bytes'] = $this->metrics['bytes']; $this->metrics['last_messages'] = $this->metrics['messages']; $this->metrics['interval_start'] = $now; return $this->metrics; }
[ "private", "function", "update_metrics", "(", ")", "{", "$", "now", "=", "time", "(", ")", ";", "if", "(", "(", "$", "this", "->", "metrics", "[", "'interval_start'", "]", "+", "$", "this", "->", "config", "[", "'streaming_metrics_interval'", "]", ")", ">", "$", "now", ")", "return", "null", ";", "$", "this", "->", "metrics", "[", "'mps'", "]", "=", "round", "(", "(", "$", "this", "->", "metrics", "[", "'messages'", "]", "-", "$", "this", "->", "metrics", "[", "'last_messages'", "]", ")", "/", "$", "this", "->", "config", "[", "'streaming_metrics_interval'", "]", ",", "2", ")", ";", "$", "this", "->", "metrics", "[", "'bps'", "]", "=", "round", "(", "(", "$", "this", "->", "metrics", "[", "'bytes'", "]", "-", "$", "this", "->", "metrics", "[", "'last_bytes'", "]", ")", "/", "$", "this", "->", "config", "[", "'streaming_metrics_interval'", "]", ",", "2", ")", ";", "$", "this", "->", "metrics", "[", "'last_bytes'", "]", "=", "$", "this", "->", "metrics", "[", "'bytes'", "]", ";", "$", "this", "->", "metrics", "[", "'last_messages'", "]", "=", "$", "this", "->", "metrics", "[", "'messages'", "]", ";", "$", "this", "->", "metrics", "[", "'interval_start'", "]", "=", "$", "now", ";", "return", "$", "this", "->", "metrics", ";", "}" ]
Handles the updating of the current Streaming API metrics. @return array the metrics for the streaming api connection
[ "Handles", "the", "updating", "of", "the", "current", "Streaming", "API", "metrics", "." ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L635-L647
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.url
public function url($request, $extension='json') { // remove multi-slashes $request = preg_replace('$([^:])//+$', '$1/', $request); if (stripos($request, 'http') === 0 || stripos($request, '//') === 0) { return $request; } $extension = strlen($extension) > 0 ? ".$extension" : ''; $proto = $this->config['use_ssl'] ? 'https:/' : 'http:/'; // trim trailing slash $request = ltrim($request, '/'); $pos = strlen($request) - strlen($extension); if (substr($request, $pos) === $extension) $request = substr_replace($request, '', $pos); return implode('/', array( $proto, $this->config['host'], $request . $extension )); }
php
public function url($request, $extension='json') { // remove multi-slashes $request = preg_replace('$([^:])//+$', '$1/', $request); if (stripos($request, 'http') === 0 || stripos($request, '//') === 0) { return $request; } $extension = strlen($extension) > 0 ? ".$extension" : ''; $proto = $this->config['use_ssl'] ? 'https:/' : 'http:/'; // trim trailing slash $request = ltrim($request, '/'); $pos = strlen($request) - strlen($extension); if (substr($request, $pos) === $extension) $request = substr_replace($request, '', $pos); return implode('/', array( $proto, $this->config['host'], $request . $extension )); }
[ "public", "function", "url", "(", "$", "request", ",", "$", "extension", "=", "'json'", ")", "{", "// remove multi-slashes", "$", "request", "=", "preg_replace", "(", "'$([^:])//+$'", ",", "'$1/'", ",", "$", "request", ")", ";", "if", "(", "stripos", "(", "$", "request", ",", "'http'", ")", "===", "0", "||", "stripos", "(", "$", "request", ",", "'//'", ")", "===", "0", ")", "{", "return", "$", "request", ";", "}", "$", "extension", "=", "strlen", "(", "$", "extension", ")", ">", "0", "?", "\".$extension\"", ":", "''", ";", "$", "proto", "=", "$", "this", "->", "config", "[", "'use_ssl'", "]", "?", "'https:/'", ":", "'http:/'", ";", "// trim trailing slash", "$", "request", "=", "ltrim", "(", "$", "request", ",", "'/'", ")", ";", "$", "pos", "=", "strlen", "(", "$", "request", ")", "-", "strlen", "(", "$", "extension", ")", ";", "if", "(", "substr", "(", "$", "request", ",", "$", "pos", ")", "===", "$", "extension", ")", "$", "request", "=", "substr_replace", "(", "$", "request", ",", "''", ",", "$", "pos", ")", ";", "return", "implode", "(", "'/'", ",", "array", "(", "$", "proto", ",", "$", "this", "->", "config", "[", "'host'", "]", ",", "$", "request", ".", "$", "extension", ")", ")", ";", "}" ]
Utility function to create the request URL in the requested format. If a fully-qualified URI is provided, it will be returned. Any multi-slashes (except for the protocol) will be replaced with a single slash. @param string $request the API method without extension @param string $extension the format of the response. Default json. Set to an empty string to exclude the format @return string the concatenation of the host, API version, API method and format, or $request if it begins with http
[ "Utility", "function", "to", "create", "the", "request", "URL", "in", "the", "requested", "format", ".", "If", "a", "fully", "-", "qualified", "URI", "is", "provided", "it", "will", "be", "returned", ".", "Any", "multi", "-", "slashes", "(", "except", "for", "the", "protocol", ")", "will", "be", "replaced", "with", "a", "single", "slash", "." ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L659-L682
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.curlHeader
private function curlHeader($ch, $header) { $this->response['raw'] .= $header; list($key, $value) = array_pad(explode(':', $header, 2), 2, null); $key = trim($key); $value = trim($value); if ( ! isset($this->response['headers'][$key])) { $this->response['headers'][$key] = $value; } else { if (!is_array($this->response['headers'][$key])) { $this->response['headers'][$key] = array($this->response['headers'][$key]); } $this->response['headers'][$key][] = $value; } return strlen($header); }
php
private function curlHeader($ch, $header) { $this->response['raw'] .= $header; list($key, $value) = array_pad(explode(':', $header, 2), 2, null); $key = trim($key); $value = trim($value); if ( ! isset($this->response['headers'][$key])) { $this->response['headers'][$key] = $value; } else { if (!is_array($this->response['headers'][$key])) { $this->response['headers'][$key] = array($this->response['headers'][$key]); } $this->response['headers'][$key][] = $value; } return strlen($header); }
[ "private", "function", "curlHeader", "(", "$", "ch", ",", "$", "header", ")", "{", "$", "this", "->", "response", "[", "'raw'", "]", ".=", "$", "header", ";", "list", "(", "$", "key", ",", "$", "value", ")", "=", "array_pad", "(", "explode", "(", "':'", ",", "$", "header", ",", "2", ")", ",", "2", ",", "null", ")", ";", "$", "key", "=", "trim", "(", "$", "key", ")", ";", "$", "value", "=", "trim", "(", "$", "value", ")", ";", "if", "(", "!", "isset", "(", "$", "this", "->", "response", "[", "'headers'", "]", "[", "$", "key", "]", ")", ")", "{", "$", "this", "->", "response", "[", "'headers'", "]", "[", "$", "key", "]", "=", "$", "value", ";", "}", "else", "{", "if", "(", "!", "is_array", "(", "$", "this", "->", "response", "[", "'headers'", "]", "[", "$", "key", "]", ")", ")", "{", "$", "this", "->", "response", "[", "'headers'", "]", "[", "$", "key", "]", "=", "array", "(", "$", "this", "->", "response", "[", "'headers'", "]", "[", "$", "key", "]", ")", ";", "}", "$", "this", "->", "response", "[", "'headers'", "]", "[", "$", "key", "]", "[", "]", "=", "$", "value", ";", "}", "return", "strlen", "(", "$", "header", ")", ";", "}" ]
Utility function to parse the returned curl headers and store them in the class array variable. @param object $ch curl handle @param string $header the response headers @return string the length of the header
[ "Utility", "function", "to", "parse", "the", "returned", "curl", "headers", "and", "store", "them", "in", "the", "class", "array", "variable", "." ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L703-L721
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.curlWrite
private function curlWrite($ch, $data) { $l = strlen($data); if (strpos($data, $this->config['streaming_eol']) === false) { $this->buffer .= $data; return $l; } $buffered = explode($this->config['streaming_eol'], $data); $content = $this->buffer . $buffered[0]; $this->metrics['messages']++; $this->metrics['bytes'] += strlen($content); if ( ! is_callable($this->config['streaming_callback'])) return 0; $metrics = $this->update_metrics(); $stop = call_user_func( $this->config['streaming_callback'], $content, strlen($content), $metrics ); $this->buffer = $buffered[1]; if ($stop) return 0; return $l; }
php
private function curlWrite($ch, $data) { $l = strlen($data); if (strpos($data, $this->config['streaming_eol']) === false) { $this->buffer .= $data; return $l; } $buffered = explode($this->config['streaming_eol'], $data); $content = $this->buffer . $buffered[0]; $this->metrics['messages']++; $this->metrics['bytes'] += strlen($content); if ( ! is_callable($this->config['streaming_callback'])) return 0; $metrics = $this->update_metrics(); $stop = call_user_func( $this->config['streaming_callback'], $content, strlen($content), $metrics ); $this->buffer = $buffered[1]; if ($stop) return 0; return $l; }
[ "private", "function", "curlWrite", "(", "$", "ch", ",", "$", "data", ")", "{", "$", "l", "=", "strlen", "(", "$", "data", ")", ";", "if", "(", "strpos", "(", "$", "data", ",", "$", "this", "->", "config", "[", "'streaming_eol'", "]", ")", "===", "false", ")", "{", "$", "this", "->", "buffer", ".=", "$", "data", ";", "return", "$", "l", ";", "}", "$", "buffered", "=", "explode", "(", "$", "this", "->", "config", "[", "'streaming_eol'", "]", ",", "$", "data", ")", ";", "$", "content", "=", "$", "this", "->", "buffer", ".", "$", "buffered", "[", "0", "]", ";", "$", "this", "->", "metrics", "[", "'messages'", "]", "++", ";", "$", "this", "->", "metrics", "[", "'bytes'", "]", "+=", "strlen", "(", "$", "content", ")", ";", "if", "(", "!", "is_callable", "(", "$", "this", "->", "config", "[", "'streaming_callback'", "]", ")", ")", "return", "0", ";", "$", "metrics", "=", "$", "this", "->", "update_metrics", "(", ")", ";", "$", "stop", "=", "call_user_func", "(", "$", "this", "->", "config", "[", "'streaming_callback'", "]", ",", "$", "content", ",", "strlen", "(", "$", "content", ")", ",", "$", "metrics", ")", ";", "$", "this", "->", "buffer", "=", "$", "buffered", "[", "1", "]", ";", "if", "(", "$", "stop", ")", "return", "0", ";", "return", "$", "l", ";", "}" ]
Utility function to parse the returned curl buffer and store them until an EOL is found. The buffer for curl is an undefined size so we need to collect the content until an EOL is found. This function calls the previously defined streaming callback method. @param object $ch curl handle @param string $data the current curl buffer @return int the length of the data string processed in this function
[ "Utility", "function", "to", "parse", "the", "returned", "curl", "buffer", "and", "store", "them", "until", "an", "EOL", "is", "found", ".", "The", "buffer", "for", "curl", "is", "an", "undefined", "size", "so", "we", "need", "to", "collect", "the", "content", "until", "an", "EOL", "is", "found", "." ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L734-L762
themattharris/tmhOAuth
tmhOAuth.php
tmhOAuth.curlit
private function curlit() { $this->response = array( 'raw' => '' ); // configure curl $c = curl_init(); if ($this->request_settings['method'] == 'GET' && isset($this->request_settings['querystring'])) { $this->request_settings['url'] = $this->request_settings['url'] . '?' . $this->request_settings['querystring']; } elseif ($this->request_settings['method'] == 'POST' || $this->request_settings['method'] == 'PUT') { $postfields = array(); if (isset($this->request_settings['postfields'])) $postfields = $this->request_settings['postfields']; curl_setopt($c, CURLOPT_POSTFIELDS, $postfields); } curl_setopt($c, CURLOPT_CUSTOMREQUEST, $this->request_settings['method']); curl_setopt_array($c, array( CURLOPT_HTTP_VERSION => $this->config['curl_http_version'], CURLOPT_USERAGENT => $this->config['user_agent'], CURLOPT_CONNECTTIMEOUT => $this->config['curl_connecttimeout'], CURLOPT_TIMEOUT => $this->config['curl_timeout'], CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => $this->config['curl_ssl_verifypeer'], CURLOPT_SSL_VERIFYHOST => $this->config['curl_ssl_verifyhost'], CURLOPT_FOLLOWLOCATION => $this->config['curl_followlocation'], CURLOPT_PROXY => $this->config['curl_proxy'], CURLOPT_ENCODING => $this->config['curl_encoding'], CURLOPT_URL => $this->request_settings['url'], // process the headers CURLOPT_HEADERFUNCTION => array($this, 'curlHeader'), CURLOPT_HEADER => false, CURLINFO_HEADER_OUT => true, )); if ($this->config['curl_cainfo'] !== false) curl_setopt($c, CURLOPT_CAINFO, $this->config['curl_cainfo']); if ($this->config['curl_capath'] !== false) curl_setopt($c, CURLOPT_CAPATH, $this->config['curl_capath']); if ($this->config['curl_proxyuserpwd'] !== false) curl_setopt($c, CURLOPT_PROXYUSERPWD, $this->config['curl_proxyuserpwd']); if ($this->config['curl_sslversion'] !== false) curl_setopt($c, CURLOPT_SSLVERSION, $this->config['curl_sslversion']); if ($this->config['is_streaming']) { // process the body $this->response['content-length'] = 0; curl_setopt($c, CURLOPT_TIMEOUT, 0); curl_setopt($c, CURLOPT_WRITEFUNCTION, array($this, 'curlWrite')); } if ( ! empty($this->request_settings['headers'])) { foreach ($this->request_settings['headers'] as $k => $v) { $headers[] = trim($k . ': ' . $v); } curl_setopt($c, CURLOPT_HTTPHEADER, $headers); } if (isset($this->config['block']) && (true === $this->config['block'])) return 0; // do it! $response = curl_exec($c); $code = curl_getinfo($c, CURLINFO_HTTP_CODE); $info = curl_getinfo($c); $error = curl_error($c); $errno = curl_errno($c); curl_close($c); // store the response $this->response['code'] = $code; $this->response['response'] = $response; $this->response['info'] = $info; $this->response['error'] = $error; $this->response['errno'] = $errno; if (!isset($this->response['raw'])) { $this->response['raw'] = ''; } $this->response['raw'] .= $response; return $code; }
php
private function curlit() { $this->response = array( 'raw' => '' ); // configure curl $c = curl_init(); if ($this->request_settings['method'] == 'GET' && isset($this->request_settings['querystring'])) { $this->request_settings['url'] = $this->request_settings['url'] . '?' . $this->request_settings['querystring']; } elseif ($this->request_settings['method'] == 'POST' || $this->request_settings['method'] == 'PUT') { $postfields = array(); if (isset($this->request_settings['postfields'])) $postfields = $this->request_settings['postfields']; curl_setopt($c, CURLOPT_POSTFIELDS, $postfields); } curl_setopt($c, CURLOPT_CUSTOMREQUEST, $this->request_settings['method']); curl_setopt_array($c, array( CURLOPT_HTTP_VERSION => $this->config['curl_http_version'], CURLOPT_USERAGENT => $this->config['user_agent'], CURLOPT_CONNECTTIMEOUT => $this->config['curl_connecttimeout'], CURLOPT_TIMEOUT => $this->config['curl_timeout'], CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => $this->config['curl_ssl_verifypeer'], CURLOPT_SSL_VERIFYHOST => $this->config['curl_ssl_verifyhost'], CURLOPT_FOLLOWLOCATION => $this->config['curl_followlocation'], CURLOPT_PROXY => $this->config['curl_proxy'], CURLOPT_ENCODING => $this->config['curl_encoding'], CURLOPT_URL => $this->request_settings['url'], // process the headers CURLOPT_HEADERFUNCTION => array($this, 'curlHeader'), CURLOPT_HEADER => false, CURLINFO_HEADER_OUT => true, )); if ($this->config['curl_cainfo'] !== false) curl_setopt($c, CURLOPT_CAINFO, $this->config['curl_cainfo']); if ($this->config['curl_capath'] !== false) curl_setopt($c, CURLOPT_CAPATH, $this->config['curl_capath']); if ($this->config['curl_proxyuserpwd'] !== false) curl_setopt($c, CURLOPT_PROXYUSERPWD, $this->config['curl_proxyuserpwd']); if ($this->config['curl_sslversion'] !== false) curl_setopt($c, CURLOPT_SSLVERSION, $this->config['curl_sslversion']); if ($this->config['is_streaming']) { // process the body $this->response['content-length'] = 0; curl_setopt($c, CURLOPT_TIMEOUT, 0); curl_setopt($c, CURLOPT_WRITEFUNCTION, array($this, 'curlWrite')); } if ( ! empty($this->request_settings['headers'])) { foreach ($this->request_settings['headers'] as $k => $v) { $headers[] = trim($k . ': ' . $v); } curl_setopt($c, CURLOPT_HTTPHEADER, $headers); } if (isset($this->config['block']) && (true === $this->config['block'])) return 0; // do it! $response = curl_exec($c); $code = curl_getinfo($c, CURLINFO_HTTP_CODE); $info = curl_getinfo($c); $error = curl_error($c); $errno = curl_errno($c); curl_close($c); // store the response $this->response['code'] = $code; $this->response['response'] = $response; $this->response['info'] = $info; $this->response['error'] = $error; $this->response['errno'] = $errno; if (!isset($this->response['raw'])) { $this->response['raw'] = ''; } $this->response['raw'] .= $response; return $code; }
[ "private", "function", "curlit", "(", ")", "{", "$", "this", "->", "response", "=", "array", "(", "'raw'", "=>", "''", ")", ";", "// configure curl", "$", "c", "=", "curl_init", "(", ")", ";", "if", "(", "$", "this", "->", "request_settings", "[", "'method'", "]", "==", "'GET'", "&&", "isset", "(", "$", "this", "->", "request_settings", "[", "'querystring'", "]", ")", ")", "{", "$", "this", "->", "request_settings", "[", "'url'", "]", "=", "$", "this", "->", "request_settings", "[", "'url'", "]", ".", "'?'", ".", "$", "this", "->", "request_settings", "[", "'querystring'", "]", ";", "}", "elseif", "(", "$", "this", "->", "request_settings", "[", "'method'", "]", "==", "'POST'", "||", "$", "this", "->", "request_settings", "[", "'method'", "]", "==", "'PUT'", ")", "{", "$", "postfields", "=", "array", "(", ")", ";", "if", "(", "isset", "(", "$", "this", "->", "request_settings", "[", "'postfields'", "]", ")", ")", "$", "postfields", "=", "$", "this", "->", "request_settings", "[", "'postfields'", "]", ";", "curl_setopt", "(", "$", "c", ",", "CURLOPT_POSTFIELDS", ",", "$", "postfields", ")", ";", "}", "curl_setopt", "(", "$", "c", ",", "CURLOPT_CUSTOMREQUEST", ",", "$", "this", "->", "request_settings", "[", "'method'", "]", ")", ";", "curl_setopt_array", "(", "$", "c", ",", "array", "(", "CURLOPT_HTTP_VERSION", "=>", "$", "this", "->", "config", "[", "'curl_http_version'", "]", ",", "CURLOPT_USERAGENT", "=>", "$", "this", "->", "config", "[", "'user_agent'", "]", ",", "CURLOPT_CONNECTTIMEOUT", "=>", "$", "this", "->", "config", "[", "'curl_connecttimeout'", "]", ",", "CURLOPT_TIMEOUT", "=>", "$", "this", "->", "config", "[", "'curl_timeout'", "]", ",", "CURLOPT_RETURNTRANSFER", "=>", "true", ",", "CURLOPT_SSL_VERIFYPEER", "=>", "$", "this", "->", "config", "[", "'curl_ssl_verifypeer'", "]", ",", "CURLOPT_SSL_VERIFYHOST", "=>", "$", "this", "->", "config", "[", "'curl_ssl_verifyhost'", "]", ",", "CURLOPT_FOLLOWLOCATION", "=>", "$", "this", "->", "config", "[", "'curl_followlocation'", "]", ",", "CURLOPT_PROXY", "=>", "$", "this", "->", "config", "[", "'curl_proxy'", "]", ",", "CURLOPT_ENCODING", "=>", "$", "this", "->", "config", "[", "'curl_encoding'", "]", ",", "CURLOPT_URL", "=>", "$", "this", "->", "request_settings", "[", "'url'", "]", ",", "// process the headers", "CURLOPT_HEADERFUNCTION", "=>", "array", "(", "$", "this", ",", "'curlHeader'", ")", ",", "CURLOPT_HEADER", "=>", "false", ",", "CURLINFO_HEADER_OUT", "=>", "true", ",", ")", ")", ";", "if", "(", "$", "this", "->", "config", "[", "'curl_cainfo'", "]", "!==", "false", ")", "curl_setopt", "(", "$", "c", ",", "CURLOPT_CAINFO", ",", "$", "this", "->", "config", "[", "'curl_cainfo'", "]", ")", ";", "if", "(", "$", "this", "->", "config", "[", "'curl_capath'", "]", "!==", "false", ")", "curl_setopt", "(", "$", "c", ",", "CURLOPT_CAPATH", ",", "$", "this", "->", "config", "[", "'curl_capath'", "]", ")", ";", "if", "(", "$", "this", "->", "config", "[", "'curl_proxyuserpwd'", "]", "!==", "false", ")", "curl_setopt", "(", "$", "c", ",", "CURLOPT_PROXYUSERPWD", ",", "$", "this", "->", "config", "[", "'curl_proxyuserpwd'", "]", ")", ";", "if", "(", "$", "this", "->", "config", "[", "'curl_sslversion'", "]", "!==", "false", ")", "curl_setopt", "(", "$", "c", ",", "CURLOPT_SSLVERSION", ",", "$", "this", "->", "config", "[", "'curl_sslversion'", "]", ")", ";", "if", "(", "$", "this", "->", "config", "[", "'is_streaming'", "]", ")", "{", "// process the body", "$", "this", "->", "response", "[", "'content-length'", "]", "=", "0", ";", "curl_setopt", "(", "$", "c", ",", "CURLOPT_TIMEOUT", ",", "0", ")", ";", "curl_setopt", "(", "$", "c", ",", "CURLOPT_WRITEFUNCTION", ",", "array", "(", "$", "this", ",", "'curlWrite'", ")", ")", ";", "}", "if", "(", "!", "empty", "(", "$", "this", "->", "request_settings", "[", "'headers'", "]", ")", ")", "{", "foreach", "(", "$", "this", "->", "request_settings", "[", "'headers'", "]", "as", "$", "k", "=>", "$", "v", ")", "{", "$", "headers", "[", "]", "=", "trim", "(", "$", "k", ".", "': '", ".", "$", "v", ")", ";", "}", "curl_setopt", "(", "$", "c", ",", "CURLOPT_HTTPHEADER", ",", "$", "headers", ")", ";", "}", "if", "(", "isset", "(", "$", "this", "->", "config", "[", "'block'", "]", ")", "&&", "(", "true", "===", "$", "this", "->", "config", "[", "'block'", "]", ")", ")", "return", "0", ";", "// do it!", "$", "response", "=", "curl_exec", "(", "$", "c", ")", ";", "$", "code", "=", "curl_getinfo", "(", "$", "c", ",", "CURLINFO_HTTP_CODE", ")", ";", "$", "info", "=", "curl_getinfo", "(", "$", "c", ")", ";", "$", "error", "=", "curl_error", "(", "$", "c", ")", ";", "$", "errno", "=", "curl_errno", "(", "$", "c", ")", ";", "curl_close", "(", "$", "c", ")", ";", "// store the response", "$", "this", "->", "response", "[", "'code'", "]", "=", "$", "code", ";", "$", "this", "->", "response", "[", "'response'", "]", "=", "$", "response", ";", "$", "this", "->", "response", "[", "'info'", "]", "=", "$", "info", ";", "$", "this", "->", "response", "[", "'error'", "]", "=", "$", "error", ";", "$", "this", "->", "response", "[", "'errno'", "]", "=", "$", "errno", ";", "if", "(", "!", "isset", "(", "$", "this", "->", "response", "[", "'raw'", "]", ")", ")", "{", "$", "this", "->", "response", "[", "'raw'", "]", "=", "''", ";", "}", "$", "this", "->", "response", "[", "'raw'", "]", ".=", "$", "response", ";", "return", "$", "code", ";", "}" ]
Makes a curl request. Takes no parameters as all should have been prepared by the request method the response data is stored in the class variable 'response' @return int the http response code for the request. 0 is returned if a connection could not be made
[ "Makes", "a", "curl", "request", ".", "Takes", "no", "parameters", "as", "all", "should", "have", "been", "prepared", "by", "the", "request", "method" ]
train
https://github.com/themattharris/tmhOAuth/blob/455552d6c57549632644b6c9ac9204766be2b5ee/tmhOAuth.php#L772-L861
troydavisson/PHRETS
src/Models/Metadata/Base.php
Base.offsetExists
public function offsetExists($offset) { foreach (array_merge($this->getXmlElements(), $this->getXmlAttributes()) as $attr) { if (strtolower($attr) == strtolower($offset)) { return true; } } return false; }
php
public function offsetExists($offset) { foreach (array_merge($this->getXmlElements(), $this->getXmlAttributes()) as $attr) { if (strtolower($attr) == strtolower($offset)) { return true; } } return false; }
[ "public", "function", "offsetExists", "(", "$", "offset", ")", "{", "foreach", "(", "array_merge", "(", "$", "this", "->", "getXmlElements", "(", ")", ",", "$", "this", "->", "getXmlAttributes", "(", ")", ")", "as", "$", "attr", ")", "{", "if", "(", "strtolower", "(", "$", "attr", ")", "==", "strtolower", "(", "$", "offset", ")", ")", "{", "return", "true", ";", "}", "}", "return", "false", ";", "}" ]
(PHP 5 &gt;= 5.0.0)<br/> Whether a offset exists @link http://php.net/manual/en/arrayaccess.offsetexists.php @param mixed $offset <p> An offset to check for. </p> @return boolean true on success or false on failure. </p> <p> The return value will be casted to boolean if non-boolean was returned.
[ "(", "PHP", "5", "&gt", ";", "=", "5", ".", "0", ".", "0", ")", "<br", "/", ">", "Whether", "a", "offset", "exists" ]
train
https://github.com/troydavisson/PHRETS/blob/8eb9c43dfea54760383e0fcba4f55468568f9fc2/src/Models/Metadata/Base.php#L87-L95
troydavisson/PHRETS
src/Models/Metadata/Base.php
Base.offsetGet
public function offsetGet($offset) { foreach (array_merge($this->getXmlElements(), $this->getXmlAttributes()) as $attr) { if (strtolower($attr) == strtolower($offset)) { return \array_get($this->values, $attr); } } return null; }
php
public function offsetGet($offset) { foreach (array_merge($this->getXmlElements(), $this->getXmlAttributes()) as $attr) { if (strtolower($attr) == strtolower($offset)) { return \array_get($this->values, $attr); } } return null; }
[ "public", "function", "offsetGet", "(", "$", "offset", ")", "{", "foreach", "(", "array_merge", "(", "$", "this", "->", "getXmlElements", "(", ")", ",", "$", "this", "->", "getXmlAttributes", "(", ")", ")", "as", "$", "attr", ")", "{", "if", "(", "strtolower", "(", "$", "attr", ")", "==", "strtolower", "(", "$", "offset", ")", ")", "{", "return", "\\", "array_get", "(", "$", "this", "->", "values", ",", "$", "attr", ")", ";", "}", "}", "return", "null", ";", "}" ]
(PHP 5 &gt;= 5.0.0)<br/> Offset to retrieve @link http://php.net/manual/en/arrayaccess.offsetget.php @param mixed $offset <p> The offset to retrieve. </p> @return mixed Can return all value types.
[ "(", "PHP", "5", "&gt", ";", "=", "5", ".", "0", ".", "0", ")", "<br", "/", ">", "Offset", "to", "retrieve" ]
train
https://github.com/troydavisson/PHRETS/blob/8eb9c43dfea54760383e0fcba4f55468568f9fc2/src/Models/Metadata/Base.php#L106-L114
troydavisson/PHRETS
src/Models/Search/Results.php
Results.keyResultsBy
public function keyResultsBy($field) { $results = clone $this->results; $this->results = new Collection; foreach ($results as $r) { $this->addRecord($r, $field); } }
php
public function keyResultsBy($field) { $results = clone $this->results; $this->results = new Collection; foreach ($results as $r) { $this->addRecord($r, $field); } }
[ "public", "function", "keyResultsBy", "(", "$", "field", ")", "{", "$", "results", "=", "clone", "$", "this", "->", "results", ";", "$", "this", "->", "results", "=", "new", "Collection", ";", "foreach", "(", "$", "results", "as", "$", "r", ")", "{", "$", "this", "->", "addRecord", "(", "$", "r", ",", "$", "field", ")", ";", "}", "}" ]
Set which field's value will be used to key the records by @param $field
[ "Set", "which", "field", "s", "value", "will", "be", "used", "to", "key", "the", "records", "by" ]
train
https://github.com/troydavisson/PHRETS/blob/8eb9c43dfea54760383e0fcba4f55468568f9fc2/src/Models/Search/Results.php#L75-L82
troydavisson/PHRETS
src/Models/Search/Results.php
Results.lists
public function lists($field) { $l = []; foreach ($this->results as $r) { $v = $r->get($field); if ($v and !$r->isRestricted($field)) { $l[] = $v; } } return $l; }
php
public function lists($field) { $l = []; foreach ($this->results as $r) { $v = $r->get($field); if ($v and !$r->isRestricted($field)) { $l[] = $v; } } return $l; }
[ "public", "function", "lists", "(", "$", "field", ")", "{", "$", "l", "=", "[", "]", ";", "foreach", "(", "$", "this", "->", "results", "as", "$", "r", ")", "{", "$", "v", "=", "$", "r", "->", "get", "(", "$", "field", ")", ";", "if", "(", "$", "v", "and", "!", "$", "r", "->", "isRestricted", "(", "$", "field", ")", ")", "{", "$", "l", "[", "]", "=", "$", "v", ";", "}", "}", "return", "$", "l", ";", "}" ]
Returns an array containing the values from the given field @param $field @return array
[ "Returns", "an", "array", "containing", "the", "values", "from", "the", "given", "field" ]
train
https://github.com/troydavisson/PHRETS/blob/8eb9c43dfea54760383e0fcba4f55468568f9fc2/src/Models/Search/Results.php#L344-L354
troydavisson/PHRETS
src/Models/Search/Results.php
Results.toCSV
public function toCSV() { // create a temporary file so we can write the CSV out $writer = Writer::createFromFileObject(new SplTempFileObject); // add the header line $writer->insertOne($this->getHeaders()); // go through each record foreach ($this->results as $r) { $record = []; // go through each field and ensure that each record is prepared in an order consistent with the headers foreach ($this->getHeaders() as $h) { $record[] = $r->get($h); } $writer->insertOne($record); } // return as a string return (string) $writer; }
php
public function toCSV() { // create a temporary file so we can write the CSV out $writer = Writer::createFromFileObject(new SplTempFileObject); // add the header line $writer->insertOne($this->getHeaders()); // go through each record foreach ($this->results as $r) { $record = []; // go through each field and ensure that each record is prepared in an order consistent with the headers foreach ($this->getHeaders() as $h) { $record[] = $r->get($h); } $writer->insertOne($record); } // return as a string return (string) $writer; }
[ "public", "function", "toCSV", "(", ")", "{", "// create a temporary file so we can write the CSV out", "$", "writer", "=", "Writer", "::", "createFromFileObject", "(", "new", "SplTempFileObject", ")", ";", "// add the header line", "$", "writer", "->", "insertOne", "(", "$", "this", "->", "getHeaders", "(", ")", ")", ";", "// go through each record", "foreach", "(", "$", "this", "->", "results", "as", "$", "r", ")", "{", "$", "record", "=", "[", "]", ";", "// go through each field and ensure that each record is prepared in an order consistent with the headers", "foreach", "(", "$", "this", "->", "getHeaders", "(", ")", "as", "$", "h", ")", "{", "$", "record", "[", "]", "=", "$", "r", "->", "get", "(", "$", "h", ")", ";", "}", "$", "writer", "->", "insertOne", "(", "$", "record", ")", ";", "}", "// return as a string", "return", "(", "string", ")", "$", "writer", ";", "}" ]
Return results as a large prepared CSV string @return string
[ "Return", "results", "as", "a", "large", "prepared", "CSV", "string" ]
train
https://github.com/troydavisson/PHRETS/blob/8eb9c43dfea54760383e0fcba4f55468568f9fc2/src/Models/Search/Results.php#L361-L382
troydavisson/PHRETS
src/Models/Search/Results.php
Results.toArray
public function toArray() { $result = []; foreach ($this->results as $r) { $result[] = $r->toArray(); } return $result; }
php
public function toArray() { $result = []; foreach ($this->results as $r) { $result[] = $r->toArray(); } return $result; }
[ "public", "function", "toArray", "(", ")", "{", "$", "result", "=", "[", "]", ";", "foreach", "(", "$", "this", "->", "results", "as", "$", "r", ")", "{", "$", "result", "[", "]", "=", "$", "r", "->", "toArray", "(", ")", ";", "}", "return", "$", "result", ";", "}" ]
Return results as a simple array @return array
[ "Return", "results", "as", "a", "simple", "array" ]
train
https://github.com/troydavisson/PHRETS/blob/8eb9c43dfea54760383e0fcba4f55468568f9fc2/src/Models/Search/Results.php#L399-L406
jhedstrom/drupalextension
src/Drupal/DrupalExtension/Context/ConfigContext.php
ConfigContext.cleanConfig
public function cleanConfig() { // Revert config that was changed. foreach ($this->config as $name => $key_value) { foreach ($key_value as $key => $value) { $this->getDriver()->configSet($name, $key, $value); } } $this->config = array(); }
php
public function cleanConfig() { // Revert config that was changed. foreach ($this->config as $name => $key_value) { foreach ($key_value as $key => $value) { $this->getDriver()->configSet($name, $key, $value); } } $this->config = array(); }
[ "public", "function", "cleanConfig", "(", ")", "{", "// Revert config that was changed.", "foreach", "(", "$", "this", "->", "config", "as", "$", "name", "=>", "$", "key_value", ")", "{", "foreach", "(", "$", "key_value", "as", "$", "key", "=>", "$", "value", ")", "{", "$", "this", "->", "getDriver", "(", ")", "->", "configSet", "(", "$", "name", ",", "$", "key", ",", "$", "value", ")", ";", "}", "}", "$", "this", "->", "config", "=", "array", "(", ")", ";", "}" ]
Revert any changed config. @AfterScenario
[ "Revert", "any", "changed", "config", "." ]
train
https://github.com/jhedstrom/drupalextension/blob/d117c1a23b83c0e88b6224ed5d1d3458ce255b5a/src/Drupal/DrupalExtension/Context/ConfigContext.php#L39-L48
jhedstrom/drupalextension
src/Drupal/DrupalExtension/Context/ConfigContext.php
ConfigContext.setComplexConfig
public function setComplexConfig($name, $key, TableNode $config_table) { $value = array(); foreach ($config_table->getHash() as $row) { // Allow json values for extra complexity. if (json_decode($row['value'])) { $row['value'] = json_decode($row['value'], true); } $value[$row['key']] = $row['value']; } $this->setConfig($name, $key, $value); }
php
public function setComplexConfig($name, $key, TableNode $config_table) { $value = array(); foreach ($config_table->getHash() as $row) { // Allow json values for extra complexity. if (json_decode($row['value'])) { $row['value'] = json_decode($row['value'], true); } $value[$row['key']] = $row['value']; } $this->setConfig($name, $key, $value); }
[ "public", "function", "setComplexConfig", "(", "$", "name", ",", "$", "key", ",", "TableNode", "$", "config_table", ")", "{", "$", "value", "=", "array", "(", ")", ";", "foreach", "(", "$", "config_table", "->", "getHash", "(", ")", "as", "$", "row", ")", "{", "// Allow json values for extra complexity.", "if", "(", "json_decode", "(", "$", "row", "[", "'value'", "]", ")", ")", "{", "$", "row", "[", "'value'", "]", "=", "json_decode", "(", "$", "row", "[", "'value'", "]", ",", "true", ")", ";", "}", "$", "value", "[", "$", "row", "[", "'key'", "]", "]", "=", "$", "row", "[", "'value'", "]", ";", "}", "$", "this", "->", "setConfig", "(", "$", "name", ",", "$", "key", ",", "$", "value", ")", ";", "}" ]
Sets complex configuration. @param string $name The name of the configuration object. @param string $key Identifier to store value in configuration. @param TableNode $config_table The table listing configuration keys and values. @Given I set the configuration item :name with key :key with values: Provide configuration data in the following format: | key | value | | foo | bar |
[ "Sets", "complex", "configuration", "." ]
train
https://github.com/jhedstrom/drupalextension/blob/d117c1a23b83c0e88b6224ed5d1d3458ce255b5a/src/Drupal/DrupalExtension/Context/ConfigContext.php#L83-L94
jhedstrom/drupalextension
src/Drupal/DrupalExtension/Context/ConfigContext.php
ConfigContext.setConfig
public function setConfig($name, $key, $value) { $backup = $this->getDriver()->configGet($name, $key); $this->getDriver()->configSet($name, $key, $value); if (!array_key_exists($name, $this->config)) { $this->config[$name][$key] = $backup; return; } if (!array_key_exists($key, $this->config[$name])) { $this->config[$name][$key] = $backup; } }
php
public function setConfig($name, $key, $value) { $backup = $this->getDriver()->configGet($name, $key); $this->getDriver()->configSet($name, $key, $value); if (!array_key_exists($name, $this->config)) { $this->config[$name][$key] = $backup; return; } if (!array_key_exists($key, $this->config[$name])) { $this->config[$name][$key] = $backup; } }
[ "public", "function", "setConfig", "(", "$", "name", ",", "$", "key", ",", "$", "value", ")", "{", "$", "backup", "=", "$", "this", "->", "getDriver", "(", ")", "->", "configGet", "(", "$", "name", ",", "$", "key", ")", ";", "$", "this", "->", "getDriver", "(", ")", "->", "configSet", "(", "$", "name", ",", "$", "key", ",", "$", "value", ")", ";", "if", "(", "!", "array_key_exists", "(", "$", "name", ",", "$", "this", "->", "config", ")", ")", "{", "$", "this", "->", "config", "[", "$", "name", "]", "[", "$", "key", "]", "=", "$", "backup", ";", "return", ";", "}", "if", "(", "!", "array_key_exists", "(", "$", "key", ",", "$", "this", "->", "config", "[", "$", "name", "]", ")", ")", "{", "$", "this", "->", "config", "[", "$", "name", "]", "[", "$", "key", "]", "=", "$", "backup", ";", "}", "}" ]
Sets a value in a configuration object. @param string $name The name of the configuration object. @param string $key Identifier to store value in configuration. @param mixed $value Value to associate with identifier.
[ "Sets", "a", "value", "in", "a", "configuration", "object", "." ]
train
https://github.com/jhedstrom/drupalextension/blob/d117c1a23b83c0e88b6224ed5d1d3458ce255b5a/src/Drupal/DrupalExtension/Context/ConfigContext.php#L106-L118