Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F4070000
Node.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
Node.php
View Options
<?php
namespace
Shellbox\ShellParser
;
use
Wikimedia\WikiPEG\InternalError
;
class
Node
{
/** @var string The node type */
public
$type
;
/** @var array The node contents (children) */
public
$contents
;
/**
* @param string $type
* @param array|Node|string $contents
*/
public
function
__construct
(
$type
,
$contents
)
{
$this
->
type
=
$type
;
if
(
!
is_array
(
$contents
)
)
{
$contents
=
[
$contents
];
}
foreach
(
$contents
as
$i
=>
$node
)
{
if
(
!
$node
instanceof
Node
&&
!
is_string
(
$node
)
&&
!
is_array
(
$node
)
)
{
$type
=
gettype
(
$node
);
throw
new
InternalError
(
'ShellParser error: node contents validation failed. '
.
"Item $i is a $type."
);
}
}
$this
->
contents
=
$contents
;
}
/**
* Dump a string representation for testing or debugging
*
* @return string
*/
public
function
dump
()
{
$inner
=
self
::
dumpArray
(
$this
->
contents
);
return
"<{$this->type}>$inner</{$this->type}>"
;
}
/**
* Dump a string representation of a node array for testing or debugging
*
* @param array $array
* @return string
*/
public
static
function
dumpArray
(
$array
)
{
$inner
=
''
;
foreach
(
$array
as
$node
)
{
if
(
$node
instanceof
Node
)
{
$inner
.=
$node
->
dump
();
}
elseif
(
is_array
(
$node
)
)
{
$inner
.=
self
::
dumpArray
(
$node
);
}
else
{
$inner
.=
htmlspecialchars
(
$node
,
ENT_COMPAT
);
}
}
return
$inner
;
}
public
function
traverse
(
callable
$visitor
,
...
$args
)
{
$visitor
(
$this
,
...
$args
);
self
::
traverseArray
(
$visitor
,
$this
->
contents
,
...
$args
);
}
public
static
function
traverseArray
(
callable
$visitor
,
$array
,
...
$args
)
{
foreach
(
$array
as
$node
)
{
if
(
$node
instanceof
Node
)
{
$node
->
traverse
(
$visitor
,
...
$args
);
}
elseif
(
is_array
(
$node
)
)
{
self
::
traverseArray
(
$visitor
,
$node
,
...
$args
);
}
else
{
$visitor
(
$node
,
...
$args
);
}
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Aug 16 2026, 23:13 (11 w, 5 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
2c/dc/1bc997dcbc4fef6179e0683bd1ca
Default Alt Text
Node.php (1 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment