|
| 1 | +<?php |
| 2 | + |
| 3 | +declare( strict_types = 1 ); |
| 4 | + |
| 5 | +namespace WMDE\VueJsTemplating\JsParsing; |
| 6 | + |
| 7 | +use Peast\Syntax\Node\CallExpression; |
| 8 | +use Peast\Syntax\Node\Expression; |
| 9 | +use Peast\Syntax\Node\Identifier; |
| 10 | +use Peast\Syntax\Node\MemberExpression; |
| 11 | +use Peast\Syntax\Node\ObjectExpression; |
| 12 | +use Peast\Syntax\Node\StringLiteral as PeastStringLiteral; |
| 13 | +use Peast\Syntax\Node\UnaryExpression; |
| 14 | + |
| 15 | +use RuntimeException; |
| 16 | + |
| 17 | +class PeastExpressionConverter { |
| 18 | + |
| 19 | + /** @var array a map of method names to their implementations in PHP */ |
| 20 | + protected array $methods; |
| 21 | + |
| 22 | + public function __construct( array $methods ) { |
| 23 | + $this->methods = $methods; |
| 24 | + } |
| 25 | + |
| 26 | + protected function convertUnaryExpression( UnaryExpression $expression ) { |
| 27 | + if ( $expression->getOperator() !== '!' ) { |
| 28 | + throw new RuntimeException( 'Unable to parse unary operator "' . $expression->getOperator() . '"' ); |
| 29 | + } |
| 30 | + return new NegationOperator( $this->convertExpression( $expression->getArgument() ) ); |
| 31 | + } |
| 32 | + |
| 33 | + protected function convertCallExpression( CallExpression $expression ) { |
| 34 | + $methodName = $expression->getCallee()->getName(); |
| 35 | + if ( !array_key_exists( $methodName, $this->methods ) ) { |
| 36 | + throw new RuntimeException( "Method '{$methodName}' is undefined" ); |
| 37 | + } |
| 38 | + $method = $this->methods[$methodName]; |
| 39 | + |
| 40 | + return new MethodCall( |
| 41 | + $method, |
| 42 | + array_map( fn ( $exp ) => $this->convertExpression( $exp ), $expression->getArguments() ) |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + protected function convertMemberExpression( MemberExpression $expression ) { |
| 47 | + $parts = []; |
| 48 | + while ( $expression !== null ) { |
| 49 | + if ( get_class( $expression ) === MemberExpression::class ) { |
| 50 | + $property = $expression->getProperty()->getName(); |
| 51 | + array_unshift( $parts, $property ); |
| 52 | + $expression = $expression->getObject(); |
| 53 | + } elseif ( get_class( $expression ) === Identifier::class ) { |
| 54 | + array_unshift( $parts, $expression->getName() ); |
| 55 | + $expression = null; |
| 56 | + } else { |
| 57 | + throw new RuntimeException( |
| 58 | + 'Unable to parse member expression with nodes of type ' . get_class( $expression ) |
| 59 | + ); |
| 60 | + } |
| 61 | + } |
| 62 | + return new VariableAccess( $parts ); |
| 63 | + } |
| 64 | + |
| 65 | + protected function convertObjectExpression( ObjectExpression $expression ) { |
| 66 | + $parsedExpressionMap = []; |
| 67 | + foreach ( $expression->getProperties() as $property ) { |
| 68 | + $parsedExpressionMap[ $this->convertKeyToLiteral( $property->getKey() ) ] = |
| 69 | + $this->convertExpression( $property->getValue() ); |
| 70 | + } |
| 71 | + return new JsDictionary( $parsedExpressionMap ); |
| 72 | + } |
| 73 | + |
| 74 | + public function convertExpression( Expression $expression ) { |
| 75 | + return match( get_class( $expression ) ) { |
| 76 | + UnaryExpression::class => $this->convertUnaryExpression( $expression ), |
| 77 | + MemberExpression::class => $this->convertMemberExpression( $expression ), |
| 78 | + PeastStringLiteral::class => new StringLiteral( $expression->getValue() ), |
| 79 | + Identifier::class => new VariableAccess( [ $expression->getName() ] ), |
| 80 | + CallExpression::class => $this->convertCallExpression( $expression ), |
| 81 | + ObjectExpression::class => $this->convertObjectExpression( $expression ), |
| 82 | + default => throw new RuntimeException( |
| 83 | + 'Unable to parse complex expression of type ' . get_class( $expression ) |
| 84 | + ) |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + protected function convertKeyToLiteral( $key ) { |
| 89 | + return match( get_class( $key ) ) { |
| 90 | + PeastStringLiteral::class => $key->getValue(), |
| 91 | + Identifier::class => $key->getName(), |
| 92 | + default => throw new RuntimeException( |
| 93 | + 'Unable to extract name from dictionary key of type ' . get_class( $key ) |
| 94 | + ) |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments