/** array('foo' => 'bar', 'foo1' => 'bar', 1 => 'foo') */
  public static HashMap<String, String> getArrayKeyValueMap(
      @NotNull ArrayCreationExpression arrayCreationExpression) {
    HashMap<String, String> keys = new HashMap<String, String>();

    for (ArrayHashElement arrayHashElement : arrayCreationExpression.getHashElements()) {
      PhpPsiElement child = arrayHashElement.getKey();
      if (child != null
          && ((child instanceof StringLiteralExpression)
              || PhpPatterns.psiElement(PhpElementTypes.NUMBER).accepts(child))) {

        String key;
        if (child instanceof StringLiteralExpression) {
          key = ((StringLiteralExpression) child).getContents();
        } else {
          key = child.getText();
        }

        if (StringUtils.isBlank(key)) {
          continue;
        }

        String value = null;

        if (arrayHashElement.getValue() instanceof StringLiteralExpression) {
          value = ((StringLiteralExpression) arrayHashElement.getValue()).getContents();
        }

        keys.put(key, value);
      }
    }

    return keys;
  }
Example #2
0
 private static List createVBArrayCreationDeclatationsAndSubstituteParams(
     List oldParameters, List newParameters, InterpretationContext context) {
   List arrayCreationDeclarations = new ArrayList();
   String s = ACE_VARIABLE_NAME;
   for (Iterator itr = oldParameters.iterator(); itr.hasNext(); ) {
     Object o = itr.next();
     if (o instanceof ArrayCreationExpression) {
       ArrayCreationExpression ace = (ArrayCreationExpression) o;
       LocalVariableDeclaration ds =
           LocalVariableDeclaration.createVBLocalVariableDeclaration(context, ace, s + aceCounter);
       arrayCreationDeclarations.add(ds);
       newParameters.add(DNVariable.createVBVariable(s + aceCounter, ace.getName() + "()"));
       aceCounter++;
     } else {
       newParameters.add(o);
     }
   }
   return arrayCreationDeclarations;
 }
  /**
   * Gets array key-value as single PsiElement map
   *
   * <p>['foo' => $bar]
   */
  @NotNull
  public static Map<String, PsiElement> getArrayCreationKeyMap(
      @NotNull ArrayCreationExpression arrayCreationExpression) {
    Map<String, PsiElement> keys = new HashMap<String, PsiElement>();

    for (ArrayHashElement arrayHashElement : arrayCreationExpression.getHashElements()) {
      PhpPsiElement child = arrayHashElement.getKey();
      if (child instanceof StringLiteralExpression) {
        keys.put(((StringLiteralExpression) child).getContents(), child);
      }
    }

    return keys;
  }
  @Nullable
  public static PhpPsiElement getArrayValue(
      ArrayCreationExpression arrayCreationExpression, String name) {

    for (ArrayHashElement arrayHashElement : arrayCreationExpression.getHashElements()) {
      PhpPsiElement child = arrayHashElement.getKey();
      if (child instanceof StringLiteralExpression) {
        if (((StringLiteralExpression) child).getContents().equals(name)) {
          return arrayHashElement.getValue();
        }
      }
    }

    return null;
  }