Skip to content

Commit f7b3469

Browse files
committed
Properly document the array encoder
1 parent e768f4d commit f7b3469

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/Encoder/ArrayEncoder.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ public function encode($value, $depth, array $options, callable $encode)
4747
return $this->getFormattedArray($value, $depth, $options, $encode);
4848
}
4949

50+
/**
51+
* Returns the PHP code for the array as inline or multi line array.
52+
* @param array $array Array to encode
53+
* @param integer $depth Current indentation depth of the output
54+
* @param array $options List of encoder options
55+
* @param callable $encode Callback used to encode values
56+
* @return string The PHP code representation for the array
57+
*/
5058
private function getFormattedArray(array $array, $depth, $options, $encode)
5159
{
5260
$lines = $this->getPairs($array, ' ', $options['array.omit'], $encode, $omitted);
@@ -62,7 +70,13 @@ private function getFormattedArray(array $array, $depth, $options, $encode)
6270
return $this->buildArray($lines, $depth, $options);
6371
}
6472

65-
private function getInlineArray($lines, $options)
73+
/**
74+
* Returns the code for the inlined array, if possible.
75+
* @param string[] $lines Encoded key and value pairs
76+
* @param array $options List of encoder options
77+
* @return string|false Array encoded as single line of PHP code or false if not possible
78+
*/
79+
private function getInlineArray(array $lines, array $options)
6680
{
6781
$output = $this->wrap(implode(', ', $lines), $options['array.short']);
6882

@@ -75,6 +89,13 @@ private function getInlineArray($lines, $options)
7589
return $output;
7690
}
7791

92+
/**
93+
* Builds the complete array from the encoded key and value pairs.
94+
* @param string[] $lines Encoded key and value pairs
95+
* @param integer $depth Current indentation depth of the output
96+
* @param array $options List of encoder options
97+
* @return string Array encoded as PHP code
98+
*/
7899
private function buildArray(array $lines, $depth, array $options)
79100
{
80101
$indent = $this->buildIndent($options['array.base'], $options['array.indent'], $depth + 1);
@@ -121,7 +142,7 @@ private function buildIndent($base, $indent, $depth)
121142
* @param callable $encode Callback used to encode values
122143
* @return string[] Each of key and value pair encoded as php
123144
*/
124-
private function getAlignedPairs($array, callable $encode)
145+
private function getAlignedPairs(array $array, callable $encode)
125146
{
126147
$keys = [];
127148
$values = [];
@@ -150,7 +171,7 @@ private function getAlignedPairs($array, callable $encode)
150171
* @param boolean $omitted Set to true, if all the keys were omitted, false otherwise
151172
* @return string[] Each of key and value pair encoded as php
152173
*/
153-
private function getPairs($array, $space, $omit, callable $encode, & $omitted = true)
174+
private function getPairs(array $array, $space, $omit, callable $encode, & $omitted = true)
154175
{
155176
$pairs = [];
156177
$nextIndex = 0;
@@ -169,6 +190,12 @@ private function getPairs($array, $space, $omit, callable $encode, & $omitted =
169190
return $pairs;
170191
}
171192

193+
/**
194+
* Tells if the key can be omitted from array output based on expected index.
195+
* @param integer|string $key Current array key
196+
* @param integer $nextIndex Next expected key that can be omitted
197+
* @return bool True if the key can be omitted, false if not
198+
*/
172199
private function canOmit($key, & $nextIndex)
173200
{
174201
if (!is_int($key) || $key < $nextIndex) {

0 commit comments

Comments
 (0)