PHP Class DiDom\Document

Datei anzeigen Open project: imangazaliev/didom Class Usage Examples

Protected Properties

Property Type Description
$document DOMDocument
$type string

Public Methods

Method Description
__construct ( string $string = null, boolean $isFile = false, string $encoding = 'UTF-8', string $type = 'html' ) Constructor.
__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.
__toString ( ) : string Convert the document to its string representation.
appendChild ( Element | DOMNode | array $nodes ) : Document Add new child at the end of the children.
count ( string $expression, string $type = Query::TYPE_CSS ) : integer Counts nodes for a given XPath expression or a CSS selector.
create ( string $string = null, boolean $isFile = false, string $encoding = 'UTF-8', string $type = 'html' ) : Document Create new document.
createElement ( string $name, string $value = null, array $attributes = [] ) : Element Create new element node.
createElementBySelector ( 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.
format ( boolean $format = true ) : Document Nicely formats output with indentation and extra space.
getDocument ( ) : DOMDocument
getElement ( ) : DOMElement
getType ( ) : string Returns the type of document (XML or HTML).
has ( string $expression, string $type = Query::TYPE_CSS ) : boolean Checks the existence of the node.
html ( integer $options = LIBXML_NOEMPTYTAG ) : string Dumps the internal document into a string using HTML formatting.
is ( Document | DOMDocument $document ) : boolean Indicates if two documents are the same document.
load ( string $string, boolean $isFile = false, string $type = 'html', integer $options ) Load HTML or XML.
loadHtml ( string $html, integer $options ) : Document Load HTML from a string.
loadHtmlFile ( string $filepath, integer $options ) : Document Load HTML from a file.
loadXml ( string $xml, integer $options ) : Document Load XML from a string.
loadXmlFile ( string $filepath, integer $options ) : Document Load XML from a file.
preserveWhiteSpace ( boolean $value = true ) : Document Set preserveWhiteSpace property.
text ( ) : string Get the text content of this node and its descendants.
toElement ( ) : Element
xml ( integer $options ) : string Dumps the internal document 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
loadFile ( string $filepath ) : strting Reads entire file into a string.
wrapNode ( $node )

Method Details

__construct() public method

Constructor.
public __construct ( string $string = null, boolean $isFile = false, string $encoding = 'UTF-8', string $type = 'html' )
$string string HTML or XML string or file path
$isFile boolean Indicates that in first parameter was passed to the file path
$encoding string The document encoding
$type string The document type

__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[]

__toString() public method

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

appendChild() public method

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

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 document.
public static create ( string $string = null, boolean $isFile = false, string $encoding = 'UTF-8', string $type = 'html' ) : Document
$string string HTML or XML string or file path
$isFile boolean Indicates that in first parameter was passed to the file path
$encoding string The document encoding
$type string The document type
return Document

createElement() public method

Create new element node.
public createElement ( string $name, string $value = null, array $attributes = [] ) : Element
$name string The tag name of the element
$value string The value of the element
$attributes array The attributes of the element
return Element created element

createElementBySelector() public method

Create new element node by CSS selector.
public createElementBySelector ( 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

format() public method

Nicely formats output with indentation and extra space.
public format ( boolean $format = true ) : Document
$format boolean Formats output if true
return Document

getDocument() public method

public getDocument ( ) : DOMDocument
return DOMDocument

getElement() public method

public getElement ( ) : DOMElement
return DOMElement

getType() public method

Returns the type of document (XML or HTML).
public getType ( ) : string
return string

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

html() public method

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

is() public method

Indicates if two documents are the same document.
public is ( Document | DOMDocument $document ) : boolean
$document Document | DOMDocument The compared document
return boolean

load() public method

Load HTML or XML.
public load ( string $string, boolean $isFile = false, string $type = 'html', integer $options )
$string string HTML or XML string or file path
$isFile boolean Indicates that in first parameter was passed to the file path
$type string Type of document
$options integer Additional parameters

loadFile() protected method

Reads entire file into a string.
protected loadFile ( string $filepath ) : strting
$filepath string The path to the file
return strting

loadHtml() public method

Load HTML from a string.
public loadHtml ( string $html, integer $options ) : Document
$html string The HTML string
$options integer Additional parameters
return Document

loadHtmlFile() public method

Load HTML from a file.
public loadHtmlFile ( string $filepath, integer $options ) : Document
$filepath string The path to the HTML file
$options integer Additional parameters
return Document

loadXml() public method

Load XML from a string.
public loadXml ( string $xml, integer $options ) : Document
$xml string The XML string
$options integer Additional parameters
return Document

loadXmlFile() public method

Load XML from a file.
public loadXmlFile ( string $filepath, integer $options ) : Document
$filepath string The path to the XML file
$options integer Additional parameters
return Document

preserveWhiteSpace() public method

Set preserveWhiteSpace property.
public preserveWhiteSpace ( boolean $value = true ) : Document
$value boolean
return Document

text() public method

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

toElement() public method

public toElement ( ) : Element
return Element

wrapNode() protected method

protected wrapNode ( $node )

xml() public method

Dumps the internal document into a string using XML formatting.
public xml ( integer $options ) : string
$options integer Additional options
return string The document 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

$document protected_oe property

protected DOMDocument $document
return DOMDocument

$type protected_oe property

protected string $type
return string