thephpleague /
commonmark
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* |
||
| 6 | * This file is part of the league/commonmark package. |
||
| 7 | * |
||
| 8 | * (c) Colin O'Dell <[email protected]> |
||
| 9 | * |
||
| 10 | * For the full copyright and license information, please view the LICENSE |
||
| 11 | * file that was distributed with this source code. |
||
| 12 | */ |
||
| 13 | |||
| 14 | namespace League\CommonMark\Reference; |
||
| 15 | |||
| 16 | final class MemoryLimitedReferenceMap implements ReferenceMapInterface |
||
| 17 | { |
||
| 18 | private ReferenceMapInterface $decorated; |
||
| 19 | |||
| 20 | private const MINIMUM_SIZE = 100_000; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 21 | |||
| 22 | private int $remaining; |
||
| 23 | |||
| 24 | 2370 | public function __construct(ReferenceMapInterface $decorated, int $maxSize) |
|
| 25 | { |
||
| 26 | 2370 | $this->decorated = $decorated; |
|
| 27 | 2370 | $this->remaining = \max(self::MINIMUM_SIZE, $maxSize); |
|
| 28 | } |
||
| 29 | |||
| 30 | public function add(ReferenceInterface $reference): void |
||
| 31 | { |
||
| 32 | $this->decorated->add($reference); |
||
| 33 | } |
||
| 34 | |||
| 35 | public function contains(string $label): bool |
||
| 36 | { |
||
| 37 | return $this->decorated->contains($label); |
||
| 38 | } |
||
| 39 | |||
| 40 | 222 | public function get(string $label): ?ReferenceInterface |
|
| 41 | { |
||
| 42 | 222 | $reference = $this->decorated->get($label); |
|
| 43 | 222 | if ($reference === null) { |
|
| 44 | 92 | return null; |
|
| 45 | } |
||
| 46 | |||
| 47 | // Check for expansion limit |
||
| 48 | 144 | $this->remaining -= \strlen($reference->getDestination()) + \strlen($reference->getTitle()); |
|
| 49 | 144 | if ($this->remaining < 0) { |
|
| 50 | return null; |
||
| 51 | } |
||
| 52 | |||
| 53 | 144 | return $reference; |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @return \Traversable<string, ReferenceInterface> |
||
| 58 | */ |
||
| 59 | public function getIterator(): \Traversable |
||
| 60 | { |
||
| 61 | return $this->decorated->getIterator(); |
||
| 62 | } |
||
| 63 | |||
| 64 | public function count(): int |
||
| 65 | { |
||
| 66 | return $this->decorated->count(); |
||
| 67 | } |
||
| 68 | } |
||
| 69 |