PHP Class DiDom\Element

显示文件 Open project: imangazaliev/didom Class Usage Examples

Protected Properties

Property Type Description
$node DOMNode; The DOM element instance.

Public Methods

Method Description
__construct ( DOMNode | string $name, string $value = null, array $attributes = [] ) Constructor.
__get ( string $name ) : string | null Dynamically access the element's attributes.
__invoke ( string $expression, string $type = Query::TYPE_CSS, boolean $wrapElement = true ) : Element[] | DOMElement[] Searches for an node in the DOM tree for a given XPath expression or a CSS selector.
__isset ( string $name ) : boolean Determine if an attribute exists on the element.
__set ( string $name, mixed $value ) : Element Dynamically set an attribute on the element.
__toString ( ) : string Convert the element to its string representation.
__unset ( string $name ) Unset an attribute on the model.
appendChild ( Element | DOMNode | array $nodes ) : Element Adds new child at the end of the children.
attr ( string $name, string $value = null ) : string | null | Element Alias for getAttribute and setAttribute methods.
attributes ( ) : array | null Returns the node attributes or null, if it is not DOMElement.
child ( $index ) : Element | null
children ( ) : Element[]
cloneNode ( boolean $deep = true ) : Element Clones a node.
closest ( string $selector, boolean $strict = false ) : Element | null Returns first parent node matches passed selector.
count ( string $expression, string $type = Query::TYPE_CSS ) : integer Counts nodes for a given XPath expression or a CSS selector.
create ( DOMNode | string $name, string $value = null, array $attributes = [] ) : Element Create new element.
createBySelector ( string $selector, string $value = null, array $attributes = [] ) : Element Create new element node by CSS selector.
find ( string $expression, string $type = Query::TYPE_CSS, boolean $wrapElement = true ) : Element[] | DOMElement[] Searches for an node in the DOM tree for a given XPath expression or a CSS selector.
first ( string $expression, string $type = Query::TYPE_CSS, boolean $wrapElement = true ) : Element | DOMElement | null Searches for an node in the DOM tree and returns first element or null.
firstChild ( ) : Element | null
getAttribute ( string $name, string $default = null ) : string | null Access to the element's attributes.
getDocument ( ) : Document | null Returns the document associated with this node.
getLineNo ( ) : integer Get line number for a node.
getNode ( ) : DOMNode Get current \DOMNode instance.
has ( string $expression, string $type = Query::TYPE_CSS ) : boolean Checks the existence of the node.
hasAttribute ( string $name ) : boolean Determine if an attribute exists on the element.
html ( integer $options = LIBXML_NOEMPTYTAG ) : string Dumps the node into a string using HTML formatting.
innerHtml ( integer $options = LIBXML_NOEMPTYTAG, sting $delimiter = '' ) : string Dumps the node descendants into a string using HTML formatting.
is ( Element | DOMNode $node ) : boolean Indicates if two nodes are the same node.
isCommentNode ( ) : boolean Returns true if current node is comment.
isTextNode ( ) : boolean Returns true if current node is text.
lastChild ( ) : Element | null
matches ( string $selector, boolean $strict = false ) : boolean Checks that the node matches selector.
nextSibling ( ) : Element | null
parent ( ) : Element | Document | null
previousSibling ( ) : Element | null
remove ( ) : Element Removes child from list of children.
removeAttribute ( string $name ) : Element Unset an attribute on the element.
replace ( $newNode, boolean $clone = true ) : Element Replaces a child.
setAttribute ( string $name, string $value ) : Element Set an attribute on the element.
setInnerHtml ( string $html ) : Element Sets inner HTML.
setValue ( string $value ) : Element Set the value of this node.
text ( ) : string Get the text content of this node and its descendants.
toDocument ( string $encoding = 'UTF-8' ) : Document Get the DOM document with the current element.
xml ( integer $options ) : string Dumps the node into a string using XML formatting.
xpath ( string $expression, boolean $wrapElement = true ) : Element[] | DOMElement[] Searches for an node in the DOM tree for a given XPath expression.

Protected Methods

Method Description
setNode ( DOMElement | DOMText | DOMComment $node ) : Element Sets current \DOMNode instance.

Method Details

__construct() public method

Constructor.
public __construct ( DOMNode | string $name, string $value = null, array $attributes = [] )
$name DOMNode | string The tag name of the element
$value string The value of the element
$attributes array The attributes of the element

__get() public method

Dynamically access the element's attributes.
public __get ( string $name ) : string | null
$name string The attribute name
return string | null

__invoke() public method

Searches for an node in the DOM tree for a given XPath expression or a CSS selector.
public __invoke ( string $expression, string $type = Query::TYPE_CSS, boolean $wrapElement = true ) : Element[] | DOMElement[]
$expression string XPath expression or a CSS selector
$type string The type of the expression
$wrapElement boolean Returns array of \DiDom\Element if true, otherwise array of \DOMElement
return Element[] | DOMElement[]

__isset() public method

Determine if an attribute exists on the element.
public __isset ( string $name ) : boolean
$name string The attribute name
return boolean

__set() public method

Dynamically set an attribute on the element.
public __set ( string $name, mixed $value ) : Element
$name string The attribute name
$value mixed The attribute value
return Element

__toString() public method

Convert the element to its string representation.
public __toString ( ) : string
return string

__unset() public method

Unset an attribute on the model.
public __unset ( string $name )
$name string The attribute name

appendChild() public method

Adds new child at the end of the children.
public appendChild ( Element | DOMNode | array $nodes ) : Element
$nodes Element | DOMNode | array The appended child
return Element

attr() public method

Alias for getAttribute and setAttribute methods.
public attr ( string $name, string $value = null ) : string | null | Element
$name string The attribute name
$value string The attribute value or null if the attribute does not exist
return string | null | Element

attributes() public method

Returns the node attributes or null, if it is not DOMElement.
public attributes ( ) : array | null
return array | null

child() public method

public child ( $index ) : Element | null
return Element | null

children() public method

public children ( ) : Element[]
return Element[]

cloneNode() public method

Clones a node.
public cloneNode ( boolean $deep = true ) : Element
$deep boolean Indicates whether to copy all descendant nodes
return Element The cloned node

closest() public method

Returns first parent node matches passed selector.
public closest ( string $selector, boolean $strict = false ) : Element | null
$selector string
$strict boolean
return Element | null

count() public method

Counts nodes for a given XPath expression or a CSS selector.
public count ( string $expression, string $type = Query::TYPE_CSS ) : integer
$expression string XPath expression or CSS selector
$type string The type of the expression
return integer

create() public static method

Create new element.
public static create ( DOMNode | string $name, string $value = null, array $attributes = [] ) : Element
$name DOMNode | string The tag name of the element
$value string The value of the element
$attributes array The attributes of the element
return Element

createBySelector() public static method

Create new element node by CSS selector.
public static createBySelector ( string $selector, string $value = null, array $attributes = [] ) : Element
$selector string
$value string
$attributes array
return Element

find() public method

Searches for an node in the DOM tree for a given XPath expression or a CSS selector.
public find ( string $expression, string $type = Query::TYPE_CSS, boolean $wrapElement = true ) : Element[] | DOMElement[]
$expression string XPath expression or a CSS selector
$type string The type of the expression
$wrapElement boolean Returns array of \DiDom\Element if true, otherwise array of \DOMElement
return Element[] | DOMElement[]

first() public method

Searches for an node in the DOM tree and returns first element or null.
public first ( string $expression, string $type = Query::TYPE_CSS, boolean $wrapElement = true ) : Element | DOMElement | null
$expression string XPath expression or a CSS selector
$type string The type of the expression
$wrapElement boolean Returns \DiDom\Element if true, otherwise \DOMElement
return Element | DOMElement | null

firstChild() public method

public firstChild ( ) : Element | null
return Element | null

