Advertisement
Guest User

Untitled

a guest
Oct 31st, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.51 KB | None | 0 0
  1. class DataHolder {
  2.     /** @var array<string>  */
  3.     private array $items;
  4.  
  5.     public function __construct(array $items)
  6.     {
  7.         $this->items = $items;
  8.     }
  9.  
  10.     public function hasSameElsCallable()
  11.     {
  12.         $this->traverseCallable(fn(?string $prev, string $current, ?string $next) => $next !== null && $current === $next);
  13.     }
  14.  
  15.     public function hasSameElsType()
  16.     {
  17.         $handler = new class implements ItemsTraverse {
  18.             public function traverse(?string $prev, string $current, ?string $next)
  19.             {
  20.                 return $next !== null && $current === $next;
  21.             }
  22.         };
  23.  
  24.         $this->traverseByType($handler);
  25.     }
  26.  
  27.     /**
  28.      * @psalm-param callable(null|string, string, null|string) $handler
  29.      */
  30.     public function traverseCallable(callable $handler)
  31.     {
  32.         for($i = 0; $i < count($this->parts); $i++) {
  33.             $prev = $parts[$i - 1] ?? null;
  34.             $current = $parts[$i];
  35.             $next = $parts[$i + 1] ?? null;
  36.  
  37.             $handler($prev, $current, $next);
  38.         }
  39.     }
  40.  
  41.     public function traverseByType(ItemsTraverse $handler)
  42.     {
  43.         for($i = 0; $i < count($this->parts); $i++) {
  44.             $prev = $parts[$i - 1] ?? null;
  45.             $current = $parts[$i];
  46.             $next = $parts[$i + 1] ?? null;
  47.  
  48.             $handler->traverse($prev, $current, $next);
  49.         }
  50.     }
  51. }
  52.  
  53. interface ItemsTraverse {
  54.     public function traverse(?string $prev, string $current, ?string $next);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement