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 39
1.84M
| func_code_tokens
sequencelengths 15
672k
| func_documentation_string
stringlengths 1
47.2k
| func_documentation_tokens
sequencelengths 1
3.92k
| split_name
stringclasses 1
value | func_code_url
stringlengths 85
339
|
---|---|---|---|---|---|---|---|---|---|---|
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.attr | public function attr($name, $value = null)
{
if ($value === null) {
return $this->getAttribute($name);
}
return $this->setAttribute($name, $value);
} | php | public function attr($name, $value = null)
{
if ($value === null) {
return $this->getAttribute($name);
}
return $this->setAttribute($name, $value);
} | [
"public",
"function",
"attr",
"(",
"$",
"name",
",",
"$",
"value",
"=",
"null",
")",
"{",
"if",
"(",
"$",
"value",
"===",
"null",
")",
"{",
"return",
"$",
"this",
"->",
"getAttribute",
"(",
"$",
"name",
")",
";",
"}",
"return",
"$",
"this",
"->",
"setAttribute",
"(",
"$",
"name",
",",
"$",
"value",
")",
";",
"}"
] | Alias for getAttribute and setAttribute methods.
@param string $name The attribute name
@param string|null $value The attribute value or null if the attribute does not exist
@return string|null|\DiDom\Element | [
"Alias",
"for",
"getAttribute",
"and",
"setAttribute",
"methods",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L550-L557 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.attributes | public function attributes(array $names = null)
{
if (!$this->node instanceof DOMElement) {
return null;
}
if ($names === null) {
$result = [];
foreach ($this->node->attributes as $name => $attribute) {
$result[$name] = $attribute->value;
}
return $result;
}
$result = [];
foreach ($this->node->attributes as $name => $attribute) {
if (in_array($name, $names, true)) {
$result[$name] = $attribute->value;
}
}
return $result;
} | php | public function attributes(array $names = null)
{
if (!$this->node instanceof DOMElement) {
return null;
}
if ($names === null) {
$result = [];
foreach ($this->node->attributes as $name => $attribute) {
$result[$name] = $attribute->value;
}
return $result;
}
$result = [];
foreach ($this->node->attributes as $name => $attribute) {
if (in_array($name, $names, true)) {
$result[$name] = $attribute->value;
}
}
return $result;
} | [
"public",
"function",
"attributes",
"(",
"array",
"$",
"names",
"=",
"null",
")",
"{",
"if",
"(",
"!",
"$",
"this",
"->",
"node",
"instanceof",
"DOMElement",
")",
"{",
"return",
"null",
";",
"}",
"if",
"(",
"$",
"names",
"===",
"null",
")",
"{",
"$",
"result",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"this",
"->",
"node",
"->",
"attributes",
"as",
"$",
"name",
"=>",
"$",
"attribute",
")",
"{",
"$",
"result",
"[",
"$",
"name",
"]",
"=",
"$",
"attribute",
"->",
"value",
";",
"}",
"return",
"$",
"result",
";",
"}",
"$",
"result",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"this",
"->",
"node",
"->",
"attributes",
"as",
"$",
"name",
"=>",
"$",
"attribute",
")",
"{",
"if",
"(",
"in_array",
"(",
"$",
"name",
",",
"$",
"names",
",",
"true",
")",
")",
"{",
"$",
"result",
"[",
"$",
"name",
"]",
"=",
"$",
"attribute",
"->",
"value",
";",
"}",
"}",
"return",
"$",
"result",
";",
"}"
] | Returns the node attributes or null, if it is not DOMElement.
@param string[] $names
@return array|null | [
"Returns",
"the",
"node",
"attributes",
"or",
"null",
"if",
"it",
"is",
"not",
"DOMElement",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L566-L591 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.classes | public function classes()
{
if ($this->classAttribute !== null) {
return $this->classAttribute;
}
if (!$this->isElementNode()) {
throw new LogicException('Class attribute is available only for element nodes');
}
$this->classAttribute = new ClassAttribute($this);
return $this->classAttribute;
} | php | public function classes()
{
if ($this->classAttribute !== null) {
return $this->classAttribute;
}
if (!$this->isElementNode()) {
throw new LogicException('Class attribute is available only for element nodes');
}
$this->classAttribute = new ClassAttribute($this);
return $this->classAttribute;
} | [
"public",
"function",
"classes",
"(",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"classAttribute",
"!==",
"null",
")",
"{",
"return",
"$",
"this",
"->",
"classAttribute",
";",
"}",
"if",
"(",
"!",
"$",
"this",
"->",
"isElementNode",
"(",
")",
")",
"{",
"throw",
"new",
"LogicException",
"(",
"'Class attribute is available only for element nodes'",
")",
";",
"}",
"$",
"this",
"->",
"classAttribute",
"=",
"new",
"ClassAttribute",
"(",
"$",
"this",
")",
";",
"return",
"$",
"this",
"->",
"classAttribute",
";",
"}"
] | @return \DiDom\ClassAttribute
@throws \LogicException if the node is not an instance of \DOMElement | [
"@return",
"\\",
"DiDom",
"\\",
"ClassAttribute"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L598-L611 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.style | public function style()
{
if ($this->styleAttribute !== null) {
return $this->styleAttribute;
}
if (!$this->isElementNode()) {
throw new LogicException('Style attribute is available only for element nodes');
}
$this->styleAttribute = new StyleAttribute($this);
return $this->styleAttribute;
} | php | public function style()
{
if ($this->styleAttribute !== null) {
return $this->styleAttribute;
}
if (!$this->isElementNode()) {
throw new LogicException('Style attribute is available only for element nodes');
}
$this->styleAttribute = new StyleAttribute($this);
return $this->styleAttribute;
} | [
"public",
"function",
"style",
"(",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"styleAttribute",
"!==",
"null",
")",
"{",
"return",
"$",
"this",
"->",
"styleAttribute",
";",
"}",
"if",
"(",
"!",
"$",
"this",
"->",
"isElementNode",
"(",
")",
")",
"{",
"throw",
"new",
"LogicException",
"(",
"'Style attribute is available only for element nodes'",
")",
";",
"}",
"$",
"this",
"->",
"styleAttribute",
"=",
"new",
"StyleAttribute",
"(",
"$",
"this",
")",
";",
"return",
"$",
"this",
"->",
"styleAttribute",
";",
"}"
] | @return \DiDom\StyleAttribute
@throws \LogicException if the node is not an instance of \DOMElement | [
"@return",
"\\",
"DiDom",
"\\",
"StyleAttribute"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L618-L631 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.outerHtml | public function outerHtml()
{
$document = new DOMDocument();
$importedNode = $document->importNode($this->node);
return $document->saveHTML($importedNode);
} | php | public function outerHtml()
{
$document = new DOMDocument();
$importedNode = $document->importNode($this->node);
return $document->saveHTML($importedNode);
} | [
"public",
"function",
"outerHtml",
"(",
")",
"{",
"$",
"document",
"=",
"new",
"DOMDocument",
"(",
")",
";",
"$",
"importedNode",
"=",
"$",
"document",
"->",
"importNode",
"(",
"$",
"this",
"->",
"node",
")",
";",
"return",
"$",
"document",
"->",
"saveHTML",
"(",
"$",
"importedNode",
")",
";",
"}"
] | Dumps the node into a string using HTML formatting (without child nodes).
@return string | [
"Dumps",
"the",
"node",
"into",
"a",
"string",
"using",
"HTML",
"formatting",
"(",
"without",
"child",
"nodes",
")",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L648-L655 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.innerHtml | public function innerHtml($delimiter = '')
{
$innerHtml = [];
foreach ($this->node->childNodes as $childNode) {
$innerHtml[] = $childNode->ownerDocument->saveHTML($childNode);
}
return implode($delimiter, $innerHtml);
} | php | public function innerHtml($delimiter = '')
{
$innerHtml = [];
foreach ($this->node->childNodes as $childNode) {
$innerHtml[] = $childNode->ownerDocument->saveHTML($childNode);
}
return implode($delimiter, $innerHtml);
} | [
"public",
"function",
"innerHtml",
"(",
"$",
"delimiter",
"=",
"''",
")",
"{",
"$",
"innerHtml",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"this",
"->",
"node",
"->",
"childNodes",
"as",
"$",
"childNode",
")",
"{",
"$",
"innerHtml",
"[",
"]",
"=",
"$",
"childNode",
"->",
"ownerDocument",
"->",
"saveHTML",
"(",
"$",
"childNode",
")",
";",
"}",
"return",
"implode",
"(",
"$",
"delimiter",
",",
"$",
"innerHtml",
")",
";",
"}"
] | Dumps the node descendants into a string using HTML formatting.
@param string $delimiter
@return string | [
"Dumps",
"the",
"node",
"descendants",
"into",
"a",
"string",
"using",
"HTML",
"formatting",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L664-L673 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.innerXml | public function innerXml($delimiter = '')
{
$innerXml = [];
foreach ($this->node->childNodes as $childNode) {
$innerXml[] = $childNode->ownerDocument->saveXML($childNode);
}
return implode($delimiter, $innerXml);
} | php | public function innerXml($delimiter = '')
{
$innerXml = [];
foreach ($this->node->childNodes as $childNode) {
$innerXml[] = $childNode->ownerDocument->saveXML($childNode);
}
return implode($delimiter, $innerXml);
} | [
"public",
"function",
"innerXml",
"(",
"$",
"delimiter",
"=",
"''",
")",
"{",
"$",
"innerXml",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"this",
"->",
"node",
"->",
"childNodes",
"as",
"$",
"childNode",
")",
"{",
"$",
"innerXml",
"[",
"]",
"=",
"$",
"childNode",
"->",
"ownerDocument",
"->",
"saveXML",
"(",
"$",
"childNode",
")",
";",
"}",
"return",
"implode",
"(",
"$",
"delimiter",
",",
"$",
"innerXml",
")",
";",
"}"
] | Dumps the node descendants into a string using XML formatting.
@param string $delimiter
@return string | [
"Dumps",
"the",
"node",
"descendants",
"into",
"a",
"string",
"using",
"XML",
"formatting",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L682-L691 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.setInnerHtml | public function setInnerHtml($html)
{
if (!is_string($html)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, (is_object($html) ? get_class($html) : gettype($html))));
}
$this->removeChildren();
if ($html !== '') {
Errors::disable();
$html = "<htmlfragment>$html</htmlfragment>";
$document = new Document($html);
$fragment = $document->first('htmlfragment')->getNode();
foreach ($fragment->childNodes as $node) {
$newNode = $this->node->ownerDocument->importNode($node, true);
$this->node->appendChild($newNode);
}
Errors::restore();
}
return $this;
} | php | public function setInnerHtml($html)
{
if (!is_string($html)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, (is_object($html) ? get_class($html) : gettype($html))));
}
$this->removeChildren();
if ($html !== '') {
Errors::disable();
$html = "<htmlfragment>$html</htmlfragment>";
$document = new Document($html);
$fragment = $document->first('htmlfragment')->getNode();
foreach ($fragment->childNodes as $node) {
$newNode = $this->node->ownerDocument->importNode($node, true);
$this->node->appendChild($newNode);
}
Errors::restore();
}
return $this;
} | [
"public",
"function",
"setInnerHtml",
"(",
"$",
"html",
")",
"{",
"if",
"(",
"!",
"is_string",
"(",
"$",
"html",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'%s expects parameter 1 to be string, %s given'",
",",
"__METHOD__",
",",
"(",
"is_object",
"(",
"$",
"html",
")",
"?",
"get_class",
"(",
"$",
"html",
")",
":",
"gettype",
"(",
"$",
"html",
")",
")",
")",
")",
";",
"}",
"$",
"this",
"->",
"removeChildren",
"(",
")",
";",
"if",
"(",
"$",
"html",
"!==",
"''",
")",
"{",
"Errors",
"::",
"disable",
"(",
")",
";",
"$",
"html",
"=",
"\"<htmlfragment>$html</htmlfragment>\"",
";",
"$",
"document",
"=",
"new",
"Document",
"(",
"$",
"html",
")",
";",
"$",
"fragment",
"=",
"$",
"document",
"->",
"first",
"(",
"'htmlfragment'",
")",
"->",
"getNode",
"(",
")",
";",
"foreach",
"(",
"$",
"fragment",
"->",
"childNodes",
"as",
"$",
"node",
")",
"{",
"$",
"newNode",
"=",
"$",
"this",
"->",
"node",
"->",
"ownerDocument",
"->",
"importNode",
"(",
"$",
"node",
",",
"true",
")",
";",
"$",
"this",
"->",
"node",
"->",
"appendChild",
"(",
"$",
"newNode",
")",
";",
"}",
"Errors",
"::",
"restore",
"(",
")",
";",
"}",
"return",
"$",
"this",
";",
"}"
] | Sets inner HTML.
@param string $html
@return \DiDom\Element
@throws InvalidArgumentException if passed argument is not a string | [
"Sets",
"inner",
"HTML",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L702-L729 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.setValue | public function setValue($value)
{
if (is_numeric($value)) {
$value = (string) $value;
}
if (!is_string($value) && $value !== null) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, (is_object($value) ? get_class($value) : gettype($value))));
}
$this->node->nodeValue = $value;
return $this;
} | php | public function setValue($value)
{
if (is_numeric($value)) {
$value = (string) $value;
}
if (!is_string($value) && $value !== null) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, (is_object($value) ? get_class($value) : gettype($value))));
}
$this->node->nodeValue = $value;
return $this;
} | [
"public",
"function",
"setValue",
"(",
"$",
"value",
")",
"{",
"if",
"(",
"is_numeric",
"(",
"$",
"value",
")",
")",
"{",
"$",
"value",
"=",
"(",
"string",
")",
"$",
"value",
";",
"}",
"if",
"(",
"!",
"is_string",
"(",
"$",
"value",
")",
"&&",
"$",
"value",
"!==",
"null",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'%s expects parameter 1 to be string, %s given'",
",",
"__METHOD__",
",",
"(",
"is_object",
"(",
"$",
"value",
")",
"?",
"get_class",
"(",
"$",
"value",
")",
":",
"gettype",
"(",
"$",
"value",
")",
")",
")",
")",
";",
"}",
"$",
"this",
"->",
"node",
"->",
"nodeValue",
"=",
"$",
"value",
";",
"return",
"$",
"this",
";",
"}"
] | Set the value of this node.
@param string $value The new value of the node
@return \DiDom\Element
@throws \InvalidArgumentException if value is not string | [
"Set",
"the",
"value",
"of",
"this",
"node",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L762-L775 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.is | public function is($node)
{
if ($node instanceof Element) {
$node = $node->getNode();
}
if (!$node instanceof DOMNode) {
throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of %s or DOMNode, %s given', __METHOD__, __CLASS__, (is_object($node) ? get_class($node) : gettype($node))));
}
return $this->node->isSameNode($node);
} | php | public function is($node)
{
if ($node instanceof Element) {
$node = $node->getNode();
}
if (!$node instanceof DOMNode) {
throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of %s or DOMNode, %s given', __METHOD__, __CLASS__, (is_object($node) ? get_class($node) : gettype($node))));
}
return $this->node->isSameNode($node);
} | [
"public",
"function",
"is",
"(",
"$",
"node",
")",
"{",
"if",
"(",
"$",
"node",
"instanceof",
"Element",
")",
"{",
"$",
"node",
"=",
"$",
"node",
"->",
"getNode",
"(",
")",
";",
"}",
"if",
"(",
"!",
"$",
"node",
"instanceof",
"DOMNode",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'Argument 1 passed to %s must be an instance of %s or DOMNode, %s given'",
",",
"__METHOD__",
",",
"__CLASS__",
",",
"(",
"is_object",
"(",
"$",
"node",
")",
"?",
"get_class",
"(",
"$",
"node",
")",
":",
"gettype",
"(",
"$",
"node",
")",
")",
")",
")",
";",
"}",
"return",
"$",
"this",
"->",
"node",
"->",
"isSameNode",
"(",
"$",
"node",
")",
";",
"}"
] | Indicates if two nodes are the same node.
@param \DiDom\Element|\DOMNode $node
@return bool
@throws \InvalidArgumentException if the provided argument is not an instance of \DOMNode | [
"Indicates",
"if",
"two",
"nodes",
"are",
"the",
"same",
"node",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L826-L837 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.closest | public function closest($selector, $strict = false)
{
$node = $this;
while (true) {
$parent = $node->parent();
if ($parent === null || $parent instanceof Document) {
return null;
}
if ($parent->matches($selector, $strict)) {
return $parent;
}
$node = $parent;
}
} | php | public function closest($selector, $strict = false)
{
$node = $this;
while (true) {
$parent = $node->parent();
if ($parent === null || $parent instanceof Document) {
return null;
}
if ($parent->matches($selector, $strict)) {
return $parent;
}
$node = $parent;
}
} | [
"public",
"function",
"closest",
"(",
"$",
"selector",
",",
"$",
"strict",
"=",
"false",
")",
"{",
"$",
"node",
"=",
"$",
"this",
";",
"while",
"(",
"true",
")",
"{",
"$",
"parent",
"=",
"$",
"node",
"->",
"parent",
"(",
")",
";",
"if",
"(",
"$",
"parent",
"===",
"null",
"||",
"$",
"parent",
"instanceof",
"Document",
")",
"{",
"return",
"null",
";",
"}",
"if",
"(",
"$",
"parent",
"->",
"matches",
"(",
"$",
"selector",
",",
"$",
"strict",
")",
")",
"{",
"return",
"$",
"parent",
";",
"}",
"$",
"node",
"=",
"$",
"parent",
";",
"}",
"}"
] | Returns first parent node matches passed selector.
@param string $selector
@param bool $strict
@return \DiDom\Element|null | [
"Returns",
"first",
"parent",
"node",
"matches",
"passed",
"selector",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L863-L880 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.previousSiblings | public function previousSiblings($selector = null, $nodeType = null)
{
if ($this->node->previousSibling === null) {
return [];
}
if ($selector !== null && $nodeType === null) {
$nodeType = 'DOMElement';
}
if ($nodeType !== null) {
if (!is_string($nodeType)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 2 to be string, %s given', __METHOD__, gettype($nodeType)));
}
$allowedTypes = ['DOMElement', 'DOMText', 'DOMComment', 'DOMCdataSection'];
if (!in_array($nodeType, $allowedTypes, true)) {
throw new RuntimeException(sprintf('Unknown node type "%s". Allowed types: %s', $nodeType, implode(', ', $allowedTypes)));
}
}
if ($selector !== null && $nodeType !== 'DOMElement') {
throw new LogicException(sprintf('Selector can be used only with DOMElement node type, %s given', $nodeType));
}
$result = [];
$node = $this->node->previousSibling;
while ($node !== null) {
$element = new Element($node);
if ($nodeType === null) {
$result[] = $element;
$node = $node->previousSibling;
continue;
}
if (get_class($node) !== $nodeType) {
$node = $node->previousSibling;
continue;
}
if ($selector === null) {
$result[] = $element;
$node = $node->previousSibling;
continue;
}
if ($element->matches($selector)) {
$result[] = $element;
}
$node = $node->previousSibling;
}
return array_reverse($result);
} | php | public function previousSiblings($selector = null, $nodeType = null)
{
if ($this->node->previousSibling === null) {
return [];
}
if ($selector !== null && $nodeType === null) {
$nodeType = 'DOMElement';
}
if ($nodeType !== null) {
if (!is_string($nodeType)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 2 to be string, %s given', __METHOD__, gettype($nodeType)));
}
$allowedTypes = ['DOMElement', 'DOMText', 'DOMComment', 'DOMCdataSection'];
if (!in_array($nodeType, $allowedTypes, true)) {
throw new RuntimeException(sprintf('Unknown node type "%s". Allowed types: %s', $nodeType, implode(', ', $allowedTypes)));
}
}
if ($selector !== null && $nodeType !== 'DOMElement') {
throw new LogicException(sprintf('Selector can be used only with DOMElement node type, %s given', $nodeType));
}
$result = [];
$node = $this->node->previousSibling;
while ($node !== null) {
$element = new Element($node);
if ($nodeType === null) {
$result[] = $element;
$node = $node->previousSibling;
continue;
}
if (get_class($node) !== $nodeType) {
$node = $node->previousSibling;
continue;
}
if ($selector === null) {
$result[] = $element;
$node = $node->previousSibling;
continue;
}
if ($element->matches($selector)) {
$result[] = $element;
}
$node = $node->previousSibling;
}
return array_reverse($result);
} | [
"public",
"function",
"previousSiblings",
"(",
"$",
"selector",
"=",
"null",
",",
"$",
"nodeType",
"=",
"null",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"node",
"->",
"previousSibling",
"===",
"null",
")",
"{",
"return",
"[",
"]",
";",
"}",
"if",
"(",
"$",
"selector",
"!==",
"null",
"&&",
"$",
"nodeType",
"===",
"null",
")",
"{",
"$",
"nodeType",
"=",
"'DOMElement'",
";",
"}",
"if",
"(",
"$",
"nodeType",
"!==",
"null",
")",
"{",
"if",
"(",
"!",
"is_string",
"(",
"$",
"nodeType",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'%s expects parameter 2 to be string, %s given'",
",",
"__METHOD__",
",",
"gettype",
"(",
"$",
"nodeType",
")",
")",
")",
";",
"}",
"$",
"allowedTypes",
"=",
"[",
"'DOMElement'",
",",
"'DOMText'",
",",
"'DOMComment'",
",",
"'DOMCdataSection'",
"]",
";",
"if",
"(",
"!",
"in_array",
"(",
"$",
"nodeType",
",",
"$",
"allowedTypes",
",",
"true",
")",
")",
"{",
"throw",
"new",
"RuntimeException",
"(",
"sprintf",
"(",
"'Unknown node type \"%s\". Allowed types: %s'",
",",
"$",
"nodeType",
",",
"implode",
"(",
"', '",
",",
"$",
"allowedTypes",
")",
")",
")",
";",
"}",
"}",
"if",
"(",
"$",
"selector",
"!==",
"null",
"&&",
"$",
"nodeType",
"!==",
"'DOMElement'",
")",
"{",
"throw",
"new",
"LogicException",
"(",
"sprintf",
"(",
"'Selector can be used only with DOMElement node type, %s given'",
",",
"$",
"nodeType",
")",
")",
";",
"}",
"$",
"result",
"=",
"[",
"]",
";",
"$",
"node",
"=",
"$",
"this",
"->",
"node",
"->",
"previousSibling",
";",
"while",
"(",
"$",
"node",
"!==",
"null",
")",
"{",
"$",
"element",
"=",
"new",
"Element",
"(",
"$",
"node",
")",
";",
"if",
"(",
"$",
"nodeType",
"===",
"null",
")",
"{",
"$",
"result",
"[",
"]",
"=",
"$",
"element",
";",
"$",
"node",
"=",
"$",
"node",
"->",
"previousSibling",
";",
"continue",
";",
"}",
"if",
"(",
"get_class",
"(",
"$",
"node",
")",
"!==",
"$",
"nodeType",
")",
"{",
"$",
"node",
"=",
"$",
"node",
"->",
"previousSibling",
";",
"continue",
";",
"}",
"if",
"(",
"$",
"selector",
"===",
"null",
")",
"{",
"$",
"result",
"[",
"]",
"=",
"$",
"element",
";",
"$",
"node",
"=",
"$",
"node",
"->",
"previousSibling",
";",
"continue",
";",
"}",
"if",
"(",
"$",
"element",
"->",
"matches",
"(",
"$",
"selector",
")",
")",
"{",
"$",
"result",
"[",
"]",
"=",
"$",
"element",
";",
"}",
"$",
"node",
"=",
"$",
"node",
"->",
"previousSibling",
";",
"}",
"return",
"array_reverse",
"(",
"$",
"result",
")",
";",
"}"
] | @param string|null $selector
@param string|null $nodeType
@return \DiDom\Element[]
@throws \InvalidArgumentException if node type is not string
@throws \RuntimeException if node type is invalid
@throws \LogicException if selector used with non DOMElement node type | [
"@param",
"string|null",
"$selector",
"@param",
"string|null",
"$nodeType"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L955-L1018 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.child | public function child($index)
{
$child = $this->node->childNodes->item($index);
return $child === null ? null : new Element($child);
} | php | public function child($index)
{
$child = $this->node->childNodes->item($index);
return $child === null ? null : new Element($child);
} | [
"public",
"function",
"child",
"(",
"$",
"index",
")",
"{",
"$",
"child",
"=",
"$",
"this",
"->",
"node",
"->",
"childNodes",
"->",
"item",
"(",
"$",
"index",
")",
";",
"return",
"$",
"child",
"===",
"null",
"?",
"null",
":",
"new",
"Element",
"(",
"$",
"child",
")",
";",
"}"
] | @param int $index
@return \DiDom\Element|null | [
"@param",
"int",
"$index"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L1163-L1168 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.removeChild | public function removeChild($childNode)
{
if ($childNode instanceof Element) {
$childNode = $childNode->getNode();
}
if (!$childNode instanceof DOMNode) {
throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of %s or DOMNode, %s given', __METHOD__, __CLASS__, (is_object($childNode) ? get_class($childNode) : gettype($childNode))));
}
$removedNode = $this->node->removeChild($childNode);
return new Element($removedNode);
} | php | public function removeChild($childNode)
{
if ($childNode instanceof Element) {
$childNode = $childNode->getNode();
}
if (!$childNode instanceof DOMNode) {
throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of %s or DOMNode, %s given', __METHOD__, __CLASS__, (is_object($childNode) ? get_class($childNode) : gettype($childNode))));
}
$removedNode = $this->node->removeChild($childNode);
return new Element($removedNode);
} | [
"public",
"function",
"removeChild",
"(",
"$",
"childNode",
")",
"{",
"if",
"(",
"$",
"childNode",
"instanceof",
"Element",
")",
"{",
"$",
"childNode",
"=",
"$",
"childNode",
"->",
"getNode",
"(",
")",
";",
"}",
"if",
"(",
"!",
"$",
"childNode",
"instanceof",
"DOMNode",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'Argument 1 passed to %s must be an instance of %s or DOMNode, %s given'",
",",
"__METHOD__",
",",
"__CLASS__",
",",
"(",
"is_object",
"(",
"$",
"childNode",
")",
"?",
"get_class",
"(",
"$",
"childNode",
")",
":",
"gettype",
"(",
"$",
"childNode",
")",
")",
")",
")",
";",
"}",
"$",
"removedNode",
"=",
"$",
"this",
"->",
"node",
"->",
"removeChild",
"(",
"$",
"childNode",
")",
";",
"return",
"new",
"Element",
"(",
"$",
"removedNode",
")",
";",
"}"
] | Removes child from list of children.
@param \DOMNode|\DiDom\Element $childNode
@return \DiDom\Element the node that has been removed | [
"Removes",
"child",
"from",
"list",
"of",
"children",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L1223-L1236 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.removeChildren | public function removeChildren()
{
// we need to collect child nodes to array
// because removing nodes from the DOMNodeList on iterating is not working
$childNodes = [];
foreach ($this->node->childNodes as $childNode) {
$childNodes[] = $childNode;
}
$removedNodes = [];
foreach ($childNodes as $childNode) {
$removedNode = $this->node->removeChild($childNode);
$removedNodes[] = new Element($removedNode);
}
return $removedNodes;
} | php | public function removeChildren()
{
$childNodes = [];
foreach ($this->node->childNodes as $childNode) {
$childNodes[] = $childNode;
}
$removedNodes = [];
foreach ($childNodes as $childNode) {
$removedNode = $this->node->removeChild($childNode);
$removedNodes[] = new Element($removedNode);
}
return $removedNodes;
} | [
"public",
"function",
"removeChildren",
"(",
")",
"{",
"// we need to collect child nodes to array",
"// because removing nodes from the DOMNodeList on iterating is not working",
"$",
"childNodes",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"this",
"->",
"node",
"->",
"childNodes",
"as",
"$",
"childNode",
")",
"{",
"$",
"childNodes",
"[",
"]",
"=",
"$",
"childNode",
";",
"}",
"$",
"removedNodes",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"childNodes",
"as",
"$",
"childNode",
")",
"{",
"$",
"removedNode",
"=",
"$",
"this",
"->",
"node",
"->",
"removeChild",
"(",
"$",
"childNode",
")",
";",
"$",
"removedNodes",
"[",
"]",
"=",
"new",
"Element",
"(",
"$",
"removedNode",
")",
";",
"}",
"return",
"$",
"removedNodes",
";",
"}"
] | Removes all child nodes.
@return \DiDom\Element[] the nodes that has been removed | [
"Removes",
"all",
"child",
"nodes",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L1243-L1262 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.remove | public function remove()
{
if ($this->node->parentNode === null) {
throw new LogicException('Can not remove element without parent node');
}
$removedNode = $this->node->parentNode->removeChild($this->node);
return new Element($removedNode);
} | php | public function remove()
{
if ($this->node->parentNode === null) {
throw new LogicException('Can not remove element without parent node');
}
$removedNode = $this->node->parentNode->removeChild($this->node);
return new Element($removedNode);
} | [
"public",
"function",
"remove",
"(",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"node",
"->",
"parentNode",
"===",
"null",
")",
"{",
"throw",
"new",
"LogicException",
"(",
"'Can not remove element without parent node'",
")",
";",
"}",
"$",
"removedNode",
"=",
"$",
"this",
"->",
"node",
"->",
"parentNode",
"->",
"removeChild",
"(",
"$",
"this",
"->",
"node",
")",
";",
"return",
"new",
"Element",
"(",
"$",
"removedNode",
")",
";",
"}"
] | Removes current node from the parent.
@return \DiDom\Element the node that has been removed
@throws \LogicException if current node has no parent node | [
"Removes",
"current",
"node",
"from",
"the",
"parent",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L1271-L1280 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.replace | public function replace($newNode, $clone = true)
{
if ($this->node->parentNode === null) {
throw new LogicException('Can not replace element without parent node');
}
if ($newNode instanceof Element) {
$newNode = $newNode->getNode();
}
if (!$newNode instanceof DOMNode) {
throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of %s or DOMNode, %s given', __METHOD__, __CLASS__, (is_object($newNode) ? get_class($newNode) : gettype($newNode))));
}
if ($clone) {
$newNode = $newNode->cloneNode(true);
}
if ($newNode->ownerDocument === null || !$this->getDocument()->is($newNode->ownerDocument)) {
$newNode = $this->node->ownerDocument->importNode($newNode, true);
}
$node = $this->node->parentNode->replaceChild($newNode, $this->node);
return new Element($node);
} | php | public function replace($newNode, $clone = true)
{
if ($this->node->parentNode === null) {
throw new LogicException('Can not replace element without parent node');
}
if ($newNode instanceof Element) {
$newNode = $newNode->getNode();
}
if (!$newNode instanceof DOMNode) {
throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of %s or DOMNode, %s given', __METHOD__, __CLASS__, (is_object($newNode) ? get_class($newNode) : gettype($newNode))));
}
if ($clone) {
$newNode = $newNode->cloneNode(true);
}
if ($newNode->ownerDocument === null || !$this->getDocument()->is($newNode->ownerDocument)) {
$newNode = $this->node->ownerDocument->importNode($newNode, true);
}
$node = $this->node->parentNode->replaceChild($newNode, $this->node);
return new Element($node);
} | [
"public",
"function",
"replace",
"(",
"$",
"newNode",
",",
"$",
"clone",
"=",
"true",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"node",
"->",
"parentNode",
"===",
"null",
")",
"{",
"throw",
"new",
"LogicException",
"(",
"'Can not replace element without parent node'",
")",
";",
"}",
"if",
"(",
"$",
"newNode",
"instanceof",
"Element",
")",
"{",
"$",
"newNode",
"=",
"$",
"newNode",
"->",
"getNode",
"(",
")",
";",
"}",
"if",
"(",
"!",
"$",
"newNode",
"instanceof",
"DOMNode",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'Argument 1 passed to %s must be an instance of %s or DOMNode, %s given'",
",",
"__METHOD__",
",",
"__CLASS__",
",",
"(",
"is_object",
"(",
"$",
"newNode",
")",
"?",
"get_class",
"(",
"$",
"newNode",
")",
":",
"gettype",
"(",
"$",
"newNode",
")",
")",
")",
")",
";",
"}",
"if",
"(",
"$",
"clone",
")",
"{",
"$",
"newNode",
"=",
"$",
"newNode",
"->",
"cloneNode",
"(",
"true",
")",
";",
"}",
"if",
"(",
"$",
"newNode",
"->",
"ownerDocument",
"===",
"null",
"||",
"!",
"$",
"this",
"->",
"getDocument",
"(",
")",
"->",
"is",
"(",
"$",
"newNode",
"->",
"ownerDocument",
")",
")",
"{",
"$",
"newNode",
"=",
"$",
"this",
"->",
"node",
"->",
"ownerDocument",
"->",
"importNode",
"(",
"$",
"newNode",
",",
"true",
")",
";",
"}",
"$",
"node",
"=",
"$",
"this",
"->",
"node",
"->",
"parentNode",
"->",
"replaceChild",
"(",
"$",
"newNode",
",",
"$",
"this",
"->",
"node",
")",
";",
"return",
"new",
"Element",
"(",
"$",
"node",
")",
";",
"}"
] | Replaces a child.
@param \DOMNode|\DiDom\Element $newNode The new node
@param bool $clone Clone the node if true, otherwise move it
@return \DiDom\Element The node that has been replaced
@throws \LogicException if current node has no parent node | [
"Replaces",
"a",
"child",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L1292-L1317 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.setNode | protected function setNode($node)
{
$allowedClasses = ['DOMElement', 'DOMText', 'DOMComment', 'DOMCdataSection'];
if (!is_object($node) || !in_array(get_class($node), $allowedClasses, true)) {
throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of DOMElement, DOMText, DOMComment or DOMCdataSection, %s given', __METHOD__, (is_object($node) ? get_class($node) : gettype($node))));
}
$this->node = $node;
return $this;
} | php | protected function setNode($node)
{
$allowedClasses = ['DOMElement', 'DOMText', 'DOMComment', 'DOMCdataSection'];
if (!is_object($node) || !in_array(get_class($node), $allowedClasses, true)) {
throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of DOMElement, DOMText, DOMComment or DOMCdataSection, %s given', __METHOD__, (is_object($node) ? get_class($node) : gettype($node))));
}
$this->node = $node;
return $this;
} | [
"protected",
"function",
"setNode",
"(",
"$",
"node",
")",
"{",
"$",
"allowedClasses",
"=",
"[",
"'DOMElement'",
",",
"'DOMText'",
",",
"'DOMComment'",
",",
"'DOMCdataSection'",
"]",
";",
"if",
"(",
"!",
"is_object",
"(",
"$",
"node",
")",
"||",
"!",
"in_array",
"(",
"get_class",
"(",
"$",
"node",
")",
",",
"$",
"allowedClasses",
",",
"true",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'Argument 1 passed to %s must be an instance of DOMElement, DOMText, DOMComment or DOMCdataSection, %s given'",
",",
"__METHOD__",
",",
"(",
"is_object",
"(",
"$",
"node",
")",
"?",
"get_class",
"(",
"$",
"node",
")",
":",
"gettype",
"(",
"$",
"node",
")",
")",
")",
")",
";",
"}",
"$",
"this",
"->",
"node",
"=",
"$",
"node",
";",
"return",
"$",
"this",
";",
"}"
] | Sets current node instance.
@param \DOMElement|\DOMText|\DOMComment|\DOMCdataSection $node
@return \DiDom\Element | [
"Sets",
"current",
"node",
"instance",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L1348-L1359 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.getDocument | public function getDocument()
{
if ($this->node->ownerDocument === null) {
return null;
}
return new Document($this->node->ownerDocument);
} | php | public function getDocument()
{
if ($this->node->ownerDocument === null) {
return null;
}
return new Document($this->node->ownerDocument);
} | [
"public",
"function",
"getDocument",
"(",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"node",
"->",
"ownerDocument",
"===",
"null",
")",
"{",
"return",
"null",
";",
"}",
"return",
"new",
"Document",
"(",
"$",
"this",
"->",
"node",
"->",
"ownerDocument",
")",
";",
"}"
] | Returns the document associated with this node.
@return \DiDom\Document|null | [
"Returns",
"the",
"document",
"associated",
"with",
"this",
"node",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L1376-L1383 |
Imangazaliev/DiDOM | src/DiDom/Element.php | Element.toDocument | public function toDocument($encoding = 'UTF-8')
{
$document = new Document(null, false, $encoding);
$document->appendChild($this->node);
return $document;
} | php | public function toDocument($encoding = 'UTF-8')
{
$document = new Document(null, false, $encoding);
$document->appendChild($this->node);
return $document;
} | [
"public",
"function",
"toDocument",
"(",
"$",
"encoding",
"=",
"'UTF-8'",
")",
"{",
"$",
"document",
"=",
"new",
"Document",
"(",
"null",
",",
"false",
",",
"$",
"encoding",
")",
";",
"$",
"document",
"->",
"appendChild",
"(",
"$",
"this",
"->",
"node",
")",
";",
"return",
"$",
"document",
";",
"}"
] | Get the DOM document with the current element.
@param string $encoding The document encoding
@return \DiDom\Document | [
"Get",
"the",
"DOM",
"document",
"with",
"the",
"current",
"element",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Element.php#L1392-L1399 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.create | public static function create($string = null, $isFile = false, $encoding = 'UTF-8', $type = Document::TYPE_HTML)
{
return new Document($string, $isFile, $encoding, $type);
} | php | public static function create($string = null, $isFile = false, $encoding = 'UTF-8', $type = Document::TYPE_HTML)
{
return new Document($string, $isFile, $encoding, $type);
} | [
"public",
"static",
"function",
"create",
"(",
"$",
"string",
"=",
"null",
",",
"$",
"isFile",
"=",
"false",
",",
"$",
"encoding",
"=",
"'UTF-8'",
",",
"$",
"type",
"=",
"Document",
"::",
"TYPE_HTML",
")",
"{",
"return",
"new",
"Document",
"(",
"$",
"string",
",",
"$",
"isFile",
",",
"$",
"encoding",
",",
"$",
"type",
")",
";",
"}"
] | Creates a new document.
@param string|null $string An HTML or XML string or a file path
@param bool $isFile Indicates that the first parameter is a path to a file
@param string $encoding The document encoding
@param string $type The document type
@return \DiDom\Document | [
"Creates",
"a",
"new",
"document",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L83-L86 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.createElement | public function createElement($name, $value = null, array $attributes = [])
{
$node = $this->document->createElement($name);
return new Element($node, $value, $attributes);
} | php | public function createElement($name, $value = null, array $attributes = [])
{
$node = $this->document->createElement($name);
return new Element($node, $value, $attributes);
} | [
"public",
"function",
"createElement",
"(",
"$",
"name",
",",
"$",
"value",
"=",
"null",
",",
"array",
"$",
"attributes",
"=",
"[",
"]",
")",
"{",
"$",
"node",
"=",
"$",
"this",
"->",
"document",
"->",
"createElement",
"(",
"$",
"name",
")",
";",
"return",
"new",
"Element",
"(",
"$",
"node",
",",
"$",
"value",
",",
"$",
"attributes",
")",
";",
"}"
] | Creates a new element node.
@param string $name The tag name of the element
@param string|null $value The value of the element
@param array $attributes The attributes of the element
@return \DiDom\Element created element | [
"Creates",
"a",
"new",
"element",
"node",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L97-L102 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.createElementBySelector | public function createElementBySelector($selector, $value = null, array $attributes = [])
{
$segments = Query::getSegments($selector);
$name = array_key_exists('tag', $segments) ? $segments['tag'] : 'div';
if (array_key_exists('attributes', $segments)) {
$attributes = array_merge($attributes, $segments['attributes']);
}
if (array_key_exists('id', $segments)) {
$attributes['id'] = $segments['id'];
}
if (array_key_exists('classes', $segments)) {
$attributes['class'] = implode(' ', $segments['classes']);
}
return $this->createElement($name, $value, $attributes);
} | php | public function createElementBySelector($selector, $value = null, array $attributes = [])
{
$segments = Query::getSegments($selector);
$name = array_key_exists('tag', $segments) ? $segments['tag'] : 'div';
if (array_key_exists('attributes', $segments)) {
$attributes = array_merge($attributes, $segments['attributes']);
}
if (array_key_exists('id', $segments)) {
$attributes['id'] = $segments['id'];
}
if (array_key_exists('classes', $segments)) {
$attributes['class'] = implode(' ', $segments['classes']);
}
return $this->createElement($name, $value, $attributes);
} | [
"public",
"function",
"createElementBySelector",
"(",
"$",
"selector",
",",
"$",
"value",
"=",
"null",
",",
"array",
"$",
"attributes",
"=",
"[",
"]",
")",
"{",
"$",
"segments",
"=",
"Query",
"::",
"getSegments",
"(",
"$",
"selector",
")",
";",
"$",
"name",
"=",
"array_key_exists",
"(",
"'tag'",
",",
"$",
"segments",
")",
"?",
"$",
"segments",
"[",
"'tag'",
"]",
":",
"'div'",
";",
"if",
"(",
"array_key_exists",
"(",
"'attributes'",
",",
"$",
"segments",
")",
")",
"{",
"$",
"attributes",
"=",
"array_merge",
"(",
"$",
"attributes",
",",
"$",
"segments",
"[",
"'attributes'",
"]",
")",
";",
"}",
"if",
"(",
"array_key_exists",
"(",
"'id'",
",",
"$",
"segments",
")",
")",
"{",
"$",
"attributes",
"[",
"'id'",
"]",
"=",
"$",
"segments",
"[",
"'id'",
"]",
";",
"}",
"if",
"(",
"array_key_exists",
"(",
"'classes'",
",",
"$",
"segments",
")",
")",
"{",
"$",
"attributes",
"[",
"'class'",
"]",
"=",
"implode",
"(",
"' '",
",",
"$",
"segments",
"[",
"'classes'",
"]",
")",
";",
"}",
"return",
"$",
"this",
"->",
"createElement",
"(",
"$",
"name",
",",
"$",
"value",
",",
"$",
"attributes",
")",
";",
"}"
] | Creates a new element node by CSS selector.
@param string $selector
@param string|null $value
@param array $attributes
@return \DiDom\Element | [
"Creates",
"a",
"new",
"element",
"node",
"by",
"CSS",
"selector",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L113-L132 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.appendChild | public function appendChild($nodes)
{
$returnArray = true;
if (!is_array($nodes)) {
$nodes = [$nodes];
$returnArray = false;
}
$result = [];
foreach ($nodes as $node) {
if ($node instanceof Element) {
$node = $node->getNode();
}
if (!$node instanceof DOMNode) {
throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of %s\Element or DOMNode, %s given', __METHOD__, __NAMESPACE__, (is_object($node) ? get_class($node) : gettype($node))));
}
Errors::disable();
$cloned = $node->cloneNode(true);
$newNode = $this->document->importNode($cloned, true);
$result[] = $this->document->appendChild($newNode);
Errors::restore();
}
$result = array_map(function (DOMNode $node) {
return new Element($node);
}, $result);
return $returnArray ? $result : $result[0];
} | php | public function appendChild($nodes)
{
$returnArray = true;
if (!is_array($nodes)) {
$nodes = [$nodes];
$returnArray = false;
}
$result = [];
foreach ($nodes as $node) {
if ($node instanceof Element) {
$node = $node->getNode();
}
if (!$node instanceof DOMNode) {
throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of %s\Element or DOMNode, %s given', __METHOD__, __NAMESPACE__, (is_object($node) ? get_class($node) : gettype($node))));
}
Errors::disable();
$cloned = $node->cloneNode(true);
$newNode = $this->document->importNode($cloned, true);
$result[] = $this->document->appendChild($newNode);
Errors::restore();
}
$result = array_map(function (DOMNode $node) {
return new Element($node);
}, $result);
return $returnArray ? $result : $result[0];
} | [
"public",
"function",
"appendChild",
"(",
"$",
"nodes",
")",
"{",
"$",
"returnArray",
"=",
"true",
";",
"if",
"(",
"!",
"is_array",
"(",
"$",
"nodes",
")",
")",
"{",
"$",
"nodes",
"=",
"[",
"$",
"nodes",
"]",
";",
"$",
"returnArray",
"=",
"false",
";",
"}",
"$",
"result",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"nodes",
"as",
"$",
"node",
")",
"{",
"if",
"(",
"$",
"node",
"instanceof",
"Element",
")",
"{",
"$",
"node",
"=",
"$",
"node",
"->",
"getNode",
"(",
")",
";",
"}",
"if",
"(",
"!",
"$",
"node",
"instanceof",
"DOMNode",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'Argument 1 passed to %s must be an instance of %s\\Element or DOMNode, %s given'",
",",
"__METHOD__",
",",
"__NAMESPACE__",
",",
"(",
"is_object",
"(",
"$",
"node",
")",
"?",
"get_class",
"(",
"$",
"node",
")",
":",
"gettype",
"(",
"$",
"node",
")",
")",
")",
")",
";",
"}",
"Errors",
"::",
"disable",
"(",
")",
";",
"$",
"cloned",
"=",
"$",
"node",
"->",
"cloneNode",
"(",
"true",
")",
";",
"$",
"newNode",
"=",
"$",
"this",
"->",
"document",
"->",
"importNode",
"(",
"$",
"cloned",
",",
"true",
")",
";",
"$",
"result",
"[",
"]",
"=",
"$",
"this",
"->",
"document",
"->",
"appendChild",
"(",
"$",
"newNode",
")",
";",
"Errors",
"::",
"restore",
"(",
")",
";",
"}",
"$",
"result",
"=",
"array_map",
"(",
"function",
"(",
"DOMNode",
"$",
"node",
")",
"{",
"return",
"new",
"Element",
"(",
"$",
"node",
")",
";",
"}",
",",
"$",
"result",
")",
";",
"return",
"$",
"returnArray",
"?",
"$",
"result",
":",
"$",
"result",
"[",
"0",
"]",
";",
"}"
] | Adds a new child at the end of the children.
@param \DiDom\Element|\DOMNode|array $nodes The appended child
@return \DiDom\Element|\DiDom\Element[]
@throws \InvalidArgumentException if the passed argument is not an instance of \DOMNode or \DiDom\Element | [
"Adds",
"a",
"new",
"child",
"at",
"the",
"end",
"of",
"the",
"children",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L173-L209 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.preserveWhiteSpace | public function preserveWhiteSpace($value = true)
{
if (!is_bool($value)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be boolean, %s given', __METHOD__, gettype($value)));
}
$this->document->preserveWhiteSpace = $value;
return $this;
} | php | public function preserveWhiteSpace($value = true)
{
if (!is_bool($value)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be boolean, %s given', __METHOD__, gettype($value)));
}
$this->document->preserveWhiteSpace = $value;
return $this;
} | [
"public",
"function",
"preserveWhiteSpace",
"(",
"$",
"value",
"=",
"true",
")",
"{",
"if",
"(",
"!",
"is_bool",
"(",
"$",
"value",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'%s expects parameter 1 to be boolean, %s given'",
",",
"__METHOD__",
",",
"gettype",
"(",
"$",
"value",
")",
")",
")",
";",
"}",
"$",
"this",
"->",
"document",
"->",
"preserveWhiteSpace",
"=",
"$",
"value",
";",
"return",
"$",
"this",
";",
"}"
] | Set preserveWhiteSpace property.
@param bool $value
@return \DiDom\Document | [
"Set",
"preserveWhiteSpace",
"property",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L218-L227 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.load | public function load($string, $isFile = false, $type = Document::TYPE_HTML, $options = null)
{
if (!is_string($string)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, (is_object($string) ? get_class($string) : gettype($string))));
}
if (!is_string($type)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 3 to be string, %s given', __METHOD__, (is_object($type) ? get_class($type) : gettype($type))));
}
if (!in_array(strtolower($type), [Document::TYPE_HTML, Document::TYPE_XML], true)) {
throw new RuntimeException(sprintf('Document type must be "xml" or "html", %s given', $type));
}
if ($options === null) {
// LIBXML_HTML_NODEFDTD - prevents a default doctype being added when one is not found
$options = LIBXML_HTML_NODEFDTD;
}
if (!is_int($options)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 4 to be integer, %s given', __METHOD__, (is_object($options) ? get_class($options) : gettype($options))));
}
$string = trim($string);
if ($isFile) {
$string = $this->loadFile($string);
}
if (strtolower($type) === Document::TYPE_HTML) {
$string = Encoder::convertToHtmlEntities($string, $this->encoding);
}
$this->type = strtolower($type);
Errors::disable();
if ($this->type === Document::TYPE_HTML) {
$this->document->loadHtml($string, $options);
} else {
$this->document->loadXml($string, $options);
}
Errors::restore();
return $this;
} | php | public function load($string, $isFile = false, $type = Document::TYPE_HTML, $options = null)
{
if (!is_string($string)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, (is_object($string) ? get_class($string) : gettype($string))));
}
if (!is_string($type)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 3 to be string, %s given', __METHOD__, (is_object($type) ? get_class($type) : gettype($type))));
}
if (!in_array(strtolower($type), [Document::TYPE_HTML, Document::TYPE_XML], true)) {
throw new RuntimeException(sprintf('Document type must be "xml" or "html", %s given', $type));
}
if ($options === null) {
$options = LIBXML_HTML_NODEFDTD;
}
if (!is_int($options)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 4 to be integer, %s given', __METHOD__, (is_object($options) ? get_class($options) : gettype($options))));
}
$string = trim($string);
if ($isFile) {
$string = $this->loadFile($string);
}
if (strtolower($type) === Document::TYPE_HTML) {
$string = Encoder::convertToHtmlEntities($string, $this->encoding);
}
$this->type = strtolower($type);
Errors::disable();
if ($this->type === Document::TYPE_HTML) {
$this->document->loadHtml($string, $options);
} else {
$this->document->loadXml($string, $options);
}
Errors::restore();
return $this;
} | [
"public",
"function",
"load",
"(",
"$",
"string",
",",
"$",
"isFile",
"=",
"false",
",",
"$",
"type",
"=",
"Document",
"::",
"TYPE_HTML",
",",
"$",
"options",
"=",
"null",
")",
"{",
"if",
"(",
"!",
"is_string",
"(",
"$",
"string",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'%s expects parameter 1 to be string, %s given'",
",",
"__METHOD__",
",",
"(",
"is_object",
"(",
"$",
"string",
")",
"?",
"get_class",
"(",
"$",
"string",
")",
":",
"gettype",
"(",
"$",
"string",
")",
")",
")",
")",
";",
"}",
"if",
"(",
"!",
"is_string",
"(",
"$",
"type",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'%s expects parameter 3 to be string, %s given'",
",",
"__METHOD__",
",",
"(",
"is_object",
"(",
"$",
"type",
")",
"?",
"get_class",
"(",
"$",
"type",
")",
":",
"gettype",
"(",
"$",
"type",
")",
")",
")",
")",
";",
"}",
"if",
"(",
"!",
"in_array",
"(",
"strtolower",
"(",
"$",
"type",
")",
",",
"[",
"Document",
"::",
"TYPE_HTML",
",",
"Document",
"::",
"TYPE_XML",
"]",
",",
"true",
")",
")",
"{",
"throw",
"new",
"RuntimeException",
"(",
"sprintf",
"(",
"'Document type must be \"xml\" or \"html\", %s given'",
",",
"$",
"type",
")",
")",
";",
"}",
"if",
"(",
"$",
"options",
"===",
"null",
")",
"{",
"// LIBXML_HTML_NODEFDTD - prevents a default doctype being added when one is not found",
"$",
"options",
"=",
"LIBXML_HTML_NODEFDTD",
";",
"}",
"if",
"(",
"!",
"is_int",
"(",
"$",
"options",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'%s expects parameter 4 to be integer, %s given'",
",",
"__METHOD__",
",",
"(",
"is_object",
"(",
"$",
"options",
")",
"?",
"get_class",
"(",
"$",
"options",
")",
":",
"gettype",
"(",
"$",
"options",
")",
")",
")",
")",
";",
"}",
"$",
"string",
"=",
"trim",
"(",
"$",
"string",
")",
";",
"if",
"(",
"$",
"isFile",
")",
"{",
"$",
"string",
"=",
"$",
"this",
"->",
"loadFile",
"(",
"$",
"string",
")",
";",
"}",
"if",
"(",
"strtolower",
"(",
"$",
"type",
")",
"===",
"Document",
"::",
"TYPE_HTML",
")",
"{",
"$",
"string",
"=",
"Encoder",
"::",
"convertToHtmlEntities",
"(",
"$",
"string",
",",
"$",
"this",
"->",
"encoding",
")",
";",
"}",
"$",
"this",
"->",
"type",
"=",
"strtolower",
"(",
"$",
"type",
")",
";",
"Errors",
"::",
"disable",
"(",
")",
";",
"if",
"(",
"$",
"this",
"->",
"type",
"===",
"Document",
"::",
"TYPE_HTML",
")",
"{",
"$",
"this",
"->",
"document",
"->",
"loadHtml",
"(",
"$",
"string",
",",
"$",
"options",
")",
";",
"}",
"else",
"{",
"$",
"this",
"->",
"document",
"->",
"loadXml",
"(",
"$",
"string",
",",
"$",
"options",
")",
";",
"}",
"Errors",
"::",
"restore",
"(",
")",
";",
"return",
"$",
"this",
";",
"}"
] | Load HTML or XML.
@param string $string HTML or XML string or file path
@param bool $isFile Indicates that in first parameter was passed to the file path
@param string $type Type of the document
@param int|null $options Additional parameters
@return \DiDom\Document
@throws \InvalidArgumentException if first parameter is not a string
@throws \InvalidArgumentException if document type parameter is not a string
@throws \RuntimeException if document type is not HTML or XML | [
"Load",
"HTML",
"or",
"XML",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L243-L289 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.loadHtml | public function loadHtml($html, $options = null)
{
return $this->load($html, false, Document::TYPE_HTML, $options);
} | php | public function loadHtml($html, $options = null)
{
return $this->load($html, false, Document::TYPE_HTML, $options);
} | [
"public",
"function",
"loadHtml",
"(",
"$",
"html",
",",
"$",
"options",
"=",
"null",
")",
"{",
"return",
"$",
"this",
"->",
"load",
"(",
"$",
"html",
",",
"false",
",",
"Document",
"::",
"TYPE_HTML",
",",
"$",
"options",
")",
";",
"}"
] | Load HTML from a string.
@param string $html The HTML string
@param int|null $options Additional parameters
@return \DiDom\Document
@throws \InvalidArgumentException if the provided argument is not a string | [
"Load",
"HTML",
"from",
"a",
"string",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L301-L304 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.loadHtmlFile | public function loadHtmlFile($filename, $options = null)
{
return $this->load($filename, true, Document::TYPE_HTML, $options);
} | php | public function loadHtmlFile($filename, $options = null)
{
return $this->load($filename, true, Document::TYPE_HTML, $options);
} | [
"public",
"function",
"loadHtmlFile",
"(",
"$",
"filename",
",",
"$",
"options",
"=",
"null",
")",
"{",
"return",
"$",
"this",
"->",
"load",
"(",
"$",
"filename",
",",
"true",
",",
"Document",
"::",
"TYPE_HTML",
",",
"$",
"options",
")",
";",
"}"
] | Load HTML from a file.
@param string $filename The path to the HTML file
@param int|null $options Additional parameters
@return \DiDom\Document
@throws \InvalidArgumentException if the file path is not a string
@throws \RuntimeException if the file does not exist
@throws \RuntimeException if you are unable to load the file | [
"Load",
"HTML",
"from",
"a",
"file",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L318-L321 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.loadXml | public function loadXml($xml, $options = null)
{
return $this->load($xml, false, Document::TYPE_XML, $options);
} | php | public function loadXml($xml, $options = null)
{
return $this->load($xml, false, Document::TYPE_XML, $options);
} | [
"public",
"function",
"loadXml",
"(",
"$",
"xml",
",",
"$",
"options",
"=",
"null",
")",
"{",
"return",
"$",
"this",
"->",
"load",
"(",
"$",
"xml",
",",
"false",
",",
"Document",
"::",
"TYPE_XML",
",",
"$",
"options",
")",
";",
"}"
] | Load XML from a string.
@param string $xml The XML string
@param int|null $options Additional parameters
@return \DiDom\Document
@throws \InvalidArgumentException if the provided argument is not a string | [
"Load",
"XML",
"from",
"a",
"string",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L333-L336 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.loadXmlFile | public function loadXmlFile($filename, $options = null)
{
return $this->load($filename, true, Document::TYPE_XML, $options);
} | php | public function loadXmlFile($filename, $options = null)
{
return $this->load($filename, true, Document::TYPE_XML, $options);
} | [
"public",
"function",
"loadXmlFile",
"(",
"$",
"filename",
",",
"$",
"options",
"=",
"null",
")",
"{",
"return",
"$",
"this",
"->",
"load",
"(",
"$",
"filename",
",",
"true",
",",
"Document",
"::",
"TYPE_XML",
",",
"$",
"options",
")",
";",
"}"
] | Load XML from a file.
@param string $filename The path to the XML file
@param int|null $options Additional parameters
@return \DiDom\Document
@throws \InvalidArgumentException if the file path is not a string
@throws \RuntimeException if the file does not exist
@throws \RuntimeException if you are unable to load the file | [
"Load",
"XML",
"from",
"a",
"file",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L350-L353 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.loadFile | protected function loadFile($filename)
{
if (!is_string($filename)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, gettype($filename)));
}
try {
$content = file_get_contents($filename);
} catch (\Exception $exception) {
throw new RuntimeException(sprintf('Could not load file %s', $filename));
}
if ($content === false) {
throw new RuntimeException(sprintf('Could not load file %s', $filename));
}
return $content;
} | php | protected function loadFile($filename)
{
if (!is_string($filename)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, gettype($filename)));
}
try {
$content = file_get_contents($filename);
} catch (\Exception $exception) {
throw new RuntimeException(sprintf('Could not load file %s', $filename));
}
if ($content === false) {
throw new RuntimeException(sprintf('Could not load file %s', $filename));
}
return $content;
} | [
"protected",
"function",
"loadFile",
"(",
"$",
"filename",
")",
"{",
"if",
"(",
"!",
"is_string",
"(",
"$",
"filename",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'%s expects parameter 1 to be string, %s given'",
",",
"__METHOD__",
",",
"gettype",
"(",
"$",
"filename",
")",
")",
")",
";",
"}",
"try",
"{",
"$",
"content",
"=",
"file_get_contents",
"(",
"$",
"filename",
")",
";",
"}",
"catch",
"(",
"\\",
"Exception",
"$",
"exception",
")",
"{",
"throw",
"new",
"RuntimeException",
"(",
"sprintf",
"(",
"'Could not load file %s'",
",",
"$",
"filename",
")",
")",
";",
"}",
"if",
"(",
"$",
"content",
"===",
"false",
")",
"{",
"throw",
"new",
"RuntimeException",
"(",
"sprintf",
"(",
"'Could not load file %s'",
",",
"$",
"filename",
")",
")",
";",
"}",
"return",
"$",
"content",
";",
"}"
] | Reads entire file into a string.
@param string $filename The path to the file
@return string
@throws \InvalidArgumentException if the file path is not a string
@throws \RuntimeException if an error occurred | [
"Reads",
"entire",
"file",
"into",
"a",
"string",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L365-L382 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.has | public function has($expression, $type = Query::TYPE_CSS)
{
$expression = Query::compile($expression, $type);
$expression = sprintf('count(%s) > 0', $expression);
return $this->createXpath()->evaluate($expression);
} | php | public function has($expression, $type = Query::TYPE_CSS)
{
$expression = Query::compile($expression, $type);
$expression = sprintf('count(%s) > 0', $expression);
return $this->createXpath()->evaluate($expression);
} | [
"public",
"function",
"has",
"(",
"$",
"expression",
",",
"$",
"type",
"=",
"Query",
"::",
"TYPE_CSS",
")",
"{",
"$",
"expression",
"=",
"Query",
"::",
"compile",
"(",
"$",
"expression",
",",
"$",
"type",
")",
";",
"$",
"expression",
"=",
"sprintf",
"(",
"'count(%s) > 0'",
",",
"$",
"expression",
")",
";",
"return",
"$",
"this",
"->",
"createXpath",
"(",
")",
"->",
"evaluate",
"(",
"$",
"expression",
")",
";",
"}"
] | Checks the existence of the node.
@param string $expression XPath expression or CSS selector
@param string $type The type of the expression
@return bool | [
"Checks",
"the",
"existence",
"of",
"the",
"node",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L392-L398 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.find | public function find($expression, $type = Query::TYPE_CSS, $wrapNode = true, $contextNode = null)
{
$expression = Query::compile($expression, $type);
if ($contextNode !== null) {
if ($contextNode instanceof Element) {
$contextNode = $contextNode->getNode();
}
if (!$contextNode instanceof DOMElement) {
throw new InvalidArgumentException(sprintf('Argument 4 passed to %s must be an instance of %s\Element or DOMElement, %s given', __METHOD__, __NAMESPACE__, (is_object($contextNode) ? get_class($contextNode) : gettype($contextNode))));
}
if ($type === Query::TYPE_CSS) {
$expression = '.' . $expression;
}
}
$nodeList = $this->createXpath()->query($expression, $contextNode);
$result = [];
if ($wrapNode) {
foreach ($nodeList as $node) {
$result[] = $this->wrapNode($node);
}
} else {
foreach ($nodeList as $node) {
$result[] = $node;
}
}
return $result;
} | php | public function find($expression, $type = Query::TYPE_CSS, $wrapNode = true, $contextNode = null)
{
$expression = Query::compile($expression, $type);
if ($contextNode !== null) {
if ($contextNode instanceof Element) {
$contextNode = $contextNode->getNode();
}
if (!$contextNode instanceof DOMElement) {
throw new InvalidArgumentException(sprintf('Argument 4 passed to %s must be an instance of %s\Element or DOMElement, %s given', __METHOD__, __NAMESPACE__, (is_object($contextNode) ? get_class($contextNode) : gettype($contextNode))));
}
if ($type === Query::TYPE_CSS) {
$expression = '.' . $expression;
}
}
$nodeList = $this->createXpath()->query($expression, $contextNode);
$result = [];
if ($wrapNode) {
foreach ($nodeList as $node) {
$result[] = $this->wrapNode($node);
}
} else {
foreach ($nodeList as $node) {
$result[] = $node;
}
}
return $result;
} | [
"public",
"function",
"find",
"(",
"$",
"expression",
",",
"$",
"type",
"=",
"Query",
"::",
"TYPE_CSS",
",",
"$",
"wrapNode",
"=",
"true",
",",
"$",
"contextNode",
"=",
"null",
")",
"{",
"$",
"expression",
"=",
"Query",
"::",
"compile",
"(",
"$",
"expression",
",",
"$",
"type",
")",
";",
"if",
"(",
"$",
"contextNode",
"!==",
"null",
")",
"{",
"if",
"(",
"$",
"contextNode",
"instanceof",
"Element",
")",
"{",
"$",
"contextNode",
"=",
"$",
"contextNode",
"->",
"getNode",
"(",
")",
";",
"}",
"if",
"(",
"!",
"$",
"contextNode",
"instanceof",
"DOMElement",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'Argument 4 passed to %s must be an instance of %s\\Element or DOMElement, %s given'",
",",
"__METHOD__",
",",
"__NAMESPACE__",
",",
"(",
"is_object",
"(",
"$",
"contextNode",
")",
"?",
"get_class",
"(",
"$",
"contextNode",
")",
":",
"gettype",
"(",
"$",
"contextNode",
")",
")",
")",
")",
";",
"}",
"if",
"(",
"$",
"type",
"===",
"Query",
"::",
"TYPE_CSS",
")",
"{",
"$",
"expression",
"=",
"'.'",
".",
"$",
"expression",
";",
"}",
"}",
"$",
"nodeList",
"=",
"$",
"this",
"->",
"createXpath",
"(",
")",
"->",
"query",
"(",
"$",
"expression",
",",
"$",
"contextNode",
")",
";",
"$",
"result",
"=",
"[",
"]",
";",
"if",
"(",
"$",
"wrapNode",
")",
"{",
"foreach",
"(",
"$",
"nodeList",
"as",
"$",
"node",
")",
"{",
"$",
"result",
"[",
"]",
"=",
"$",
"this",
"->",
"wrapNode",
"(",
"$",
"node",
")",
";",
"}",
"}",
"else",
"{",
"foreach",
"(",
"$",
"nodeList",
"as",
"$",
"node",
")",
"{",
"$",
"result",
"[",
"]",
"=",
"$",
"node",
";",
"}",
"}",
"return",
"$",
"result",
";",
"}"
] | Searches for an node in the DOM tree for a given XPath expression or a CSS selector.
@param string $expression XPath expression or a CSS selector
@param string $type The type of the expression
@param bool $wrapNode Returns array of \DiDom\Element if true, otherwise array of \DOMElement
@param \DOMElement|null $contextNode The node in which the search will be performed
@return \DiDom\Element[]|\DOMElement[]
@throws InvalidArgumentException if context node is not \DOMElement | [
"Searches",
"for",
"an",
"node",
"in",
"the",
"DOM",
"tree",
"for",
"a",
"given",
"XPath",
"expression",
"or",
"a",
"CSS",
"selector",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L412-L445 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.first | public function first($expression, $type = Query::TYPE_CSS, $wrapNode = true, $contextNode = null)
{
$expression = Query::compile($expression, $type);
if ($contextNode !== null && $type === Query::TYPE_CSS) {
$expression = '.' . $expression;
}
$expression = sprintf('(%s)[1]', $expression);
$nodes = $this->find($expression, Query::TYPE_XPATH, false, $contextNode);
if (count($nodes) === 0) {
return null;
}
return $wrapNode ? $this->wrapNode($nodes[0]) : $nodes[0];
} | php | public function first($expression, $type = Query::TYPE_CSS, $wrapNode = true, $contextNode = null)
{
$expression = Query::compile($expression, $type);
if ($contextNode !== null && $type === Query::TYPE_CSS) {
$expression = '.' . $expression;
}
$expression = sprintf('(%s)[1]', $expression);
$nodes = $this->find($expression, Query::TYPE_XPATH, false, $contextNode);
if (count($nodes) === 0) {
return null;
}
return $wrapNode ? $this->wrapNode($nodes[0]) : $nodes[0];
} | [
"public",
"function",
"first",
"(",
"$",
"expression",
",",
"$",
"type",
"=",
"Query",
"::",
"TYPE_CSS",
",",
"$",
"wrapNode",
"=",
"true",
",",
"$",
"contextNode",
"=",
"null",
")",
"{",
"$",
"expression",
"=",
"Query",
"::",
"compile",
"(",
"$",
"expression",
",",
"$",
"type",
")",
";",
"if",
"(",
"$",
"contextNode",
"!==",
"null",
"&&",
"$",
"type",
"===",
"Query",
"::",
"TYPE_CSS",
")",
"{",
"$",
"expression",
"=",
"'.'",
".",
"$",
"expression",
";",
"}",
"$",
"expression",
"=",
"sprintf",
"(",
"'(%s)[1]'",
",",
"$",
"expression",
")",
";",
"$",
"nodes",
"=",
"$",
"this",
"->",
"find",
"(",
"$",
"expression",
",",
"Query",
"::",
"TYPE_XPATH",
",",
"false",
",",
"$",
"contextNode",
")",
";",
"if",
"(",
"count",
"(",
"$",
"nodes",
")",
"===",
"0",
")",
"{",
"return",
"null",
";",
"}",
"return",
"$",
"wrapNode",
"?",
"$",
"this",
"->",
"wrapNode",
"(",
"$",
"nodes",
"[",
"0",
"]",
")",
":",
"$",
"nodes",
"[",
"0",
"]",
";",
"}"
] | Searches for an node in the DOM tree and returns first element or null.
@param string $expression XPath expression or a CSS selector
@param string $type The type of the expression
@param bool $wrapNode Returns array of \DiDom\Element if true, otherwise array of \DOMElement
@param \DOMElement|null $contextNode The node in which the search will be performed
@return \DiDom\Element|\DOMElement|null | [
"Searches",
"for",
"an",
"node",
"in",
"the",
"DOM",
"tree",
"and",
"returns",
"first",
"element",
"or",
"null",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L457-L474 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.wrapNode | protected function wrapNode($node)
{
switch (get_class($node)) {
case 'DOMElement':
return new Element($node);
case 'DOMText':
return $node->data;
case 'DOMComment':
return new Element($node);
case 'DOMCdataSection':
return new Element($node);
case 'DOMAttr':
return $node->value;
}
throw new InvalidArgumentException(sprintf('Unknown node type "%s"', get_class($node)));
} | php | protected function wrapNode($node)
{
switch (get_class($node)) {
case 'DOMElement':
return new Element($node);
case 'DOMText':
return $node->data;
case 'DOMComment':
return new Element($node);
case 'DOMCdataSection':
return new Element($node);
case 'DOMAttr':
return $node->value;
}
throw new InvalidArgumentException(sprintf('Unknown node type "%s"', get_class($node)));
} | [
"protected",
"function",
"wrapNode",
"(",
"$",
"node",
")",
"{",
"switch",
"(",
"get_class",
"(",
"$",
"node",
")",
")",
"{",
"case",
"'DOMElement'",
":",
"return",
"new",
"Element",
"(",
"$",
"node",
")",
";",
"case",
"'DOMText'",
":",
"return",
"$",
"node",
"->",
"data",
";",
"case",
"'DOMComment'",
":",
"return",
"new",
"Element",
"(",
"$",
"node",
")",
";",
"case",
"'DOMCdataSection'",
":",
"return",
"new",
"Element",
"(",
"$",
"node",
")",
";",
"case",
"'DOMAttr'",
":",
"return",
"$",
"node",
"->",
"value",
";",
"}",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'Unknown node type \"%s\"'",
",",
"get_class",
"(",
"$",
"node",
")",
")",
")",
";",
"}"
] | @param \DOMElement|\DOMText|\DOMAttr $node
@return \DiDom\Element|string
@throws InvalidArgumentException if node is not DOMElement, DOMText, DOMComment, DOMCdataSection or DOMAttr | [
"@param",
"\\",
"DOMElement|",
"\\",
"DOMText|",
"\\",
"DOMAttr",
"$node"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L483-L503 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.count | public function count($expression, $type = Query::TYPE_CSS)
{
$expression = Query::compile($expression, $type);
$expression = sprintf('count(%s)', $expression);
return (int) $this->createXpath()->evaluate($expression);
} | php | public function count($expression, $type = Query::TYPE_CSS)
{
$expression = Query::compile($expression, $type);
$expression = sprintf('count(%s)', $expression);
return (int) $this->createXpath()->evaluate($expression);
} | [
"public",
"function",
"count",
"(",
"$",
"expression",
",",
"$",
"type",
"=",
"Query",
"::",
"TYPE_CSS",
")",
"{",
"$",
"expression",
"=",
"Query",
"::",
"compile",
"(",
"$",
"expression",
",",
"$",
"type",
")",
";",
"$",
"expression",
"=",
"sprintf",
"(",
"'count(%s)'",
",",
"$",
"expression",
")",
";",
"return",
"(",
"int",
")",
"$",
"this",
"->",
"createXpath",
"(",
")",
"->",
"evaluate",
"(",
"$",
"expression",
")",
";",
"}"
] | Counts nodes for a given XPath expression or a CSS selector.
@param string $expression XPath expression or CSS selector
@param string $type The type of the expression
@return int | [
"Counts",
"nodes",
"for",
"a",
"given",
"XPath",
"expression",
"or",
"a",
"CSS",
"selector",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L527-L533 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.format | public function format($format = true)
{
if (!is_bool($format)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be boolean, %s given', __METHOD__, gettype($format)));
}
$this->document->formatOutput = $format;
return $this;
} | php | public function format($format = true)
{
if (!is_bool($format)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be boolean, %s given', __METHOD__, gettype($format)));
}
$this->document->formatOutput = $format;
return $this;
} | [
"public",
"function",
"format",
"(",
"$",
"format",
"=",
"true",
")",
"{",
"if",
"(",
"!",
"is_bool",
"(",
"$",
"format",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'%s expects parameter 1 to be boolean, %s given'",
",",
"__METHOD__",
",",
"gettype",
"(",
"$",
"format",
")",
")",
")",
";",
"}",
"$",
"this",
"->",
"document",
"->",
"formatOutput",
"=",
"$",
"format",
";",
"return",
"$",
"this",
";",
"}"
] | Nicely formats output with indentation and extra space.
@param bool $format Formats output if true
@return \DiDom\Document | [
"Nicely",
"formats",
"output",
"with",
"indentation",
"and",
"extra",
"space",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L577-L586 |
Imangazaliev/DiDOM | src/DiDom/Document.php | Document.is | public function is($document)
{
if ($document instanceof Document) {
$element = $document->getElement();
} else {
if (!$document instanceof DOMDocument) {
throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of %s or DOMDocument, %s given', __METHOD__, __CLASS__, (is_object($document) ? get_class($document) : gettype($document))));
}
$element = $document->documentElement;
}
if ($element === null) {
return false;
}
return $this->getElement()->isSameNode($element);
} | php | public function is($document)
{
if ($document instanceof Document) {
$element = $document->getElement();
} else {
if (!$document instanceof DOMDocument) {
throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of %s or DOMDocument, %s given', __METHOD__, __CLASS__, (is_object($document) ? get_class($document) : gettype($document))));
}
$element = $document->documentElement;
}
if ($element === null) {
return false;
}
return $this->getElement()->isSameNode($element);
} | [
"public",
"function",
"is",
"(",
"$",
"document",
")",
"{",
"if",
"(",
"$",
"document",
"instanceof",
"Document",
")",
"{",
"$",
"element",
"=",
"$",
"document",
"->",
"getElement",
"(",
")",
";",
"}",
"else",
"{",
"if",
"(",
"!",
"$",
"document",
"instanceof",
"DOMDocument",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'Argument 1 passed to %s must be an instance of %s or DOMDocument, %s given'",
",",
"__METHOD__",
",",
"__CLASS__",
",",
"(",
"is_object",
"(",
"$",
"document",
")",
"?",
"get_class",
"(",
"$",
"document",
")",
":",
"gettype",
"(",
"$",
"document",
")",
")",
")",
")",
";",
"}",
"$",
"element",
"=",
"$",
"document",
"->",
"documentElement",
";",
"}",
"if",
"(",
"$",
"element",
"===",
"null",
")",
"{",
"return",
"false",
";",
"}",
"return",
"$",
"this",
"->",
"getElement",
"(",
")",
"->",
"isSameNode",
"(",
"$",
"element",
")",
";",
"}"
] | Indicates if two documents are the same document.
@param \DiDom\Document|\DOMDocument $document The compared document
@return bool
@throws \InvalidArgumentException if the provided argument is not an instance of \DOMDocument or \DiDom\Document | [
"Indicates",
"if",
"two",
"documents",
"are",
"the",
"same",
"document",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Document.php#L607-L624 |
Imangazaliev/DiDOM | src/DiDom/ClassAttribute.php | ClassAttribute.parseClassAttribute | protected function parseClassAttribute()
{
if (!$this->element->hasAttribute('class')) {
// possible if class attribute has been removed
if ($this->classesString !== '') {
$this->classesString = '';
$this->classes = [];
}
return;
}
// if class attribute is not changed
if ($this->element->getAttribute('class') === $this->classesString) {
return;
}
// save class attribute as is (without trimming)
$this->classesString = $this->element->getAttribute('class');
$classesString = trim($this->classesString);
if ($classesString === '') {
$this->classes = [];
return;
}
$classes = explode(' ', $classesString);
$classes = array_map('trim', $classes);
$classes = array_filter($classes);
$classes = array_unique($classes);
$this->classes = array_values($classes);
} | php | protected function parseClassAttribute()
{
if (!$this->element->hasAttribute('class')) {
if ($this->classesString !== '') {
$this->classesString = '';
$this->classes = [];
}
return;
}
if ($this->element->getAttribute('class') === $this->classesString) {
return;
}
$this->classesString = $this->element->getAttribute('class');
$classesString = trim($this->classesString);
if ($classesString === '') {
$this->classes = [];
return;
}
$classes = explode(' ', $classesString);
$classes = array_map('trim', $classes);
$classes = array_filter($classes);
$classes = array_unique($classes);
$this->classes = array_values($classes);
} | [
"protected",
"function",
"parseClassAttribute",
"(",
")",
"{",
"if",
"(",
"!",
"$",
"this",
"->",
"element",
"->",
"hasAttribute",
"(",
"'class'",
")",
")",
"{",
"// possible if class attribute has been removed",
"if",
"(",
"$",
"this",
"->",
"classesString",
"!==",
"''",
")",
"{",
"$",
"this",
"->",
"classesString",
"=",
"''",
";",
"$",
"this",
"->",
"classes",
"=",
"[",
"]",
";",
"}",
"return",
";",
"}",
"// if class attribute is not changed",
"if",
"(",
"$",
"this",
"->",
"element",
"->",
"getAttribute",
"(",
"'class'",
")",
"===",
"$",
"this",
"->",
"classesString",
")",
"{",
"return",
";",
"}",
"// save class attribute as is (without trimming)",
"$",
"this",
"->",
"classesString",
"=",
"$",
"this",
"->",
"element",
"->",
"getAttribute",
"(",
"'class'",
")",
";",
"$",
"classesString",
"=",
"trim",
"(",
"$",
"this",
"->",
"classesString",
")",
";",
"if",
"(",
"$",
"classesString",
"===",
"''",
")",
"{",
"$",
"this",
"->",
"classes",
"=",
"[",
"]",
";",
"return",
";",
"}",
"$",
"classes",
"=",
"explode",
"(",
"' '",
",",
"$",
"classesString",
")",
";",
"$",
"classes",
"=",
"array_map",
"(",
"'trim'",
",",
"$",
"classes",
")",
";",
"$",
"classes",
"=",
"array_filter",
"(",
"$",
"classes",
")",
";",
"$",
"classes",
"=",
"array_unique",
"(",
"$",
"classes",
")",
";",
"$",
"this",
"->",
"classes",
"=",
"array_values",
"(",
"$",
"classes",
")",
";",
"}"
] | Parses class attribute of the element. | [
"Parses",
"class",
"attribute",
"of",
"the",
"element",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/ClassAttribute.php#L47-L82 |
Imangazaliev/DiDOM | src/DiDom/ClassAttribute.php | ClassAttribute.updateClassAttribute | protected function updateClassAttribute()
{
$this->classesString = implode(' ', $this->classes);
$this->element->setAttribute('class', $this->classesString);
} | php | protected function updateClassAttribute()
{
$this->classesString = implode(' ', $this->classes);
$this->element->setAttribute('class', $this->classesString);
} | [
"protected",
"function",
"updateClassAttribute",
"(",
")",
"{",
"$",
"this",
"->",
"classesString",
"=",
"implode",
"(",
"' '",
",",
"$",
"this",
"->",
"classes",
")",
";",
"$",
"this",
"->",
"element",
"->",
"setAttribute",
"(",
"'class'",
",",
"$",
"this",
"->",
"classesString",
")",
";",
"}"
] | Updates class attribute of the element. | [
"Updates",
"class",
"attribute",
"of",
"the",
"element",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/ClassAttribute.php#L87-L92 |
Imangazaliev/DiDOM | src/DiDom/ClassAttribute.php | ClassAttribute.add | public function add($className)
{
if (!is_string($className)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, (is_object($className) ? get_class($className) : gettype($className))));
}
$this->parseClassAttribute();
if (in_array($className, $this->classes, true)) {
return $this;
}
$this->classes[] = $className;
$this->updateClassAttribute();
return $this;
} | php | public function add($className)
{
if (!is_string($className)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, (is_object($className) ? get_class($className) : gettype($className))));
}
$this->parseClassAttribute();
if (in_array($className, $this->classes, true)) {
return $this;
}
$this->classes[] = $className;
$this->updateClassAttribute();
return $this;
} | [
"public",
"function",
"add",
"(",
"$",
"className",
")",
"{",
"if",
"(",
"!",
"is_string",
"(",
"$",
"className",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'%s expects parameter 1 to be string, %s given'",
",",
"__METHOD__",
",",
"(",
"is_object",
"(",
"$",
"className",
")",
"?",
"get_class",
"(",
"$",
"className",
")",
":",
"gettype",
"(",
"$",
"className",
")",
")",
")",
")",
";",
"}",
"$",
"this",
"->",
"parseClassAttribute",
"(",
")",
";",
"if",
"(",
"in_array",
"(",
"$",
"className",
",",
"$",
"this",
"->",
"classes",
",",
"true",
")",
")",
"{",
"return",
"$",
"this",
";",
"}",
"$",
"this",
"->",
"classes",
"[",
"]",
"=",
"$",
"className",
";",
"$",
"this",
"->",
"updateClassAttribute",
"(",
")",
";",
"return",
"$",
"this",
";",
"}"
] | @param string $className
@return \DiDom\ClassAttribute
@throws \InvalidArgumentException if class name is not a string | [
"@param",
"string",
"$className"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/ClassAttribute.php#L101-L118 |
Imangazaliev/DiDOM | src/DiDom/ClassAttribute.php | ClassAttribute.addMultiple | public function addMultiple(array $classNames)
{
$this->parseClassAttribute();
foreach ($classNames as $className) {
if (!is_string($className)) {
throw new InvalidArgumentException(sprintf('Class name must be a string, %s given', (is_object($className) ? get_class($className) : gettype($className))));
}
if (in_array($className, $this->classes, true)) {
continue;
}
$this->classes[] = $className;
}
$this->updateClassAttribute();
return $this;
} | php | public function addMultiple(array $classNames)
{
$this->parseClassAttribute();
foreach ($classNames as $className) {
if (!is_string($className)) {
throw new InvalidArgumentException(sprintf('Class name must be a string, %s given', (is_object($className) ? get_class($className) : gettype($className))));
}
if (in_array($className, $this->classes, true)) {
continue;
}
$this->classes[] = $className;
}
$this->updateClassAttribute();
return $this;
} | [
"public",
"function",
"addMultiple",
"(",
"array",
"$",
"classNames",
")",
"{",
"$",
"this",
"->",
"parseClassAttribute",
"(",
")",
";",
"foreach",
"(",
"$",
"classNames",
"as",
"$",
"className",
")",
"{",
"if",
"(",
"!",
"is_string",
"(",
"$",
"className",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'Class name must be a string, %s given'",
",",
"(",
"is_object",
"(",
"$",
"className",
")",
"?",
"get_class",
"(",
"$",
"className",
")",
":",
"gettype",
"(",
"$",
"className",
")",
")",
")",
")",
";",
"}",
"if",
"(",
"in_array",
"(",
"$",
"className",
",",
"$",
"this",
"->",
"classes",
",",
"true",
")",
")",
"{",
"continue",
";",
"}",
"$",
"this",
"->",
"classes",
"[",
"]",
"=",
"$",
"className",
";",
"}",
"$",
"this",
"->",
"updateClassAttribute",
"(",
")",
";",
"return",
"$",
"this",
";",
"}"
] | @param array $classNames
@return \DiDom\ClassAttribute
@throws \InvalidArgumentException if class name is not a string | [
"@param",
"array",
"$classNames"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/ClassAttribute.php#L127-L146 |
Imangazaliev/DiDOM | src/DiDom/ClassAttribute.php | ClassAttribute.contains | public function contains($className)
{
if (!is_string($className)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, (is_object($className) ? get_class($className) : gettype($className))));
}
$this->parseClassAttribute();
return in_array($className, $this->classes, true);
} | php | public function contains($className)
{
if (!is_string($className)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, (is_object($className) ? get_class($className) : gettype($className))));
}
$this->parseClassAttribute();
return in_array($className, $this->classes, true);
} | [
"public",
"function",
"contains",
"(",
"$",
"className",
")",
"{",
"if",
"(",
"!",
"is_string",
"(",
"$",
"className",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'%s expects parameter 1 to be string, %s given'",
",",
"__METHOD__",
",",
"(",
"is_object",
"(",
"$",
"className",
")",
"?",
"get_class",
"(",
"$",
"className",
")",
":",
"gettype",
"(",
"$",
"className",
")",
")",
")",
")",
";",
"}",
"$",
"this",
"->",
"parseClassAttribute",
"(",
")",
";",
"return",
"in_array",
"(",
"$",
"className",
",",
"$",
"this",
"->",
"classes",
",",
"true",
")",
";",
"}"
] | @param string $className
@return bool | [
"@param",
"string",
"$className"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/ClassAttribute.php#L163-L172 |
Imangazaliev/DiDOM | src/DiDom/ClassAttribute.php | ClassAttribute.remove | public function remove($className)
{
if (!is_string($className)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, (is_object($className) ? get_class($className) : gettype($className))));
}
$this->parseClassAttribute();
$classIndex = array_search($className, $this->classes);
if ($classIndex === false) {
return $this;
}
unset($this->classes[$classIndex]);
$this->updateClassAttribute();
return $this;
} | php | public function remove($className)
{
if (!is_string($className)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, (is_object($className) ? get_class($className) : gettype($className))));
}
$this->parseClassAttribute();
$classIndex = array_search($className, $this->classes);
if ($classIndex === false) {
return $this;
}
unset($this->classes[$classIndex]);
$this->updateClassAttribute();
return $this;
} | [
"public",
"function",
"remove",
"(",
"$",
"className",
")",
"{",
"if",
"(",
"!",
"is_string",
"(",
"$",
"className",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'%s expects parameter 1 to be string, %s given'",
",",
"__METHOD__",
",",
"(",
"is_object",
"(",
"$",
"className",
")",
"?",
"get_class",
"(",
"$",
"className",
")",
":",
"gettype",
"(",
"$",
"className",
")",
")",
")",
")",
";",
"}",
"$",
"this",
"->",
"parseClassAttribute",
"(",
")",
";",
"$",
"classIndex",
"=",
"array_search",
"(",
"$",
"className",
",",
"$",
"this",
"->",
"classes",
")",
";",
"if",
"(",
"$",
"classIndex",
"===",
"false",
")",
"{",
"return",
"$",
"this",
";",
"}",
"unset",
"(",
"$",
"this",
"->",
"classes",
"[",
"$",
"classIndex",
"]",
")",
";",
"$",
"this",
"->",
"updateClassAttribute",
"(",
")",
";",
"return",
"$",
"this",
";",
"}"
] | @param string $className
@return \DiDom\ClassAttribute
@throws \InvalidArgumentException if class name is not a string | [
"@param",
"string",
"$className"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/ClassAttribute.php#L181-L200 |
Imangazaliev/DiDOM | src/DiDom/ClassAttribute.php | ClassAttribute.removeMultiple | public function removeMultiple(array $classNames)
{
$this->parseClassAttribute();
foreach ($classNames as $className) {
if (!is_string($className)) {
throw new InvalidArgumentException(sprintf('Class name must be a string, %s given', (is_object($className) ? get_class($className) : gettype($className))));
}
$classIndex = array_search($className, $this->classes);
if ($classIndex === false) {
continue;
}
unset($this->classes[$classIndex]);
}
$this->updateClassAttribute();
return $this;
} | php | public function removeMultiple(array $classNames)
{
$this->parseClassAttribute();
foreach ($classNames as $className) {
if (!is_string($className)) {
throw new InvalidArgumentException(sprintf('Class name must be a string, %s given', (is_object($className) ? get_class($className) : gettype($className))));
}
$classIndex = array_search($className, $this->classes);
if ($classIndex === false) {
continue;
}
unset($this->classes[$classIndex]);
}
$this->updateClassAttribute();
return $this;
} | [
"public",
"function",
"removeMultiple",
"(",
"array",
"$",
"classNames",
")",
"{",
"$",
"this",
"->",
"parseClassAttribute",
"(",
")",
";",
"foreach",
"(",
"$",
"classNames",
"as",
"$",
"className",
")",
"{",
"if",
"(",
"!",
"is_string",
"(",
"$",
"className",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'Class name must be a string, %s given'",
",",
"(",
"is_object",
"(",
"$",
"className",
")",
"?",
"get_class",
"(",
"$",
"className",
")",
":",
"gettype",
"(",
"$",
"className",
")",
")",
")",
")",
";",
"}",
"$",
"classIndex",
"=",
"array_search",
"(",
"$",
"className",
",",
"$",
"this",
"->",
"classes",
")",
";",
"if",
"(",
"$",
"classIndex",
"===",
"false",
")",
"{",
"continue",
";",
"}",
"unset",
"(",
"$",
"this",
"->",
"classes",
"[",
"$",
"classIndex",
"]",
")",
";",
"}",
"$",
"this",
"->",
"updateClassAttribute",
"(",
")",
";",
"return",
"$",
"this",
";",
"}"
] | @param array $classNames
@return \DiDom\ClassAttribute
@throws \InvalidArgumentException if class name is not a string | [
"@param",
"array",
"$classNames"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/ClassAttribute.php#L209-L230 |
Imangazaliev/DiDOM | src/DiDom/ClassAttribute.php | ClassAttribute.removeAll | public function removeAll(array $exclusions = [])
{
$this->parseClassAttribute();
$preservedClasses = [];
foreach ($exclusions as $className) {
if (!is_string($className)) {
throw new InvalidArgumentException(sprintf('Class name must be a string, %s given', (is_object($className) ? get_class($className) : gettype($className))));
}
if (!in_array($className, $this->classes, true)) {
continue;
}
$preservedClasses[] = $className;
}
$this->classes = $preservedClasses;
$this->updateClassAttribute();
return $this;
} | php | public function removeAll(array $exclusions = [])
{
$this->parseClassAttribute();
$preservedClasses = [];
foreach ($exclusions as $className) {
if (!is_string($className)) {
throw new InvalidArgumentException(sprintf('Class name must be a string, %s given', (is_object($className) ? get_class($className) : gettype($className))));
}
if (!in_array($className, $this->classes, true)) {
continue;
}
$preservedClasses[] = $className;
}
$this->classes = $preservedClasses;
$this->updateClassAttribute();
return $this;
} | [
"public",
"function",
"removeAll",
"(",
"array",
"$",
"exclusions",
"=",
"[",
"]",
")",
"{",
"$",
"this",
"->",
"parseClassAttribute",
"(",
")",
";",
"$",
"preservedClasses",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"exclusions",
"as",
"$",
"className",
")",
"{",
"if",
"(",
"!",
"is_string",
"(",
"$",
"className",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'Class name must be a string, %s given'",
",",
"(",
"is_object",
"(",
"$",
"className",
")",
"?",
"get_class",
"(",
"$",
"className",
")",
":",
"gettype",
"(",
"$",
"className",
")",
")",
")",
")",
";",
"}",
"if",
"(",
"!",
"in_array",
"(",
"$",
"className",
",",
"$",
"this",
"->",
"classes",
",",
"true",
")",
")",
"{",
"continue",
";",
"}",
"$",
"preservedClasses",
"[",
"]",
"=",
"$",
"className",
";",
"}",
"$",
"this",
"->",
"classes",
"=",
"$",
"preservedClasses",
";",
"$",
"this",
"->",
"updateClassAttribute",
"(",
")",
";",
"return",
"$",
"this",
";",
"}"
] | @param string[] $exclusions
@return \DiDom\ClassAttribute | [
"@param",
"string",
"[]",
"$exclusions"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/ClassAttribute.php#L237-L260 |
Imangazaliev/DiDOM | src/DiDom/Query.php | Query.compile | public static function compile($expression, $type = self::TYPE_CSS)
{
if (!is_string($expression)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, gettype($expression)));
}
if (!is_string($type)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 2 to be string, %s given', __METHOD__, gettype($type)));
}
if (strcasecmp($type, self::TYPE_XPATH) !== 0 && strcasecmp($type, self::TYPE_CSS) !== 0) {
throw new RuntimeException(sprintf('Unknown expression type "%s"', $type));
}
$expression = trim($expression);
if ($expression === '') {
throw new InvalidSelectorException('The expression must not be empty');
}
if (strcasecmp($type, self::TYPE_XPATH) === 0) {
return $expression;
}
if (!array_key_exists($expression, static::$compiled)) {
static::$compiled[$expression] = static::cssToXpath($expression);
}
return static::$compiled[$expression];
} | php | public static function compile($expression, $type = self::TYPE_CSS)
{
if (!is_string($expression)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, gettype($expression)));
}
if (!is_string($type)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 2 to be string, %s given', __METHOD__, gettype($type)));
}
if (strcasecmp($type, self::TYPE_XPATH) !== 0 && strcasecmp($type, self::TYPE_CSS) !== 0) {
throw new RuntimeException(sprintf('Unknown expression type "%s"', $type));
}
$expression = trim($expression);
if ($expression === '') {
throw new InvalidSelectorException('The expression must not be empty');
}
if (strcasecmp($type, self::TYPE_XPATH) === 0) {
return $expression;
}
if (!array_key_exists($expression, static::$compiled)) {
static::$compiled[$expression] = static::cssToXpath($expression);
}
return static::$compiled[$expression];
} | [
"public",
"static",
"function",
"compile",
"(",
"$",
"expression",
",",
"$",
"type",
"=",
"self",
"::",
"TYPE_CSS",
")",
"{",
"if",
"(",
"!",
"is_string",
"(",
"$",
"expression",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'%s expects parameter 1 to be string, %s given'",
",",
"__METHOD__",
",",
"gettype",
"(",
"$",
"expression",
")",
")",
")",
";",
"}",
"if",
"(",
"!",
"is_string",
"(",
"$",
"type",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"'%s expects parameter 2 to be string, %s given'",
",",
"__METHOD__",
",",
"gettype",
"(",
"$",
"type",
")",
")",
")",
";",
"}",
"if",
"(",
"strcasecmp",
"(",
"$",
"type",
",",
"self",
"::",
"TYPE_XPATH",
")",
"!==",
"0",
"&&",
"strcasecmp",
"(",
"$",
"type",
",",
"self",
"::",
"TYPE_CSS",
")",
"!==",
"0",
")",
"{",
"throw",
"new",
"RuntimeException",
"(",
"sprintf",
"(",
"'Unknown expression type \"%s\"'",
",",
"$",
"type",
")",
")",
";",
"}",
"$",
"expression",
"=",
"trim",
"(",
"$",
"expression",
")",
";",
"if",
"(",
"$",
"expression",
"===",
"''",
")",
"{",
"throw",
"new",
"InvalidSelectorException",
"(",
"'The expression must not be empty'",
")",
";",
"}",
"if",
"(",
"strcasecmp",
"(",
"$",
"type",
",",
"self",
"::",
"TYPE_XPATH",
")",
"===",
"0",
")",
"{",
"return",
"$",
"expression",
";",
"}",
"if",
"(",
"!",
"array_key_exists",
"(",
"$",
"expression",
",",
"static",
"::",
"$",
"compiled",
")",
")",
"{",
"static",
"::",
"$",
"compiled",
"[",
"$",
"expression",
"]",
"=",
"static",
"::",
"cssToXpath",
"(",
"$",
"expression",
")",
";",
"}",
"return",
"static",
"::",
"$",
"compiled",
"[",
"$",
"expression",
"]",
";",
"}"
] | Converts a CSS selector into an XPath expression.
@param string $expression XPath expression or CSS selector
@param string $type The type of the expression
@return string XPath expression
@throws InvalidSelectorException if the expression is empty | [
"Converts",
"a",
"CSS",
"selector",
"into",
"an",
"XPath",
"expression",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Query.php#L34-L63 |
Imangazaliev/DiDOM | src/DiDom/Query.php | Query.cssToXpath | public static function cssToXpath($selector, $prefix = '//')
{
$paths = [];
while ($selector !== '') {
list($xpath, $selector) = static::parseAndConvertSelector($selector, $prefix);
if (substr($selector, 0, 1) === ',') {
$selector = trim($selector, ', ');
}
$paths[] = $xpath;
}
return implode('|', $paths);
} | php | public static function cssToXpath($selector, $prefix = '
{
$paths = [];
while ($selector !== '') {
list($xpath, $selector) = static::parseAndConvertSelector($selector, $prefix);
if (substr($selector, 0, 1) === ',') {
$selector = trim($selector, ', ');
}
$paths[] = $xpath;
}
return implode('|', $paths);
} | [
"public",
"static",
"function",
"cssToXpath",
"(",
"$",
"selector",
",",
"$",
"prefix",
"=",
"'//'",
")",
"{",
"$",
"paths",
"=",
"[",
"]",
";",
"while",
"(",
"$",
"selector",
"!==",
"''",
")",
"{",
"list",
"(",
"$",
"xpath",
",",
"$",
"selector",
")",
"=",
"static",
"::",
"parseAndConvertSelector",
"(",
"$",
"selector",
",",
"$",
"prefix",
")",
";",
"if",
"(",
"substr",
"(",
"$",
"selector",
",",
"0",
",",
"1",
")",
"===",
"','",
")",
"{",
"$",
"selector",
"=",
"trim",
"(",
"$",
"selector",
",",
"', '",
")",
";",
"}",
"$",
"paths",
"[",
"]",
"=",
"$",
"xpath",
";",
"}",
"return",
"implode",
"(",
"'|'",
",",
"$",
"paths",
")",
";",
"}"
] | Converts a CSS selector into an XPath expression.
@param string $selector A CSS selector
@param string $prefix Specifies the nesting of nodes
@return string XPath expression | [
"Converts",
"a",
"CSS",
"selector",
"into",
"an",
"XPath",
"expression",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Query.php#L73-L88 |
Imangazaliev/DiDOM | src/DiDom/Query.php | Query.parseAndConvertSelector | protected static function parseAndConvertSelector($selector, $prefix = '//')
{
if (substr($selector, 0, 1) === '>') {
$prefix = '/';
$selector = ltrim($selector, '> ');
}
$segments = self::getSegments($selector);
$xpath = '';
while (count($segments) > 0) {
$xpath .= self::buildXpath($segments, $prefix);
$selector = trim(substr($selector, strlen($segments['selector'])));
$prefix = isset($segments['rel']) ? '/' : '//';
if ($selector === '' || substr($selector, 0, 2) === '::' || substr($selector, 0, 1) === ',') {
break;
}
$segments = self::getSegments($selector);
}
// if selector has property
if (substr($selector, 0, 2) === '::') {
$property = self::parseProperty($selector);
$propertyXpath = self::convertProperty($property['name'], $property['args']);
$selector = substr($selector, strlen($property['property']));
$selector = trim($selector);
$xpath .= '/' . $propertyXpath;
}
return [$xpath, $selector];
} | php | protected static function parseAndConvertSelector($selector, $prefix = '
{
if (substr($selector, 0, 1) === '>') {
$prefix = '/';
$selector = ltrim($selector, '> ');
}
$segments = self::getSegments($selector);
$xpath = '';
while (count($segments) > 0) {
$xpath .= self::buildXpath($segments, $prefix);
$selector = trim(substr($selector, strlen($segments['selector'])));
$prefix = isset($segments['rel']) ? '/' : '
if ($selector === '' || substr($selector, 0, 2) === '::' || substr($selector, 0, 1) === ',') {
break;
}
$segments = self::getSegments($selector);
}
if (substr($selector, 0, 2) === '::') {
$property = self::parseProperty($selector);
$propertyXpath = self::convertProperty($property['name'], $property['args']);
$selector = substr($selector, strlen($property['property']));
$selector = trim($selector);
$xpath .= '/' . $propertyXpath;
}
return [$xpath, $selector];
} | [
"protected",
"static",
"function",
"parseAndConvertSelector",
"(",
"$",
"selector",
",",
"$",
"prefix",
"=",
"'//'",
")",
"{",
"if",
"(",
"substr",
"(",
"$",
"selector",
",",
"0",
",",
"1",
")",
"===",
"'>'",
")",
"{",
"$",
"prefix",
"=",
"'/'",
";",
"$",
"selector",
"=",
"ltrim",
"(",
"$",
"selector",
",",
"'> '",
")",
";",
"}",
"$",
"segments",
"=",
"self",
"::",
"getSegments",
"(",
"$",
"selector",
")",
";",
"$",
"xpath",
"=",
"''",
";",
"while",
"(",
"count",
"(",
"$",
"segments",
")",
">",
"0",
")",
"{",
"$",
"xpath",
".=",
"self",
"::",
"buildXpath",
"(",
"$",
"segments",
",",
"$",
"prefix",
")",
";",
"$",
"selector",
"=",
"trim",
"(",
"substr",
"(",
"$",
"selector",
",",
"strlen",
"(",
"$",
"segments",
"[",
"'selector'",
"]",
")",
")",
")",
";",
"$",
"prefix",
"=",
"isset",
"(",
"$",
"segments",
"[",
"'rel'",
"]",
")",
"?",
"'/'",
":",
"'//'",
";",
"if",
"(",
"$",
"selector",
"===",
"''",
"||",
"substr",
"(",
"$",
"selector",
",",
"0",
",",
"2",
")",
"===",
"'::'",
"||",
"substr",
"(",
"$",
"selector",
",",
"0",
",",
"1",
")",
"===",
"','",
")",
"{",
"break",
";",
"}",
"$",
"segments",
"=",
"self",
"::",
"getSegments",
"(",
"$",
"selector",
")",
";",
"}",
"// if selector has property",
"if",
"(",
"substr",
"(",
"$",
"selector",
",",
"0",
",",
"2",
")",
"===",
"'::'",
")",
"{",
"$",
"property",
"=",
"self",
"::",
"parseProperty",
"(",
"$",
"selector",
")",
";",
"$",
"propertyXpath",
"=",
"self",
"::",
"convertProperty",
"(",
"$",
"property",
"[",
"'name'",
"]",
",",
"$",
"property",
"[",
"'args'",
"]",
")",
";",
"$",
"selector",
"=",
"substr",
"(",
"$",
"selector",
",",
"strlen",
"(",
"$",
"property",
"[",
"'property'",
"]",
")",
")",
";",
"$",
"selector",
"=",
"trim",
"(",
"$",
"selector",
")",
";",
"$",
"xpath",
".=",
"'/'",
".",
"$",
"propertyXpath",
";",
"}",
"return",
"[",
"$",
"xpath",
",",
"$",
"selector",
"]",
";",
"}"
] | @param string $selector
@param string $prefix
@return array | [
"@param",
"string",
"$selector",
"@param",
"string",
"$prefix"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Query.php#L96-L132 |
Imangazaliev/DiDOM | src/DiDom/Query.php | Query.parseProperty | protected static function parseProperty($selector)
{
$name = '(?P<name>[\w\-]+)';
$args = '(?:\((?P<args>[^\)]+)?\))?';
$regexp = '/^::' . $name . $args . '/is';
if (preg_match($regexp, $selector, $matches) !== 1) {
throw new InvalidSelectorException(sprintf('Invalid property "%s"', $selector));
}
$result = [];
$result['property'] = $matches[0];
$result['name'] = $matches['name'];
$result['args'] = isset($matches['args']) ? explode(',', $matches['args']) : [];
$result['args'] = array_map('trim', $result['args']);
return $result;
} | php | protected static function parseProperty($selector)
{
$name = '(?P<name>[\w\-]+)';
$args = '(?:\((?P<args>[^\)]+)?\))?';
$regexp = '/^::' . $name . $args . '/is';
if (preg_match($regexp, $selector, $matches) !== 1) {
throw new InvalidSelectorException(sprintf('Invalid property "%s"', $selector));
}
$result = [];
$result['property'] = $matches[0];
$result['name'] = $matches['name'];
$result['args'] = isset($matches['args']) ? explode(',', $matches['args']) : [];
$result['args'] = array_map('trim', $result['args']);
return $result;
} | [
"protected",
"static",
"function",
"parseProperty",
"(",
"$",
"selector",
")",
"{",
"$",
"name",
"=",
"'(?P<name>[\\w\\-]+)'",
";",
"$",
"args",
"=",
"'(?:\\((?P<args>[^\\)]+)?\\))?'",
";",
"$",
"regexp",
"=",
"'/^::'",
".",
"$",
"name",
".",
"$",
"args",
".",
"'/is'",
";",
"if",
"(",
"preg_match",
"(",
"$",
"regexp",
",",
"$",
"selector",
",",
"$",
"matches",
")",
"!==",
"1",
")",
"{",
"throw",
"new",
"InvalidSelectorException",
"(",
"sprintf",
"(",
"'Invalid property \"%s\"'",
",",
"$",
"selector",
")",
")",
";",
"}",
"$",
"result",
"=",
"[",
"]",
";",
"$",
"result",
"[",
"'property'",
"]",
"=",
"$",
"matches",
"[",
"0",
"]",
";",
"$",
"result",
"[",
"'name'",
"]",
"=",
"$",
"matches",
"[",
"'name'",
"]",
";",
"$",
"result",
"[",
"'args'",
"]",
"=",
"isset",
"(",
"$",
"matches",
"[",
"'args'",
"]",
")",
"?",
"explode",
"(",
"','",
",",
"$",
"matches",
"[",
"'args'",
"]",
")",
":",
"[",
"]",
";",
"$",
"result",
"[",
"'args'",
"]",
"=",
"array_map",
"(",
"'trim'",
",",
"$",
"result",
"[",
"'args'",
"]",
")",
";",
"return",
"$",
"result",
";",
"}"
] | @param string $selector
@return array
@throws InvalidSelectorException | [
"@param",
"string",
"$selector"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Query.php#L141-L161 |
Imangazaliev/DiDOM | src/DiDom/Query.php | Query.convertProperty | protected static function convertProperty($name, array $args = [])
{
if ($name === 'text') {
return 'text()';
}
if ($name === 'attr') {
if (count($args) === 0) {
return '@*';
}
$attributes = [];
foreach ($args as $attribute) {
$attributes[] = sprintf('name() = "%s"', $attribute);
}
return sprintf('@*[%s]', implode(' or ', $attributes));
}
throw new InvalidSelectorException(sprintf('Unknown property "%s"', $name));
} | php | protected static function convertProperty($name, array $args = [])
{
if ($name === 'text') {
return 'text()';
}
if ($name === 'attr') {
if (count($args) === 0) {
return '@*';
}
$attributes = [];
foreach ($args as $attribute) {
$attributes[] = sprintf('name() = "%s"', $attribute);
}
return sprintf('@*[%s]', implode(' or ', $attributes));
}
throw new InvalidSelectorException(sprintf('Unknown property "%s"', $name));
} | [
"protected",
"static",
"function",
"convertProperty",
"(",
"$",
"name",
",",
"array",
"$",
"args",
"=",
"[",
"]",
")",
"{",
"if",
"(",
"$",
"name",
"===",
"'text'",
")",
"{",
"return",
"'text()'",
";",
"}",
"if",
"(",
"$",
"name",
"===",
"'attr'",
")",
"{",
"if",
"(",
"count",
"(",
"$",
"args",
")",
"===",
"0",
")",
"{",
"return",
"'@*'",
";",
"}",
"$",
"attributes",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"args",
"as",
"$",
"attribute",
")",
"{",
"$",
"attributes",
"[",
"]",
"=",
"sprintf",
"(",
"'name() = \"%s\"'",
",",
"$",
"attribute",
")",
";",
"}",
"return",
"sprintf",
"(",
"'@*[%s]'",
",",
"implode",
"(",
"' or '",
",",
"$",
"attributes",
")",
")",
";",
"}",
"throw",
"new",
"InvalidSelectorException",
"(",
"sprintf",
"(",
"'Unknown property \"%s\"'",
",",
"$",
"name",
")",
")",
";",
"}"
] | @param string $name
@param array $args
@return string
@throws InvalidSelectorException if the passed property is unknown | [
"@param",
"string",
"$name",
"@param",
"array",
"$args"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Query.php#L171-L192 |
Imangazaliev/DiDOM | src/DiDom/Query.php | Query.convertPseudo | protected static function convertPseudo($pseudo, &$tagName, array $parameters = [])
{
switch ($pseudo) {
case 'first-child':
return 'position() = 1';
break;
case 'last-child':
return 'position() = last()';
break;
case 'nth-child':
$xpath = sprintf('(name()="%s") and (%s)', $tagName, self::convertNthExpression($parameters[0]));
$tagName = '*';
return $xpath;
break;
case 'contains':
$string = trim($parameters[0], '\'"');
if (count($parameters) === 1) {
return self::convertContains($string);
}
if ($parameters[1] !== 'true' && $parameters[1] !== 'false') {
throw new InvalidSelectorException(sprintf('Parameter 2 of "contains" pseudo-class must be equal true or false, "%s" given', $parameters[1]));
}
$caseSensitive = $parameters[1] === 'true';
if (count($parameters) === 2) {
return self::convertContains($string, $caseSensitive);
}
if ($parameters[2] !== 'true' && $parameters[2] !== 'false') {
throw new InvalidSelectorException(sprintf('Parameter 3 of "contains" pseudo-class must be equal true or false, "%s" given', $parameters[2]));
}
$fullMatch = $parameters[2] === 'true';
return self::convertContains($string, $caseSensitive, $fullMatch);
break;
case 'has':
return self::cssToXpath($parameters[0], './/');
break;
case 'not':
return sprintf('not(self::%s)', self::cssToXpath($parameters[0], ''));
break;
case 'nth-of-type':
return self::convertNthExpression($parameters[0]);
break;
case 'empty':
return 'count(descendant::*) = 0';
break;
case 'not-empty':
return 'count(descendant::*) > 0';
break;
}
throw new InvalidSelectorException(sprintf('Unknown pseudo-class "%s"', $pseudo));
} | php | protected static function convertPseudo($pseudo, &$tagName, array $parameters = [])
{
switch ($pseudo) {
case 'first-child':
return 'position() = 1';
break;
case 'last-child':
return 'position() = last()';
break;
case 'nth-child':
$xpath = sprintf('(name()="%s") and (%s)', $tagName, self::convertNthExpression($parameters[0]));
$tagName = '*';
return $xpath;
break;
case 'contains':
$string = trim($parameters[0], '\'"');
if (count($parameters) === 1) {
return self::convertContains($string);
}
if ($parameters[1] !== 'true' && $parameters[1] !== 'false') {
throw new InvalidSelectorException(sprintf('Parameter 2 of "contains" pseudo-class must be equal true or false, "%s" given', $parameters[1]));
}
$caseSensitive = $parameters[1] === 'true';
if (count($parameters) === 2) {
return self::convertContains($string, $caseSensitive);
}
if ($parameters[2] !== 'true' && $parameters[2] !== 'false') {
throw new InvalidSelectorException(sprintf('Parameter 3 of "contains" pseudo-class must be equal true or false, "%s" given', $parameters[2]));
}
$fullMatch = $parameters[2] === 'true';
return self::convertContains($string, $caseSensitive, $fullMatch);
break;
case 'has':
return self::cssToXpath($parameters[0], '.
break;
case 'not':
return sprintf('not(self::%s)', self::cssToXpath($parameters[0], ''));
break;
case 'nth-of-type':
return self::convertNthExpression($parameters[0]);
break;
case 'empty':
return 'count(descendant::*) = 0';
break;
case 'not-empty':
return 'count(descendant::*) > 0';
break;
}
throw new InvalidSelectorException(sprintf('Unknown pseudo-class "%s"', $pseudo));
} | [
"protected",
"static",
"function",
"convertPseudo",
"(",
"$",
"pseudo",
",",
"&",
"$",
"tagName",
",",
"array",
"$",
"parameters",
"=",
"[",
"]",
")",
"{",
"switch",
"(",
"$",
"pseudo",
")",
"{",
"case",
"'first-child'",
":",
"return",
"'position() = 1'",
";",
"break",
";",
"case",
"'last-child'",
":",
"return",
"'position() = last()'",
";",
"break",
";",
"case",
"'nth-child'",
":",
"$",
"xpath",
"=",
"sprintf",
"(",
"'(name()=\"%s\") and (%s)'",
",",
"$",
"tagName",
",",
"self",
"::",
"convertNthExpression",
"(",
"$",
"parameters",
"[",
"0",
"]",
")",
")",
";",
"$",
"tagName",
"=",
"'*'",
";",
"return",
"$",
"xpath",
";",
"break",
";",
"case",
"'contains'",
":",
"$",
"string",
"=",
"trim",
"(",
"$",
"parameters",
"[",
"0",
"]",
",",
"'\\'\"'",
")",
";",
"if",
"(",
"count",
"(",
"$",
"parameters",
")",
"===",
"1",
")",
"{",
"return",
"self",
"::",
"convertContains",
"(",
"$",
"string",
")",
";",
"}",
"if",
"(",
"$",
"parameters",
"[",
"1",
"]",
"!==",
"'true'",
"&&",
"$",
"parameters",
"[",
"1",
"]",
"!==",
"'false'",
")",
"{",
"throw",
"new",
"InvalidSelectorException",
"(",
"sprintf",
"(",
"'Parameter 2 of \"contains\" pseudo-class must be equal true or false, \"%s\" given'",
",",
"$",
"parameters",
"[",
"1",
"]",
")",
")",
";",
"}",
"$",
"caseSensitive",
"=",
"$",
"parameters",
"[",
"1",
"]",
"===",
"'true'",
";",
"if",
"(",
"count",
"(",
"$",
"parameters",
")",
"===",
"2",
")",
"{",
"return",
"self",
"::",
"convertContains",
"(",
"$",
"string",
",",
"$",
"caseSensitive",
")",
";",
"}",
"if",
"(",
"$",
"parameters",
"[",
"2",
"]",
"!==",
"'true'",
"&&",
"$",
"parameters",
"[",
"2",
"]",
"!==",
"'false'",
")",
"{",
"throw",
"new",
"InvalidSelectorException",
"(",
"sprintf",
"(",
"'Parameter 3 of \"contains\" pseudo-class must be equal true or false, \"%s\" given'",
",",
"$",
"parameters",
"[",
"2",
"]",
")",
")",
";",
"}",
"$",
"fullMatch",
"=",
"$",
"parameters",
"[",
"2",
"]",
"===",
"'true'",
";",
"return",
"self",
"::",
"convertContains",
"(",
"$",
"string",
",",
"$",
"caseSensitive",
",",
"$",
"fullMatch",
")",
";",
"break",
";",
"case",
"'has'",
":",
"return",
"self",
"::",
"cssToXpath",
"(",
"$",
"parameters",
"[",
"0",
"]",
",",
"'.//'",
")",
";",
"break",
";",
"case",
"'not'",
":",
"return",
"sprintf",
"(",
"'not(self::%s)'",
",",
"self",
"::",
"cssToXpath",
"(",
"$",
"parameters",
"[",
"0",
"]",
",",
"''",
")",
")",
";",
"break",
";",
"case",
"'nth-of-type'",
":",
"return",
"self",
"::",
"convertNthExpression",
"(",
"$",
"parameters",
"[",
"0",
"]",
")",
";",
"break",
";",
"case",
"'empty'",
":",
"return",
"'count(descendant::*) = 0'",
";",
"break",
";",
"case",
"'not-empty'",
":",
"return",
"'count(descendant::*) > 0'",
";",
"break",
";",
"}",
"throw",
"new",
"InvalidSelectorException",
"(",
"sprintf",
"(",
"'Unknown pseudo-class \"%s\"'",
",",
"$",
"pseudo",
")",
")",
";",
"}"
] | Converts a CSS pseudo-class into an XPath expression.
@param string $pseudo Pseudo-class
@param string $tagName
@param array $parameters
@return string
@throws InvalidSelectorException if passed an unknown pseudo-class | [
"Converts",
"a",
"CSS",
"pseudo",
"-",
"class",
"into",
"an",
"XPath",
"expression",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Query.php#L205-L263 |
Imangazaliev/DiDOM | src/DiDom/Query.php | Query.buildXpath | public static function buildXpath(array $segments, $prefix = '//')
{
$tagName = isset($segments['tag']) ? $segments['tag'] : '*';
$attributes = [];
// if the id attribute specified
if (isset($segments['id'])) {
$attributes[] = sprintf('@id="%s"', $segments['id']);
}
// if the class attribute specified
if (isset($segments['classes'])) {
foreach ($segments['classes'] as $class) {
$attributes[] = sprintf('contains(concat(" ", normalize-space(@class), " "), " %s ")', $class);
}
}
// if the attributes specified
if (isset($segments['attributes'])) {
foreach ($segments['attributes'] as $name => $value) {
$attributes[] = self::convertAttribute($name, $value);
}
}
// if the pseudo class specified
if (isset($segments['pseudo'])) {
$expression = isset($segments['expr']) ? trim($segments['expr']) : '';
$parameters = explode(',', $expression);
$parameters = array_map('trim', $parameters);
$attributes[] = self::convertPseudo($segments['pseudo'], $tagName, $parameters);
}
if (count($attributes) === 0 && !isset($segments['tag'])) {
throw new InvalidArgumentException('The array of segments must contain the name of the tag or at least one attribute');
}
$xpath = $prefix . $tagName;
if ($count = count($attributes)) {
$xpath .= ($count > 1) ? sprintf('[(%s)]', implode(') and (', $attributes)) : sprintf('[%s]', $attributes[0]);
}
return $xpath;
} | php | public static function buildXpath(array $segments, $prefix = '
{
$tagName = isset($segments['tag']) ? $segments['tag'] : '*';
$attributes = [];
if (isset($segments['id'])) {
$attributes[] = sprintf('@id="%s"', $segments['id']);
}
if (isset($segments['classes'])) {
foreach ($segments['classes'] as $class) {
$attributes[] = sprintf('contains(concat(" ", normalize-space(@class), " "), " %s ")', $class);
}
}
if (isset($segments['attributes'])) {
foreach ($segments['attributes'] as $name => $value) {
$attributes[] = self::convertAttribute($name, $value);
}
}
if (isset($segments['pseudo'])) {
$expression = isset($segments['expr']) ? trim($segments['expr']) : '';
$parameters = explode(',', $expression);
$parameters = array_map('trim', $parameters);
$attributes[] = self::convertPseudo($segments['pseudo'], $tagName, $parameters);
}
if (count($attributes) === 0 && !isset($segments['tag'])) {
throw new InvalidArgumentException('The array of segments must contain the name of the tag or at least one attribute');
}
$xpath = $prefix . $tagName;
if ($count = count($attributes)) {
$xpath .= ($count > 1) ? sprintf('[(%s)]', implode(') and (', $attributes)) : sprintf('[%s]', $attributes[0]);
}
return $xpath;
} | [
"public",
"static",
"function",
"buildXpath",
"(",
"array",
"$",
"segments",
",",
"$",
"prefix",
"=",
"'//'",
")",
"{",
"$",
"tagName",
"=",
"isset",
"(",
"$",
"segments",
"[",
"'tag'",
"]",
")",
"?",
"$",
"segments",
"[",
"'tag'",
"]",
":",
"'*'",
";",
"$",
"attributes",
"=",
"[",
"]",
";",
"// if the id attribute specified",
"if",
"(",
"isset",
"(",
"$",
"segments",
"[",
"'id'",
"]",
")",
")",
"{",
"$",
"attributes",
"[",
"]",
"=",
"sprintf",
"(",
"'@id=\"%s\"'",
",",
"$",
"segments",
"[",
"'id'",
"]",
")",
";",
"}",
"// if the class attribute specified",
"if",
"(",
"isset",
"(",
"$",
"segments",
"[",
"'classes'",
"]",
")",
")",
"{",
"foreach",
"(",
"$",
"segments",
"[",
"'classes'",
"]",
"as",
"$",
"class",
")",
"{",
"$",
"attributes",
"[",
"]",
"=",
"sprintf",
"(",
"'contains(concat(\" \", normalize-space(@class), \" \"), \" %s \")'",
",",
"$",
"class",
")",
";",
"}",
"}",
"// if the attributes specified",
"if",
"(",
"isset",
"(",
"$",
"segments",
"[",
"'attributes'",
"]",
")",
")",
"{",
"foreach",
"(",
"$",
"segments",
"[",
"'attributes'",
"]",
"as",
"$",
"name",
"=>",
"$",
"value",
")",
"{",
"$",
"attributes",
"[",
"]",
"=",
"self",
"::",
"convertAttribute",
"(",
"$",
"name",
",",
"$",
"value",
")",
";",
"}",
"}",
"// if the pseudo class specified",
"if",
"(",
"isset",
"(",
"$",
"segments",
"[",
"'pseudo'",
"]",
")",
")",
"{",
"$",
"expression",
"=",
"isset",
"(",
"$",
"segments",
"[",
"'expr'",
"]",
")",
"?",
"trim",
"(",
"$",
"segments",
"[",
"'expr'",
"]",
")",
":",
"''",
";",
"$",
"parameters",
"=",
"explode",
"(",
"','",
",",
"$",
"expression",
")",
";",
"$",
"parameters",
"=",
"array_map",
"(",
"'trim'",
",",
"$",
"parameters",
")",
";",
"$",
"attributes",
"[",
"]",
"=",
"self",
"::",
"convertPseudo",
"(",
"$",
"segments",
"[",
"'pseudo'",
"]",
",",
"$",
"tagName",
",",
"$",
"parameters",
")",
";",
"}",
"if",
"(",
"count",
"(",
"$",
"attributes",
")",
"===",
"0",
"&&",
"!",
"isset",
"(",
"$",
"segments",
"[",
"'tag'",
"]",
")",
")",
"{",
"throw",
"new",
"InvalidArgumentException",
"(",
"'The array of segments must contain the name of the tag or at least one attribute'",
")",
";",
"}",
"$",
"xpath",
"=",
"$",
"prefix",
".",
"$",
"tagName",
";",
"if",
"(",
"$",
"count",
"=",
"count",
"(",
"$",
"attributes",
")",
")",
"{",
"$",
"xpath",
".=",
"(",
"$",
"count",
">",
"1",
")",
"?",
"sprintf",
"(",
"'[(%s)]'",
",",
"implode",
"(",
"') and ('",
",",
"$",
"attributes",
")",
")",
":",
"sprintf",
"(",
"'[%s]'",
",",
"$",
"attributes",
"[",
"0",
"]",
")",
";",
"}",
"return",
"$",
"xpath",
";",
"}"
] | @param array $segments
@param string $prefix Specifies the nesting of nodes
@return string XPath expression
@throws InvalidArgumentException if you neither specify tag name nor attributes | [
"@param",
"array",
"$segments",
"@param",
"string",
"$prefix",
"Specifies",
"the",
"nesting",
"of",
"nodes"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Query.php#L273-L319 |
Imangazaliev/DiDOM | src/DiDom/Query.php | Query.convertAttribute | protected static function convertAttribute($name, $value)
{
$isSimpleSelector = !in_array(substr($name, 0, 1), ['^', '!'], true);
$isSimpleSelector = $isSimpleSelector && (!in_array(substr($name, -1), ['^', '$', '*', '!', '~'], true));
if ($isSimpleSelector) {
// if specified only the attribute name
$xpath = $value === null ? '@'.$name : sprintf('@%s="%s"', $name, $value);
return $xpath;
}
// if the attribute name starts with ^
// example: *[^data-]
if (substr($name, 0, 1) === '^') {
$xpath = sprintf('@*[starts-with(name(), "%s")]', substr($name, 1));
return $value === null ? $xpath : sprintf('%s="%s"', $xpath, $value);
}
// if the attribute name starts with !
// example: input[!disabled]
if (substr($name, 0, 1) === '!') {
$xpath = sprintf('not(@%s)', substr($name, 1));
return $xpath;
}
$symbol = substr($name, -1);
$name = substr($name, 0, -1);
switch ($symbol) {
case '^':
$xpath = sprintf('starts-with(@%s, "%s")', $name, $value);
break;
case '$':
$xpath = sprintf('substring(@%s, string-length(@%s) - string-length("%s") + 1) = "%s"', $name, $name, $value, $value);
break;
case '*':
$xpath = sprintf('contains(@%s, "%s")', $name, $value);
break;
case '!':
$xpath = sprintf('not(@%s="%s")', $name, $value);
break;
case '~':
$xpath = sprintf('contains(concat(" ", normalize-space(@%s), " "), " %s ")', $name, $value);
break;
}
return $xpath;
} | php | protected static function convertAttribute($name, $value)
{
$isSimpleSelector = !in_array(substr($name, 0, 1), ['^', '!'], true);
$isSimpleSelector = $isSimpleSelector && (!in_array(substr($name, -1), ['^', '$', '*', '!', '~'], true));
if ($isSimpleSelector) {
$xpath = $value === null ? '@'.$name : sprintf('@%s="%s"', $name, $value);
return $xpath;
}
if (substr($name, 0, 1) === '^') {
$xpath = sprintf('@*[starts-with(name(), "%s")]', substr($name, 1));
return $value === null ? $xpath : sprintf('%s="%s"', $xpath, $value);
}
if (substr($name, 0, 1) === '!') {
$xpath = sprintf('not(@%s)', substr($name, 1));
return $xpath;
}
$symbol = substr($name, -1);
$name = substr($name, 0, -1);
switch ($symbol) {
case '^':
$xpath = sprintf('starts-with(@%s, "%s")', $name, $value);
break;
case '$':
$xpath = sprintf('substring(@%s, string-length(@%s) - string-length("%s") + 1) = "%s"', $name, $name, $value, $value);
break;
case '*':
$xpath = sprintf('contains(@%s, "%s")', $name, $value);
break;
case '!':
$xpath = sprintf('not(@%s="%s")', $name, $value);
break;
case '~':
$xpath = sprintf('contains(concat(" ", normalize-space(@%s), " "), " %s ")', $name, $value);
break;
}
return $xpath;
} | [
"protected",
"static",
"function",
"convertAttribute",
"(",
"$",
"name",
",",
"$",
"value",
")",
"{",
"$",
"isSimpleSelector",
"=",
"!",
"in_array",
"(",
"substr",
"(",
"$",
"name",
",",
"0",
",",
"1",
")",
",",
"[",
"'^'",
",",
"'!'",
"]",
",",
"true",
")",
";",
"$",
"isSimpleSelector",
"=",
"$",
"isSimpleSelector",
"&&",
"(",
"!",
"in_array",
"(",
"substr",
"(",
"$",
"name",
",",
"-",
"1",
")",
",",
"[",
"'^'",
",",
"'$'",
",",
"'*'",
",",
"'!'",
",",
"'~'",
"]",
",",
"true",
")",
")",
";",
"if",
"(",
"$",
"isSimpleSelector",
")",
"{",
"// if specified only the attribute name",
"$",
"xpath",
"=",
"$",
"value",
"===",
"null",
"?",
"'@'",
".",
"$",
"name",
":",
"sprintf",
"(",
"'@%s=\"%s\"'",
",",
"$",
"name",
",",
"$",
"value",
")",
";",
"return",
"$",
"xpath",
";",
"}",
"// if the attribute name starts with ^",
"// example: *[^data-]",
"if",
"(",
"substr",
"(",
"$",
"name",
",",
"0",
",",
"1",
")",
"===",
"'^'",
")",
"{",
"$",
"xpath",
"=",
"sprintf",
"(",
"'@*[starts-with(name(), \"%s\")]'",
",",
"substr",
"(",
"$",
"name",
",",
"1",
")",
")",
";",
"return",
"$",
"value",
"===",
"null",
"?",
"$",
"xpath",
":",
"sprintf",
"(",
"'%s=\"%s\"'",
",",
"$",
"xpath",
",",
"$",
"value",
")",
";",
"}",
"// if the attribute name starts with !",
"// example: input[!disabled]",
"if",
"(",
"substr",
"(",
"$",
"name",
",",
"0",
",",
"1",
")",
"===",
"'!'",
")",
"{",
"$",
"xpath",
"=",
"sprintf",
"(",
"'not(@%s)'",
",",
"substr",
"(",
"$",
"name",
",",
"1",
")",
")",
";",
"return",
"$",
"xpath",
";",
"}",
"$",
"symbol",
"=",
"substr",
"(",
"$",
"name",
",",
"-",
"1",
")",
";",
"$",
"name",
"=",
"substr",
"(",
"$",
"name",
",",
"0",
",",
"-",
"1",
")",
";",
"switch",
"(",
"$",
"symbol",
")",
"{",
"case",
"'^'",
":",
"$",
"xpath",
"=",
"sprintf",
"(",
"'starts-with(@%s, \"%s\")'",
",",
"$",
"name",
",",
"$",
"value",
")",
";",
"break",
";",
"case",
"'$'",
":",
"$",
"xpath",
"=",
"sprintf",
"(",
"'substring(@%s, string-length(@%s) - string-length(\"%s\") + 1) = \"%s\"'",
",",
"$",
"name",
",",
"$",
"name",
",",
"$",
"value",
",",
"$",
"value",
")",
";",
"break",
";",
"case",
"'*'",
":",
"$",
"xpath",
"=",
"sprintf",
"(",
"'contains(@%s, \"%s\")'",
",",
"$",
"name",
",",
"$",
"value",
")",
";",
"break",
";",
"case",
"'!'",
":",
"$",
"xpath",
"=",
"sprintf",
"(",
"'not(@%s=\"%s\")'",
",",
"$",
"name",
",",
"$",
"value",
")",
";",
"break",
";",
"case",
"'~'",
":",
"$",
"xpath",
"=",
"sprintf",
"(",
"'contains(concat(\" \", normalize-space(@%s), \" \"), \" %s \")'",
",",
"$",
"name",
",",
"$",
"value",
")",
";",
"break",
";",
"}",
"return",
"$",
"xpath",
";",
"}"
] | @param string $name The attribute name
@param string $value The attribute value
@return string | [
"@param",
"string",
"$name",
"The",
"attribute",
"name",
"@param",
"string",
"$value",
"The",
"attribute",
"value"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Query.php#L327-L377 |
Imangazaliev/DiDOM | src/DiDom/Query.php | Query.convertNthExpression | protected static function convertNthExpression($expression)
{
if ($expression === '') {
throw new InvalidSelectorException('nth-child (or nth-last-child) expression must not be empty');
}
if ($expression === 'odd') {
return 'position() mod 2 = 1 and position() >= 1';
}
if ($expression === 'even') {
return 'position() mod 2 = 0 and position() >= 0';
}
if (is_numeric($expression)) {
return sprintf('position() = %d', $expression);
}
if (preg_match("/^(?P<mul>[0-9]?n)(?:(?P<sign>\+|\-)(?P<pos>[0-9]+))?$/is", $expression, $segments)) {
if (isset($segments['mul'])) {
$multiplier = $segments['mul'] === 'n' ? 1 : trim($segments['mul'], 'n');
$sign = (isset($segments['sign']) && $segments['sign'] === '+') ? '-' : '+';
$position = isset($segments['pos']) ? $segments['pos'] : 0;
return sprintf('(position() %s %d) mod %d = 0 and position() >= %d', $sign, $position, $multiplier, $position);
}
}
throw new InvalidSelectorException(sprintf('Invalid nth-child expression "%s"', $expression));
} | php | protected static function convertNthExpression($expression)
{
if ($expression === '') {
throw new InvalidSelectorException('nth-child (or nth-last-child) expression must not be empty');
}
if ($expression === 'odd') {
return 'position() mod 2 = 1 and position() >= 1';
}
if ($expression === 'even') {
return 'position() mod 2 = 0 and position() >= 0';
}
if (is_numeric($expression)) {
return sprintf('position() = %d', $expression);
}
if (preg_match("/^(?P<mul>[0-9]?n)(?:(?P<sign>\+|\-)(?P<pos>[0-9]+))?$/is", $expression, $segments)) {
if (isset($segments['mul'])) {
$multiplier = $segments['mul'] === 'n' ? 1 : trim($segments['mul'], 'n');
$sign = (isset($segments['sign']) && $segments['sign'] === '+') ? '-' : '+';
$position = isset($segments['pos']) ? $segments['pos'] : 0;
return sprintf('(position() %s %d) mod %d = 0 and position() >= %d', $sign, $position, $multiplier, $position);
}
}
throw new InvalidSelectorException(sprintf('Invalid nth-child expression "%s"', $expression));
} | [
"protected",
"static",
"function",
"convertNthExpression",
"(",
"$",
"expression",
")",
"{",
"if",
"(",
"$",
"expression",
"===",
"''",
")",
"{",
"throw",
"new",
"InvalidSelectorException",
"(",
"'nth-child (or nth-last-child) expression must not be empty'",
")",
";",
"}",
"if",
"(",
"$",
"expression",
"===",
"'odd'",
")",
"{",
"return",
"'position() mod 2 = 1 and position() >= 1'",
";",
"}",
"if",
"(",
"$",
"expression",
"===",
"'even'",
")",
"{",
"return",
"'position() mod 2 = 0 and position() >= 0'",
";",
"}",
"if",
"(",
"is_numeric",
"(",
"$",
"expression",
")",
")",
"{",
"return",
"sprintf",
"(",
"'position() = %d'",
",",
"$",
"expression",
")",
";",
"}",
"if",
"(",
"preg_match",
"(",
"\"/^(?P<mul>[0-9]?n)(?:(?P<sign>\\+|\\-)(?P<pos>[0-9]+))?$/is\"",
",",
"$",
"expression",
",",
"$",
"segments",
")",
")",
"{",
"if",
"(",
"isset",
"(",
"$",
"segments",
"[",
"'mul'",
"]",
")",
")",
"{",
"$",
"multiplier",
"=",
"$",
"segments",
"[",
"'mul'",
"]",
"===",
"'n'",
"?",
"1",
":",
"trim",
"(",
"$",
"segments",
"[",
"'mul'",
"]",
",",
"'n'",
")",
";",
"$",
"sign",
"=",
"(",
"isset",
"(",
"$",
"segments",
"[",
"'sign'",
"]",
")",
"&&",
"$",
"segments",
"[",
"'sign'",
"]",
"===",
"'+'",
")",
"?",
"'-'",
":",
"'+'",
";",
"$",
"position",
"=",
"isset",
"(",
"$",
"segments",
"[",
"'pos'",
"]",
")",
"?",
"$",
"segments",
"[",
"'pos'",
"]",
":",
"0",
";",
"return",
"sprintf",
"(",
"'(position() %s %d) mod %d = 0 and position() >= %d'",
",",
"$",
"sign",
",",
"$",
"position",
",",
"$",
"multiplier",
",",
"$",
"position",
")",
";",
"}",
"}",
"throw",
"new",
"InvalidSelectorException",
"(",
"sprintf",
"(",
"'Invalid nth-child expression \"%s\"'",
",",
"$",
"expression",
")",
")",
";",
"}"
] | Converts nth-expression into an XPath expression.
@param string $expression nth-expression
@return string
@throws InvalidSelectorException if passed nth-child is empty
@throws InvalidSelectorException if passed an unknown nth-child expression | [
"Converts",
"nth",
"-",
"expression",
"into",
"an",
"XPath",
"expression",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Query.php#L389-L418 |
Imangazaliev/DiDOM | src/DiDom/Query.php | Query.convertContains | protected static function convertContains($string, $caseSensitive = true, $fullMatch = false)
{
if ($caseSensitive && $fullMatch) {
return sprintf('text() = "%s"', $string);
}
if ($caseSensitive && !$fullMatch) {
return sprintf('contains(text(), "%s")', $string);
}
$strToLowerFunction = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
if (!$caseSensitive && $fullMatch) {
return sprintf("php:functionString(\"{$strToLowerFunction}\", .) = php:functionString(\"{$strToLowerFunction}\", \"%s\")", $string);
}
// if !$caseSensitive and !$fullMatch
return sprintf("contains(php:functionString(\"{$strToLowerFunction}\", .), php:functionString(\"{$strToLowerFunction}\", \"%s\"))", $string);
} | php | protected static function convertContains($string, $caseSensitive = true, $fullMatch = false)
{
if ($caseSensitive && $fullMatch) {
return sprintf('text() = "%s"', $string);
}
if ($caseSensitive && !$fullMatch) {
return sprintf('contains(text(), "%s")', $string);
}
$strToLowerFunction = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
if (!$caseSensitive && $fullMatch) {
return sprintf("php:functionString(\"{$strToLowerFunction}\", .) = php:functionString(\"{$strToLowerFunction}\", \"%s\")", $string);
}
return sprintf("contains(php:functionString(\"{$strToLowerFunction}\", .), php:functionString(\"{$strToLowerFunction}\", \"%s\"))", $string);
} | [
"protected",
"static",
"function",
"convertContains",
"(",
"$",
"string",
",",
"$",
"caseSensitive",
"=",
"true",
",",
"$",
"fullMatch",
"=",
"false",
")",
"{",
"if",
"(",
"$",
"caseSensitive",
"&&",
"$",
"fullMatch",
")",
"{",
"return",
"sprintf",
"(",
"'text() = \"%s\"'",
",",
"$",
"string",
")",
";",
"}",
"if",
"(",
"$",
"caseSensitive",
"&&",
"!",
"$",
"fullMatch",
")",
"{",
"return",
"sprintf",
"(",
"'contains(text(), \"%s\")'",
",",
"$",
"string",
")",
";",
"}",
"$",
"strToLowerFunction",
"=",
"function_exists",
"(",
"'mb_strtolower'",
")",
"?",
"'mb_strtolower'",
":",
"'strtolower'",
";",
"if",
"(",
"!",
"$",
"caseSensitive",
"&&",
"$",
"fullMatch",
")",
"{",
"return",
"sprintf",
"(",
"\"php:functionString(\\\"{$strToLowerFunction}\\\", .) = php:functionString(\\\"{$strToLowerFunction}\\\", \\\"%s\\\")\"",
",",
"$",
"string",
")",
";",
"}",
"// if !$caseSensitive and !$fullMatch",
"return",
"sprintf",
"(",
"\"contains(php:functionString(\\\"{$strToLowerFunction}\\\", .), php:functionString(\\\"{$strToLowerFunction}\\\", \\\"%s\\\"))\"",
",",
"$",
"string",
")",
";",
"}"
] | @param string $string
@param bool $caseSensitive
@param bool $fullMatch
@return string | [
"@param",
"string",
"$string",
"@param",
"bool",
"$caseSensitive",
"@param",
"bool",
"$fullMatch"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Query.php#L427-L445 |
Imangazaliev/DiDOM | src/DiDom/Query.php | Query.getSegments | public static function getSegments($selector)
{
$selector = trim($selector);
if ($selector === '') {
throw new InvalidSelectorException('The selector must not be empty');
}
$tag = '(?P<tag>[\*|\w|\-]+)?';
$id = '(?:#(?P<id>[\w|\-]+))?';
$classes = '(?P<classes>\.[\w|\-|\.]+)*';
$attrs = '(?P<attrs>(?:\[.+?\])*)?';
$name = '(?P<pseudo>[\w\-]+)';
$expr = '(?:\((?P<expr>[^\)]+)\))';
$pseudo = '(?::'.$name.$expr.'?)?';
$rel = '\s*(?P<rel>>)?';
$regexp = '/'.$tag.$id.$classes.$attrs.$pseudo.$rel.'/is';
if (preg_match($regexp, $selector, $segments)) {
if ($segments[0] === '') {
throw new InvalidSelectorException(sprintf('Invalid selector "%s"', $selector));
}
$result['selector'] = $segments[0];
if (isset($segments['tag']) && $segments['tag'] !== '') {
$result['tag'] = $segments['tag'];
}
// if the id attribute specified
if (isset($segments['id']) && $segments['id'] !== '') {
$result['id'] = $segments['id'];
}
// if the attributes specified
if (isset($segments['attrs'])) {
$attributes = trim($segments['attrs'], '[]');
$attributes = explode('][', $attributes);
foreach ($attributes as $attribute) {
if ($attribute !== '') {
list($name, $value) = array_pad(explode('=', $attribute, 2), 2, null);
if ($name === '') {
throw new InvalidSelectorException(sprintf('Invalid selector "%s": attribute name must not be empty', $selector));
}
// equal null if specified only the attribute name
$result['attributes'][$name] = is_string($value) ? trim($value, '\'"') : null;
}
}
}
// if the class attribute specified
if (isset($segments['classes'])) {
$classes = trim($segments['classes'], '.');
$classes = explode('.', $classes);
foreach ($classes as $class) {
if ($class !== '') {
$result['classes'][] = $class;
}
}
}
// if the pseudo class specified
if (isset($segments['pseudo']) && $segments['pseudo'] !== '') {
$result['pseudo'] = $segments['pseudo'];
if (isset($segments['expr']) && $segments['expr'] !== '') {
$result['expr'] = $segments['expr'];
}
}
// if it is a direct descendant
if (isset($segments['rel'])) {
$result['rel'] = $segments['rel'];
}
return $result;
}
throw new InvalidSelectorException(sprintf('Invalid selector "%s"', $selector));
} | php | public static function getSegments($selector)
{
$selector = trim($selector);
if ($selector === '') {
throw new InvalidSelectorException('The selector must not be empty');
}
$tag = '(?P<tag>[\*|\w|\-]+)?';
$id = '(?:
$classes = '(?P<classes>\.[\w|\-|\.]+)*';
$attrs = '(?P<attrs>(?:\[.+?\])*)?';
$name = '(?P<pseudo>[\w\-]+)';
$expr = '(?:\((?P<expr>[^\)]+)\))';
$pseudo = '(?::'.$name.$expr.'?)?';
$rel = '\s*(?P<rel>>)?';
$regexp = '/'.$tag.$id.$classes.$attrs.$pseudo.$rel.'/is';
if (preg_match($regexp, $selector, $segments)) {
if ($segments[0] === '') {
throw new InvalidSelectorException(sprintf('Invalid selector "%s"', $selector));
}
$result['selector'] = $segments[0];
if (isset($segments['tag']) && $segments['tag'] !== '') {
$result['tag'] = $segments['tag'];
}
if (isset($segments['id']) && $segments['id'] !== '') {
$result['id'] = $segments['id'];
}
if (isset($segments['attrs'])) {
$attributes = trim($segments['attrs'], '[]');
$attributes = explode('][', $attributes);
foreach ($attributes as $attribute) {
if ($attribute !== '') {
list($name, $value) = array_pad(explode('=', $attribute, 2), 2, null);
if ($name === '') {
throw new InvalidSelectorException(sprintf('Invalid selector "%s": attribute name must not be empty', $selector));
}
$result['attributes'][$name] = is_string($value) ? trim($value, '\'"') : null;
}
}
}
if (isset($segments['classes'])) {
$classes = trim($segments['classes'], '.');
$classes = explode('.', $classes);
foreach ($classes as $class) {
if ($class !== '') {
$result['classes'][] = $class;
}
}
}
if (isset($segments['pseudo']) && $segments['pseudo'] !== '') {
$result['pseudo'] = $segments['pseudo'];
if (isset($segments['expr']) && $segments['expr'] !== '') {
$result['expr'] = $segments['expr'];
}
}
if (isset($segments['rel'])) {
$result['rel'] = $segments['rel'];
}
return $result;
}
throw new InvalidSelectorException(sprintf('Invalid selector "%s"', $selector));
} | [
"public",
"static",
"function",
"getSegments",
"(",
"$",
"selector",
")",
"{",
"$",
"selector",
"=",
"trim",
"(",
"$",
"selector",
")",
";",
"if",
"(",
"$",
"selector",
"===",
"''",
")",
"{",
"throw",
"new",
"InvalidSelectorException",
"(",
"'The selector must not be empty'",
")",
";",
"}",
"$",
"tag",
"=",
"'(?P<tag>[\\*|\\w|\\-]+)?'",
";",
"$",
"id",
"=",
"'(?:#(?P<id>[\\w|\\-]+))?'",
";",
"$",
"classes",
"=",
"'(?P<classes>\\.[\\w|\\-|\\.]+)*'",
";",
"$",
"attrs",
"=",
"'(?P<attrs>(?:\\[.+?\\])*)?'",
";",
"$",
"name",
"=",
"'(?P<pseudo>[\\w\\-]+)'",
";",
"$",
"expr",
"=",
"'(?:\\((?P<expr>[^\\)]+)\\))'",
";",
"$",
"pseudo",
"=",
"'(?::'",
".",
"$",
"name",
".",
"$",
"expr",
".",
"'?)?'",
";",
"$",
"rel",
"=",
"'\\s*(?P<rel>>)?'",
";",
"$",
"regexp",
"=",
"'/'",
".",
"$",
"tag",
".",
"$",
"id",
".",
"$",
"classes",
".",
"$",
"attrs",
".",
"$",
"pseudo",
".",
"$",
"rel",
".",
"'/is'",
";",
"if",
"(",
"preg_match",
"(",
"$",
"regexp",
",",
"$",
"selector",
",",
"$",
"segments",
")",
")",
"{",
"if",
"(",
"$",
"segments",
"[",
"0",
"]",
"===",
"''",
")",
"{",
"throw",
"new",
"InvalidSelectorException",
"(",
"sprintf",
"(",
"'Invalid selector \"%s\"'",
",",
"$",
"selector",
")",
")",
";",
"}",
"$",
"result",
"[",
"'selector'",
"]",
"=",
"$",
"segments",
"[",
"0",
"]",
";",
"if",
"(",
"isset",
"(",
"$",
"segments",
"[",
"'tag'",
"]",
")",
"&&",
"$",
"segments",
"[",
"'tag'",
"]",
"!==",
"''",
")",
"{",
"$",
"result",
"[",
"'tag'",
"]",
"=",
"$",
"segments",
"[",
"'tag'",
"]",
";",
"}",
"// if the id attribute specified",
"if",
"(",
"isset",
"(",
"$",
"segments",
"[",
"'id'",
"]",
")",
"&&",
"$",
"segments",
"[",
"'id'",
"]",
"!==",
"''",
")",
"{",
"$",
"result",
"[",
"'id'",
"]",
"=",
"$",
"segments",
"[",
"'id'",
"]",
";",
"}",
"// if the attributes specified",
"if",
"(",
"isset",
"(",
"$",
"segments",
"[",
"'attrs'",
"]",
")",
")",
"{",
"$",
"attributes",
"=",
"trim",
"(",
"$",
"segments",
"[",
"'attrs'",
"]",
",",
"'[]'",
")",
";",
"$",
"attributes",
"=",
"explode",
"(",
"']['",
",",
"$",
"attributes",
")",
";",
"foreach",
"(",
"$",
"attributes",
"as",
"$",
"attribute",
")",
"{",
"if",
"(",
"$",
"attribute",
"!==",
"''",
")",
"{",
"list",
"(",
"$",
"name",
",",
"$",
"value",
")",
"=",
"array_pad",
"(",
"explode",
"(",
"'='",
",",
"$",
"attribute",
",",
"2",
")",
",",
"2",
",",
"null",
")",
";",
"if",
"(",
"$",
"name",
"===",
"''",
")",
"{",
"throw",
"new",
"InvalidSelectorException",
"(",
"sprintf",
"(",
"'Invalid selector \"%s\": attribute name must not be empty'",
",",
"$",
"selector",
")",
")",
";",
"}",
"// equal null if specified only the attribute name",
"$",
"result",
"[",
"'attributes'",
"]",
"[",
"$",
"name",
"]",
"=",
"is_string",
"(",
"$",
"value",
")",
"?",
"trim",
"(",
"$",
"value",
",",
"'\\'\"'",
")",
":",
"null",
";",
"}",
"}",
"}",
"// if the class attribute specified",
"if",
"(",
"isset",
"(",
"$",
"segments",
"[",
"'classes'",
"]",
")",
")",
"{",
"$",
"classes",
"=",
"trim",
"(",
"$",
"segments",
"[",
"'classes'",
"]",
",",
"'.'",
")",
";",
"$",
"classes",
"=",
"explode",
"(",
"'.'",
",",
"$",
"classes",
")",
";",
"foreach",
"(",
"$",
"classes",
"as",
"$",
"class",
")",
"{",
"if",
"(",
"$",
"class",
"!==",
"''",
")",
"{",
"$",
"result",
"[",
"'classes'",
"]",
"[",
"]",
"=",
"$",
"class",
";",
"}",
"}",
"}",
"// if the pseudo class specified",
"if",
"(",
"isset",
"(",
"$",
"segments",
"[",
"'pseudo'",
"]",
")",
"&&",
"$",
"segments",
"[",
"'pseudo'",
"]",
"!==",
"''",
")",
"{",
"$",
"result",
"[",
"'pseudo'",
"]",
"=",
"$",
"segments",
"[",
"'pseudo'",
"]",
";",
"if",
"(",
"isset",
"(",
"$",
"segments",
"[",
"'expr'",
"]",
")",
"&&",
"$",
"segments",
"[",
"'expr'",
"]",
"!==",
"''",
")",
"{",
"$",
"result",
"[",
"'expr'",
"]",
"=",
"$",
"segments",
"[",
"'expr'",
"]",
";",
"}",
"}",
"// if it is a direct descendant",
"if",
"(",
"isset",
"(",
"$",
"segments",
"[",
"'rel'",
"]",
")",
")",
"{",
"$",
"result",
"[",
"'rel'",
"]",
"=",
"$",
"segments",
"[",
"'rel'",
"]",
";",
"}",
"return",
"$",
"result",
";",
"}",
"throw",
"new",
"InvalidSelectorException",
"(",
"sprintf",
"(",
"'Invalid selector \"%s\"'",
",",
"$",
"selector",
")",
")",
";",
"}"
] | Splits the CSS selector into parts (tag name, ID, classes, attributes, pseudo-class).
@param string $selector CSS selector
@return array
@throws InvalidSelectorException if the selector is empty or not valid | [
"Splits",
"the",
"CSS",
"selector",
"into",
"parts",
"(",
"tag",
"name",
"ID",
"classes",
"attributes",
"pseudo",
"-",
"class",
")",
"."
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Query.php#L456-L540 |
Imangazaliev/DiDOM | src/DiDom/Encoder.php | Encoder.convertToHtmlEntities | public static function convertToHtmlEntities($string, $encoding)
{
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding($string, 'HTML-ENTITIES', $encoding);
}
if ('UTF-8' !== $encoding) {
$string = iconv($encoding, 'UTF-8//IGNORE', $string);
}
return preg_replace_callback('/[\x80-\xFF]+/', [__CLASS__, 'htmlEncodingCallback'], $string);
} | php | public static function convertToHtmlEntities($string, $encoding)
{
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding($string, 'HTML-ENTITIES', $encoding);
}
if ('UTF-8' !== $encoding) {
$string = iconv($encoding, 'UTF-8
}
return preg_replace_callback('/[\x80-\xFF]+/', [__CLASS__, 'htmlEncodingCallback'], $string);
} | [
"public",
"static",
"function",
"convertToHtmlEntities",
"(",
"$",
"string",
",",
"$",
"encoding",
")",
"{",
"if",
"(",
"function_exists",
"(",
"'mb_convert_encoding'",
")",
")",
"{",
"return",
"mb_convert_encoding",
"(",
"$",
"string",
",",
"'HTML-ENTITIES'",
",",
"$",
"encoding",
")",
";",
"}",
"if",
"(",
"'UTF-8'",
"!==",
"$",
"encoding",
")",
"{",
"$",
"string",
"=",
"iconv",
"(",
"$",
"encoding",
",",
"'UTF-8//IGNORE'",
",",
"$",
"string",
")",
";",
"}",
"return",
"preg_replace_callback",
"(",
"'/[\\x80-\\xFF]+/'",
",",
"[",
"__CLASS__",
",",
"'htmlEncodingCallback'",
"]",
",",
"$",
"string",
")",
";",
"}"
] | @param string $string
@param string $encoding
@return string | [
"@param",
"string",
"$string",
"@param",
"string",
"$encoding"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Encoder.php#L13-L24 |
Imangazaliev/DiDOM | src/DiDom/Encoder.php | Encoder.htmlEncodingCallback | private static function htmlEncodingCallback($matches)
{
$characterIndex = 1;
$entities = '';
$codes = unpack('C*', htmlentities($matches[0], ENT_COMPAT, 'UTF-8'));
while (isset($codes[$characterIndex])) {
if (0x80 > $codes[$characterIndex]) {
$entities .= chr($codes[$characterIndex++]);
continue;
}
if (0xF0 <= $codes[$characterIndex]) {
$code = (($codes[$characterIndex++] - 0xF0) << 18) + (($codes[$characterIndex++] - 0x80) << 12) + (($codes[$characterIndex++] - 0x80) << 6) + $codes[$characterIndex++] - 0x80;
} elseif (0xE0 <= $codes[$characterIndex]) {
$code = (($codes[$characterIndex++] - 0xE0) << 12) + (($codes[$characterIndex++] - 0x80) << 6) + $codes[$characterIndex++] - 0x80;
} else {
$code = (($codes[$characterIndex++] - 0xC0) << 6) + $codes[$characterIndex++] - 0x80;
}
$entities .= '&#' . $code . ';';
}
return $entities;
} | php | private static function htmlEncodingCallback($matches)
{
$characterIndex = 1;
$entities = '';
$codes = unpack('C*', htmlentities($matches[0], ENT_COMPAT, 'UTF-8'));
while (isset($codes[$characterIndex])) {
if (0x80 > $codes[$characterIndex]) {
$entities .= chr($codes[$characterIndex++]);
continue;
}
if (0xF0 <= $codes[$characterIndex]) {
$code = (($codes[$characterIndex++] - 0xF0) << 18) + (($codes[$characterIndex++] - 0x80) << 12) + (($codes[$characterIndex++] - 0x80) << 6) + $codes[$characterIndex++] - 0x80;
} elseif (0xE0 <= $codes[$characterIndex]) {
$code = (($codes[$characterIndex++] - 0xE0) << 12) + (($codes[$characterIndex++] - 0x80) << 6) + $codes[$characterIndex++] - 0x80;
} else {
$code = (($codes[$characterIndex++] - 0xC0) << 6) + $codes[$characterIndex++] - 0x80;
}
$entities .= '&
}
return $entities;
} | [
"private",
"static",
"function",
"htmlEncodingCallback",
"(",
"$",
"matches",
")",
"{",
"$",
"characterIndex",
"=",
"1",
";",
"$",
"entities",
"=",
"''",
";",
"$",
"codes",
"=",
"unpack",
"(",
"'C*'",
",",
"htmlentities",
"(",
"$",
"matches",
"[",
"0",
"]",
",",
"ENT_COMPAT",
",",
"'UTF-8'",
")",
")",
";",
"while",
"(",
"isset",
"(",
"$",
"codes",
"[",
"$",
"characterIndex",
"]",
")",
")",
"{",
"if",
"(",
"0x80",
">",
"$",
"codes",
"[",
"$",
"characterIndex",
"]",
")",
"{",
"$",
"entities",
".=",
"chr",
"(",
"$",
"codes",
"[",
"$",
"characterIndex",
"++",
"]",
")",
";",
"continue",
";",
"}",
"if",
"(",
"0xF0",
"<=",
"$",
"codes",
"[",
"$",
"characterIndex",
"]",
")",
"{",
"$",
"code",
"=",
"(",
"(",
"$",
"codes",
"[",
"$",
"characterIndex",
"++",
"]",
"-",
"0xF0",
")",
"<<",
"18",
")",
"+",
"(",
"(",
"$",
"codes",
"[",
"$",
"characterIndex",
"++",
"]",
"-",
"0x80",
")",
"<<",
"12",
")",
"+",
"(",
"(",
"$",
"codes",
"[",
"$",
"characterIndex",
"++",
"]",
"-",
"0x80",
")",
"<<",
"6",
")",
"+",
"$",
"codes",
"[",
"$",
"characterIndex",
"++",
"]",
"-",
"0x80",
";",
"}",
"elseif",
"(",
"0xE0",
"<=",
"$",
"codes",
"[",
"$",
"characterIndex",
"]",
")",
"{",
"$",
"code",
"=",
"(",
"(",
"$",
"codes",
"[",
"$",
"characterIndex",
"++",
"]",
"-",
"0xE0",
")",
"<<",
"12",
")",
"+",
"(",
"(",
"$",
"codes",
"[",
"$",
"characterIndex",
"++",
"]",
"-",
"0x80",
")",
"<<",
"6",
")",
"+",
"$",
"codes",
"[",
"$",
"characterIndex",
"++",
"]",
"-",
"0x80",
";",
"}",
"else",
"{",
"$",
"code",
"=",
"(",
"(",
"$",
"codes",
"[",
"$",
"characterIndex",
"++",
"]",
"-",
"0xC0",
")",
"<<",
"6",
")",
"+",
"$",
"codes",
"[",
"$",
"characterIndex",
"++",
"]",
"-",
"0x80",
";",
"}",
"$",
"entities",
".=",
"'&#'",
".",
"$",
"code",
".",
"';'",
";",
"}",
"return",
"$",
"entities",
";",
"}"
] | @param string[] $matches
@return string | [
"@param",
"string",
"[]",
"$matches"
] | train | https://github.com/Imangazaliev/DiDOM/blob/a8389cc26897f6bb8843363b4ea16e7ef16fb3c3/src/DiDom/Encoder.php#L31-L57 |
dg/rss-php | src/Feed.php | Feed.load | public static function load($url, $user = null, $pass = null)
{
$xml = self::loadXml($url, $user, $pass);
if ($xml->channel) {
return self::fromRss($xml);
} else {
return self::fromAtom($xml);
}
} | php | public static function load($url, $user = null, $pass = null)
{
$xml = self::loadXml($url, $user, $pass);
if ($xml->channel) {
return self::fromRss($xml);
} else {
return self::fromAtom($xml);
}
} | [
"public",
"static",
"function",
"load",
"(",
"$",
"url",
",",
"$",
"user",
"=",
"null",
",",
"$",
"pass",
"=",
"null",
")",
"{",
"$",
"xml",
"=",
"self",
"::",
"loadXml",
"(",
"$",
"url",
",",
"$",
"user",
",",
"$",
"pass",
")",
";",
"if",
"(",
"$",
"xml",
"->",
"channel",
")",
"{",
"return",
"self",
"::",
"fromRss",
"(",
"$",
"xml",
")",
";",
"}",
"else",
"{",
"return",
"self",
"::",
"fromAtom",
"(",
"$",
"xml",
")",
";",
"}",
"}"
] | Loads RSS or Atom feed.
@param string
@param string
@param string
@return Feed
@throws FeedException | [
"Loads",
"RSS",
"or",
"Atom",
"feed",
"."
] | train | https://github.com/dg/rss-php/blob/08622f48dc249961f3d5af90932240c17608190e/src/Feed.php#L30-L38 |
dg/rss-php | src/Feed.php | Feed.loadRss | public static function loadRss($url, $user = null, $pass = null)
{
return self::fromRss(self::loadXml($url, $user, $pass));
} | php | public static function loadRss($url, $user = null, $pass = null)
{
return self::fromRss(self::loadXml($url, $user, $pass));
} | [
"public",
"static",
"function",
"loadRss",
"(",
"$",
"url",
",",
"$",
"user",
"=",
"null",
",",
"$",
"pass",
"=",
"null",
")",
"{",
"return",
"self",
"::",
"fromRss",
"(",
"self",
"::",
"loadXml",
"(",
"$",
"url",
",",
"$",
"user",
",",
"$",
"pass",
")",
")",
";",
"}"
] | Loads RSS feed.
@param string RSS feed URL
@param string optional user name
@param string optional password
@return Feed
@throws FeedException | [
"Loads",
"RSS",
"feed",
"."
] | train | https://github.com/dg/rss-php/blob/08622f48dc249961f3d5af90932240c17608190e/src/Feed.php#L49-L52 |
dg/rss-php | src/Feed.php | Feed.loadAtom | public static function loadAtom($url, $user = null, $pass = null)
{
return self::fromAtom(self::loadXml($url, $user, $pass));
} | php | public static function loadAtom($url, $user = null, $pass = null)
{
return self::fromAtom(self::loadXml($url, $user, $pass));
} | [
"public",
"static",
"function",
"loadAtom",
"(",
"$",
"url",
",",
"$",
"user",
"=",
"null",
",",
"$",
"pass",
"=",
"null",
")",
"{",
"return",
"self",
"::",
"fromAtom",
"(",
"self",
"::",
"loadXml",
"(",
"$",
"url",
",",
"$",
"user",
",",
"$",
"pass",
")",
")",
";",
"}"
] | Loads Atom feed.
@param string Atom feed URL
@param string optional user name
@param string optional password
@return Feed
@throws FeedException | [
"Loads",
"Atom",
"feed",
"."
] | train | https://github.com/dg/rss-php/blob/08622f48dc249961f3d5af90932240c17608190e/src/Feed.php#L63-L66 |
dg/rss-php | src/Feed.php | Feed.toArray | public function toArray(SimpleXMLElement $xml = null)
{
if ($xml === null) {
$xml = $this->xml;
}
if (!$xml->children()) {
return (string) $xml;
}
$arr = array();
foreach ($xml->children() as $tag => $child) {
if (count($xml->$tag) === 1) {
$arr[$tag] = $this->toArray($child);
} else {
$arr[$tag][] = $this->toArray($child);
}
}
return $arr;
} | php | public function toArray(SimpleXMLElement $xml = null)
{
if ($xml === null) {
$xml = $this->xml;
}
if (!$xml->children()) {
return (string) $xml;
}
$arr = array();
foreach ($xml->children() as $tag => $child) {
if (count($xml->$tag) === 1) {
$arr[$tag] = $this->toArray($child);
} else {
$arr[$tag][] = $this->toArray($child);
}
}
return $arr;
} | [
"public",
"function",
"toArray",
"(",
"SimpleXMLElement",
"$",
"xml",
"=",
"null",
")",
"{",
"if",
"(",
"$",
"xml",
"===",
"null",
")",
"{",
"$",
"xml",
"=",
"$",
"this",
"->",
"xml",
";",
"}",
"if",
"(",
"!",
"$",
"xml",
"->",
"children",
"(",
")",
")",
"{",
"return",
"(",
"string",
")",
"$",
"xml",
";",
"}",
"$",
"arr",
"=",
"array",
"(",
")",
";",
"foreach",
"(",
"$",
"xml",
"->",
"children",
"(",
")",
"as",
"$",
"tag",
"=>",
"$",
"child",
")",
"{",
"if",
"(",
"count",
"(",
"$",
"xml",
"->",
"$",
"tag",
")",
"===",
"1",
")",
"{",
"$",
"arr",
"[",
"$",
"tag",
"]",
"=",
"$",
"this",
"->",
"toArray",
"(",
"$",
"child",
")",
";",
"}",
"else",
"{",
"$",
"arr",
"[",
"$",
"tag",
"]",
"[",
"]",
"=",
"$",
"this",
"->",
"toArray",
"(",
"$",
"child",
")",
";",
"}",
"}",
"return",
"$",
"arr",
";",
"}"
] | Converts a SimpleXMLElement into an array.
@param SimpleXMLElement
@return array | [
"Converts",
"a",
"SimpleXMLElement",
"into",
"an",
"array",
"."
] | train | https://github.com/dg/rss-php/blob/08622f48dc249961f3d5af90932240c17608190e/src/Feed.php#L140-L160 |
dg/rss-php | src/Feed.php | Feed.loadXml | private static function loadXml($url, $user, $pass)
{
$e = self::$cacheExpire;
$cacheFile = self::$cacheDir . '/feed.' . md5(serialize(func_get_args())) . '.xml';
if (self::$cacheDir
&& (time() - @filemtime($cacheFile) <= (is_string($e) ? strtotime($e) - time() : $e))
&& $data = @file_get_contents($cacheFile)
) {
// ok
} elseif ($data = trim(self::httpRequest($url, $user, $pass))) {
if (self::$cacheDir) {
file_put_contents($cacheFile, $data);
}
} elseif (self::$cacheDir && $data = @file_get_contents($cacheFile)) {
// ok
} else {
throw new FeedException('Cannot load feed.');
}
return new SimpleXMLElement($data, LIBXML_NOWARNING | LIBXML_NOERROR);
} | php | private static function loadXml($url, $user, $pass)
{
$e = self::$cacheExpire;
$cacheFile = self::$cacheDir . '/feed.' . md5(serialize(func_get_args())) . '.xml';
if (self::$cacheDir
&& (time() - @filemtime($cacheFile) <= (is_string($e) ? strtotime($e) - time() : $e))
&& $data = @file_get_contents($cacheFile)
) {
} elseif ($data = trim(self::httpRequest($url, $user, $pass))) {
if (self::$cacheDir) {
file_put_contents($cacheFile, $data);
}
} elseif (self::$cacheDir && $data = @file_get_contents($cacheFile)) {
} else {
throw new FeedException('Cannot load feed.');
}
return new SimpleXMLElement($data, LIBXML_NOWARNING | LIBXML_NOERROR);
} | [
"private",
"static",
"function",
"loadXml",
"(",
"$",
"url",
",",
"$",
"user",
",",
"$",
"pass",
")",
"{",
"$",
"e",
"=",
"self",
"::",
"$",
"cacheExpire",
";",
"$",
"cacheFile",
"=",
"self",
"::",
"$",
"cacheDir",
".",
"'/feed.'",
".",
"md5",
"(",
"serialize",
"(",
"func_get_args",
"(",
")",
")",
")",
".",
"'.xml'",
";",
"if",
"(",
"self",
"::",
"$",
"cacheDir",
"&&",
"(",
"time",
"(",
")",
"-",
"@",
"filemtime",
"(",
"$",
"cacheFile",
")",
"<=",
"(",
"is_string",
"(",
"$",
"e",
")",
"?",
"strtotime",
"(",
"$",
"e",
")",
"-",
"time",
"(",
")",
":",
"$",
"e",
")",
")",
"&&",
"$",
"data",
"=",
"@",
"file_get_contents",
"(",
"$",
"cacheFile",
")",
")",
"{",
"// ok",
"}",
"elseif",
"(",
"$",
"data",
"=",
"trim",
"(",
"self",
"::",
"httpRequest",
"(",
"$",
"url",
",",
"$",
"user",
",",
"$",
"pass",
")",
")",
")",
"{",
"if",
"(",
"self",
"::",
"$",
"cacheDir",
")",
"{",
"file_put_contents",
"(",
"$",
"cacheFile",
",",
"$",
"data",
")",
";",
"}",
"}",
"elseif",
"(",
"self",
"::",
"$",
"cacheDir",
"&&",
"$",
"data",
"=",
"@",
"file_get_contents",
"(",
"$",
"cacheFile",
")",
")",
"{",
"// ok",
"}",
"else",
"{",
"throw",
"new",
"FeedException",
"(",
"'Cannot load feed.'",
")",
";",
"}",
"return",
"new",
"SimpleXMLElement",
"(",
"$",
"data",
",",
"LIBXML_NOWARNING",
"|",
"LIBXML_NOERROR",
")",
";",
"}"
] | Load XML from cache or HTTP.
@param string
@param string
@param string
@return SimpleXMLElement
@throws FeedException | [
"Load",
"XML",
"from",
"cache",
"or",
"HTTP",
"."
] | train | https://github.com/dg/rss-php/blob/08622f48dc249961f3d5af90932240c17608190e/src/Feed.php#L171-L192 |
dg/rss-php | src/Feed.php | Feed.httpRequest | private static function httpRequest($url, $user, $pass)
{
if (extension_loaded('curl')) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
if ($user !== null || $pass !== null) {
curl_setopt($curl, CURLOPT_USERPWD, "$user:$pass");
}
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_ENCODING, '');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // no echo, just return result
if (!ini_get('open_basedir')) {
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // sometime is useful :)
}
$result = curl_exec($curl);
return curl_errno($curl) === 0 && curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200
? $result
: false;
} elseif ($user === null && $pass === null) {
return file_get_contents($url);
} else {
throw new FeedException('PHP extension CURL is not loaded.');
}
} | php | private static function httpRequest($url, $user, $pass)
{
if (extension_loaded('curl')) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
if ($user !== null || $pass !== null) {
curl_setopt($curl, CURLOPT_USERPWD, "$user:$pass");
}
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_ENCODING, '');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if (!ini_get('open_basedir')) {
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
}
$result = curl_exec($curl);
return curl_errno($curl) === 0 && curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200
? $result
: false;
} elseif ($user === null && $pass === null) {
return file_get_contents($url);
} else {
throw new FeedException('PHP extension CURL is not loaded.');
}
} | [
"private",
"static",
"function",
"httpRequest",
"(",
"$",
"url",
",",
"$",
"user",
",",
"$",
"pass",
")",
"{",
"if",
"(",
"extension_loaded",
"(",
"'curl'",
")",
")",
"{",
"$",
"curl",
"=",
"curl_init",
"(",
")",
";",
"curl_setopt",
"(",
"$",
"curl",
",",
"CURLOPT_URL",
",",
"$",
"url",
")",
";",
"if",
"(",
"$",
"user",
"!==",
"null",
"||",
"$",
"pass",
"!==",
"null",
")",
"{",
"curl_setopt",
"(",
"$",
"curl",
",",
"CURLOPT_USERPWD",
",",
"\"$user:$pass\"",
")",
";",
"}",
"curl_setopt",
"(",
"$",
"curl",
",",
"CURLOPT_HEADER",
",",
"false",
")",
";",
"curl_setopt",
"(",
"$",
"curl",
",",
"CURLOPT_TIMEOUT",
",",
"20",
")",
";",
"curl_setopt",
"(",
"$",
"curl",
",",
"CURLOPT_ENCODING",
",",
"''",
")",
";",
"curl_setopt",
"(",
"$",
"curl",
",",
"CURLOPT_RETURNTRANSFER",
",",
"true",
")",
";",
"// no echo, just return result",
"if",
"(",
"!",
"ini_get",
"(",
"'open_basedir'",
")",
")",
"{",
"curl_setopt",
"(",
"$",
"curl",
",",
"CURLOPT_FOLLOWLOCATION",
",",
"true",
")",
";",
"// sometime is useful :)",
"}",
"$",
"result",
"=",
"curl_exec",
"(",
"$",
"curl",
")",
";",
"return",
"curl_errno",
"(",
"$",
"curl",
")",
"===",
"0",
"&&",
"curl_getinfo",
"(",
"$",
"curl",
",",
"CURLINFO_HTTP_CODE",
")",
"===",
"200",
"?",
"$",
"result",
":",
"false",
";",
"}",
"elseif",
"(",
"$",
"user",
"===",
"null",
"&&",
"$",
"pass",
"===",
"null",
")",
"{",
"return",
"file_get_contents",
"(",
"$",
"url",
")",
";",
"}",
"else",
"{",
"throw",
"new",
"FeedException",
"(",
"'PHP extension CURL is not loaded.'",
")",
";",
"}",
"}"
] | Process HTTP request.
@param string
@param string
@param string
@return string|false
@throws FeedException | [
"Process",
"HTTP",
"request",
"."
] | train | https://github.com/dg/rss-php/blob/08622f48dc249961f3d5af90932240c17608190e/src/Feed.php#L203-L229 |
dg/rss-php | src/Feed.php | Feed.adjustNamespaces | private static function adjustNamespaces($el)
{
foreach ($el->getNamespaces(true) as $prefix => $ns) {
$children = $el->children($ns);
foreach ($children as $tag => $content) {
$el->{$prefix . ':' . $tag} = $content;
}
}
} | php | private static function adjustNamespaces($el)
{
foreach ($el->getNamespaces(true) as $prefix => $ns) {
$children = $el->children($ns);
foreach ($children as $tag => $content) {
$el->{$prefix . ':' . $tag} = $content;
}
}
} | [
"private",
"static",
"function",
"adjustNamespaces",
"(",
"$",
"el",
")",
"{",
"foreach",
"(",
"$",
"el",
"->",
"getNamespaces",
"(",
"true",
")",
"as",
"$",
"prefix",
"=>",
"$",
"ns",
")",
"{",
"$",
"children",
"=",
"$",
"el",
"->",
"children",
"(",
"$",
"ns",
")",
";",
"foreach",
"(",
"$",
"children",
"as",
"$",
"tag",
"=>",
"$",
"content",
")",
"{",
"$",
"el",
"->",
"{",
"$",
"prefix",
".",
"':'",
".",
"$",
"tag",
"}",
"=",
"$",
"content",
";",
"}",
"}",
"}"
] | Generates better accessible namespaced tags.
@param SimpleXMLElement
@return void | [
"Generates",
"better",
"accessible",
"namespaced",
"tags",
"."
] | train | https://github.com/dg/rss-php/blob/08622f48dc249961f3d5af90932240c17608190e/src/Feed.php#L237-L245 |
reactphp/http | src/Io/IniUtil.php | IniUtil.iniSizeToBytes | public static function iniSizeToBytes($size)
{
if (\is_numeric($size)) {
return (int)$size;
}
$suffix = \strtoupper(\substr($size, -1));
$strippedSize = \substr($size, 0, -1);
if (!\is_numeric($strippedSize)) {
throw new \InvalidArgumentException("$size is not a valid ini size");
}
if ($strippedSize <= 0) {
throw new \InvalidArgumentException("Expect $size to be higher isn't zero or lower");
}
if ($suffix === 'K') {
return $strippedSize * 1024;
}
if ($suffix === 'M') {
return $strippedSize * 1024 * 1024;
}
if ($suffix === 'G') {
return $strippedSize * 1024 * 1024 * 1024;
}
if ($suffix === 'T') {
return $strippedSize * 1024 * 1024 * 1024 * 1024;
}
return (int)$size;
} | php | public static function iniSizeToBytes($size)
{
if (\is_numeric($size)) {
return (int)$size;
}
$suffix = \strtoupper(\substr($size, -1));
$strippedSize = \substr($size, 0, -1);
if (!\is_numeric($strippedSize)) {
throw new \InvalidArgumentException("$size is not a valid ini size");
}
if ($strippedSize <= 0) {
throw new \InvalidArgumentException("Expect $size to be higher isn't zero or lower");
}
if ($suffix === 'K') {
return $strippedSize * 1024;
}
if ($suffix === 'M') {
return $strippedSize * 1024 * 1024;
}
if ($suffix === 'G') {
return $strippedSize * 1024 * 1024 * 1024;
}
if ($suffix === 'T') {
return $strippedSize * 1024 * 1024 * 1024 * 1024;
}
return (int)$size;
} | [
"public",
"static",
"function",
"iniSizeToBytes",
"(",
"$",
"size",
")",
"{",
"if",
"(",
"\\",
"is_numeric",
"(",
"$",
"size",
")",
")",
"{",
"return",
"(",
"int",
")",
"$",
"size",
";",
"}",
"$",
"suffix",
"=",
"\\",
"strtoupper",
"(",
"\\",
"substr",
"(",
"$",
"size",
",",
"-",
"1",
")",
")",
";",
"$",
"strippedSize",
"=",
"\\",
"substr",
"(",
"$",
"size",
",",
"0",
",",
"-",
"1",
")",
";",
"if",
"(",
"!",
"\\",
"is_numeric",
"(",
"$",
"strippedSize",
")",
")",
"{",
"throw",
"new",
"\\",
"InvalidArgumentException",
"(",
"\"$size is not a valid ini size\"",
")",
";",
"}",
"if",
"(",
"$",
"strippedSize",
"<=",
"0",
")",
"{",
"throw",
"new",
"\\",
"InvalidArgumentException",
"(",
"\"Expect $size to be higher isn't zero or lower\"",
")",
";",
"}",
"if",
"(",
"$",
"suffix",
"===",
"'K'",
")",
"{",
"return",
"$",
"strippedSize",
"*",
"1024",
";",
"}",
"if",
"(",
"$",
"suffix",
"===",
"'M'",
")",
"{",
"return",
"$",
"strippedSize",
"*",
"1024",
"*",
"1024",
";",
"}",
"if",
"(",
"$",
"suffix",
"===",
"'G'",
")",
"{",
"return",
"$",
"strippedSize",
"*",
"1024",
"*",
"1024",
"*",
"1024",
";",
"}",
"if",
"(",
"$",
"suffix",
"===",
"'T'",
")",
"{",
"return",
"$",
"strippedSize",
"*",
"1024",
"*",
"1024",
"*",
"1024",
"*",
"1024",
";",
"}",
"return",
"(",
"int",
")",
"$",
"size",
";",
"}"
] | Convert a ini like size to a numeric size in bytes.
@param string $size
@return int | [
"Convert",
"a",
"ini",
"like",
"size",
"to",
"a",
"numeric",
"size",
"in",
"bytes",
"."
] | train | https://github.com/reactphp/http/blob/c02fc4bf7b0541130176a669c5e76b4f4e8eb2a9/src/Io/IniUtil.php#L16-L47 |
barryvdh/laravel-snappy | src/ImageWrapper.php | ImageWrapper.loadHTML | public function loadHTML($string)
{
$this->html = (string) $string;
$this->file = null;
return $this;
} | php | public function loadHTML($string)
{
$this->html = (string) $string;
$this->file = null;
return $this;
} | [
"public",
"function",
"loadHTML",
"(",
"$",
"string",
")",
"{",
"$",
"this",
"->",
"html",
"=",
"(",
"string",
")",
"$",
"string",
";",
"$",
"this",
"->",
"file",
"=",
"null",
";",
"return",
"$",
"this",
";",
"}"
] | Load a HTML string
@param string $string
@return static | [
"Load",
"a",
"HTML",
"string"
] | train | https://github.com/barryvdh/laravel-snappy/blob/02345b4d362c8a50e42953ccadd626dd3dbff24a/src/ImageWrapper.php#L63-L68 |
barryvdh/laravel-snappy | src/ImageWrapper.php | ImageWrapper.save | public function save($filename, $overwrite = false)
{
if ($this->html)
{
$this->snappy->generateFromHtml($this->html, $filename, $this->options, $overwrite);
}
elseif ($this->file)
{
$this->snappy->generate($this->file, $filename, $this->options, $overwrite);
}
return $this;
} | php | public function save($filename, $overwrite = false)
{
if ($this->html)
{
$this->snappy->generateFromHtml($this->html, $filename, $this->options, $overwrite);
}
elseif ($this->file)
{
$this->snappy->generate($this->file, $filename, $this->options, $overwrite);
}
return $this;
} | [
"public",
"function",
"save",
"(",
"$",
"filename",
",",
"$",
"overwrite",
"=",
"false",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"html",
")",
"{",
"$",
"this",
"->",
"snappy",
"->",
"generateFromHtml",
"(",
"$",
"this",
"->",
"html",
",",
"$",
"filename",
",",
"$",
"this",
"->",
"options",
",",
"$",
"overwrite",
")",
";",
"}",
"elseif",
"(",
"$",
"this",
"->",
"file",
")",
"{",
"$",
"this",
"->",
"snappy",
"->",
"generate",
"(",
"$",
"this",
"->",
"file",
",",
"$",
"filename",
",",
"$",
"this",
"->",
"options",
",",
"$",
"overwrite",
")",
";",
"}",
"return",
"$",
"this",
";",
"}"
] | Save the image to a file
@param $filename
@return static | [
"Save",
"the",
"image",
"to",
"a",
"file"
] | train | https://github.com/barryvdh/laravel-snappy/blob/02345b4d362c8a50e42953ccadd626dd3dbff24a/src/ImageWrapper.php#L117-L130 |
barryvdh/laravel-snappy | src/PdfFaker.php | PdfFaker.loadView | public function loadView($view, $data = array(), $mergeData = array())
{
$this->view = View::make($view, $data, $mergeData);
return parent::loadView($view, $data, $mergeData);
} | php | public function loadView($view, $data = array(), $mergeData = array())
{
$this->view = View::make($view, $data, $mergeData);
return parent::loadView($view, $data, $mergeData);
} | [
"public",
"function",
"loadView",
"(",
"$",
"view",
",",
"$",
"data",
"=",
"array",
"(",
")",
",",
"$",
"mergeData",
"=",
"array",
"(",
")",
")",
"{",
"$",
"this",
"->",
"view",
"=",
"View",
"::",
"make",
"(",
"$",
"view",
",",
"$",
"data",
",",
"$",
"mergeData",
")",
";",
"return",
"parent",
"::",
"loadView",
"(",
"$",
"view",
",",
"$",
"data",
",",
"$",
"mergeData",
")",
";",
"}"
] | Load a View and convert to HTML
@param string $view
@param array $data
@param array $mergeData
@return $this | [
"Load",
"a",
"View",
"and",
"convert",
"to",
"HTML"
] | train | https://github.com/barryvdh/laravel-snappy/blob/02345b4d362c8a50e42953ccadd626dd3dbff24a/src/PdfFaker.php#L18-L22 |
barryvdh/laravel-snappy | src/PdfFaker.php | PdfFaker.ensureResponseHasView | protected function ensureResponseHasView()
{
if (! isset($this->view) || ! $this->view instanceof View) {
return PHPUnit::fail('The response is not a view.');
}
return $this;
} | php | protected function ensureResponseHasView()
{
if (! isset($this->view) || ! $this->view instanceof View) {
return PHPUnit::fail('The response is not a view.');
}
return $this;
} | [
"protected",
"function",
"ensureResponseHasView",
"(",
")",
"{",
"if",
"(",
"!",
"isset",
"(",
"$",
"this",
"->",
"view",
")",
"||",
"!",
"$",
"this",
"->",
"view",
"instanceof",
"View",
")",
"{",
"return",
"PHPUnit",
"::",
"fail",
"(",
"'The response is not a view.'",
")",
";",
"}",
"return",
"$",
"this",
";",
"}"
] | Ensure that the response has a view as its original content.
@return $this | [
"Ensure",
"that",
"the",
"response",
"has",
"a",
"view",
"as",
"its",
"original",
"content",
"."
] | train | https://github.com/barryvdh/laravel-snappy/blob/02345b4d362c8a50e42953ccadd626dd3dbff24a/src/PdfFaker.php#L30-L37 |
barryvdh/laravel-snappy | src/PdfFaker.php | PdfFaker.assertViewHas | public function assertViewHas($key, $value = null)
{
if (is_array($key)) {
return $this->assertViewHasAll($key);
}
$this->ensureResponseHasView();
if (is_null($value)) {
PHPUnit::assertArrayHasKey($key, $this->view->getData());
} elseif ($value instanceof Closure) {
PHPUnit::assertTrue($value($this->view->$key));
} else {
PHPUnit::assertEquals($value, $this->view->$key);
}
return $this;
} | php | public function assertViewHas($key, $value = null)
{
if (is_array($key)) {
return $this->assertViewHasAll($key);
}
$this->ensureResponseHasView();
if (is_null($value)) {
PHPUnit::assertArrayHasKey($key, $this->view->getData());
} elseif ($value instanceof Closure) {
PHPUnit::assertTrue($value($this->view->$key));
} else {
PHPUnit::assertEquals($value, $this->view->$key);
}
return $this;
} | [
"public",
"function",
"assertViewHas",
"(",
"$",
"key",
",",
"$",
"value",
"=",
"null",
")",
"{",
"if",
"(",
"is_array",
"(",
"$",
"key",
")",
")",
"{",
"return",
"$",
"this",
"->",
"assertViewHasAll",
"(",
"$",
"key",
")",
";",
"}",
"$",
"this",
"->",
"ensureResponseHasView",
"(",
")",
";",
"if",
"(",
"is_null",
"(",
"$",
"value",
")",
")",
"{",
"PHPUnit",
"::",
"assertArrayHasKey",
"(",
"$",
"key",
",",
"$",
"this",
"->",
"view",
"->",
"getData",
"(",
")",
")",
";",
"}",
"elseif",
"(",
"$",
"value",
"instanceof",
"Closure",
")",
"{",
"PHPUnit",
"::",
"assertTrue",
"(",
"$",
"value",
"(",
"$",
"this",
"->",
"view",
"->",
"$",
"key",
")",
")",
";",
"}",
"else",
"{",
"PHPUnit",
"::",
"assertEquals",
"(",
"$",
"value",
",",
"$",
"this",
"->",
"view",
"->",
"$",
"key",
")",
";",
"}",
"return",
"$",
"this",
";",
"}"
] | Assert that the response view has a given piece of bound data.
@param string|array $key
@param mixed $value
@return $this | [
"Assert",
"that",
"the",
"response",
"view",
"has",
"a",
"given",
"piece",
"of",
"bound",
"data",
"."
] | train | https://github.com/barryvdh/laravel-snappy/blob/02345b4d362c8a50e42953ccadd626dd3dbff24a/src/PdfFaker.php#L53-L70 |
barryvdh/laravel-snappy | src/PdfFaker.php | PdfFaker.assertViewHasAll | public function assertViewHasAll(array $bindings)
{
foreach ($bindings as $key => $value) {
if (is_int($key)) {
$this->assertViewHas($value);
} else {
$this->assertViewHas($key, $value);
}
}
return $this;
} | php | public function assertViewHasAll(array $bindings)
{
foreach ($bindings as $key => $value) {
if (is_int($key)) {
$this->assertViewHas($value);
} else {
$this->assertViewHas($key, $value);
}
}
return $this;
} | [
"public",
"function",
"assertViewHasAll",
"(",
"array",
"$",
"bindings",
")",
"{",
"foreach",
"(",
"$",
"bindings",
"as",
"$",
"key",
"=>",
"$",
"value",
")",
"{",
"if",
"(",
"is_int",
"(",
"$",
"key",
")",
")",
"{",
"$",
"this",
"->",
"assertViewHas",
"(",
"$",
"value",
")",
";",
"}",
"else",
"{",
"$",
"this",
"->",
"assertViewHas",
"(",
"$",
"key",
",",
"$",
"value",
")",
";",
"}",
"}",
"return",
"$",
"this",
";",
"}"
] | Assert that the response view has a given list of bound data.
@param array $bindings
@return $this | [
"Assert",
"that",
"the",
"response",
"view",
"has",
"a",
"given",
"list",
"of",
"bound",
"data",
"."
] | train | https://github.com/barryvdh/laravel-snappy/blob/02345b4d362c8a50e42953ccadd626dd3dbff24a/src/PdfFaker.php#L78-L89 |
barryvdh/laravel-snappy | src/PdfFaker.php | PdfFaker.assertViewMissing | public function assertViewMissing($key)
{
$this->ensureResponseHasView();
PHPUnit::assertArrayNotHasKey($key, $this->view->getData());
return $this;
} | php | public function assertViewMissing($key)
{
$this->ensureResponseHasView();
PHPUnit::assertArrayNotHasKey($key, $this->view->getData());
return $this;
} | [
"public",
"function",
"assertViewMissing",
"(",
"$",
"key",
")",
"{",
"$",
"this",
"->",
"ensureResponseHasView",
"(",
")",
";",
"PHPUnit",
"::",
"assertArrayNotHasKey",
"(",
"$",
"key",
",",
"$",
"this",
"->",
"view",
"->",
"getData",
"(",
")",
")",
";",
"return",
"$",
"this",
";",
"}"
] | Assert that the response view is missing a piece of bound data.
@param string $key
@return $this | [
"Assert",
"that",
"the",
"response",
"view",
"is",
"missing",
"a",
"piece",
"of",
"bound",
"data",
"."
] | train | https://github.com/barryvdh/laravel-snappy/blob/02345b4d362c8a50e42953ccadd626dd3dbff24a/src/PdfFaker.php#L97-L104 |
barryvdh/laravel-snappy | src/PdfWrapper.php | PdfWrapper.setPaper | public function setPaper($paper, $orientation=null)
{
$this->snappy->setOption('page-size', $paper);
if($orientation){
$this->snappy->setOption('orientation', $orientation);
}
return $this;
} | php | public function setPaper($paper, $orientation=null)
{
$this->snappy->setOption('page-size', $paper);
if($orientation){
$this->snappy->setOption('orientation', $orientation);
}
return $this;
} | [
"public",
"function",
"setPaper",
"(",
"$",
"paper",
",",
"$",
"orientation",
"=",
"null",
")",
"{",
"$",
"this",
"->",
"snappy",
"->",
"setOption",
"(",
"'page-size'",
",",
"$",
"paper",
")",
";",
"if",
"(",
"$",
"orientation",
")",
"{",
"$",
"this",
"->",
"snappy",
"->",
"setOption",
"(",
"'orientation'",
",",
"$",
"orientation",
")",
";",
"}",
"return",
"$",
"this",
";",
"}"
] | Set the paper size (default A4)
@param string $paper
@param string $orientation
@return $this | [
"Set",
"the",
"paper",
"size",
"(",
"default",
"A4",
")"
] | train | https://github.com/barryvdh/laravel-snappy/blob/02345b4d362c8a50e42953ccadd626dd3dbff24a/src/PdfWrapper.php#L64-L71 |
barryvdh/laravel-snappy | src/PdfWrapper.php | PdfWrapper.loadHTML | public function loadHTML($html)
{
if ($html instanceof Renderable) {
$html = $html->render();
}
$this->html = $html;
$this->file = null;
return $this;
} | php | public function loadHTML($html)
{
if ($html instanceof Renderable) {
$html = $html->render();
}
$this->html = $html;
$this->file = null;
return $this;
} | [
"public",
"function",
"loadHTML",
"(",
"$",
"html",
")",
"{",
"if",
"(",
"$",
"html",
"instanceof",
"Renderable",
")",
"{",
"$",
"html",
"=",
"$",
"html",
"->",
"render",
"(",
")",
";",
"}",
"$",
"this",
"->",
"html",
"=",
"$",
"html",
";",
"$",
"this",
"->",
"file",
"=",
"null",
";",
"return",
"$",
"this",
";",
"}"
] | Load a HTML string
@param Array|string|Renderable $html
@return $this | [
"Load",
"a",
"HTML",
"string"
] | train | https://github.com/barryvdh/laravel-snappy/blob/02345b4d362c8a50e42953ccadd626dd3dbff24a/src/PdfWrapper.php#L128-L136 |
barryvdh/laravel-snappy | src/PdfWrapper.php | PdfWrapper.loadView | public function loadView($view, $data = array(), $mergeData = array())
{
$view = View::make($view, $data, $mergeData);
return $this->loadHTML($view);
} | php | public function loadView($view, $data = array(), $mergeData = array())
{
$view = View::make($view, $data, $mergeData);
return $this->loadHTML($view);
} | [
"public",
"function",
"loadView",
"(",
"$",
"view",
",",
"$",
"data",
"=",
"array",
"(",
")",
",",
"$",
"mergeData",
"=",
"array",
"(",
")",
")",
"{",
"$",
"view",
"=",
"View",
"::",
"make",
"(",
"$",
"view",
",",
"$",
"data",
",",
"$",
"mergeData",
")",
";",
"return",
"$",
"this",
"->",
"loadHTML",
"(",
"$",
"view",
")",
";",
"}"
] | Load a View and convert to HTML
@param string $view
@param array $data
@param array $mergeData
@return $this | [
"Load",
"a",
"View",
"and",
"convert",
"to",
"HTML"
] | train | https://github.com/barryvdh/laravel-snappy/blob/02345b4d362c8a50e42953ccadd626dd3dbff24a/src/PdfWrapper.php#L159-L164 |
barryvdh/laravel-snappy | src/PdfWrapper.php | PdfWrapper.output | public function output()
{
if ($this->html)
{
return $this->snappy->getOutputFromHtml($this->html, $this->options);
}
if ($this->file)
{
return $this->snappy->getOutput($this->file, $this->options);
}
throw new \InvalidArgumentException('PDF Generator requires a html or file in order to produce output.');
} | php | public function output()
{
if ($this->html)
{
return $this->snappy->getOutputFromHtml($this->html, $this->options);
}
if ($this->file)
{
return $this->snappy->getOutput($this->file, $this->options);
}
throw new \InvalidArgumentException('PDF Generator requires a html or file in order to produce output.');
} | [
"public",
"function",
"output",
"(",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"html",
")",
"{",
"return",
"$",
"this",
"->",
"snappy",
"->",
"getOutputFromHtml",
"(",
"$",
"this",
"->",
"html",
",",
"$",
"this",
"->",
"options",
")",
";",
"}",
"if",
"(",
"$",
"this",
"->",
"file",
")",
"{",
"return",
"$",
"this",
"->",
"snappy",
"->",
"getOutput",
"(",
"$",
"this",
"->",
"file",
",",
"$",
"this",
"->",
"options",
")",
";",
"}",
"throw",
"new",
"\\",
"InvalidArgumentException",
"(",
"'PDF Generator requires a html or file in order to produce output.'",
")",
";",
"}"
] | Output the PDF as a string.
@return string The rendered PDF as string
@throws \InvalidArgumentException | [
"Output",
"the",
"PDF",
"as",
"a",
"string",
"."
] | train | https://github.com/barryvdh/laravel-snappy/blob/02345b4d362c8a50e42953ccadd626dd3dbff24a/src/PdfWrapper.php#L172-L185 |
yidas/codeigniter-model | example/userACL/My_model.php | My_model._globalScopes | protected function _globalScopes()
{
if ($this->companyAttribute) {
$this->getBuilder()->where(
$this->_field($this->companyAttribute),
$this->user->getCompanyID();
);
}
if ($this->userAttribute) {
$this->getBuilder()->where(
$this->_field($this->userAttribute),
$this->user->getID();
);
}
return parent::_globalScopes();
} | php | protected function _globalScopes()
{
if ($this->companyAttribute) {
$this->getBuilder()->where(
$this->_field($this->companyAttribute),
$this->user->getCompanyID();
);
}
if ($this->userAttribute) {
$this->getBuilder()->where(
$this->_field($this->userAttribute),
$this->user->getID();
);
}
return parent::_globalScopes();
} | [
"protected",
"function",
"_globalScopes",
"(",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"companyAttribute",
")",
"{",
"$",
"this",
"->",
"getBuilder",
"(",
")",
"->",
"where",
"(",
"$",
"this",
"->",
"_field",
"(",
"$",
"this",
"->",
"companyAttribute",
")",
",",
"$",
"this",
"->",
"user",
"->",
"getCompanyID",
"(",
")",
";",
")",
";",
"}",
"if",
"(",
"$",
"this",
"->",
"userAttribute",
")",
"{",
"$",
"this",
"->",
"getBuilder",
"(",
")",
"->",
"where",
"(",
"$",
"this",
"->",
"_field",
"(",
"$",
"this",
"->",
"userAttribute",
")",
",",
"$",
"this",
"->",
"user",
"->",
"getID",
"(",
")",
";",
")",
";",
"}",
"return",
"parent",
"::",
"_globalScopes",
"(",
")",
";",
"}"
] | Override _globalScopes with User & Company validation | [
"Override",
"_globalScopes",
"with",
"User",
"&",
"Company",
"validation"
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/example/userACL/My_model.php#L90-L108 |
yidas/codeigniter-model | example/userACL/My_model.php | My_model._attrEventBeforeInsert | protected function _attrEventBeforeInsert(&$attributes)
{
// Auto Company
if ($this->companyAttribute && !isset($attributes[$this->companyAttribute])) {
$attributes[$this->companyAttribute] = $this->user->getCompanyID();;
}
// Auto User
if ($this->userAttribute && !isset($attributes[$this->userAttribute])) {
$attributes[$this->userAttribute] = $this->user->getID();;
}
// Auto created_by
if ($this->createdUserAttribute && !isset($attributes[$this->createdUserAttribute])) {
$attributes[$this->createdUserAttribute] = $this->user->getID();
}
return parent::_attrEventBeforeInsert($attributes);
} | php | protected function _attrEventBeforeInsert(&$attributes)
{
if ($this->companyAttribute && !isset($attributes[$this->companyAttribute])) {
$attributes[$this->companyAttribute] = $this->user->getCompanyID();;
}
if ($this->userAttribute && !isset($attributes[$this->userAttribute])) {
$attributes[$this->userAttribute] = $this->user->getID();;
}
if ($this->createdUserAttribute && !isset($attributes[$this->createdUserAttribute])) {
$attributes[$this->createdUserAttribute] = $this->user->getID();
}
return parent::_attrEventBeforeInsert($attributes);
} | [
"protected",
"function",
"_attrEventBeforeInsert",
"(",
"&",
"$",
"attributes",
")",
"{",
"// Auto Company\r",
"if",
"(",
"$",
"this",
"->",
"companyAttribute",
"&&",
"!",
"isset",
"(",
"$",
"attributes",
"[",
"$",
"this",
"->",
"companyAttribute",
"]",
")",
")",
"{",
"$",
"attributes",
"[",
"$",
"this",
"->",
"companyAttribute",
"]",
"=",
"$",
"this",
"->",
"user",
"->",
"getCompanyID",
"(",
")",
";",
";",
"}",
"// Auto User\r",
"if",
"(",
"$",
"this",
"->",
"userAttribute",
"&&",
"!",
"isset",
"(",
"$",
"attributes",
"[",
"$",
"this",
"->",
"userAttribute",
"]",
")",
")",
"{",
"$",
"attributes",
"[",
"$",
"this",
"->",
"userAttribute",
"]",
"=",
"$",
"this",
"->",
"user",
"->",
"getID",
"(",
")",
";",
";",
"}",
"// Auto created_by\r",
"if",
"(",
"$",
"this",
"->",
"createdUserAttribute",
"&&",
"!",
"isset",
"(",
"$",
"attributes",
"[",
"$",
"this",
"->",
"createdUserAttribute",
"]",
")",
")",
"{",
"$",
"attributes",
"[",
"$",
"this",
"->",
"createdUserAttribute",
"]",
"=",
"$",
"this",
"->",
"user",
"->",
"getID",
"(",
")",
";",
"}",
"return",
"parent",
"::",
"_attrEventBeforeInsert",
"(",
"$",
"attributes",
")",
";",
"}"
] | Override _attrEventBeforeInsert() | [
"Override",
"_attrEventBeforeInsert",
"()"
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/example/userACL/My_model.php#L112-L130 |
yidas/codeigniter-model | example/userACL/My_model.php | My_model._attrEventBeforeUpdate | public function _attrEventBeforeUpdate(&$attributes)
{
// Auto updated_by
if ($this->updatedUserAttribute && !isset($attributes[$this->updatedUserAttribute])) {
$attributes[$this->updatedUserAttribute] = $this->user->getID();
}
return parent::_attrEventBeforeUpdate($attributes);
} | php | public function _attrEventBeforeUpdate(&$attributes)
{
if ($this->updatedUserAttribute && !isset($attributes[$this->updatedUserAttribute])) {
$attributes[$this->updatedUserAttribute] = $this->user->getID();
}
return parent::_attrEventBeforeUpdate($attributes);
} | [
"public",
"function",
"_attrEventBeforeUpdate",
"(",
"&",
"$",
"attributes",
")",
"{",
"// Auto updated_by\r",
"if",
"(",
"$",
"this",
"->",
"updatedUserAttribute",
"&&",
"!",
"isset",
"(",
"$",
"attributes",
"[",
"$",
"this",
"->",
"updatedUserAttribute",
"]",
")",
")",
"{",
"$",
"attributes",
"[",
"$",
"this",
"->",
"updatedUserAttribute",
"]",
"=",
"$",
"this",
"->",
"user",
"->",
"getID",
"(",
")",
";",
"}",
"return",
"parent",
"::",
"_attrEventBeforeUpdate",
"(",
"$",
"attributes",
")",
";",
"}"
] | Override _attrEventBeforeUpdate() | [
"Override",
"_attrEventBeforeUpdate",
"()"
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/example/userACL/My_model.php#L134-L141 |
yidas/codeigniter-model | example/userACL/My_model.php | My_model._attrEventBeforeDelete | public function _attrEventBeforeDelete(&$attributes)
{
// Auto deleted_by
if ($this->deletedUserAttribute && !isset($attributes[$this->deletedUserAttribute])) {
$attributes[$this->deletedUserAttribute] = $this->user->getID();
}
return parent::_attrEventBeforeDelete($attributes);
} | php | public function _attrEventBeforeDelete(&$attributes)
{
if ($this->deletedUserAttribute && !isset($attributes[$this->deletedUserAttribute])) {
$attributes[$this->deletedUserAttribute] = $this->user->getID();
}
return parent::_attrEventBeforeDelete($attributes);
} | [
"public",
"function",
"_attrEventBeforeDelete",
"(",
"&",
"$",
"attributes",
")",
"{",
"// Auto deleted_by\r",
"if",
"(",
"$",
"this",
"->",
"deletedUserAttribute",
"&&",
"!",
"isset",
"(",
"$",
"attributes",
"[",
"$",
"this",
"->",
"deletedUserAttribute",
"]",
")",
")",
"{",
"$",
"attributes",
"[",
"$",
"this",
"->",
"deletedUserAttribute",
"]",
"=",
"$",
"this",
"->",
"user",
"->",
"getID",
"(",
")",
";",
"}",
"return",
"parent",
"::",
"_attrEventBeforeDelete",
"(",
"$",
"attributes",
")",
";",
"}"
] | Override _attrEventBeforeDelete() | [
"Override",
"_attrEventBeforeDelete",
"()"
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/example/userACL/My_model.php#L145-L152 |
yidas/codeigniter-model | example/requestLog/My_model.php | My_model._attrEventBeforeInsert | protected function _attrEventBeforeInsert(&$attributes)
{
// Auto IP
if ($this->createdIpAttribute && !isset($attributes[$this->createdIpAttribute])) {
$attributes[$this->createdIpAttribute] = $this->input->ip_address();
}
// Auto User Agent
if ($this->createdUserAgentAttribute && !isset($attributes[$this->createdUserAgentAttribute])) {
$attributes[$this->createdUserAgentAttribute] = $this->input->user_agent();
}
// Auto Request URI (`$this->uri->uri_string()` couldn't include QUERY_STRING)
if ($this->createdRequestUriAttribute && !isset($attributes[$this->createdRequestUriAttribute])) {
$attributes[$this->createdRequestUriAttribute] = isset($_SERVER['REQUEST_URI'])
? $_SERVER['REQUEST_URI'] : '';
}
// Auto Hedaers
foreach ((array)$this->createdHeaderAttributes as $header => $field) {
if ($field && !isset($attributes[$field])) {
$attributes[$field] = isset($_SERVER[$header])
? $_SERVER[$header] : '';
}
}
return parent::_attrEventBeforeInsert($attributes);
} | php | protected function _attrEventBeforeInsert(&$attributes)
{
if ($this->createdIpAttribute && !isset($attributes[$this->createdIpAttribute])) {
$attributes[$this->createdIpAttribute] = $this->input->ip_address();
}
if ($this->createdUserAgentAttribute && !isset($attributes[$this->createdUserAgentAttribute])) {
$attributes[$this->createdUserAgentAttribute] = $this->input->user_agent();
}
if ($this->createdRequestUriAttribute && !isset($attributes[$this->createdRequestUriAttribute])) {
$attributes[$this->createdRequestUriAttribute] = isset($_SERVER['REQUEST_URI'])
? $_SERVER['REQUEST_URI'] : '';
}
foreach ((array)$this->createdHeaderAttributes as $header => $field) {
if ($field && !isset($attributes[$field])) {
$attributes[$field] = isset($_SERVER[$header])
? $_SERVER[$header] : '';
}
}
return parent::_attrEventBeforeInsert($attributes);
} | [
"protected",
"function",
"_attrEventBeforeInsert",
"(",
"&",
"$",
"attributes",
")",
"{",
"// Auto IP\r",
"if",
"(",
"$",
"this",
"->",
"createdIpAttribute",
"&&",
"!",
"isset",
"(",
"$",
"attributes",
"[",
"$",
"this",
"->",
"createdIpAttribute",
"]",
")",
")",
"{",
"$",
"attributes",
"[",
"$",
"this",
"->",
"createdIpAttribute",
"]",
"=",
"$",
"this",
"->",
"input",
"->",
"ip_address",
"(",
")",
";",
"}",
"// Auto User Agent\r",
"if",
"(",
"$",
"this",
"->",
"createdUserAgentAttribute",
"&&",
"!",
"isset",
"(",
"$",
"attributes",
"[",
"$",
"this",
"->",
"createdUserAgentAttribute",
"]",
")",
")",
"{",
"$",
"attributes",
"[",
"$",
"this",
"->",
"createdUserAgentAttribute",
"]",
"=",
"$",
"this",
"->",
"input",
"->",
"user_agent",
"(",
")",
";",
"}",
"// Auto Request URI (`$this->uri->uri_string()` couldn't include QUERY_STRING)\r",
"if",
"(",
"$",
"this",
"->",
"createdRequestUriAttribute",
"&&",
"!",
"isset",
"(",
"$",
"attributes",
"[",
"$",
"this",
"->",
"createdRequestUriAttribute",
"]",
")",
")",
"{",
"$",
"attributes",
"[",
"$",
"this",
"->",
"createdRequestUriAttribute",
"]",
"=",
"isset",
"(",
"$",
"_SERVER",
"[",
"'REQUEST_URI'",
"]",
")",
"?",
"$",
"_SERVER",
"[",
"'REQUEST_URI'",
"]",
":",
"''",
";",
"}",
"// Auto Hedaers\r",
"foreach",
"(",
"(",
"array",
")",
"$",
"this",
"->",
"createdHeaderAttributes",
"as",
"$",
"header",
"=>",
"$",
"field",
")",
"{",
"if",
"(",
"$",
"field",
"&&",
"!",
"isset",
"(",
"$",
"attributes",
"[",
"$",
"field",
"]",
")",
")",
"{",
"$",
"attributes",
"[",
"$",
"field",
"]",
"=",
"isset",
"(",
"$",
"_SERVER",
"[",
"$",
"header",
"]",
")",
"?",
"$",
"_SERVER",
"[",
"$",
"header",
"]",
":",
"''",
";",
"}",
"}",
"return",
"parent",
"::",
"_attrEventBeforeInsert",
"(",
"$",
"attributes",
")",
";",
"}"
] | Override _attrEventBeforeInsert() | [
"Override",
"_attrEventBeforeInsert",
"()"
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/example/requestLog/My_model.php#L67-L91 |
yidas/codeigniter-model | src/Model.php | Model.validate | public function validate($attributes=[], $returnData=false)
{
// Data fetched by ORM or input
$data = ($attributes) ? $attributes : $this->_writeProperties;
// Filter first
$data = $this->filter($data);
// ORM re-assign properties
$this->_writeProperties = (!$attributes) ? $data : $this->_writeProperties;
// Get validation rules from function setting
$rules = $this->rules();
// The ORM update will only collect rules with corresponding modified attributes.
if ($this->_selfCondition) {
$newRules = [];
foreach ((array) $rules as $key => $rule) {
if (isset($this->_writeProperties[$rule['field']])) {
// Add into new rules for updating
$newRules[] = $rule;
}
}
// Replace with mapping rules
$rules = $newRules;
}
// Check if has rules
if (empty($rules))
return ($returnData) ? $data : true;
// CodeIgniter form_validation doesn't work with empty array data
if (empty($data))
return false;
// Load CodeIgniter form_validation library for yidas/model namespace, which has no effect on common one
get_instance()->load->library('form_validation', null, 'yidas_model_form_validation');
// Get CodeIgniter validator
$validator = get_instance()->yidas_model_form_validation;
$validator->reset_validation();
$validator->set_data($data);
$validator->set_rules($rules);
// Run Validate
$result = $validator->run();
// Result handle
if ($result===false) {
$this->_errors = $validator->error_array();
return false;
} else {
return ($returnData) ? $data : true;
}
} | php | public function validate($attributes=[], $returnData=false)
{
$data = ($attributes) ? $attributes : $this->_writeProperties;
$data = $this->filter($data);
$this->_writeProperties = (!$attributes) ? $data : $this->_writeProperties;
$rules = $this->rules();
if ($this->_selfCondition) {
$newRules = [];
foreach ((array) $rules as $key => $rule) {
if (isset($this->_writeProperties[$rule['field']])) {
$newRules[] = $rule;
}
}
$rules = $newRules;
}
if (empty($rules))
return ($returnData) ? $data : true;
if (empty($data))
return false;
get_instance()->load->library('form_validation', null, 'yidas_model_form_validation');
$validator = get_instance()->yidas_model_form_validation;
$validator->reset_validation();
$validator->set_data($data);
$validator->set_rules($rules);
$result = $validator->run();
if ($result===false) {
$this->_errors = $validator->error_array();
return false;
} else {
return ($returnData) ? $data : true;
}
} | [
"public",
"function",
"validate",
"(",
"$",
"attributes",
"=",
"[",
"]",
",",
"$",
"returnData",
"=",
"false",
")",
"{",
"// Data fetched by ORM or input",
"$",
"data",
"=",
"(",
"$",
"attributes",
")",
"?",
"$",
"attributes",
":",
"$",
"this",
"->",
"_writeProperties",
";",
"// Filter first",
"$",
"data",
"=",
"$",
"this",
"->",
"filter",
"(",
"$",
"data",
")",
";",
"// ORM re-assign properties",
"$",
"this",
"->",
"_writeProperties",
"=",
"(",
"!",
"$",
"attributes",
")",
"?",
"$",
"data",
":",
"$",
"this",
"->",
"_writeProperties",
";",
"// Get validation rules from function setting",
"$",
"rules",
"=",
"$",
"this",
"->",
"rules",
"(",
")",
";",
"// The ORM update will only collect rules with corresponding modified attributes.",
"if",
"(",
"$",
"this",
"->",
"_selfCondition",
")",
"{",
"$",
"newRules",
"=",
"[",
"]",
";",
"foreach",
"(",
"(",
"array",
")",
"$",
"rules",
"as",
"$",
"key",
"=>",
"$",
"rule",
")",
"{",
"if",
"(",
"isset",
"(",
"$",
"this",
"->",
"_writeProperties",
"[",
"$",
"rule",
"[",
"'field'",
"]",
"]",
")",
")",
"{",
"// Add into new rules for updating",
"$",
"newRules",
"[",
"]",
"=",
"$",
"rule",
";",
"}",
"}",
"// Replace with mapping rules",
"$",
"rules",
"=",
"$",
"newRules",
";",
"}",
"// Check if has rules",
"if",
"(",
"empty",
"(",
"$",
"rules",
")",
")",
"return",
"(",
"$",
"returnData",
")",
"?",
"$",
"data",
":",
"true",
";",
"// CodeIgniter form_validation doesn't work with empty array data",
"if",
"(",
"empty",
"(",
"$",
"data",
")",
")",
"return",
"false",
";",
"// Load CodeIgniter form_validation library for yidas/model namespace, which has no effect on common one",
"get_instance",
"(",
")",
"->",
"load",
"->",
"library",
"(",
"'form_validation'",
",",
"null",
",",
"'yidas_model_form_validation'",
")",
";",
"// Get CodeIgniter validator",
"$",
"validator",
"=",
"get_instance",
"(",
")",
"->",
"yidas_model_form_validation",
";",
"$",
"validator",
"->",
"reset_validation",
"(",
")",
";",
"$",
"validator",
"->",
"set_data",
"(",
"$",
"data",
")",
";",
"$",
"validator",
"->",
"set_rules",
"(",
"$",
"rules",
")",
";",
"// Run Validate",
"$",
"result",
"=",
"$",
"validator",
"->",
"run",
"(",
")",
";",
"// Result handle",
"if",
"(",
"$",
"result",
"===",
"false",
")",
"{",
"$",
"this",
"->",
"_errors",
"=",
"$",
"validator",
"->",
"error_array",
"(",
")",
";",
"return",
"false",
";",
"}",
"else",
"{",
"return",
"(",
"$",
"returnData",
")",
"?",
"$",
"data",
":",
"true",
";",
"}",
"}"
] | Performs the data validation with filters
ORM only performs validation for assigned properties.
@param array Data of attributes
@param boolean Return filtered data
@return boolean Result
@return mixed Data after filter ($returnData is true) | [
"Performs",
"the",
"data",
"validation",
"with",
"filters"
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/src/Model.php#L330-L383 |
yidas/codeigniter-model | src/Model.php | Model.filter | public function filter($data)
{
// Get filter rules
$filters = $this->filters();
// Filter process with setting check
if (!empty($filters) && is_array($filters)) {
foreach ($filters as $key => $filter) {
if (!isset($filter[0]))
throw new Exception("No attributes defined in \$filters from " . get_called_class() . " (" . __CLASS__ . ")", 500);
if (!isset($filter[1]))
throw new Exception("No function defined in \$filters from " . get_called_class() . " (" . __CLASS__ . ")", 500);
list($attributes, $function) = $filter;
$attributes = (is_array($attributes)) ? $attributes : [$attributes];
// Filter each attribute
foreach ($attributes as $key => $attribute) {
if (!isset($data[$attribute]))
continue;
$data[$attribute] = call_user_func($function, $data[$attribute]);
}
}
}
return $data;
} | php | public function filter($data)
{
$filters = $this->filters();
if (!empty($filters) && is_array($filters)) {
foreach ($filters as $key => $filter) {
if (!isset($filter[0]))
throw new Exception("No attributes defined in \$filters from " . get_called_class() . " (" . __CLASS__ . ")", 500);
if (!isset($filter[1]))
throw new Exception("No function defined in \$filters from " . get_called_class() . " (" . __CLASS__ . ")", 500);
list($attributes, $function) = $filter;
$attributes = (is_array($attributes)) ? $attributes : [$attributes];
foreach ($attributes as $key => $attribute) {
if (!isset($data[$attribute]))
continue;
$data[$attribute] = call_user_func($function, $data[$attribute]);
}
}
}
return $data;
} | [
"public",
"function",
"filter",
"(",
"$",
"data",
")",
"{",
"// Get filter rules",
"$",
"filters",
"=",
"$",
"this",
"->",
"filters",
"(",
")",
";",
"// Filter process with setting check",
"if",
"(",
"!",
"empty",
"(",
"$",
"filters",
")",
"&&",
"is_array",
"(",
"$",
"filters",
")",
")",
"{",
"foreach",
"(",
"$",
"filters",
"as",
"$",
"key",
"=>",
"$",
"filter",
")",
"{",
"if",
"(",
"!",
"isset",
"(",
"$",
"filter",
"[",
"0",
"]",
")",
")",
"throw",
"new",
"Exception",
"(",
"\"No attributes defined in \\$filters from \"",
".",
"get_called_class",
"(",
")",
".",
"\" (\"",
".",
"__CLASS__",
".",
"\")\"",
",",
"500",
")",
";",
"if",
"(",
"!",
"isset",
"(",
"$",
"filter",
"[",
"1",
"]",
")",
")",
"throw",
"new",
"Exception",
"(",
"\"No function defined in \\$filters from \"",
".",
"get_called_class",
"(",
")",
".",
"\" (\"",
".",
"__CLASS__",
".",
"\")\"",
",",
"500",
")",
";",
"list",
"(",
"$",
"attributes",
",",
"$",
"function",
")",
"=",
"$",
"filter",
";",
"$",
"attributes",
"=",
"(",
"is_array",
"(",
"$",
"attributes",
")",
")",
"?",
"$",
"attributes",
":",
"[",
"$",
"attributes",
"]",
";",
"// Filter each attribute",
"foreach",
"(",
"$",
"attributes",
"as",
"$",
"key",
"=>",
"$",
"attribute",
")",
"{",
"if",
"(",
"!",
"isset",
"(",
"$",
"data",
"[",
"$",
"attribute",
"]",
")",
")",
"continue",
";",
"$",
"data",
"[",
"$",
"attribute",
"]",
"=",
"call_user_func",
"(",
"$",
"function",
",",
"$",
"data",
"[",
"$",
"attribute",
"]",
")",
";",
"}",
"}",
"}",
"return",
"$",
"data",
";",
"}"
] | Filter process
@param array $data Attributes
@return array Filtered data | [
"Filter",
"process"
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/src/Model.php#L413-L445 |
yidas/codeigniter-model | src/Model.php | Model.find | public function find($withAll=false)
{
$instance = (isset($this)) ? $this : new static;
// One time setting reset mechanism
if ($instance->_cleanNextFind === true) {
// Reset alias
$instance->setAlias(null);
} else {
// Turn on clean for next find
$instance->_cleanNextFind = true;
}
// Alias option for FROM
$sqlFrom = ($instance->alias) ? "{$instance->table} AS {$instance->alias}" : $instance->table;
$instance->_dbr->from($sqlFrom);
// WithAll helper
if ($withAll===true) {
$instance->withAll();
}
// Scope condition
$instance->_addGlobalScopeCondition();
// Soft Deleted condition
$instance->_addSoftDeletedCondition();
return $instance->_dbr;
} | php | public function find($withAll=false)
{
$instance = (isset($this)) ? $this : new static;
if ($instance->_cleanNextFind === true) {
$instance->setAlias(null);
} else {
$instance->_cleanNextFind = true;
}
$sqlFrom = ($instance->alias) ? "{$instance->table} AS {$instance->alias}" : $instance->table;
$instance->_dbr->from($sqlFrom);
if ($withAll===true) {
$instance->withAll();
}
$instance->_addGlobalScopeCondition();
$instance->_addSoftDeletedCondition();
return $instance->_dbr;
} | [
"public",
"function",
"find",
"(",
"$",
"withAll",
"=",
"false",
")",
"{",
"$",
"instance",
"=",
"(",
"isset",
"(",
"$",
"this",
")",
")",
"?",
"$",
"this",
":",
"new",
"static",
";",
"// One time setting reset mechanism",
"if",
"(",
"$",
"instance",
"->",
"_cleanNextFind",
"===",
"true",
")",
"{",
"// Reset alias",
"$",
"instance",
"->",
"setAlias",
"(",
"null",
")",
";",
"}",
"else",
"{",
"// Turn on clean for next find",
"$",
"instance",
"->",
"_cleanNextFind",
"=",
"true",
";",
"}",
"// Alias option for FROM",
"$",
"sqlFrom",
"=",
"(",
"$",
"instance",
"->",
"alias",
")",
"?",
"\"{$instance->table} AS {$instance->alias}\"",
":",
"$",
"instance",
"->",
"table",
";",
"$",
"instance",
"->",
"_dbr",
"->",
"from",
"(",
"$",
"sqlFrom",
")",
";",
"// WithAll helper",
"if",
"(",
"$",
"withAll",
"===",
"true",
")",
"{",
"$",
"instance",
"->",
"withAll",
"(",
")",
";",
"}",
"// Scope condition",
"$",
"instance",
"->",
"_addGlobalScopeCondition",
"(",
")",
";",
"// Soft Deleted condition",
"$",
"instance",
"->",
"_addSoftDeletedCondition",
"(",
")",
";",
"return",
"$",
"instance",
"->",
"_dbr",
";",
"}"
] | Create an existent CI Query Builder instance with Model features for query purpose.
@param boolean $withAll withAll() switch helper
@return object CI_DB_query_builder
@example
$posts = $this->PostModel->find()
->where('is_public', '1')
->limit(0,25)
->order_by('id')
->get()
->result_array();
@example
// Without all featured conditions for next find()
$posts = $this->PostModel->find(true)
->where('is_deleted', '1')
->get()
->result_array();
// This is equal to withAll() method
$this->PostModel->withAll()->find(); | [
"Create",
"an",
"existent",
"CI",
"Query",
"Builder",
"instance",
"with",
"Model",
"features",
"for",
"query",
"purpose",
"."
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/src/Model.php#L485-L515 |
yidas/codeigniter-model | src/Model.php | Model.findOne | public static function findOne($condition=[])
{
$instance = (isset($this)) ? $this : new static;
$record = $instance->_findByCondition($condition)
->limit(1)
->get()->row_array();
// Record check
if (!$record) {
return $record;
}
return $instance->createActiveRecord($record, $record[$instance->primaryKey]);
} | php | public static function findOne($condition=[])
{
$instance = (isset($this)) ? $this : new static;
$record = $instance->_findByCondition($condition)
->limit(1)
->get()->row_array();
if (!$record) {
return $record;
}
return $instance->createActiveRecord($record, $record[$instance->primaryKey]);
} | [
"public",
"static",
"function",
"findOne",
"(",
"$",
"condition",
"=",
"[",
"]",
")",
"{",
"$",
"instance",
"=",
"(",
"isset",
"(",
"$",
"this",
")",
")",
"?",
"$",
"this",
":",
"new",
"static",
";",
"$",
"record",
"=",
"$",
"instance",
"->",
"_findByCondition",
"(",
"$",
"condition",
")",
"->",
"limit",
"(",
"1",
")",
"->",
"get",
"(",
")",
"->",
"row_array",
"(",
")",
";",
"// Record check",
"if",
"(",
"!",
"$",
"record",
")",
"{",
"return",
"$",
"record",
";",
"}",
"return",
"$",
"instance",
"->",
"createActiveRecord",
"(",
"$",
"record",
",",
"$",
"record",
"[",
"$",
"instance",
"->",
"primaryKey",
"]",
")",
";",
"}"
] | Return a single active record model instance by a primary key or an array of column values.
@param mixed $condition Refer to _findByCondition() for the explanation of this parameter
@return object ActiveRecord(Model)
@example
$post = $this->Model->findOne(123);
@example
// Query builder ORM usage
$this->Model->find()->where('id', 123);
$this->Model->findOne(); | [
"Return",
"a",
"single",
"active",
"record",
"model",
"instance",
"by",
"a",
"primary",
"key",
"or",
"an",
"array",
"of",
"column",
"values",
"."
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/src/Model.php#L539-L553 |
yidas/codeigniter-model | src/Model.php | Model.findAll | public static function findAll($condition=[], $limit=null)
{
$instance = (isset($this)) ? $this : new static;
$query = $instance->_findByCondition($condition);
// Limit / offset
if ($limit) {
$offset = null;
if (is_array($limit) && isset($limit[1])) {
// Prevent list() variable effect
$set = $limit;
list($offset, $limit) = $set;
}
$query = ($limit) ? $query->limit($limit) : $query;
$query = ($offset) ? $query->offset($offset) : $query;
}
$records = $query->get()->result_array();
// Record check
if (!$records) {
return $records;
}
$set = [];
// Each ActiveRecord
foreach ((array)$records as $key => $record) {
// Check primary key setting
if (!isset($record[$instance->primaryKey])) {
throw new Exception("Model's primary key not set", 500);
}
// Create an ActiveRecord into collect
$set[] = $instance->createActiveRecord($record, $record[$instance->primaryKey]);
}
return $set;
} | php | public static function findAll($condition=[], $limit=null)
{
$instance = (isset($this)) ? $this : new static;
$query = $instance->_findByCondition($condition);
if ($limit) {
$offset = null;
if (is_array($limit) && isset($limit[1])) {
$set = $limit;
list($offset, $limit) = $set;
}
$query = ($limit) ? $query->limit($limit) : $query;
$query = ($offset) ? $query->offset($offset) : $query;
}
$records = $query->get()->result_array();
if (!$records) {
return $records;
}
$set = [];
foreach ((array)$records as $key => $record) {
if (!isset($record[$instance->primaryKey])) {
throw new Exception("Model's primary key not set", 500);
}
$set[] = $instance->createActiveRecord($record, $record[$instance->primaryKey]);
}
return $set;
} | [
"public",
"static",
"function",
"findAll",
"(",
"$",
"condition",
"=",
"[",
"]",
",",
"$",
"limit",
"=",
"null",
")",
"{",
"$",
"instance",
"=",
"(",
"isset",
"(",
"$",
"this",
")",
")",
"?",
"$",
"this",
":",
"new",
"static",
";",
"$",
"query",
"=",
"$",
"instance",
"->",
"_findByCondition",
"(",
"$",
"condition",
")",
";",
"// Limit / offset",
"if",
"(",
"$",
"limit",
")",
"{",
"$",
"offset",
"=",
"null",
";",
"if",
"(",
"is_array",
"(",
"$",
"limit",
")",
"&&",
"isset",
"(",
"$",
"limit",
"[",
"1",
"]",
")",
")",
"{",
"// Prevent list() variable effect",
"$",
"set",
"=",
"$",
"limit",
";",
"list",
"(",
"$",
"offset",
",",
"$",
"limit",
")",
"=",
"$",
"set",
";",
"}",
"$",
"query",
"=",
"(",
"$",
"limit",
")",
"?",
"$",
"query",
"->",
"limit",
"(",
"$",
"limit",
")",
":",
"$",
"query",
";",
"$",
"query",
"=",
"(",
"$",
"offset",
")",
"?",
"$",
"query",
"->",
"offset",
"(",
"$",
"offset",
")",
":",
"$",
"query",
";",
"}",
"$",
"records",
"=",
"$",
"query",
"->",
"get",
"(",
")",
"->",
"result_array",
"(",
")",
";",
"// Record check",
"if",
"(",
"!",
"$",
"records",
")",
"{",
"return",
"$",
"records",
";",
"}",
"$",
"set",
"=",
"[",
"]",
";",
"// Each ActiveRecord",
"foreach",
"(",
"(",
"array",
")",
"$",
"records",
"as",
"$",
"key",
"=>",
"$",
"record",
")",
"{",
"// Check primary key setting",
"if",
"(",
"!",
"isset",
"(",
"$",
"record",
"[",
"$",
"instance",
"->",
"primaryKey",
"]",
")",
")",
"{",
"throw",
"new",
"Exception",
"(",
"\"Model's primary key not set\"",
",",
"500",
")",
";",
"}",
"// Create an ActiveRecord into collect",
"$",
"set",
"[",
"]",
"=",
"$",
"instance",
"->",
"createActiveRecord",
"(",
"$",
"record",
",",
"$",
"record",
"[",
"$",
"instance",
"->",
"primaryKey",
"]",
")",
";",
"}",
"return",
"$",
"set",
";",
"}"
] | Returns a list of active record models that match the specified primary key value(s) or a set of column values.
@param mixed $condition Refer to _findByCondition() for the explanation
@param integer|array $limit Limit or [offset, limit]
@return array Set of ActiveRecord(Model)s
@example
$post = $this->PostModel->findAll([3,21,135]);
@example
// Query builder ORM usage
$this->Model->find()->where_in('id', [3,21,135]);
$this->Model->findAll(); | [
"Returns",
"a",
"list",
"of",
"active",
"record",
"models",
"that",
"match",
"the",
"specified",
"primary",
"key",
"value",
"(",
"s",
")",
"or",
"a",
"set",
"of",
"column",
"values",
"."
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/src/Model.php#L568-L608 |
yidas/codeigniter-model | src/Model.php | Model.batchInsert | public function batchInsert($data, $runValidation=true)
{
foreach ($data as $key => &$attributes) {
// Validation
if ($runValidation && false===$attributes=$this->validate($attributes, true))
return false;
$this->_attrEventBeforeInsert($attributes);
}
return $this->_db->insert_batch($this->table, $data);
} | php | public function batchInsert($data, $runValidation=true)
{
foreach ($data as $key => &$attributes) {
if ($runValidation && false===$attributes=$this->validate($attributes, true))
return false;
$this->_attrEventBeforeInsert($attributes);
}
return $this->_db->insert_batch($this->table, $data);
} | [
"public",
"function",
"batchInsert",
"(",
"$",
"data",
",",
"$",
"runValidation",
"=",
"true",
")",
"{",
"foreach",
"(",
"$",
"data",
"as",
"$",
"key",
"=>",
"&",
"$",
"attributes",
")",
"{",
"// Validation",
"if",
"(",
"$",
"runValidation",
"&&",
"false",
"===",
"$",
"attributes",
"=",
"$",
"this",
"->",
"validate",
"(",
"$",
"attributes",
",",
"true",
")",
")",
"return",
"false",
";",
"$",
"this",
"->",
"_attrEventBeforeInsert",
"(",
"$",
"attributes",
")",
";",
"}",
"return",
"$",
"this",
"->",
"_db",
"->",
"insert_batch",
"(",
"$",
"this",
"->",
"table",
",",
"$",
"data",
")",
";",
"}"
] | Insert a batch of rows with Timestamps feature into the associated database table using the attribute values of this record.
@param array $data The rows to be batch inserted
@param boolean $runValidation Whether to perform validation (calling validate()) before manipulate the record.
@return int Number of rows inserted or FALSE on failure
@example
$result = $this->Model->batchInsert([
['name' => 'Nick Tsai', 'email' => '[email protected]'],
['name' => 'Yidas', 'email' => '[email protected]']
]); | [
"Insert",
"a",
"batch",
"of",
"rows",
"with",
"Timestamps",
"feature",
"into",
"the",
"associated",
"database",
"table",
"using",
"the",
"attribute",
"values",
"of",
"this",
"record",
"."
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/src/Model.php#L661-L673 |
yidas/codeigniter-model | src/Model.php | Model.replace | public function replace($attributes, $runValidation=true)
{
// Validation
if ($runValidation && false===$attributes=$this->validate($attributes, true))
return false;
$this->_attrEventBeforeInsert($attributes);
return $this->_db->replace($this->table, $attributes);
} | php | public function replace($attributes, $runValidation=true)
{
if ($runValidation && false===$attributes=$this->validate($attributes, true))
return false;
$this->_attrEventBeforeInsert($attributes);
return $this->_db->replace($this->table, $attributes);
} | [
"public",
"function",
"replace",
"(",
"$",
"attributes",
",",
"$",
"runValidation",
"=",
"true",
")",
"{",
"// Validation",
"if",
"(",
"$",
"runValidation",
"&&",
"false",
"===",
"$",
"attributes",
"=",
"$",
"this",
"->",
"validate",
"(",
"$",
"attributes",
",",
"true",
")",
")",
"return",
"false",
";",
"$",
"this",
"->",
"_attrEventBeforeInsert",
"(",
"$",
"attributes",
")",
";",
"return",
"$",
"this",
"->",
"_db",
"->",
"replace",
"(",
"$",
"this",
"->",
"table",
",",
"$",
"attributes",
")",
";",
"}"
] | Replace a row with Timestamps feature into the associated database table using the attribute values of this record.
@param array $attributes
@param boolean $runValidation Whether to perform validation (calling validate()) before manipulate the record.
@return bool Result
@example
$result = $this->Model->replace([
'id' => 1,
'name' => 'Nick Tsai',
'email' => '[email protected]',
]); | [
"Replace",
"a",
"row",
"with",
"Timestamps",
"feature",
"into",
"the",
"associated",
"database",
"table",
"using",
"the",
"attribute",
"values",
"of",
"this",
"record",
"."
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/src/Model.php#L698-L707 |
yidas/codeigniter-model | src/Model.php | Model.update | public function update($attributes, $condition=NULL, $runValidation=true)
{
// Validation
if ($runValidation && false===$attributes=$this->validate($attributes, true))
return false;
// Model Condition
$query = $this->_findByCondition($condition);
$attributes = $this->_attrEventBeforeUpdate($attributes);
// Pack query then move it to write DB from read DB
$sql = $this->_dbr->set($attributes)->get_compiled_update();
$this->_dbr->reset_query();
return $this->_db->query($sql);
} | php | public function update($attributes, $condition=NULL, $runValidation=true)
{
if ($runValidation && false===$attributes=$this->validate($attributes, true))
return false;
$query = $this->_findByCondition($condition);
$attributes = $this->_attrEventBeforeUpdate($attributes);
$sql = $this->_dbr->set($attributes)->get_compiled_update();
$this->_dbr->reset_query();
return $this->_db->query($sql);
} | [
"public",
"function",
"update",
"(",
"$",
"attributes",
",",
"$",
"condition",
"=",
"NULL",
",",
"$",
"runValidation",
"=",
"true",
")",
"{",
"// Validation",
"if",
"(",
"$",
"runValidation",
"&&",
"false",
"===",
"$",
"attributes",
"=",
"$",
"this",
"->",
"validate",
"(",
"$",
"attributes",
",",
"true",
")",
")",
"return",
"false",
";",
"// Model Condition",
"$",
"query",
"=",
"$",
"this",
"->",
"_findByCondition",
"(",
"$",
"condition",
")",
";",
"$",
"attributes",
"=",
"$",
"this",
"->",
"_attrEventBeforeUpdate",
"(",
"$",
"attributes",
")",
";",
"// Pack query then move it to write DB from read DB",
"$",
"sql",
"=",
"$",
"this",
"->",
"_dbr",
"->",
"set",
"(",
"$",
"attributes",
")",
"->",
"get_compiled_update",
"(",
")",
";",
"$",
"this",
"->",
"_dbr",
"->",
"reset_query",
"(",
")",
";",
"return",
"$",
"this",
"->",
"_db",
"->",
"query",
"(",
"$",
"sql",
")",
";",
"}"
] | Save the changes with Timestamps feature to the selected record(s) into the associated database table.
@param array $attributes
@param mixed $condition Refer to _findByCondition() for the explanation
@param boolean $runValidation Whether to perform validation (calling validate()) before manipulate the record.
@return bool Result
@example
$this->Model->update(['status'=>'off'], 123)
@example
// Query builder ORM usage
$this->Model->find()->where('id', 123);
$this->Model->update(['status'=>'off']); | [
"Save",
"the",
"changes",
"with",
"Timestamps",
"feature",
"to",
"the",
"selected",
"record",
"(",
"s",
")",
"into",
"the",
"associated",
"database",
"table",
"."
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/src/Model.php#L724-L740 |
yidas/codeigniter-model | src/Model.php | Model.batchUpdate | public function batchUpdate(Array $dataSet, $withAll=false, $maxLength=4*1024*1024, $runValidation=true)
{
$count = 0;
$sqlBatch = '';
foreach ($dataSet as $key => &$each) {
// Data format
list($attributes, $condition) = $each;
// Check attributes
if (!is_array($attributes) || !$attributes)
continue;
// Validation
if ($runValidation && false===$attributes=$this->validate($attributes, true))
continue;
// WithAll helper
if ($withAll===true) {
$this->withAll();
}
// Model Condition
$query = $this->_findByCondition($condition);
$attributes = $this->_attrEventBeforeUpdate($attributes);
// Pack query then move it to write DB from read DB
$sql = $this->_dbr->set($attributes)->get_compiled_update();
$this->_dbr->reset_query();
// Last batch check: First single query & Max length
// The first single query needs to be sent ahead to prevent the limitation that PDO transaction could not
// use multiple SQL line in one query, but allows if the multi-line query is behind a single query.
if (($count==0 && $sqlBatch) || strlen($sqlBatch)>=$maxLength) {
// Each batch of query
$result = $this->_db->query($sqlBatch);
$sqlBatch = "";
$count = ($result) ? $count + 1 : $count;
}
// Keep Combining query
$sqlBatch .= "{$sql};\n";
}
// Last batch of query
$result = $this->_db->query($sqlBatch);
return ($result) ? $count + 1 : $count;
} | php | public function batchUpdate(Array $dataSet, $withAll=false, $maxLength=4*1024*1024, $runValidation=true)
{
$count = 0;
$sqlBatch = '';
foreach ($dataSet as $key => &$each) {
list($attributes, $condition) = $each;
if (!is_array($attributes) || !$attributes)
continue;
if ($runValidation && false===$attributes=$this->validate($attributes, true))
continue;
if ($withAll===true) {
$this->withAll();
}
$query = $this->_findByCondition($condition);
$attributes = $this->_attrEventBeforeUpdate($attributes);
$sql = $this->_dbr->set($attributes)->get_compiled_update();
$this->_dbr->reset_query();
if (($count==0 && $sqlBatch) || strlen($sqlBatch)>=$maxLength) {
$result = $this->_db->query($sqlBatch);
$sqlBatch = "";
$count = ($result) ? $count + 1 : $count;
}
$sqlBatch .= "{$sql};\n";
}
$result = $this->_db->query($sqlBatch);
return ($result) ? $count + 1 : $count;
} | [
"public",
"function",
"batchUpdate",
"(",
"Array",
"$",
"dataSet",
",",
"$",
"withAll",
"=",
"false",
",",
"$",
"maxLength",
"=",
"4",
"*",
"1024",
"*",
"1024",
",",
"$",
"runValidation",
"=",
"true",
")",
"{",
"$",
"count",
"=",
"0",
";",
"$",
"sqlBatch",
"=",
"''",
";",
"foreach",
"(",
"$",
"dataSet",
"as",
"$",
"key",
"=>",
"&",
"$",
"each",
")",
"{",
"// Data format",
"list",
"(",
"$",
"attributes",
",",
"$",
"condition",
")",
"=",
"$",
"each",
";",
"// Check attributes",
"if",
"(",
"!",
"is_array",
"(",
"$",
"attributes",
")",
"||",
"!",
"$",
"attributes",
")",
"continue",
";",
"// Validation",
"if",
"(",
"$",
"runValidation",
"&&",
"false",
"===",
"$",
"attributes",
"=",
"$",
"this",
"->",
"validate",
"(",
"$",
"attributes",
",",
"true",
")",
")",
"continue",
";",
"// WithAll helper",
"if",
"(",
"$",
"withAll",
"===",
"true",
")",
"{",
"$",
"this",
"->",
"withAll",
"(",
")",
";",
"}",
"// Model Condition",
"$",
"query",
"=",
"$",
"this",
"->",
"_findByCondition",
"(",
"$",
"condition",
")",
";",
"$",
"attributes",
"=",
"$",
"this",
"->",
"_attrEventBeforeUpdate",
"(",
"$",
"attributes",
")",
";",
"// Pack query then move it to write DB from read DB",
"$",
"sql",
"=",
"$",
"this",
"->",
"_dbr",
"->",
"set",
"(",
"$",
"attributes",
")",
"->",
"get_compiled_update",
"(",
")",
";",
"$",
"this",
"->",
"_dbr",
"->",
"reset_query",
"(",
")",
";",
"// Last batch check: First single query & Max length",
"// The first single query needs to be sent ahead to prevent the limitation that PDO transaction could not ",
"// use multiple SQL line in one query, but allows if the multi-line query is behind a single query. ",
"if",
"(",
"(",
"$",
"count",
"==",
"0",
"&&",
"$",
"sqlBatch",
")",
"||",
"strlen",
"(",
"$",
"sqlBatch",
")",
">=",
"$",
"maxLength",
")",
"{",
"// Each batch of query",
"$",
"result",
"=",
"$",
"this",
"->",
"_db",
"->",
"query",
"(",
"$",
"sqlBatch",
")",
";",
"$",
"sqlBatch",
"=",
"\"\"",
";",
"$",
"count",
"=",
"(",
"$",
"result",
")",
"?",
"$",
"count",
"+",
"1",
":",
"$",
"count",
";",
"}",
"// Keep Combining query",
"$",
"sqlBatch",
".=",
"\"{$sql};\\n\"",
";",
"}",
"// Last batch of query",
"$",
"result",
"=",
"$",
"this",
"->",
"_db",
"->",
"query",
"(",
"$",
"sqlBatch",
")",
";",
"return",
"(",
"$",
"result",
")",
"?",
"$",
"count",
"+",
"1",
":",
"$",
"count",
";",
"}"
] | Update a batch of update queries into combined query strings.
@param array $dataSet [[[Attributes], [Condition]], ]
@param boolean $withAll withAll() switch helper
@param integer $maxLenth MySQL max_allowed_packet
@param boolean $runValidation Whether to perform validation (calling validate()) before manipulate the record.
@return integer Count of successful query pack(s)
@example
$result = $this->Model->batchUpdate([
[['title'=>'A1', 'modified'=>'1'], ['id'=>1]],
[['title'=>'A2', 'modified'=>'1'], ['id'=>2]],
];); | [
"Update",
"a",
"batch",
"of",
"update",
"queries",
"into",
"combined",
"query",
"strings",
"."
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/src/Model.php#L756-L806 |
yidas/codeigniter-model | src/Model.php | Model.delete | public function delete($condition=NULL, $forceDelete=false, $attributes=[])
{
// Check is Active Record
if ($this->_readProperties) {
// Reset condition and find single by self condition
$this->reset();
$condition = $this->_selfCondition;
}
// Model Condition by $forceDelete switch
$query = ($forceDelete)
? $this->withTrashed()->_findByCondition($condition)
: $this->_findByCondition($condition);
/* Soft Delete Mode */
if (static::SOFT_DELETED
&& isset($this->softDeletedTrueValue)
&& !$forceDelete) {
// Mark the records as deleted
$attributes[static::SOFT_DELETED] = $this->softDeletedTrueValue;
$attributes = $this->_attrEventBeforeDelete($attributes);
// Pack query then move it to write DB from read DB
$sql = $this->_dbr->set($attributes)->get_compiled_update();
$this->_dbr->reset_query();
} else {
/* Hard Delete */
// Pack query then move it to write DB from read DB
$sql = $this->_dbr->get_compiled_delete();
$this->_dbr->reset_query();
}
return $this->_db->query($sql);
} | php | public function delete($condition=NULL, $forceDelete=false, $attributes=[])
{
if ($this->_readProperties) {
$this->reset();
$condition = $this->_selfCondition;
}
$query = ($forceDelete)
? $this->withTrashed()->_findByCondition($condition)
: $this->_findByCondition($condition);
if (static::SOFT_DELETED
&& isset($this->softDeletedTrueValue)
&& !$forceDelete) {
$attributes[static::SOFT_DELETED] = $this->softDeletedTrueValue;
$attributes = $this->_attrEventBeforeDelete($attributes);
$sql = $this->_dbr->set($attributes)->get_compiled_update();
$this->_dbr->reset_query();
} else {
$sql = $this->_dbr->get_compiled_delete();
$this->_dbr->reset_query();
}
return $this->_db->query($sql);
} | [
"public",
"function",
"delete",
"(",
"$",
"condition",
"=",
"NULL",
",",
"$",
"forceDelete",
"=",
"false",
",",
"$",
"attributes",
"=",
"[",
"]",
")",
"{",
"// Check is Active Record",
"if",
"(",
"$",
"this",
"->",
"_readProperties",
")",
"{",
"// Reset condition and find single by self condition",
"$",
"this",
"->",
"reset",
"(",
")",
";",
"$",
"condition",
"=",
"$",
"this",
"->",
"_selfCondition",
";",
"}",
"// Model Condition by $forceDelete switch",
"$",
"query",
"=",
"(",
"$",
"forceDelete",
")",
"?",
"$",
"this",
"->",
"withTrashed",
"(",
")",
"->",
"_findByCondition",
"(",
"$",
"condition",
")",
":",
"$",
"this",
"->",
"_findByCondition",
"(",
"$",
"condition",
")",
";",
"/* Soft Delete Mode */",
"if",
"(",
"static",
"::",
"SOFT_DELETED",
"&&",
"isset",
"(",
"$",
"this",
"->",
"softDeletedTrueValue",
")",
"&&",
"!",
"$",
"forceDelete",
")",
"{",
"// Mark the records as deleted",
"$",
"attributes",
"[",
"static",
"::",
"SOFT_DELETED",
"]",
"=",
"$",
"this",
"->",
"softDeletedTrueValue",
";",
"$",
"attributes",
"=",
"$",
"this",
"->",
"_attrEventBeforeDelete",
"(",
"$",
"attributes",
")",
";",
"// Pack query then move it to write DB from read DB",
"$",
"sql",
"=",
"$",
"this",
"->",
"_dbr",
"->",
"set",
"(",
"$",
"attributes",
")",
"->",
"get_compiled_update",
"(",
")",
";",
"$",
"this",
"->",
"_dbr",
"->",
"reset_query",
"(",
")",
";",
"}",
"else",
"{",
"/* Hard Delete */",
"// Pack query then move it to write DB from read DB",
"$",
"sql",
"=",
"$",
"this",
"->",
"_dbr",
"->",
"get_compiled_delete",
"(",
")",
";",
"$",
"this",
"->",
"_dbr",
"->",
"reset_query",
"(",
")",
";",
"}",
"return",
"$",
"this",
"->",
"_db",
"->",
"query",
"(",
"$",
"sql",
")",
";",
"}"
] | Delete the selected record(s) with Timestamps feature into the associated database table.
@param mixed $condition Refer to _findByCondition() for the explanation
@param boolean $forceDelete Force to hard delete
@param array $attributes Extended attributes for Soft Delete Mode
@return bool Result
@example
$this->Model->delete(123);
@example
// Query builder ORM usage
$this->Model->find()->where('id', 123);
$this->Model->delete();
@example
// Force delete for SOFT_DELETED mode
$this->Model->delete(123, true); | [
"Delete",
"the",
"selected",
"record",
"(",
"s",
")",
"with",
"Timestamps",
"feature",
"into",
"the",
"associated",
"database",
"table",
"."
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/src/Model.php#L826-L863 |
yidas/codeigniter-model | src/Model.php | Model.restore | public function restore($condition=NULL)
{
// Model Condition with Trashed
$query = $this->withTrashed()->_findByCondition($condition);
/* Soft Delete Mode */
if (static::SOFT_DELETED
&& isset($this->softDeletedFalseValue)) {
// Mark the records as deleted
$attributes[static::SOFT_DELETED] = $this->softDeletedFalseValue;
return $query->update($this->table, $attributes);
} else {
return false;
}
} | php | public function restore($condition=NULL)
{
$query = $this->withTrashed()->_findByCondition($condition);
if (static::SOFT_DELETED
&& isset($this->softDeletedFalseValue)) {
$attributes[static::SOFT_DELETED] = $this->softDeletedFalseValue;
return $query->update($this->table, $attributes);
} else {
return false;
}
} | [
"public",
"function",
"restore",
"(",
"$",
"condition",
"=",
"NULL",
")",
"{",
"// Model Condition with Trashed",
"$",
"query",
"=",
"$",
"this",
"->",
"withTrashed",
"(",
")",
"->",
"_findByCondition",
"(",
"$",
"condition",
")",
";",
"/* Soft Delete Mode */",
"if",
"(",
"static",
"::",
"SOFT_DELETED",
"&&",
"isset",
"(",
"$",
"this",
"->",
"softDeletedFalseValue",
")",
")",
"{",
"// Mark the records as deleted",
"$",
"attributes",
"[",
"static",
"::",
"SOFT_DELETED",
"]",
"=",
"$",
"this",
"->",
"softDeletedFalseValue",
";",
"return",
"$",
"query",
"->",
"update",
"(",
"$",
"this",
"->",
"table",
",",
"$",
"attributes",
")",
";",
"}",
"else",
"{",
"return",
"false",
";",
"}",
"}"
] | Restore SOFT_DELETED field value to the selected record(s) into the associated database table.
@param mixed $condition Refer to _findByCondition() for the explanation
@return bool Result
@example
$this->Model->restore(123)
@example
// Query builder ORM usage
$this->Model->withTrashed()->find()->where('id', 123);
$this->Model->restore(); | [
"Restore",
"SOFT_DELETED",
"field",
"value",
"to",
"the",
"selected",
"record",
"(",
"s",
")",
"into",
"the",
"associated",
"database",
"table",
"."
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/src/Model.php#L906-L924 |
yidas/codeigniter-model | src/Model.php | Model.lockForUpdate | public function lockForUpdate()
{
// Pack query then move it to write DB from read DB for transaction
$sql = $this->_dbr->get_compiled_select();
$this->_dbr->reset_query();
return $this->_db->query("{$sql} FOR UPDATE");
} | php | public function lockForUpdate()
{
$sql = $this->_dbr->get_compiled_select();
$this->_dbr->reset_query();
return $this->_db->query("{$sql} FOR UPDATE");
} | [
"public",
"function",
"lockForUpdate",
"(",
")",
"{",
"// Pack query then move it to write DB from read DB for transaction",
"$",
"sql",
"=",
"$",
"this",
"->",
"_dbr",
"->",
"get_compiled_select",
"(",
")",
";",
"$",
"this",
"->",
"_dbr",
"->",
"reset_query",
"(",
")",
";",
"return",
"$",
"this",
"->",
"_db",
"->",
"query",
"(",
"\"{$sql} FOR UPDATE\"",
")",
";",
"}"
] | Lock the selected rows in the table for updating.
sharedLock locks only for write, lockForUpdate also prevents them from being selected
@example
$this->Model->find()->where('id', 123)
$result = $this->Model->lockForUpdate()->row_array();
@example
// This transaction block will lock selected rows for next same selected
// rows with `FOR UPDATE` lock:
$this->Model->getDB()->trans_start();
$this->Model->find()->where('id', 123)
$result = $this->Model->lockForUpdate()->row_array();
$this->Model->getDB()->trans_complete();
@return object CI_DB_result | [
"Lock",
"the",
"selected",
"rows",
"in",
"the",
"table",
"for",
"updating",
"."
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/src/Model.php#L955-L962 |
yidas/codeigniter-model | src/Model.php | Model.sharedLock | public function sharedLock()
{
// Pack query then move it to write DB from read DB for transaction
$sql = $this->_dbr->get_compiled_select();
$this->_dbr->reset_query();
return $this->_db->query("{$sql} LOCK IN SHARE MODE");
} | php | public function sharedLock()
{
$sql = $this->_dbr->get_compiled_select();
$this->_dbr->reset_query();
return $this->_db->query("{$sql} LOCK IN SHARE MODE");
} | [
"public",
"function",
"sharedLock",
"(",
")",
"{",
"// Pack query then move it to write DB from read DB for transaction",
"$",
"sql",
"=",
"$",
"this",
"->",
"_dbr",
"->",
"get_compiled_select",
"(",
")",
";",
"$",
"this",
"->",
"_dbr",
"->",
"reset_query",
"(",
")",
";",
"return",
"$",
"this",
"->",
"_db",
"->",
"query",
"(",
"\"{$sql} LOCK IN SHARE MODE\"",
")",
";",
"}"
] | Share lock the selected rows in the table.
@example
$this->Model->find()->where('id', 123)
$result = $this->Model->sharedLock()->row_array();'
@return object CI_DB_result | [
"Share",
"lock",
"the",
"selected",
"rows",
"in",
"the",
"table",
"."
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/src/Model.php#L973-L980 |
yidas/codeigniter-model | src/Model.php | Model.createActiveRecord | public function createActiveRecord($readProperties, $selfCondition)
{
$activeRecord = new static();
// ORM handling
$activeRecord->_readProperties = $readProperties;
// Primary key condition to ensure single query result
$activeRecord->_selfCondition = $selfCondition;
return $activeRecord;
} | php | public function createActiveRecord($readProperties, $selfCondition)
{
$activeRecord = new static();
$activeRecord->_readProperties = $readProperties;
$activeRecord->_selfCondition = $selfCondition;
return $activeRecord;
} | [
"public",
"function",
"createActiveRecord",
"(",
"$",
"readProperties",
",",
"$",
"selfCondition",
")",
"{",
"$",
"activeRecord",
"=",
"new",
"static",
"(",
")",
";",
"// ORM handling",
"$",
"activeRecord",
"->",
"_readProperties",
"=",
"$",
"readProperties",
";",
"// Primary key condition to ensure single query result ",
"$",
"activeRecord",
"->",
"_selfCondition",
"=",
"$",
"selfCondition",
";",
"return",
"$",
"activeRecord",
";",
"}"
] | New a Active Record from Model by data
@param array $readProperties
@param array $selfCondition
@return object ActiveRecord(Model) | [
"New",
"a",
"Active",
"Record",
"from",
"Model",
"by",
"data"
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/src/Model.php#L1034-L1043 |
yidas/codeigniter-model | src/Model.php | Model.save | public function save($runValidation=true)
{
// if (empty($this->_writeProperties))
// return false;
// ORM status distinguishing
if (!$this->_selfCondition) {
// Event
if (!$this->beforeSave(true)) {
return false;
}
$result = $this->insert($this->_writeProperties, $runValidation);
// Change this ActiveRecord to update mode
if ($result) {
// ORM handling
$this->_readProperties = $this->_writeProperties;
$insertID = $this->getLastInsertID();
$this->_readProperties[$this->primaryKey] = $insertID;
$this->_selfCondition = $insertID;
// Event
$this->afterSave(true, $this->_readProperties);
// Reset properties
$this->_writeProperties = [];
}
} else {
// Event
if (!$this->beforeSave(false)) {
return false;
}
$result = ($this->_writeProperties) ? $this->update($this->_writeProperties, $this->_selfCondition, $runValidation) : true;
// Check the primary key is changed
if ($result) {
// Primary key condition to ensure single query result
if (isset($this->_writeProperties[$this->primaryKey])) {
$this->_selfCondition = $this->_writeProperties[$this->primaryKey];
}
$this->_readProperties = array_merge($this->_readProperties, $this->_writeProperties);
// Event
$this->afterSave(true, $this->_readProperties);
// Reset properties
$this->_writeProperties = [];
}
}
return $result;
} | php | public function save($runValidation=true)
{
if (!$this->_selfCondition) {
if (!$this->beforeSave(true)) {
return false;
}
$result = $this->insert($this->_writeProperties, $runValidation);
if ($result) {
$this->_readProperties = $this->_writeProperties;
$insertID = $this->getLastInsertID();
$this->_readProperties[$this->primaryKey] = $insertID;
$this->_selfCondition = $insertID;
$this->afterSave(true, $this->_readProperties);
$this->_writeProperties = [];
}
} else {
if (!$this->beforeSave(false)) {
return false;
}
$result = ($this->_writeProperties) ? $this->update($this->_writeProperties, $this->_selfCondition, $runValidation) : true;
if ($result) {
if (isset($this->_writeProperties[$this->primaryKey])) {
$this->_selfCondition = $this->_writeProperties[$this->primaryKey];
}
$this->_readProperties = array_merge($this->_readProperties, $this->_writeProperties);
$this->afterSave(true, $this->_readProperties);
$this->_writeProperties = [];
}
}
return $result;
} | [
"public",
"function",
"save",
"(",
"$",
"runValidation",
"=",
"true",
")",
"{",
"// if (empty($this->_writeProperties))",
"// return false;",
"// ORM status distinguishing",
"if",
"(",
"!",
"$",
"this",
"->",
"_selfCondition",
")",
"{",
"// Event",
"if",
"(",
"!",
"$",
"this",
"->",
"beforeSave",
"(",
"true",
")",
")",
"{",
"return",
"false",
";",
"}",
"$",
"result",
"=",
"$",
"this",
"->",
"insert",
"(",
"$",
"this",
"->",
"_writeProperties",
",",
"$",
"runValidation",
")",
";",
"// Change this ActiveRecord to update mode",
"if",
"(",
"$",
"result",
")",
"{",
"// ORM handling",
"$",
"this",
"->",
"_readProperties",
"=",
"$",
"this",
"->",
"_writeProperties",
";",
"$",
"insertID",
"=",
"$",
"this",
"->",
"getLastInsertID",
"(",
")",
";",
"$",
"this",
"->",
"_readProperties",
"[",
"$",
"this",
"->",
"primaryKey",
"]",
"=",
"$",
"insertID",
";",
"$",
"this",
"->",
"_selfCondition",
"=",
"$",
"insertID",
";",
"// Event",
"$",
"this",
"->",
"afterSave",
"(",
"true",
",",
"$",
"this",
"->",
"_readProperties",
")",
";",
"// Reset properties",
"$",
"this",
"->",
"_writeProperties",
"=",
"[",
"]",
";",
"}",
"}",
"else",
"{",
"// Event",
"if",
"(",
"!",
"$",
"this",
"->",
"beforeSave",
"(",
"false",
")",
")",
"{",
"return",
"false",
";",
"}",
"$",
"result",
"=",
"(",
"$",
"this",
"->",
"_writeProperties",
")",
"?",
"$",
"this",
"->",
"update",
"(",
"$",
"this",
"->",
"_writeProperties",
",",
"$",
"this",
"->",
"_selfCondition",
",",
"$",
"runValidation",
")",
":",
"true",
";",
"// Check the primary key is changed",
"if",
"(",
"$",
"result",
")",
"{",
"// Primary key condition to ensure single query result ",
"if",
"(",
"isset",
"(",
"$",
"this",
"->",
"_writeProperties",
"[",
"$",
"this",
"->",
"primaryKey",
"]",
")",
")",
"{",
"$",
"this",
"->",
"_selfCondition",
"=",
"$",
"this",
"->",
"_writeProperties",
"[",
"$",
"this",
"->",
"primaryKey",
"]",
";",
"}",
"$",
"this",
"->",
"_readProperties",
"=",
"array_merge",
"(",
"$",
"this",
"->",
"_readProperties",
",",
"$",
"this",
"->",
"_writeProperties",
")",
";",
"// Event",
"$",
"this",
"->",
"afterSave",
"(",
"true",
",",
"$",
"this",
"->",
"_readProperties",
")",
";",
"// Reset properties",
"$",
"this",
"->",
"_writeProperties",
"=",
"[",
"]",
";",
"}",
"}",
"return",
"$",
"result",
";",
"}"
] | Active Record (ORM) save for insert or update
@param boolean $runValidation Whether to perform validation (calling validate()) before manipulate the record.
@return bool Result of CI insert | [
"Active",
"Record",
"(",
"ORM",
")",
"save",
"for",
"insert",
"or",
"update"
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/src/Model.php#L1051-L1102 |
yidas/codeigniter-model | src/Model.php | Model.hasMany | public function hasMany($modelName, $foreignKey=null, $localKey=null)
{
return $this->_relationship($modelName, __FUNCTION__, $foreignKey, $localKey);
} | php | public function hasMany($modelName, $foreignKey=null, $localKey=null)
{
return $this->_relationship($modelName, __FUNCTION__, $foreignKey, $localKey);
} | [
"public",
"function",
"hasMany",
"(",
"$",
"modelName",
",",
"$",
"foreignKey",
"=",
"null",
",",
"$",
"localKey",
"=",
"null",
")",
"{",
"return",
"$",
"this",
"->",
"_relationship",
"(",
"$",
"modelName",
",",
"__FUNCTION__",
",",
"$",
"foreignKey",
",",
"$",
"localKey",
")",
";",
"}"
] | Declares a has-many relation.
@param string $modelName The model class name of the related record
@param string $foreignKey
@param string $localKey
@return object CI_DB_query_builder | [
"Declares",
"a",
"has",
"-",
"many",
"relation",
"."
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/src/Model.php#L1142-L1145 |
yidas/codeigniter-model | src/Model.php | Model.hasOne | public function hasOne($modelName, $foreignKey=null, $localKey=null)
{
return $this->_relationship($modelName, __FUNCTION__, $foreignKey, $localKey);
} | php | public function hasOne($modelName, $foreignKey=null, $localKey=null)
{
return $this->_relationship($modelName, __FUNCTION__, $foreignKey, $localKey);
} | [
"public",
"function",
"hasOne",
"(",
"$",
"modelName",
",",
"$",
"foreignKey",
"=",
"null",
",",
"$",
"localKey",
"=",
"null",
")",
"{",
"return",
"$",
"this",
"->",
"_relationship",
"(",
"$",
"modelName",
",",
"__FUNCTION__",
",",
"$",
"foreignKey",
",",
"$",
"localKey",
")",
";",
"}"
] | Declares a has-many relation.
@param string $modelName The model class name of the related record
@param string $foreignKey
@param string $localKey
@return object CI_DB_query_builder | [
"Declares",
"a",
"has",
"-",
"many",
"relation",
"."
] | train | https://github.com/yidas/codeigniter-model/blob/14cd273bb0620dc0e2993fe924ee37f00301992f/src/Model.php#L1155-L1158 |