getAttribute() public method

Access to the element's attributes.
public getAttribute ( string $name, string $default = null ) : string | null
$name string The attribute name
$default string The value returned if the attribute does not exist
return string | null The value of the attribute or null if attribute does not exist

getDocument() public method

Returns the document associated with this node.
public getDocument ( ) : Document | null
return Document | null

getLineNo() public method

Get line number for a node.
public getLineNo ( ) : integer
return integer

getNode() public method

Get current \DOMNode instance.
public getNode ( ) : DOMNode
return DOMNode

has() public method

Checks the existence of the node.
public has ( string $expression, string $type = Query::TYPE_CSS ) : boolean
$expression string XPath expression or CSS selector
$type string The type of the expression
return boolean

hasAttribute() public method

Determine if an attribute exists on the element.
public hasAttribute ( string $name ) : boolean
$name string The attribute name
return boolean

html() public method

Dumps the node into a string using HTML formatting.
public html ( integer $options = LIBXML_NOEMPTYTAG ) : string
$options integer Additional options
return string The node HTML

innerHtml() public method

Dumps the node descendants into a string using HTML formatting.
public innerHtml ( integer $options = LIBXML_NOEMPTYTAG, sting $delimiter = '' ) : string
$options integer Additional options
$delimiter sting
return string

is() public method

Indicates if two nodes are the same node.
public is ( Element | DOMNode $node ) : boolean
$node Element | DOMNode
return boolean

isCommentNode() public method

Returns true if current node is comment.
public isCommentNode ( ) : boolean
return boolean

isTextNode() public method

Returns true if current node is text.
public isTextNode ( ) : boolean
return boolean

lastChild() public method

public lastChild ( ) : Element | null
return Element | null

matches() public method

Checks that the node matches selector.
public matches ( string $selector, boolean $strict = false ) : boolean
$selector string CSS selector
$strict boolean
return boolean

nextSibling() public method

public nextSibling ( ) : Element | null
return Element | null

parent() public method

public parent ( ) : Element | Document | null
return Element | Document | null

previousSibling() public method

public previousSibling ( ) : Element | null
return Element | null

remove() public method

Removes child from list of children.
public remove ( ) : Element
return Element the node that has been removed

removeAttribute() public method

Unset an attribute on the element.
public removeAttribute ( string $name ) : Element
$name string The attribute name
return Element

replace() public method

Replaces a child.
public replace ( $newNode, boolean $clone = true ) : Element
$clone boolean Clone the node if true, otherwise move it
return Element The node that has been replaced

setAttribute() public method

Set an attribute on the element.
public setAttribute ( string $name, string $value ) : Element
$name string The attribute name
$value string The attribute value
return Element

setInnerHtml() public method

Sets inner HTML.
public setInnerHtml ( string $html ) : Element
$html string
return Element

setNode() protected method

Sets current \DOMNode instance.
protected setNode ( DOMElement | DOMText | DOMComment $node ) : Element
$node DOMElement | DOMText | DOMComment
return Element

setValue() public method

Set the value of this node.
public setValue ( string $value ) : Element
$value string The new value of the node
return Element

text() public method

Get the text content of this node and its descendants.
public text ( ) : string
return string The node value

toDocument() public method

Get the DOM document with the current element.
public toDocument ( string $encoding = 'UTF-8' ) : Document
$encoding string The document encoding
return Document

xml() public method

Dumps the node into a string using XML formatting.
public xml ( integer $options ) : string
$options integer Additional options
return string The node XML

xpath() public method

Searches for an node in the DOM tree for a given XPath expression.
public xpath ( string $expression, boolean $wrapElement = true ) : Element[] | DOMElement[]
$expression string XPath expression
$wrapElement boolean Returns array of \DiDom\Element if true, otherwise array of \DOMElement
return Element[] | DOMElement[]

Property Details

$node protected_oe property

The DOM element instance.
protected DOMNode; $node
return DOMNode;