Skip to content

Commit

Permalink
drafting
Browse files Browse the repository at this point in the history
  • Loading branch information
dakujem committed Sep 25, 2023
1 parent 0f2ba73 commit 711b962
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
},
"autoload": {
"psr-4": {
"Oliva\\Utils\\Tree\\": "src/"
"Oliva\\Utils\\Tree\\": "src/",
"Dakujem\\Oliva\\": "lib/"
}
},
"scripts": {
Expand Down
18 changes: 18 additions & 0 deletions lib/DataNodeContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Dakujem\Oliva;

/**
* DataNodeContract
*
* @author Andrej Rypak <xrypak@gmail.com>
*/
interface DataNodeContract
{
/**
* @return mixed
*/
public function data(); //:mixed;
}
35 changes: 35 additions & 0 deletions lib/TreeNodeContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Dakujem\Oliva;

/**
* Base contract supporting tree traversals.
*
* @author Andrej Rypak <xrypak@gmail.com>
*/
interface TreeNodeContract
{
public function parent(): ?TreeNodeContract;

public function children(): iterable;

/**
* Returns `true` if the node has no children.
*/
public function isLeaf(): bool;

/**
* Get a specific child.
*
* @param string|int $index
* @return mixed
*/
public function child(/*string|int*/ $index): ?TreeNodeContract;

/**
* @return string|int|null
*/
public function childIndex(TreeNodeContract $node);//:mixed;
}
3 changes: 3 additions & 0 deletions lib/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ refactor
- traits into classes
- ditch all PHP 5 constructs
- fully typed

add
- address object/interface
22 changes: 18 additions & 4 deletions tests/builder.SimpleBuilder.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,24 @@ subroutine2();

function subroutine1()
{
$data = ['a' => 'a', 'b' => 'b', 'children' => [
['c' => 'c', 'children' => [['e' => 'e', 'f' => 'f',]]],
['d' => 'd', 'children' => [['g' => 'g', 'h' => 'h',]]],
]];
$data = [
'a' => 'a',
'b' => 'b',
'children' => [
[
'c' => 'c',
'children' => [
['e' => 'e', 'f' => 'f',],
],
],
[
'd' => 'd',
'children' => [
['g' => 'g', 'h' => 'h',],
],
],
],
];

$builder = new SimpleTreeBuilder('children');
$root = $builder->build($data);
Expand Down

0 comments on commit 711b962

Please sign in to comment.