/**
  * Rewrites a Drupal 7 field lookup like so:
  *
  * $node->body[LANGUAGE_NONE][0]['value'] --> $node->body[0]->value
  * $node->body['fr'][0]['value'] --> $node->getTranslation('fr')->body[0]->value
  *
  * @param \Pharborist\ArrayLookupNode $node
  *  The original field lookup.
  *
  * @return \Pharborist\ExpressionNode
  */
 public static function rewriteFieldLookup(ArrayLookupNode $node)
 {
     $keys = $node->getKeys();
     /** @var \Pharborist\Objects\ObjectPropertyNode $root */
     $root = $node->getRootArray();
     $expr = $root->getObject()->getText();
     if (self::isTranslation($keys[0])) {
         $expr .= '->getTranslation(' . $keys[0] . ')';
     }
     $expr .= '->' . $root->getPropertyName() . '[' . $keys[1] . ']';
     /** @var \Pharborist\Types\StringNode|\Pharborist\Node $column */
     foreach (array_slice($keys, 2) as $column) {
         $expr .= '->';
         $expr .= $column instanceof StringNode ? $column->toValue() : $column->getText();
     }
     return Parser::parseExpression($expr);
 }
 public function testCreate()
 {
     $lookup = ArrayLookupNode::create(Token::variable('$form_state'), new StringNode(T_CONSTANT_ENCAPSED_STRING, "'storage'"));
     $this->assertEquals('$form_state[\'storage\']', $lookup->getText());
 }
Esempio n. 3
0
 /**
  * Apply any array deference to operand.
  * @param Node $node
  * @return Node
  */
 private function arrayDeference(Node $node)
 {
     while ($this->currentType === '[') {
         $n = $node;
         $node = new ArrayLookupNode();
         $node->addChild($n, 'array');
         $this->dimOffset($node);
     }
     return $node;
 }