Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DataHolder {
- /** @var array<string> */
- private array $items;
- public function __construct(array $items)
- {
- $this->items = $items;
- }
- public function hasSameElsCallable()
- {
- $this->traverseCallable(fn(?string $prev, string $current, ?string $next) => $next !== null && $current === $next);
- }
- public function hasSameElsType()
- {
- $handler = new class implements ItemsTraverse {
- public function traverse(?string $prev, string $current, ?string $next)
- {
- return $next !== null && $current === $next;
- }
- };
- $this->traverseByType($handler);
- }
- /**
- * @psalm-param callable(null|string, string, null|string) $handler
- */
- public function traverseCallable(callable $handler)
- {
- for($i = 0; $i < count($this->parts); $i++) {
- $prev = $parts[$i - 1] ?? null;
- $current = $parts[$i];
- $next = $parts[$i + 1] ?? null;
- $handler($prev, $current, $next);
- }
- }
- public function traverseByType(ItemsTraverse $handler)
- {
- for($i = 0; $i < count($this->parts); $i++) {
- $prev = $parts[$i - 1] ?? null;
- $current = $parts[$i];
- $next = $parts[$i + 1] ?? null;
- $handler->traverse($prev, $current, $next);
- }
- }
- }
- interface ItemsTraverse {
- public function traverse(?string $prev, string $current, ?string $next);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement