fab2s /
YaEtl
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of YaEtl |
||
| 5 | * (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl |
||
| 6 | * This source file is licensed under the MIT license which you will |
||
| 7 | * find in the LICENSE file or at https://opensource.org/licenses/MIT |
||
| 8 | */ |
||
| 9 | |||
| 10 | namespace fab2s\YaEtl\Qualifiers; |
||
| 11 | |||
| 12 | use fab2s\NodalFlow\Flows\InterrupterInterface; |
||
| 13 | use fab2s\NodalFlow\Interrupter; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Class LimitQualifier |
||
| 17 | */ |
||
| 18 | class LimitQualifier extends QualifierAbstract |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var int|null |
||
| 22 | */ |
||
| 23 | protected $limit; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | protected $count = 0; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $target; |
||
| 34 | |||
| 35 | public function __construct(?int $limit = null, string $target = InterrupterInterface::TARGET_SELF) |
||
| 36 | { |
||
| 37 | parent::__construct(); |
||
| 38 | |||
| 39 | $this->setLimit($limit) |
||
| 40 | ->setTarget($target); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param int|null $limit |
||
| 45 | * |
||
| 46 | * @return LimitQualifier |
||
| 47 | */ |
||
| 48 | public function setLimit(?int $limit): LimitQualifier |
||
| 49 | { |
||
| 50 | $this->limit = $limit; |
||
| 51 | |||
| 52 | return $this; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return string |
||
| 57 | */ |
||
| 58 | public function getTarget(): string |
||
| 59 | { |
||
| 60 | return $this->target; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param string $target |
||
| 65 | * |
||
| 66 | * @return LimitQualifier |
||
| 67 | */ |
||
| 68 | public function setTarget(string $target): LimitQualifier |
||
| 69 | { |
||
| 70 | $this->target = $target === InterrupterInterface::TARGET_TOP ? InterrupterInterface::TARGET_TOP : InterrupterInterface::TARGET_SELF; |
||
| 71 | |||
| 72 | return $this; |
||
| 73 | } |
||
| 74 | |||
| 75 | public function qualify($param) |
||
| 76 | { |
||
| 77 | if ($this->limit && ++$this->count >= $this->limit + 1) { |
||
|
0 ignored issues
–
show
|
|||
| 78 | return new Interrupter($this->getTarget(), null, InterrupterInterface::TYPE_BREAK); |
||
| 79 | } |
||
| 80 | |||
| 81 | return true; |
||
| 82 | } |
||
| 83 | } |
||
| 84 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: