The XMLCollection class provides a powerful interface for working with imported XML data. It implements ArrayAccess, Countable, IteratorAggregate, and JsonSerializable interfaces.
Constructor
new XMLCollection(XMLElement $items)
The XML element to wrap in a collection.
You typically don’t instantiate XMLCollection directly. Use XML::import() instead.
Core Methods
raw()
Get the raw, unprocessed XML data.
public function raw(): XMLElement
Returns the original XMLElement instance.
$xml = XML::import('data.xml');
$rawElement = $xml->raw();
collect()
Convert the XML data to a Laravel Collection.
public function collect(): Collection
Returns an Illuminate\Support\Collection instance.
$collection = $xml->collect();
$filtered = $collection->filter(fn($item) => $item->active);
get()
Get the processed XML data with transformations applied.
public function get(): XMLObject|array
Returns the processed data as XMLObject or array.
$data = $xml->transform('users')->with('App\Transformers\UserTransformer')
->get();
getAsArray()
Get the XML data as a plain array.
public function getAsArray(): array
Returns the data as a plain PHP array.
$array = $xml->getAsArray();
toArray()
Convert the collection to a plain array with nested conversions.
public function toArray(): array
Returns the collection as an array with all nested items converted.
$plainArray = $xml->toArray();
toJson()
Convert the collection to JSON.
public function toJson(int $options = 0): string
JSON encoding options (same as json_encode()).
Returns the JSON string representation.
$json = $xml->toJson(JSON_PRETTY_PRINT);
Start a transformation for a specific key.
public function transform(string $key): PendingTransform
Returns a PendingTransform instance for chaining.
$xml->transform('users')
->with('App\Transformers\UserTransformer')
->get();
// Using callable
$xml->transform('price')
->with(fn($value) => (float) $value)
->get();
expect()
Alias for the transform() method.
public function expect(string $key): PendingTransform
Returns a PendingTransform instance.
$xml->expect('users')
->with('App\Transformers\UserTransformer');
cast()
Cast a key to a specific class or type.
public function cast(string $key): PendingCast
Returns a PendingCast instance for chaining.
$xml->cast('user')->to(User::class);
$xml->cast('users')->to(User::class); // Works with arrays too
Optimization Methods
optimize()
Apply key name optimization to the XML data.
public function optimize(string $type = XML::OPTIMIZE_UNDERSCORE): self
type
string
default:"XML::OPTIMIZE_UNDERSCORE"
The optimization type. Options:
XML::OPTIMIZE_UNDERSCORE - Convert to snake_case
XML::OPTIMIZE_CAMELCASE - Convert to camelCase
XML::OPTIMIZE_NONE - No transformation
Returns the collection instance for chaining.
use Flowgistics\XML\XML;
// Convert keys to snake_case
$xml->optimize(XML::OPTIMIZE_UNDERSCORE)->get();
// Convert keys to camelCase
$xml->optimize(XML::OPTIMIZE_CAMELCASE)->get();
// No optimization
$xml->optimize(XML::OPTIMIZE_NONE)->get();
Optimization is applied recursively to all nested keys in the XML structure.
Array Access Methods
XMLCollection implements ArrayAccess, allowing you to use array syntax:
Array Access Examples
// Check if key exists
if (isset($xml['users'])) {
// Key exists
}
// Get value
$users = $xml['users'];
// Set value
$xml['users'] = $newUsers;
// Unset value
unset($xml['users']);
offsetExists()
public function offsetExists(mixed $offset): bool
offsetGet()
public function offsetGet(mixed $offset): mixed
offsetSet()
public function offsetSet(mixed $offset, mixed $value): void
offsetUnset()
public function offsetUnset(mixed $offset): void
Magic Methods
__get()
Get an item from the XML using property access.
public function __get(string $key): mixed
__set()
Set an item in the XML using property access.
public function __set(string $name, mixed $value): void
__isset()
Check if an item exists using property access.
public function __isset(string $name): bool
if (isset($xml->users)) {
// Property exists
}
__call()
Forward method calls to the underlying XMLObject.
public function __call(string $name, array $arguments): mixed
// Any method called on XMLCollection is forwarded to XMLObject
$xml->customMethod();
Countable Interface
count()
Count the number of items in the collection.
public function count(): int
Returns the number of items.
$itemCount = count($xml);
// or
$itemCount = $xml->count();
IteratorAggregate Interface
getIterator()
Get an iterator for the items.
public function getIterator(): ArrayIterator
Returns an ArrayIterator instance.
foreach ($xml as $key => $value) {
// Iterate through items
}
Complete Example
use Flowgistics\XML\Facades\XML;
use App\Transformers\UserTransformer;
use App\Models\User;
// Import and process XML
$xml = XML::import('users.xml')
->optimize(XML::OPTIMIZE_UNDERSCORE)
->transform('users')
->with(UserTransformer::class)
->cast('created_at')
->to('datetime');
// Access data
$users = $xml->users;
// Convert to array
$array = $xml->toArray();
// Convert to JSON
$json = $xml->toJson(JSON_PRETTY_PRINT);
// Iterate
foreach ($xml as $key => $value) {
echo "$key: $value";
}
// Count items
echo count($xml);