Beispiel #1
0
 public function testFindChildren()
 {
     $node = new Node();
     $node->appendChild(new A())->appendChild((new B())->appendChild(new B()))->appendChild(new B())->appendChild(new D())->appendChild((new B())->appendChild((new B())->appendChild(new B())))->appendChild(new C())->appendChild(new D())->appendChild(new C())->appendChild(new D())->appendChild(new C())->appendChild(new D());
     $aChildren = $node->findChildrenArray(function (LeafInterface $leaf) {
         return $leaf->isInstanceOf(A::class);
     });
     $bDeepChildren = $node->findChildrenArray(function (LeafInterface $leaf) {
         return $leaf->isInstanceOf(B::class);
     });
     $bFirstChildren = $node->findChildrenArray(function (LeafInterface $leaf) {
         return $leaf->isInstanceOf(B::class);
     }, 0);
     $bSecondChildren = $node->findChildrenArray(function (LeafInterface $leaf) {
         return $leaf->isInstanceOf(B::class);
     }, 1);
     $cChildren = $node->findChildrenArray(function (LeafInterface $leaf) {
         return $leaf->isInstanceOf(C::class);
     });
     $dChildren = $node->findChildrenArray(function (LeafInterface $leaf) {
         return $leaf->isInstanceOf(D::class);
     });
     $this->assertCount(1, $aChildren, 'A children');
     $this->assertCount(6, $bDeepChildren, 'B deep children');
     $this->assertCount(3, $bFirstChildren, 'B first level');
     $this->assertCount(5, $bSecondChildren, 'B 2 levels');
     $this->assertCount(3, $cChildren, 'C children');
     $this->assertCount(4, $dChildren, 'D children');
 }
Beispiel #2
0
 /**
  * @param string             $name
  * @param array|null         $attributes
  * @param NodeInterface|null $parent
  * @param array|null         $children
  */
 public function __construct($name, array $attributes = null, NodeInterface $parent = null, array $children = null)
 {
     parent::__construct($parent, $children);
     $this->name = $name;
     $this->attributes = $attributes ?: [];
 }