1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\SweetDOM |
5
|
|
|
* @copyright Copyright (c) The s9e authors |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
7
|
|
|
*/ |
8
|
|
|
namespace s9e\SweetDOM\NodeTraits; |
9
|
|
|
|
10
|
|
|
use BadMethodCallException; |
11
|
|
|
use DOMNode; |
12
|
|
|
use function is_callable, method_exists, preg_match; |
13
|
|
|
|
14
|
|
|
trait MagicMethods |
15
|
|
|
{ |
16
|
|
|
public function __call(string $name, array $arguments): DOMNode |
17
|
|
|
{ |
18
|
|
|
if (!preg_match('(^(after|append|before|prepend|replaceWith)(\\w+)$)D', $name, $m)) |
19
|
|
|
{ |
20
|
|
|
// Use is_callable() to set $callableName |
21
|
|
|
is_callable([$this, $name], true, $callableName); |
22
|
|
|
|
23
|
|
|
throw new BadMethodCallException('Call to undefined method ' . $callableName . '()'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$action = $m[1]; |
27
|
|
|
$actionCallback = [$this, $action]; |
28
|
|
|
$nodeCallback = [$this->ownerDocument->nodeCreator, 'create' . $m[2]]; |
29
|
|
|
if (!method_exists($this, $action)) |
30
|
|
|
{ |
31
|
|
|
is_callable([$this, $name], true, $callableName); |
32
|
|
|
is_callable($actionCallback, true, $dependentName); |
33
|
|
|
|
34
|
|
|
throw new BadMethodCallException('Call to unsupported method ' . $callableName . '() dependent of ' . $dependentName . '()'); |
35
|
|
|
} |
36
|
|
|
if (!is_callable($nodeCallback, false, $dependentName)) |
37
|
|
|
{ |
38
|
|
|
is_callable([$this, $name], true, $callableName); |
39
|
|
|
|
40
|
|
|
throw new BadMethodCallException('Call to unsupported method ' . $callableName . '() dependent of ' . $dependentName . '()'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$node = $nodeCallback(...$arguments); |
44
|
|
|
$actionCallback($node); |
45
|
|
|
|
46
|
|
|
return match ($action) |
47
|
|
|
{ |
48
|
|
|
'after' => $this->nextSibling, |
49
|
|
|
'append' => $this->lastChild, |
50
|
|
|
'before' => $this->previousSibling, |
51
|
|
|
'prepend' => $this->firstChild, |
52
|
|
|
'replaceWith' => $node |
53
|
|
|
}; |
54
|
|
|
} |
55
|
|
|
} |