You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.5 KiB
53 lines
1.5 KiB
3 years ago
|
<?php
|
||
|
/**
|
||
|
* @license MIT
|
||
|
* Copyright 2017 Dustin Wilson, J. King, et al.
|
||
|
* See LICENSE and AUTHORS files for details
|
||
|
*/
|
||
|
|
||
|
declare(strict_types=1);
|
||
3 years ago
|
namespace MensBeam\HTML\DOM\Inner;
|
||
3 years ago
|
use MensBeam\HTML\DOM\Node as WrapperNode;
|
||
|
|
||
|
|
||
3 years ago
|
class NodeCache {
|
||
3 years ago
|
protected $wrapperArray = [];
|
||
|
protected $innerArray = [];
|
||
|
|
||
|
|
||
3 years ago
|
public function delete(\DOMNode|WrapperNode $node): void {
|
||
3 years ago
|
$key = $this->key($node);
|
||
3 years ago
|
if ($key !== false) {
|
||
|
unset($this->wrapperArray[$key]);
|
||
|
unset($this->innerArray[$key]);
|
||
|
$this->wrapperArray = array_values($this->wrapperArray);
|
||
|
$this->innerArray = array_values($this->innerArray);
|
||
3 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
public function get(\DOMNode|WrapperNode $node): \DOMNode|WrapperNode|null {
|
||
3 years ago
|
$key = $this->key($node);
|
||
|
if ($key === false) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
return ($node instanceof WrapperNode) ? $this->innerArray[$key] : $this->wrapperArray[$key];
|
||
|
}
|
||
|
|
||
|
public function has(\DOMNode|WrapperNode $node): bool {
|
||
|
return ($this->key($node) !== false);
|
||
|
}
|
||
|
|
||
3 years ago
|
public function set(WrapperNode $wrapper, \DOMNode $inner): void {
|
||
3 years ago
|
if (!$this->has($wrapper)) {
|
||
|
$this->wrapperArray[] = $wrapper;
|
||
|
$this->innerArray[] = $inner;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
protected function key(\DOMNode|WrapperNode $node): int|false {
|
||
|
return array_search($node, ($node instanceof WrapperNode) ? $this->wrapperArray : $this->innerArray, true);
|
||
|
}
|
||
|
}
|