1. <?php
  2. class DOMLogger
  3. {
  4. private $log = []; // Array to store log entries
  5. public function __construct()
  6. {
  7. // Initialize the log array
  8. }
  9. /**
  10. * Logs an operation performed on a DOM element.
  11. * @param DOMElement $element The DOM element being operated on.
  12. * @param string $operation The operation performed (e.g., 'setAttribute', 'addClass', 'removeChild').
  13. * @param mixed $value The value used in the operation (if applicable).
  14. * @return bool True on success, false on failure.
  15. */
  16. public function logOperation(DOMElement $element, string $operation, $value = null): bool
  17. {
  18. try {
  19. $logEntry = [
  20. 'timestamp' => date('Y-m-d H:i:s'),
  21. 'elementId' => $element->id, // Or another unique identifier
  22. 'operation' => $operation,
  23. 'value' => $value,
  24. 'details' => $element->outerHTML // Store the element's HTML content
  25. ];
  26. $this->log[] = $logEntry; // Add the log entry to the array
  27. return true; // Operation successful
  28. } catch (Exception $e) {
  29. error_log("DOMLogger: Operation '$operation' failed on element with id '$element->id'. Error: " . $e->getMessage());
  30. return false; // Operation failed
  31. }
  32. }
  33. /**
  34. * Clears the log.
  35. */
  36. public function clearLog(): void
  37. {
  38. $this->log = [];
  39. }
  40. /**
  41. * Returns the log entries.
  42. * @return array The log entries.
  43. */
  44. public function getLog(): array
  45. {
  46. return $this->log;
  47. }
  48. }
  49. // Example Usage (Dummy DOMElement for demonstration)
  50. class DOMElement implements DOMDocumentInterface {
  51. public $id;
  52. public $outerHTML;
  53. public function __construct(string $id, string $outerHTML) {
  54. $this->id = $id;
  55. $this->outerHTML = $outerHTML;
  56. }
  57. public function setId(string $id) {
  58. $this->id = $id;
  59. return $this;
  60. }
  61. public function getNativeElement() {
  62. return null;
  63. }
  64. public function getParentNode() {
  65. return null;
  66. }
  67. public function getChildNodes() {
  68. return [];
  69. }
  70. public function appendChild(DOMElement $node) {
  71. return true;
  72. }
  73. public function removeChild(DOMElement $node) {
  74. return true;
  75. }
  76. public function hasChildNodes() {
  77. return false;
  78. }
  79. public function createElement(string $tag) {
  80. return new DOMElement('dummy', '');
  81. }
  82. public function setAttribute(string $name, string $value) {
  83. $this->outerHTML = str_replace($name."=\"".$value, $name."=\"".$value, $this->outerHTML);
  84. return true;
  85. }
  86. public function addClass(string $className) {
  87. $this->outerHTML = str_replace('class="', 'class="' . $className . '"', $this->outerHTML);
  88. return true;
  89. }
  90. public function removeClass(string $className) {
  91. $this->outerHTML = str_replace('class="' . $className . '"', '', $this->outerHTML);
  92. return true;
  93. }
  94. }
  95. interface DOMDocumentInterface {
  96. public function setId(string $id): self;
  97. public function getNativeElement(): ?DOMDocument;
  98. public function getParentNode(): ?DOMNode;
  99. public function getChildNodes(): DOMNodeList;
  100. public function appendChild(DOMNode $node): bool;
  101. public function removeChild(DOMNode $node): bool;
  102. public function hasChildNodes(): bool;
  103. public function createElement(string $tag): DOMNode;
  104. public function setAttribute(string $name, string $value): bool;
  105. public function addClass(string $className): bool;
  106. public function removeClass(string $className): bool;
  107. }
  108. interface DOMNode {
  109. }
  110. interface DOMNodeList {
  111. }
  112. ?>

Add your comment