TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/eclipxe13/CfdiUtils/llms.txt
Use this file to discover all available pages before exploring further.
CfdiUtils\Cfdi class is the primary entry point for reading Mexican electronic invoice (CFDI) documents in PHP. It parses an XML document and converts it into an internal Nodes structure — a structured, traversable tree of NodeInterface objects — making it straightforward to extract data from CFDI 3.2, 3.3, and 4.0 files. No validation is performed at read time; the class only checks that the document is a recognisable CFDI.
The Cfdi Class
The CfdiUtils\Cfdi class supports CFDI versions 3.2, 3.3, and 4.0. When you construct an instance it converts the raw XML into the internal Nodes data structure. All navigation, attribute lookup, and child traversal happens through the resulting NodeInterface object returned by getNode().
The
Cfdi class only reads documents — it does not validate them. Use the dedicated validation classes for CFDI validation.Create a Cfdi Object
The recommended way to create a Cfdi instance is through the static factory method Cfdi::newFromString(). Pass the raw XML string, and the method returns a fully initialised Cfdi object.
| Method | Return type | Description |
|---|---|---|
getVersion() | string | Detected version string: "3.2", "3.3", or "4.0" |
getDocument() | DOMDocument | A clone of the internal DOMDocument |
getSource() | string | The original XML source string |
getNode() | NodeInterface | Root node for the cfdi:Comprobante element |
Constructor Validation Rules
When aCfdi object is constructed (either directly or via newFromString()), the following rules are applied to the DOMDocument:
- Namespace — The document must implement the CFDI namespace
http://www.sat.gob.mx/cfd/3(versions 3.2 and 3.3) orhttp://www.sat.gob.mx/cfd/4(version 4.0). - Namespace prefix — The CFDI namespace must be bound to the prefix
cfdi. - Root element name — The root element must be named
cfdi:Comprobante. - Version attribute — The
Versionattribute on the root element must match the namespace in use.
CfdiCreateObjectException is thrown listing the individual exceptions for each attempted version.
The constructor performs structural checks only — it does not validate business rules or SAT compliance. Construct a
Cfdi object to read the document; use the validator classes to verify it.Navigating Nodes
Once you have the rootNodeInterface from getNode(), you can read attributes, check for optional fields, and traverse child nodes.
Read an Attribute
Access any attribute by name using array notation. If the attribute is absent, an empty string is returned — no exception is thrown.Check Whether an Attribute is Set
Useisset() to distinguish between an absent attribute and one explicitly set to an empty value.
Find All Matching Child Nodes
UsesearchNodes() to retrieve all descendants matching a path of node names. It returns an iterable collection.
Find the First Matching Child Node
UsesearchNode() to find the first descendant matching a path. Returns the node or null if not found.
searchNode() (singular) returns a single NodeInterface|null. searchNodes() (plural) returns a collection. Do not confuse the two.Working with Complementos
Thecfdi:Complemento node holds the Timbre Fiscal Digital (stamp) and any other complements added by the PAC. Reach it via searchNode() and then navigate to the specific complement by its prefixed name.
Version Detection
When you already have the XML content but want to check the version before constructing aCfdi object, use CfdiUtils\CfdiVersion. This is useful if your application needs to route different versions to different processing paths.
| Method | Use when you have… |
|---|---|
getFromXmlString(string $xml) | Raw XML string |
getFromNode(NodeInterface $node) | An existing NodeInterface |
getFromDOMDocument(DOMDocument $doc) | A loaded DOMDocument |
getFromDOMElement(DOMElement $el) | A DOMElement |
"3.3") or an empty string if no supported version is detected.
Cfdi::newFromString() performs version detection internally. Call CfdiVersion directly only when you need the version without constructing the full Cfdi object.Cleaning Before Reading
CFDI files received from third parties often contain minor XML errors introduced after signing. Run the cleaner before constructing theCfdi object to avoid parsing failures:
Quick Read Alternative
If you only need to read data — not edit the document or validate its structure — consider usinggetQuickReader() for a simpler, case-insensitive navigation API.