Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
# These are supported funding model platforms

github: byjg
12 changes: 8 additions & 4 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ on:
jobs:
Build:
runs-on: 'ubuntu-latest'
container: 'byjg/php:${{ matrix.php-version }}-cli'
container:
image: 'byjg/php:${{ matrix.php-version }}-cli'
options: --user root --privileged
strategy:
matrix:
php-version:
- "8.4"
- "8.3"
- "8.2"
- "8.1"

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v5
- run: composer install
- run: ./vendor/bin/phpunit --stderr
- run: ./vendor/bin/psalm
- run: ./vendor/bin/phpunit

Documentation:
if: github.ref == 'refs/heads/master'
Expand All @@ -33,4 +36,5 @@ jobs:
with:
folder: php
project: ${{ github.event.repository.name }}
secrets: inherit
secrets:
DOC_TOKEN: ${{ secrets.DOC_TOKEN }}
8 changes: 0 additions & 8 deletions .run/Psalm.run.xml

This file was deleted.

8 changes: 8 additions & 0 deletions .run/psalm.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="psalm" type="ComposerRunConfigurationType" factoryName="Composer Script">
<option name="commandLineParameters" value="" />
<option name="pathToComposerJson" value="$PROJECT_DIR$/composer.json" />
<option name="script" value="psalm" />
<method v="2" />
</configuration>
</component>
35 changes: 35 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug current Script in Console",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9003,
"runtimeArgs": [
"-dxdebug.start_with_request=yes"
],
"env": {
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
},
{
"name": "PHPUnit Debug",
"type": "php",
"request": "launch",
"program": "${workspaceFolder}/vendor/bin/phpunit",
"cwd": "${workspaceFolder}",
"port": 9003,
"runtimeArgs": [
"-dxdebug.start_with_request=yes"
],
"env": {
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
}
]
}
86 changes: 73 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,95 @@
# XmlUtil

