|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Phug\Parser\TokenHandler; |
| 4 | + |
| 5 | +use Phug\Lexer\TokenInterface; |
| 6 | +use Phug\Parser\Node\ElementNode; |
| 7 | +use Phug\Parser\Node\MixinCallNode; |
| 8 | +use Phug\Parser\State; |
| 9 | +use Phug\Parser\TokenHandlerInterface; |
| 10 | + |
| 11 | +abstract class AbstractTokenHandler implements TokenHandlerInterface |
| 12 | +{ |
| 13 | + const TOKEN_TYPE = TokenInterface::class; |
| 14 | + |
| 15 | + public function handleToken(TokenInterface $token, State $state) |
| 16 | + { |
| 17 | + if (!is_a($token, static::TOKEN_TYPE)) { |
| 18 | + $name = $this->getTypeName(static::TOKEN_TYPE); |
| 19 | + $handler = $this->getClassLastPart(static::class); |
| 20 | + |
| 21 | + throw new \RuntimeException( |
| 22 | + "You can only pass $name tokens to $handler" |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + $method = 'handle'.$this->getClassLastPart(get_class($token)); |
| 27 | + $this->$method($token, $state); |
| 28 | + } |
| 29 | + |
| 30 | + protected function onlyOnElement(TokenInterface $token, State $state) |
| 31 | + { |
| 32 | + $this->createElementNodeIfMissing($token, $state); |
| 33 | + $this->assertCurrentNodeIs($token, $state, [ElementNode::class, MixinCallNode::class]); |
| 34 | + } |
| 35 | + |
| 36 | + protected function createElementNodeIfMissing(TokenInterface $token, State $state) |
| 37 | + { |
| 38 | + if (!$state->getCurrentNode()) { |
| 39 | + $state->setCurrentNode($state->createNode(ElementNode::class, $token)); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + protected function assertCurrentNodeIs(TokenInterface $token, State $state, array $nodeTypes) |
| 44 | + { |
| 45 | + if (!$state->currentNodeIs($nodeTypes)) { |
| 46 | + $nodeTypes = array_values($nodeTypes); |
| 47 | + $list = $this->getPluralTypeName(array_shift($nodeTypes)); |
| 48 | + $count = count($nodeTypes); |
| 49 | + |
| 50 | + foreach ($nodeTypes as $i => $nodeType) { |
| 51 | + $list .= ($i === $count - 1 ? ' and ' : ', ').$this->getPluralTypeName($nodeType); |
| 52 | + } |
| 53 | + |
| 54 | + $state->throwException( |
| 55 | + ucfirst($this->getPluralTypeName(get_class($token))).' can only happen on '.$list, |
| 56 | + 0, |
| 57 | + $token |
| 58 | + ); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private function getClassLastPart($class) |
| 63 | + { |
| 64 | + $parts = explode('\\', $class); |
| 65 | + |
| 66 | + return end($parts); |
| 67 | + } |
| 68 | + |
| 69 | + private function getTypeName($class) |
| 70 | + { |
| 71 | + $name = preg_replace('/(Token|Node)$/', '', $this->getClassLastPart($class)); |
| 72 | + $name = preg_replace('/([a-z])([A-Z])/', '$1-$2', $name); |
| 73 | + |
| 74 | + return strtolower($name); |
| 75 | + } |
| 76 | + |
| 77 | + private function getPluralTypeName($class) |
| 78 | + { |
| 79 | + $name = $this->getTypeName($class); |
| 80 | + |
| 81 | + if ($name === 'class') { |
| 82 | + return 'classes'; |
| 83 | + } |
| 84 | + |
| 85 | + return $name.'s'; |
| 86 | + } |
| 87 | +} |
0 commit comments