Skip to content

Commit 12203db

Browse files
committed
Added BooleanCondition
1 parent 059cdf1 commit 12203db

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/Label/BooleanCondition.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PhpAidc LabelPrinter package.
5+
*
6+
* © Appwilio (https://appwilio.com)
7+
* © JhaoDa (https://github.com/jhaoda)
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
declare(strict_types=1);
14+
15+
namespace PhpAidc\LabelPrinter\Label;
16+
17+
use PhpAidc\LabelPrinter\Contract\Condition;
18+
use PhpAidc\LabelPrinter\Contract\Label as LabelContract;
19+
20+
final class BooleanCondition implements Condition
21+
{
22+
/** @var mixed */
23+
private $value;
24+
25+
/** @var callable */
26+
private $callback;
27+
28+
public function __construct($value, callable $callback)
29+
{
30+
$this->value = $value;
31+
$this->callback = $callback;
32+
}
33+
34+
public function isTruthy(): bool
35+
{
36+
return (bool) $this->value;
37+
}
38+
39+
public function apply(LabelContract $label): LabelContract
40+
{
41+
\call_user_func($this->callback, $label, $this->value);
42+
43+
return $label;
44+
}
45+
}

src/Label/Label.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,19 @@ public function for(string $language, callable $callback)
145145

146146
return $this;
147147
}
148+
149+
/**
150+
* Apply the callback if the value is truthy.
151+
*
152+
* @param mixed $value
153+
* @param callable $callback
154+
*
155+
* @return $this
156+
*/
157+
public function when($value, callable $callback)
158+
{
159+
$this->commands[] = new BooleanCondition($value, $callback);
160+
161+
return $this;
162+
}
148163
}

0 commit comments

Comments
 (0)