[![Build Status](https://github.com/byjg/php-xmlutil/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/php-xmlutil/actions/workflows/phpunit.yml)
[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/php-xmlutil/)
[![GitHub license](https://img.shields.io/github/license/byjg/php-xmlutil.svg)](https://opensource.byjg.com/opensource/licensing.html)
[![GitHub release](https://img.shields.io/github/release/byjg/php-xmlutil.svg)](https://github.com/byjg/php-xmlutil/releases/)

A utility class to make it easy work with XML in PHP
# PHP XML Util

A powerful and intuitive PHP library for working with XML documents. This utility makes XML manipulation, querying,
and conversion simple and straightforward in PHP.

## Overview

PHP XML Util provides a comprehensive set of tools for XML manipulation in PHP applications. It simplifies common
XML operations with an intuitive API, allowing developers to create, modify, query, and validate XML documents
with minimal code.

The library is designed to be lightweight yet powerful, offering features that go beyond
PHP's built-in XML functionality while maintaining a clean and easy-to-use interface.

## Examples
## Key Features

- [Create a new XML Document using the API](docs/using-api.md)
- [Working with namespaces](docs/namespaces.md)
- [Query a XMLDocument](docs/query-document.md)
- [Convert any model to XML](docs/convert-model-xml.md)
- [Use Attributes to help in the conversion](docs/convert-model-xml-withattributes.md)
- [Clean an XML document removing specific tags](docs/clean-document.md)
- **Simple XML Creation API** - Create and manipulate XML documents programmatically with an intuitive API
- **XPath Querying** - Easily query and navigate XML documents using XPath expressions
- **PHP Model ↔ XML Conversion** - Seamlessly convert between PHP objects and XML representations
- **Attribute-Based Mapping** - Use PHP attributes to control XML serialization behavior
- **Namespace Support** - Full support for XML namespaces in all operations
- **Document Cleaning** - Selectively remove specific tags from XML documents
- **XML Validation** - Validate XML documents against schemas
- **File Handling** - Convenient methods for loading and saving XML from/to files

## Install
## Quick Example

```php
<?php
use ByJG\XmlUtil\XmlDocument;

// Create a new XML document
$xml = new XmlDocument('<root />');

// Build the document structure
$myNode = $xml->appendChild('mynode');
$myNode->appendChild('subnode', 'text');
$myNode->appendChild('subnode', 'more text');
$otherNode = $myNode->appendChild('othersubnode', 'other text');
$otherNode->addAttribute('attr', 'value');

// Output formatted XML
echo $xml->toString(format: true);
```

Output:
```xml
<?xml version="1.0" encoding="utf-8"?>
<root>
<mynode>
<subnode>text</subnode>
<subnode>more text</subnode>
<othersubnode attr="value">other text</othersubnode>
</mynode>
</root>
```

## Documentation

The library is fully documented with detailed guides and examples for each feature:

- [Creating XML Documents](docs/using-api.md): Learn how to create and manipulate XML documents using the API
- [Working with Namespaces](docs/namespaces.md): Guide to handling XML namespaces properly
- [Querying with XPath](docs/query-document.md): How to use XPath expressions to query XML documents
- [PHP Models to XML](docs/convert-model-xml.md): Converting PHP objects to XML and vice versa
- [Attribute-Based Mapping](docs/convert-model-xml-withattributes.md): Using PHP attributes to control XML serialization
- [Cleaning Documents](docs/clean-document.md): Removing specific tags from XML documents
- [File Operations](docs/file-handling.md): Loading and saving XML from/to files
- [XML Validation](docs/validate-document.md): Validating XML documents against schemas

## Installation

```bash
composer require "byjg/xmlutil"
```

## Running the Tests
## Running Tests

```bash
vendor/bin/phpunit
```

## License

MIT

## Dependencies

```mermaid
Expand All @@ -38,6 +99,5 @@ flowchart TD
byjg/xmlutil --> byjg/serializer
```


----
[Open source ByJG](http://opensource.byjg.com)
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=8.1 <8.4",
"php": ">=8.1 <8.5",
"ext-dom": "*",
"ext-libxml": "*",
"ext-simplexml": "*",
"byjg/serializer": "^5.0"
"byjg/serializer": "^6.0"
},
"require-dev": {
"phpunit/phpunit": "^9.6",
"vimeo/psalm": "^5.20"
"phpunit/phpunit": "^10.5|^11.5",
"vimeo/psalm": "^5.9|^6.2"
},
"scripts": {
"test": "vendor/bin/phpunit",
"psalm": "vendor/bin/psalm"
},
"license": "MIT"
}
72 changes: 68 additions & 4 deletions docs/clean-document.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ sidebar_position: 6

# Clean Document

XmlUtil have a class for selectively remove specific marks (tags)
from the document or remove all marks.
XmlUtil provides a dedicated `CleanDocument` class for selectively removing specific tags or content from XML or HTML documents. This is useful for cleaning up documents before processing or display.

Example:
## Basic Usage

```php
```php title="Basic document cleaning example"
<?php

$document = new \ByJG\XmlUtil\CleanDocument($documentXmlOrHtml);
Expand All @@ -20,3 +19,68 @@ $document
->stripTagsExcept(['img'])
->get();
```

## Available Methods

### stripAllTags()

Removes all HTML/XML tags from the document.

```php title="Removing all tags"
$document = new \ByJG\XmlUtil\CleanDocument($html);
$plainText = $document->stripAllTags();
```

### stripTagsExcept(array $allowedTags)

Strips all HTML/XML tags except those specified in the array.

```php title="Keeping only specific tags"
$document = new \ByJG\XmlUtil\CleanDocument($html);
$cleanHtml = $document->stripTagsExcept(['p', 'div', 'span'])->get();
```

### removeContentByProperty(string $property)

Removes content from any tag that contains the specified property.

```php title="Removing tags by property"
$document = new \ByJG\XmlUtil\CleanDocument($html);
// Removes all tags containing the "style" property and their content
$cleanHtml = $document->removeContentByProperty('style')->get();
```

### removeContentByTag(string $tag, string $property = '')

Removes content from the specified tag, optionally filtering by a property.

```php title="Removing specific tags"
$document = new \ByJG\XmlUtil\CleanDocument($html);
// Removes all <script> tags and their content
$cleanHtml = $document->removeContentByTag('script')->get();

// Removes all <a> tags that have an "onclick" property
$cleanHtml = $document->removeContentByTag('a', 'onclick')->get();
```

### removeContentByTagWithoutProperty(string $tag, string $property)

Removes content from the specified tag that does NOT have the specified property.

```php title="Removing tags without specific property"
$document = new \ByJG\XmlUtil\CleanDocument($html);
// Removes all <a> tags that don't have an "href" property
$cleanHtml = $document->removeContentByTagWithoutProperty('a', 'href')->get();
```

### get()

Returns the cleaned document as a string.

```php title="Getting the cleaned result"
$document = new \ByJG\XmlUtil\CleanDocument($html);
// Apply cleaning operations
$document->removeContentByTag('script');
// Get the final result
$cleanHtml = $document->get();
```
Loading