|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Dingo\Api\Http\Response\Format; |
| 4 | + |
| 5 | +trait JsonOptionalFormatting |
| 6 | +{ |
| 7 | + /* |
| 8 | + * Supported JSON pretty print indent styles. |
| 9 | + * |
| 10 | + * @var array |
| 11 | + */ |
| 12 | + protected $indentStyles = [ |
| 13 | + 'tab', |
| 14 | + 'space', |
| 15 | + ]; |
| 16 | + |
| 17 | + /* |
| 18 | + * Indent styles, that are allowed to have various indent size. |
| 19 | + * |
| 20 | + * @var array |
| 21 | + */ |
| 22 | + protected $hasVariousIndentSize = [ |
| 23 | + 'space', |
| 24 | + ]; |
| 25 | + |
| 26 | + /* |
| 27 | + * Indent chars, associated with indent styles. |
| 28 | + * |
| 29 | + * @var array |
| 30 | + */ |
| 31 | + protected $indentChars = [ |
| 32 | + 'tab' => "\t", |
| 33 | + 'space' => ' ', |
| 34 | + ]; |
| 35 | + |
| 36 | + /* |
| 37 | + * JSON constants, that are allowed to be used as options while encoding. |
| 38 | + * Whitelist can be extended by other options in the future. |
| 39 | + * |
| 40 | + * @see http://php.net/manual/ru/json.constants.php |
| 41 | + * |
| 42 | + * @var array |
| 43 | + */ |
| 44 | + protected $jsonEncodeOptionsWhitelist = [ |
| 45 | + JSON_PRETTY_PRINT, |
| 46 | + ]; |
| 47 | + |
| 48 | + /** |
| 49 | + * Determine if JSON pretty print option is set to true. |
| 50 | + * |
| 51 | + * @return bool |
| 52 | + */ |
| 53 | + protected function isJsonPrettyPrintEnabled() |
| 54 | + { |
| 55 | + return isset($this->options['pretty_print']) && $this->options['pretty_print'] === true; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Determine if JSON custom indent style is set. |
| 60 | + * |
| 61 | + * @return bool |
| 62 | + */ |
| 63 | + protected function isCustomIndentStyleRequired() |
| 64 | + { |
| 65 | + return $this->isJsonPrettyPrintEnabled() && |
| 66 | + isset($this->options['indent_style']) && |
| 67 | + in_array($this->options['indent_style'], $this->indentStyles); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Perform JSON encode. |
| 72 | + * |
| 73 | + * @param string $content |
| 74 | + * @param array $jsonEncodeOptions |
| 75 | + * |
| 76 | + * @return string |
| 77 | + */ |
| 78 | + protected function performJsonEncoding($content, array $jsonEncodeOptions = []) |
| 79 | + { |
| 80 | + $jsonEncodeOptions = $this->filterJsonEncodeOptions($jsonEncodeOptions); |
| 81 | + |
| 82 | + $optionsBitmask = $this->calucateJsonEncodeOptionsBitmask($jsonEncodeOptions); |
| 83 | + |
| 84 | + if (($encodedString = json_encode($content, $optionsBitmask)) === false) { |
| 85 | + throw new \ErrorException('Error encoding data in JSON format: '.json_last_error()); |
| 86 | + } |
| 87 | + |
| 88 | + return $encodedString; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Filter JSON encode options array against the whitelist array. |
| 93 | + * |
| 94 | + * @param array $jsonEncodeOptions |
| 95 | + * |
| 96 | + * @return array |
| 97 | + */ |
| 98 | + protected function filterJsonEncodeOptions(array $jsonEncodeOptions) |
| 99 | + { |
| 100 | + return array_intersect($jsonEncodeOptions, $this->jsonEncodeOptionsWhitelist); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Sweep JSON encode options together to get options' bitmask. |
| 105 | + * |
| 106 | + * @param array $jsonEncodeOptions |
| 107 | + * |
| 108 | + * @return int |
| 109 | + */ |
| 110 | + protected function calucateJsonEncodeOptionsBitmask(array $jsonEncodeOptions) |
| 111 | + { |
| 112 | + return array_sum($jsonEncodeOptions); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Indent pretty printed JSON string, using given indent style. |
| 117 | + * |
| 118 | + * @param string $jsonString |
| 119 | + * @param string $indentStyle |
| 120 | + * @param int $defaultIndentSize |
| 121 | + * |
| 122 | + * @return string |
| 123 | + */ |
| 124 | + protected function indentPrettyPrintedJson($jsonString, $indentStyle, $defaultIndentSize = 2) |
| 125 | + { |
| 126 | + $indentChar = $this->getIndentCharForIndentStyle($indentStyle); |
| 127 | + $indentSize = $this->getPrettyPrintIndentSize() ?: $defaultIndentSize; |
| 128 | + |
| 129 | + // If the given indentation style is allowed to have various indent size |
| 130 | + // (number of chars, that are used to indent one level in each line), |
| 131 | + // indent the JSON string with given (or default) indent size. |
| 132 | + if ($this->hasVariousIndentSize($indentStyle)) { |
| 133 | + return $this->peformIndentation($jsonString, $indentChar, $indentSize); |
| 134 | + } |
| 135 | + |
| 136 | + // Otherwise following the convention, that indent styles, that does not |
| 137 | + // allowed to have various indent size (e.g. tab) are indented using |
| 138 | + // one tabulation character per one indent level in each line. |
| 139 | + return $this->peformIndentation($jsonString, $indentChar); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Get indent char for given indent style. |
| 144 | + * |
| 145 | + * @param string $indentStyle |
| 146 | + * |
| 147 | + * @return string |
| 148 | + */ |
| 149 | + protected function getIndentCharForIndentStyle($indentStyle) |
| 150 | + { |
| 151 | + return $this->indentChars[$indentStyle]; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Get indent size for pretty printed JSON string. |
| 156 | + * |
| 157 | + * @return int|null |
| 158 | + */ |
| 159 | + protected function getPrettyPrintIndentSize() |
| 160 | + { |
| 161 | + return isset($this->options['indent_size']) |
| 162 | + ? (int) $this->options['indent_size'] |
| 163 | + : null; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Determine if indent style is allowed to have various indent size. |
| 168 | + * |
| 169 | + * @param string $indentStyle |
| 170 | + * |
| 171 | + * @return bool |
| 172 | + */ |
| 173 | + protected function hasVariousIndentSize($indentStyle) |
| 174 | + { |
| 175 | + return in_array($indentStyle, $this->hasVariousIndentSize); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Perform indentation for pretty printed JSON string with a given |
| 180 | + * indent char, repeated N times, as determined by indent size. |
| 181 | + * |
| 182 | + * @param string $jsonString JSON string, which must be indented |
| 183 | + * @param string $indentChar Char, used for indent (default is tab) |
| 184 | + * @param int $indentSize Number of times to repeat indent char per one indent level |
| 185 | + * @param int $defaultSpaces Default number of indent spaces after json_encode() |
| 186 | + * |
| 187 | + * @return string |
| 188 | + */ |
| 189 | + protected function peformIndentation($jsonString, $indentChar = "\t", $indentSize = 1, $defaultSpaces = 4) |
| 190 | + { |
| 191 | + $pattern = '/(^|\G) {'.$defaultSpaces.'}/m'; |
| 192 | + $replacement = str_repeat($indentChar, $indentSize).'$1'; |
| 193 | + |
| 194 | + return preg_replace($pattern, $replacement, $jsonString); |
| 195 | + } |
| 196 | +} |
0 commit comments