コード例 #1
0
  /** 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;
  }
コード例 #2
0
  /**
   * 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;
  }
コード例 #3
0
  @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;
  }