/
Parser.php
62 lines (45 loc) · 1.73 KB
/
Parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace Anh\MarkupBundle;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Anh\MarkupBundle\Event\MarkupEvent;
use Anh\MarkupBundle\Event\MarkupCreateEvent;
use Anh\MarkupBundle\Event\MarkupParseEvent;
use Anh\MarkupBundle\Event\MarkupValidateEvent;
use Anh\MarkupBundle\Event\MarkupCommandEvent;
class Parser
{
public function __construct(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}
public function create($type, $markup = '', $options = array())
{
$event = new MarkupCreateEvent($type, $markup, $options);
$this->dispatcher->dispatch(MarkupEvent::CREATE, $event);
return $event->getParser();
}
public function parse($type, $markup, $options = array())
{
$parser = $this->create($type, $markup, $options);
$event = new MarkupParseEvent($type, $markup, $options);
$event->setParser($parser);
$this->dispatcher->dispatch(MarkupEvent::PARSE, $event);
return $event->getText();
}
public function validate($type, $markup, $options = array())
{
$parser = $this->create($type, $markup, $options);
$event = new MarkupValidateEvent($type, $markup, $options);
$event->setParser($parser);
$this->dispatcher->dispatch(MarkupEvent::VALIDATE, $event);
return $event->getErrors();
}
public function command($command, $type, $markup = '', $options = array())
{
$parser = $this->create($type, $markup, $options);
$event = new MarkupCommandEvent($command, $type, $markup, $options);
$event->setParser($parser);
$this->dispatcher->dispatch(MarkupEvent::COMMAND, $event);
return $event->getResult();
}
}