@Nullable public static String getArrayHashValue( ArrayCreationExpression arrayCreationExpression, String keyName) { ArrayHashElement translationArrayHashElement = PsiElementUtils.getChildrenOfType( arrayCreationExpression, PlatformPatterns.psiElement(ArrayHashElement.class) .withFirstChild( PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY) .withText( PlatformPatterns.string() .oneOf("'" + keyName + "'", "\"" + keyName + "\"")))); if (translationArrayHashElement == null) { return null; } if (!(translationArrayHashElement.getValue() instanceof StringLiteralExpression)) { return null; } StringLiteralExpression valueString = (StringLiteralExpression) translationArrayHashElement.getValue(); if (valueString == null) { return null; } return valueString.getContents(); }
/** 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; }
/** * 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; }