CfdiUtils represents every CFDI document — and every complement or addenda attached to it — as an in-memory tree ofDocumentation 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.
Node objects. The tree is intentionally lightweight: each node stores only a name, a flat map of string attributes, and an ordered list of child nodes. This design mirrors the structure of CFDI XML, where all meaningful data lives in element attributes and element nesting, not in text content.
The Node and Nodes classes live in the CfdiUtils\Nodes namespace and are the foundation that every other CfdiUtils component — validators, creators, and element builders — builds on top of.
The Node Class
CfdiUtils\Nodes\Node is the fundamental building block. Its constructor accepts three parameters and an optional fourth:
The XML element name. Whitespace is trimmed from both ends. The name must be a valid XML element name; an invalid name throws
\UnexpectedValueException. Once set, the name is immutable.An associative array of
name => value pairs imported as the node’s initial attributes.An array of
Node (or any NodeInterface) objects imported as the initial child collection.Optional text content of the element, used by the few SAT complements that store values as text nodes rather than attributes.
name() method returns the element name as a string. You cannot rename a node after construction.
Attribute Access
Attributes are managed internally by aCfdiUtils\Nodes\Attributes collection and are exposed through PHP’s ArrayAccess interface directly on the Node object.
The access rules are:
- Read always returns a
string. Reading an attribute that does not exist returns an empty string (''), nevernull. - Write accepts a
stringor any object implementing__toString(). isset()returnstrueonly when the attribute has been explicitly set.unset()removes the attribute entirely.
attributes() method returns the underlying Attributes collection. You can also call addAttributes(array $attributes) to merge an associative array of values into the node at once.
Child Node Access
Child nodes are managed by aCfdiUtils\Nodes\Nodes collection. Iterating a Node directly (via foreach) iterates over its children — not its attributes.
Node:
| Method | Return type | Description |
|---|---|---|
addChild(NodeInterface $node) | NodeInterface | Appends $node to the children collection and returns it |
children() | Nodes | Returns the mutable Nodes collection |
count() | int | Number of direct children |
Search Methods
Node provides three recursive search helpers that traverse the child tree by element name path:
Walks the given path of element names, returning the first matching node at the end of the path, or
null if any step fails.Like
searchNode, but returns all sibling nodes matching the last name in the path, wrapped in a new Nodes collection. Adding or removing from this collection does not affect the original tree; however, mutating the returned nodes’ attributes does.Like
searchNode, but the last argument is treated as an attribute name. Returns the attribute value if found, or an empty string otherwise.The Nodes Collection
CfdiUtils\Nodes\Nodes is a typed, ordered collection of NodeInterface objects. It implements Countable and IteratorAggregate.
Core methods:
| Method | Description |
|---|---|
add(NodeInterface ...$nodes) | Adds one or more nodes; duplicates (same instance) are silently ignored |
remove(NodeInterface $node) | Removes the given node instance |
removeAll() | Clears the collection |
exists(NodeInterface $node) | Returns true if the exact instance is already in the collection |
indexOf(NodeInterface $node) | Returns the zero-based position, or -1 if not found |
get(int $position) | Returns the node at the given zero-based position |
first() | Returns the first node or null |
firstNodeWithName(string $nodeName) | Returns the first node whose name() matches, or null |
getNodesByName(string $nodeName) | Returns a new Nodes containing all nodes with the given name |
importFromArray(NodeInterface[] $nodes) | Bulk-adds an array of NodeInterface objects |
setOrder(string[] $names) | Sets the preferred child element sort order |
count() | Number of nodes |
XmlNodeUtils
CfdiUtils\Nodes\XmlNodeUtils is a static utility class for converting between XML strings/DOM objects and the Node tree.
Parses a valid XML string and returns the root
Node. Useful for loading a CFDI from a file.Serializes a
Node tree back to an XML string. Pass true for $withXmlHeader to include the <?xml ...?> declaration.Converts a
DOMElement to a Node.Converts a
Node to a DOMElement (inside an owning DOMDocument).Converts a
SimpleXMLElement to a Node.Converts a
Node to a SimpleXMLElement.Text Content (NodeHasValueInterface)
Most CFDI documents store all meaningful data in attributes. However, a small number of SAT complements — notably the Complemento de facturas del sector de ventas al detalle — store values as element text nodes.
Node implements NodeHasValueInterface, which provides:
| Method | Description |
|---|---|
value(): string | Reads the text content of the element |
setValue(string $value): void | Sets the text content of the element |