YamlSourceManipulator
in package
A class that modifies YAML source, while keeping comments & formatting.
This is designed to work for the most common syntaxes, but not all YAML syntaxes. If content cannot be updated safely, an exception is thrown.
Table of Contents
Constants
- ARRAY_FORMAT_INLINE = 'inline'
- ARRAY_FORMAT_MULTILINE = 'multi'
- ARRAY_TYPE_HASH = 'hash'
- ARRAY_TYPE_SEQUENCE = 'sequence'
- COMMENT_PLACEHOLDER_VALUE = '__COMMENT__'
- EMPTY_LINE_PLACEHOLDER_VALUE = '__EMPTY_LINE__'
- UNSET_KEY_FLAG = '__MAKER_VALUE_UNSET'
Properties
- $arrayFormatForDepths : mixed
- $arrayTypeForDepths : mixed
- $contents : string
- $currentData : mixed
- $currentPath : mixed
- $currentPosition : mixed
- $depth : mixed
- $indentationForDepths : mixed
- $logger : LoggerInterface|null
- $previousPath : mixed
Methods
- __construct() : mixed
- createCommentLine() : string
- createEmptyLine() : string
- getContents() : string
- getData() : array<string|int, mixed>
- setData() : mixed
- setLogger() : mixed
- addNewKeyToYaml() : mixed
- Adds a new key to current position in the YAML.
- advanceBeyondEndOfPreviousKey() : mixed
- advanceBeyondKey() : mixed
- advanceBeyondMultilineArrayLastItem() : mixed
- advanceBeyondValue() : mixed
- advanceBeyondWhitespace() : mixed
- advanceCurrentPosition() : mixed
- advanceToEndOfLine() : mixed
- appendToArrayAtCurrentPath() : array<string|int, mixed>
- Adds a new item (with the given key) to the $data array at the correct position.
- changeValueInYaml() : mixed
- Replaces the value at the current position with this value.
- convertToYaml() : mixed
- decrementDepth() : mixed
- findEndPositionOfValue() : mixed
- findNextLineBreak() : mixed
- findPositionOfMultilineCharInLine() : int|null
- findPositionOfNextCharacter() : mixed
- Advance until you find *one* of the characters in $chars.
- getCurrentData() : mixed
- Returns the value in the current data that is currently being looked at.
- getCurrentIndentation() : string
- getCurrentLine() : mixed
- getEndOfKeyPosition() : mixed
- getEndOfPreviousKeyPosition() : int
- Finds the end position of the key that comes *before* this key.
- getKeyRegex() : mixed
- getPreferredIndentationSize() : int
- Try to guess a preferred indentation level.
- getPreviousLine() : mixed
- guessNextArrayTypeAndAdvance() : string
- Attempts to guess if the array at the current position is a multi-line array or an inline array.
- indentMultilineYamlArray() : string
- Takes an unindented multi-line YAML string and indents it so it can be inserted into the current position.
- isCharLineBreak() : bool
- isCurrentArrayMultiline() : bool
- isCurrentArraySequence() : bool
- isCurrentLineComment() : bool
- isEOF() : mixed
- isFinalLineComment() : bool
- isHash() : bool
- Duplicated from Symfony's Inline::isHash().
- isLineComment() : bool
- isMultilineString() : bool
- isPositionAtBeginningOfArray() : bool
- For the array currently being processed, are we currently handling the first key inside of it?
- isPreviousLineComment() : bool
- log() : mixed
- manuallyIncrementIndentation() : mixed
- normalizeSequences() : mixed
- removeKeyAtCurrentPath() : array<string|int, mixed>
- removeKeyFromYaml() : mixed
- removeMetadataKeys() : mixed
- replaceSpecialMetadataCharacters() : mixed
- setValueAtCurrentPath() : mixed
- updateContents() : mixed
- updateData() : mixed
Constants
ARRAY_FORMAT_INLINE
public
mixed
ARRAY_FORMAT_INLINE
= 'inline'
ARRAY_FORMAT_MULTILINE
public
mixed
ARRAY_FORMAT_MULTILINE
= 'multi'
ARRAY_TYPE_HASH
public
mixed
ARRAY_TYPE_HASH
= 'hash'
ARRAY_TYPE_SEQUENCE
public
mixed
ARRAY_TYPE_SEQUENCE
= 'sequence'
COMMENT_PLACEHOLDER_VALUE
public
mixed
COMMENT_PLACEHOLDER_VALUE
= '__COMMENT__'
EMPTY_LINE_PLACEHOLDER_VALUE
public
mixed
EMPTY_LINE_PLACEHOLDER_VALUE
= '__EMPTY_LINE__'
UNSET_KEY_FLAG
public
mixed
UNSET_KEY_FLAG
= '__MAKER_VALUE_UNSET'
Properties
$arrayFormatForDepths
private
mixed
$arrayFormatForDepths
= []
$arrayTypeForDepths
private
mixed
$arrayTypeForDepths
= []
$contents
private
string
$contents
$currentData
private
mixed
$currentData
$currentPath
private
mixed
$currentPath
= []
$currentPosition
private
mixed
$currentPosition
= 0
$depth
private
mixed
$depth
= 0
$indentationForDepths
private
mixed
$indentationForDepths
= []
$logger
private
LoggerInterface|null
$logger
$previousPath
private
mixed
$previousPath
= []
Methods
__construct()
public
__construct(string $contents) : mixed
Parameters
- $contents : string
createCommentLine()
public
createCommentLine(string $comment) : string
Parameters
- $comment : string
Return values
stringcreateEmptyLine()
public
createEmptyLine() : string
Return values
stringgetContents()
public
getContents() : string
Return values
stringgetData()
public
getData() : array<string|int, mixed>
Return values
array<string|int, mixed>setData()
public
setData(array<string|int, mixed> $newData) : mixed
Parameters
- $newData : array<string|int, mixed>
setLogger()
public
setLogger(LoggerInterface $logger) : mixed
Parameters
- $logger : LoggerInterface
addNewKeyToYaml()
Adds a new key to current position in the YAML.
private
addNewKeyToYaml(mixed $key, mixed $value) : mixed
The position should be set right where this new key should be inserted.
Parameters
- $key : mixed
- $value : mixed
advanceBeyondEndOfPreviousKey()
private
advanceBeyondEndOfPreviousKey(mixed $key) : mixed
Parameters
- $key : mixed
advanceBeyondKey()
private
advanceBeyondKey(mixed $key) : mixed
Parameters
- $key : mixed
advanceBeyondMultilineArrayLastItem()
private
advanceBeyondMultilineArrayLastItem(array<string|int, mixed> $currentData, array<string|int, mixed> $newData) : mixed
Parameters
- $currentData : array<string|int, mixed>
- $newData : array<string|int, mixed>
advanceBeyondValue()
private
advanceBeyondValue(mixed $value) : mixed
Parameters
- $value : mixed
advanceBeyondWhitespace()
private
advanceBeyondWhitespace() : mixed
advanceCurrentPosition()
private
advanceCurrentPosition(int $newPosition) : mixed
Parameters
- $newPosition : int
advanceToEndOfLine()
private
advanceToEndOfLine() : mixed
appendToArrayAtCurrentPath()
Adds a new item (with the given key) to the $data array at the correct position.
private
appendToArrayAtCurrentPath(mixed $key, mixed $value, array<string|int, mixed> $data) : array<string|int, mixed>
The $data should be the simple array that should be updated and that the current path is pointing to. The current path is used to determine where in the array to put the new item (so that it's placed in the middle when necessary).
Parameters
- $key : mixed
- $value : mixed
- $data : array<string|int, mixed>
Return values
array<string|int, mixed>changeValueInYaml()
Replaces the value at the current position with this value.
private
changeValueInYaml(mixed $value) : mixed
The position should be set right at the start of this value (i.e. after its key).
Parameters
- $value : mixed
-
The new value to set into YAML
convertToYaml()
private
convertToYaml(mixed $data) : mixed
Parameters
- $data : mixed
decrementDepth()
private
decrementDepth() : mixed
findEndPositionOfValue()
private
findEndPositionOfValue(mixed $value[, mixed $offset = null ]) : mixed
Parameters
- $value : mixed
- $offset : mixed = null
findNextLineBreak()
private
findNextLineBreak(int $position) : mixed
Parameters
- $position : int
findPositionOfMultilineCharInLine()
private
findPositionOfMultilineCharInLine(int $position) : int|null
Parameters
- $position : int
Return values
int|nullfindPositionOfNextCharacter()
Advance until you find *one* of the characters in $chars.
private
findPositionOfNextCharacter(array<string|int, mixed> $chars) : mixed
Parameters
- $chars : array<string|int, mixed>
getCurrentData()
Returns the value in the current data that is currently being looked at.
private
getCurrentData([int $limitLevels = 0 ]) : mixed
This could fail if the currentPath is for new data.
Parameters
- $limitLevels : int = 0
-
If set to 1, the data 1 level up will be returned
getCurrentIndentation()
private
getCurrentIndentation([int $override = null ]) : string
Parameters
- $override : int = null
Return values
stringgetCurrentLine()
private
getCurrentLine(int $position) : mixed
Parameters
- $position : int
getEndOfKeyPosition()
private
getEndOfKeyPosition(mixed $key) : mixed
Parameters
- $key : mixed
getEndOfPreviousKeyPosition()
Finds the end position of the key that comes *before* this key.
private
getEndOfPreviousKeyPosition(mixed $key) : int
Parameters
- $key : mixed
Return values
intgetKeyRegex()
private
getKeyRegex(mixed $key) : mixed
Parameters
- $key : mixed
getPreferredIndentationSize()
Try to guess a preferred indentation level.
private
getPreferredIndentationSize() : int
Return values
intgetPreviousLine()
private
getPreviousLine(int $position) : mixed
Parameters
- $position : int
guessNextArrayTypeAndAdvance()
Attempts to guess if the array at the current position is a multi-line array or an inline array.
private
guessNextArrayTypeAndAdvance() : string
Return values
stringindentMultilineYamlArray()
Takes an unindented multi-line YAML string and indents it so it can be inserted into the current position.
private
indentMultilineYamlArray(string $yaml[, int $indentOverride = null ]) : string
Usually an empty line needs to be prepended to this result before adding to the content.
Parameters
- $yaml : string
- $indentOverride : int = null
Return values
stringisCharLineBreak()
private
isCharLineBreak(string $char) : bool
Parameters
- $char : string
Return values
boolisCurrentArrayMultiline()
private
isCurrentArrayMultiline() : bool
Return values
boolisCurrentArraySequence()
private
isCurrentArraySequence() : bool
Return values
boolisCurrentLineComment()
private
isCurrentLineComment(int $position) : bool
Parameters
- $position : int
Return values
boolisEOF()
private
isEOF([int $position = null ]) : mixed
Parameters
- $position : int = null
isFinalLineComment()
private
isFinalLineComment(string $content) : bool
Parameters
- $content : string
Return values
boolisHash()
Duplicated from Symfony's Inline::isHash().
private
isHash(mixed $value) : bool
Returns true if the value must be rendered as a hash, which includes an indexed array, if the indexes are not sequential.
Parameters
- $value : mixed
Return values
boolisLineComment()
private
isLineComment(string $line) : bool
Parameters
- $line : string
Return values
boolisMultilineString()
private
isMultilineString(mixed $value) : bool
Parameters
- $value : mixed
Return values
boolisPositionAtBeginningOfArray()
For the array currently being processed, are we currently handling the first key inside of it?
private
isPositionAtBeginningOfArray() : bool
Return values
boolisPreviousLineComment()
private
isPreviousLineComment(int $position) : bool
Parameters
- $position : int
Return values
boollog()
private
log(string $message[, mixed $includeContent = false ]) : mixed
Parameters
- $message : string
- $includeContent : mixed = false
manuallyIncrementIndentation()
private
manuallyIncrementIndentation() : mixed
normalizeSequences()
private
normalizeSequences(array<string|int, mixed> $data) : mixed
Parameters
- $data : array<string|int, mixed>
removeKeyAtCurrentPath()
private
removeKeyAtCurrentPath(array<string|int, mixed> $data) : array<string|int, mixed>
Parameters
- $data : array<string|int, mixed>
Return values
array<string|int, mixed>removeKeyFromYaml()
private
removeKeyFromYaml(mixed $key, mixed $currentVal) : mixed
Parameters
- $key : mixed
- $currentVal : mixed
removeMetadataKeys()
private
removeMetadataKeys(array<string|int, mixed> $data) : mixed
Parameters
- $data : array<string|int, mixed>
replaceSpecialMetadataCharacters()
private
replaceSpecialMetadataCharacters() : mixed
setValueAtCurrentPath()
private
setValueAtCurrentPath(mixed $value, array<string|int, mixed> $data[, int $limitLevels = 0 ]) : mixed
Parameters
- $value : mixed
- $data : array<string|int, mixed>
- $limitLevels : int = 0
updateContents()
private
updateContents(string $newContents, array<string|int, mixed> $newData, int $newPosition) : mixed
Parameters
- $newContents : string
- $newData : array<string|int, mixed>
- $newPosition : int
updateData()
private
updateData(array<string|int, mixed> $newData) : mixed
Parameters
- $newData : array<string|int, mixed>