Skip to content

Commit

Permalink
feat: object test
Browse files Browse the repository at this point in the history
  • Loading branch information
PrInStPL committed Jun 30, 2024
1 parent 10ac1b8 commit cb3a3db
Show file tree
Hide file tree
Showing 15 changed files with 1,590 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Classes/ChainPlainClass802.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace PhpArrayVsObjectBenchmark\Classes;

use function spl_object_id;

class ChainPlainClass802 extends PrinstChain802 {
public function __construct(
public ?string $info = null,
public ?string $first = null,
public ?int $second = null,
?self $previous = null
) {
parent::__construct($previous);
}

public function __debugInfo(): array
{
return [
'info' => $this->info,
'first' => $this->first,
'second' => $this->second,
'object id' => spl_object_id($this),
'previous object id' => $this->previous ? spl_object_id($this->previous) : null,
'next object id' => $this->next ? spl_object_id($this->next) : null,
];
}
}
97 changes: 97 additions & 0 deletions src/Classes/PrinstChain802.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php /** @noinspection PhpUnusedPrivateFieldInspection */

declare(strict_types=1);

namespace PhpArrayVsObjectBenchmark\Classes;

use IteratorAggregate;

abstract class PrinstChain802 implements IteratorAggregate
{
private ?PrinstChain802Iterator $iterator = null;
protected ?self $next = null;


/**
* @param static|null $previous
*/
public function __construct(protected ?self $previous = null)
{
$this->previous?->setNext($this);
$this->iterator?->append($this);
}

public function __destruct()
{
$this->previous?->setNext($this->next);
$this->next?->setPrevious($this->previous);

if ($current = ($this->previous ?? $this->next)) {
$this->iterator?->recreate($current);
}
}

/**
* @inheritDoc
*/
public function getIterator(): PrinstChain802Iterator
{
if (!$this->iterator) {
while ($element = ($element ?? $this)->getPrevious()) {
$this->iterator = $element->iterator;

if ($this->iterator) {
return $this->iterator;
}
}

while ($element = ($element ?? $this)->getNext()) {
$this->iterator = $element->iterator;

if ($this->iterator) {
return $this->iterator;
}
}

$this->iterator = new PrinstChain802Iterator($this);
}

return $this->iterator;
}

public function getNext(): ?static
{
return $this->next;
}

public function setNext(?self $next, bool $chainThisToNextPrevious = true): ?static
{
$this->next = $next;

if ($chainThisToNextPrevious && $this !== $next?->getPrevious()) {
$this->next?->setPrevious($this);
}

// ToDo: chain old next to him second previous if exists

return $this;
}

public function getPrevious(): ?static
{
return $this->previous;
}

public function setPrevious(?self $previous, bool $chainThisToPreviousNext = true): ?static
{
$this->previous = $previous;

if ($chainThisToPreviousNext && $this !== $previous?->getNext()) {
$this->previous?->setNext($this);
}

// ToDo: chain old previous to him second next if exists

return $this;
}
}
Loading

0 comments on commit cb3a3db

Please sign in to comment.