1: <?php declare(strict_types=1);
2:
3: namespace Salient\Core\Concern;
4:
5: use Salient\Contract\Core\Chainable;
6:
7: /**
8: * @api
9: *
10: * @phpstan-require-implements Chainable
11: */
12: trait ChainableTrait
13: {
14: /**
15: * @inheritDoc
16: */
17: public function apply(callable $callback)
18: {
19: return $callback($this);
20: }
21:
22: /**
23: * @inheritDoc
24: */
25: public function applyIf($condition, ?callable $then = null, ?callable $else = null)
26: {
27: if (!is_bool($condition)) {
28: $condition = $condition($this);
29: }
30:
31: return $condition
32: ? ($then === null ? $this : $then($this))
33: : ($else === null ? $this : $else($this));
34: }
35:
36: /**
37: * @inheritDoc
38: */
39: public function applyForEach(iterable $items, callable $callback)
40: {
41: $instance = $this;
42: foreach ($items as $key => $value) {
43: $instance = $callback($instance, $value, $key);
44: }
45: return $instance;
46: }
47: }
48: