125 lines
1.9 KiB
PHP
125 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace alveolata\xml;
|
|
|
|
require_once(DIR_ALVEOLATA . '/list/functions.php');
|
|
require_once(DIR_ALVEOLATA . '/xml/abstract/interface.php');
|
|
require_once(DIR_ALVEOLATA . '/xml/concrete/document/type.php');
|
|
|
|
|
|
/**
|
|
* @author Christian Fraß <frass@greenscale.de>
|
|
*/
|
|
class implementation_document implements interface_node
|
|
{
|
|
|
|
/**
|
|
* @var \alveolata\xml\struct_document
|
|
*/
|
|
private $subject;
|
|
|
|
|
|
/**
|
|
*/
|
|
public function __construct(
|
|
struct_document $subject
|
|
)
|
|
{
|
|
$this->subject = $subject;
|
|
}
|
|
|
|
|
|
/**
|
|
* @implementation
|
|
*/
|
|
public function find(
|
|
\Closure $sub,
|
|
\Closure $predicate,
|
|
?int $max_depth
|
|
) : array
|
|
{
|
|
return (
|
|
((! \is_null($max_depth)) && ($max_depth <= 0))
|
|
? []
|
|
: \alveolata\list_\reduce(
|
|
\alveolata\list_\map(
|
|
$this->subject->children,
|
|
fn($child) => ($sub)(
|
|
$child,
|
|
$predicate,
|
|
(\is_null($max_depth) ? null : ($max_depth - 1))
|
|
)
|
|
),
|
|
(
|
|
($predicate)($this->subject)
|
|
? [$this->subject]
|
|
: []
|
|
),
|
|
fn($x, $y) => \array_merge($x, $y)
|
|
)
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
* @implementation
|
|
*/
|
|
public function transform(
|
|
\Closure $sub,
|
|
\Closure $function
|
|
)
|
|
{
|
|
return ($function)(
|
|
new struct_document(
|
|
(
|
|
\is_null($this->subject->children)
|
|
? null
|
|
: \alveolata\list_\map(
|
|
$this->subject->children,
|
|
fn($child) => ($sub)($child, $function)
|
|
)
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
* @implementation
|
|
*/
|
|
public function to_raw(
|
|
\Closure $sub
|
|
)
|
|
{
|
|
return [
|
|
'kind' => 'document',
|
|
'data' => [
|
|
'children' => \alveolata\list_\map(
|
|
$this->subject->children,
|
|
fn($child) => ($sub)($child)
|
|
)
|
|
],
|
|
];
|
|
}
|
|
|
|
|
|
/**
|
|
* @implementation
|
|
*/
|
|
public function to_string(
|
|
\Closure $sub,
|
|
int $depth
|
|
) : string
|
|
{
|
|
return \implode(
|
|
'',
|
|
\alveolata\list_\map(
|
|
$this->subject->children,
|
|
fn($child) => ($sub)($child, ['depth' => ($depth + 1)])
|
|
)
|
|
);
|
|
}
|
|
|
|
}
|
|
|
|
?>
|