<?php
class DOMLogger
{
private $log = []; // Array to store log entries
public function __construct()
{
// Initialize the log array
}
/**
* Logs an operation performed on a DOM element.
* @param DOMElement $element The DOM element being operated on.
* @param string $operation The operation performed (e.g., 'setAttribute', 'addClass', 'removeChild').
* @param mixed $value The value used in the operation (if applicable).
* @return bool True on success, false on failure.
*/
public function logOperation(DOMElement $element, string $operation, $value = null): bool
{
try {
$logEntry = [
'timestamp' => date('Y-m-d H:i:s'),
'elementId' => $element->id, // Or another unique identifier
'operation' => $operation,
'value' => $value,
'details' => $element->outerHTML // Store the element's HTML content
];
$this->log[] = $logEntry; // Add the log entry to the array
return true; // Operation successful
} catch (Exception $e) {
error_log("DOMLogger: Operation '$operation' failed on element with id '$element->id'. Error: " . $e->getMessage());
return false; // Operation failed
}
}
/**
* Clears the log.
*/
public function clearLog(): void
{
$this->log = [];
}
/**
* Returns the log entries.
* @return array The log entries.
*/
public function getLog(): array
{
return $this->log;
}
}
// Example Usage (Dummy DOMElement for demonstration)
class DOMElement implements DOMDocumentInterface {
public $id;
public $outerHTML;
public function __construct(string $id, string $outerHTML) {
$this->id = $id;
$this->outerHTML = $outerHTML;
}
public function setId(string $id) {
$this->id = $id;
return $this;
}
public function getNativeElement() {
return null;
}
public function getParentNode() {
return null;
}
public function getChildNodes() {
return [];
}
public function appendChild(DOMElement $node) {
return true;
}
public function removeChild(DOMElement $node) {
return true;
}
public function hasChildNodes() {
return false;
}
public function createElement(string $tag) {
return new DOMElement('dummy', '');
}
public function setAttribute(string $name, string $value) {
$this->outerHTML = str_replace($name."=\"".$value, $name."=\"".$value, $this->outerHTML);
return true;
}
public function addClass(string $className) {
$this->outerHTML = str_replace('class="', 'class="' . $className . '"', $this->outerHTML);
return true;
}
public function removeClass(string $className) {
$this->outerHTML = str_replace('class="' . $className . '"', '', $this->outerHTML);
return true;
}
}
interface DOMDocumentInterface {
public function setId(string $id): self;
public function getNativeElement(): ?DOMDocument;
public function getParentNode(): ?DOMNode;
public function getChildNodes(): DOMNodeList;
public function appendChild(DOMNode $node): bool;
public function removeChild(DOMNode $node): bool;
public function hasChildNodes(): bool;
public function createElement(string $tag): DOMNode;
public function setAttribute(string $name, string $value): bool;
public function addClass(string $className): bool;
public function removeClass(string $className): bool;
}
interface DOMNode {
}
interface DOMNodeList {
}
?>
Add your comment