Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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 <colinodell@gmail.com>
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\Extension\Mention\Generator;
15
 
991 lars 16
use League\CommonMark\Exception\LogicException;
148 lars 17
use League\CommonMark\Extension\Mention\Mention;
18
use League\CommonMark\Node\Inline\AbstractInline;
19
 
20
final class CallbackGenerator implements MentionGeneratorInterface
21
{
22
    /**
23
     * A callback function which sets the URL on the passed mention and returns the mention, return a new AbstractInline based object or null if the mention is not a match
24
     *
25
     * @var callable(Mention): ?AbstractInline
26
     */
27
    private $callback;
28
 
29
    public function __construct(callable $callback)
30
    {
31
        $this->callback = $callback;
32
    }
33
 
991 lars 34
    /**
35
     * @throws LogicException
36
     */
148 lars 37
    public function generateMention(Mention $mention): ?AbstractInline
38
    {
39
        $result = \call_user_func($this->callback, $mention);
40
        if ($result === null) {
41
            return null;
42
        }
43
 
44
        if ($result instanceof AbstractInline && ! ($result instanceof Mention)) {
45
            return $result;
46
        }
47
 
48
        if ($result instanceof Mention && $result->hasUrl()) {
49
            return $mention;
50
        }
51
 
991 lars 52
        throw new LogicException('CallbackGenerator callable must set the URL on the passed mention and return the mention, return a new AbstractInline based object or null if the mention is not a match');
148 lars 53
    }
54
}