talesoft/tale-dom

XML and HTML manipulation

0.1 2016-05-20 22:09 UTC

This package is auto-updated.

Last update: 2024-04-10 07:24:48 UTC


README

A Tale Framework Component

What is Tale Dom?

Installation

Install via Composer

composer require "talesoft/tale-dom:*"
composer install

Usage

Parsing

use Tale\Dom;

$element = Dom::fromString('<h1>Hello World!</h1>');

var_dump($element->getName()); //h1
var_dump($element->getText()); //Hello World!

Manipulation

use Tale\Dom;

$m = Dom::manipulate('<config />');

$m->append('db')
    ->append('host')->setText('localhost')
    ->after('password')->setText('12345')
    ->parent
  ->after('logging')
    ->append('adapter')->setText('file')
    ->append('path')->setText('./errors.log')
    
echo $m; //<config><db><host>localhost</host><password>12345</password>...</config>

...or shorter...

use Tale\Dom;

$m = Dom::manipulate('
<config>
    <db>
        <host />
        <password />
    </db>
    <logging>
        <adapter />
        <path id="logPath" />
    </logging>
</config>');

$m->query('host')->setText('localhost');
$m->query('db > password')->setText('12345');
$m->query('logging adapter')->setText('file');
$m->query('#logPath')->setText('./errors.log');
    
echo $m; //<config><db><host>localhost</host><password>12345</password>...</config>

...or even shorter...

use Tale\Dom;

$m = Dom::manipulate([
    'config' => [
        'host' => 'localhost',
        'password' => '12345'
    ],
    'logging' => [
        'adapter' => 'file',
        'path#logPath'
    ]
]);

$m->query('#logPath')->setText('./errors.log');
    
echo $m; //<config><db><host>localhost</host><password>12345</password>...</config>

Dumping

use Tale\Dom;

$element = Dom::fromString([
    'html' => [
        'head' => [
            'meta[charset="utf-8"]',
            'title' => 'My awesome Tale Dom Website!'
        ]
    ]
]);

$prettyFormatter = new Dom\Formatter(['pretty' => true]);
$htmlFormatter = new Dom\Html\Formatter(['pretty' => true]);

echo $element; //<html><head><meta charset="utf-8" /><title>...</html>

echo $element->getString($prettyFormatter);
/*
<html>
  <head>
    <meta charset="utf-8" />
    <title>My awesome Tale Dom Website!</title>
    ...
</html>
*/


echo $element->getString($htmlFormatter);
/*
<html>
  <head>
    <meta charset="utf-8">
    <title>My awesome Tale Dom Website!</title>
    ...
</html>
*/