<?php
$category = function(string $name, $parent = null) {
return new class($name, $parent) {
public $name;
public $parent;
public function __construct(string $name, $parent = null) {
$this->name = $name;
$this->parent = $parent;
}
public function breadcrumbs(array $trail = []) {
$trail[] = $this->name;
return $this->parent ? $this->parent->breadcrumbs($trail) : $trail;
}
};
};
$product = function(string $name, ...$categories) use($category) {
$categories = array_reduce(array_reverse($categories), function($set, $item) use($category) {
if (!$set) {
return $category($item);
}
return $category($item, $set);
});
return new class($name, $categories) {
public $name;
public $categories;
public function __construct(string $name, $categories) {
$this->name = $name;
$this->categories = $categories;
}
public function trail(string $separator = ' / '): string {
return implode($separator, $this->categories->breadcrumbs());
}
};
};
$book = $product('Harry Potter', 'books', 'hardback', 'young-adult');
$shoes = $product('Air Jordans', 'shoes', 'mens', 'sports', 'basketball');
$tapeMeasure = $product('Stanley 25', 'hardware', 'tools', 'measurement');
var_dump(
$book->trail(), // books / hardback / young-adult
$shoes->trail(), // shoes / mens / sports / basketball
$tapeMeasure->trail() // hardware / tools / measurement
);
- Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- string(30) "books / hardback / young-adult"
string(34) "shoes / mens / sports / basketball"
string(30) "hardware / tools / measurement"
preferences:
146.73 ms | 407 KiB | 5 Q