|
| 1 | +# JSON To PHP |
| 2 | + |
| 3 | +`JSON To PHP` is an npm package that converts JSON strings into PHP-style associative arrays. It is useful when working |
| 4 | +with JavaScript applications that need to generate PHP code for dynamic content or server-side processing. |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +You can install the package via npm: |
| 9 | + |
| 10 | +```bash |
| 11 | +npm install jsontophp |
| 12 | +``` |
| 13 | + |
| 14 | +## Options: |
| 15 | + |
| 16 | +```typescript |
| 17 | +interface Options { |
| 18 | + beautify?: boolean; |
| 19 | + indentLevel?: number; |
| 20 | + indentSize?: number; |
| 21 | + useTabs?: boolean; |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +The Options interface allows you to customize the behavior of the jsonToPhp function. Below are the available |
| 26 | +properties: |
| 27 | + |
| 28 | +* `beautify` (optional, boolean): When set to true, the PHP array is formatted with line breaks and indentation for |
| 29 | + readability. Default is false. |
| 30 | +* `indentLevel` (optional, number): Specifies the initial indentation level for the PHP array. Default is 0. |
| 31 | +* `indentSize` (optional, number): Specifies the number of spaces for indentation when beautify is set to true. Ignored |
| 32 | + when `useTabs` is `true` Default is 4. |
| 33 | +* `useTabs` (optional, boolean): When set to true, tabs are used for indentation instead of spaces. Default is false. |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +After installation, you can import and use `jsonToPhp` to convert JSON strings into PHP array format. |
| 38 | + |
| 39 | +### Basic Conversion |
| 40 | + |
| 41 | +The `jsonToPhp` function takes a JSON string and converts it to a PHP-style associative array. |
| 42 | + |
| 43 | +```javascript |
| 44 | +import {jsonToPhp} from 'jsontophp'; |
| 45 | + |
| 46 | +const jsonString = '{"name": "John", "age": 30}'; |
| 47 | + |
| 48 | +const phpArray = jsonToPhp(jsonString); |
| 49 | +console.log(phpArray); |
| 50 | +// ['name' => 'John', 'age' => 30] |
| 51 | +``` |
| 52 | + |
| 53 | +### Nested JSON Conversion |
| 54 | + |
| 55 | +The package supports nested JSON objects, converting them into nested PHP arrays. |
| 56 | + |
| 57 | +```javascript |
| 58 | +const nestedJson = '{"user": {"name": "Alice", "email": "alice@example.com"}, "active": true}'; |
| 59 | + |
| 60 | +console.log(jsonToPhp(nestedJson)); |
| 61 | +// ['user' => ['name' => 'Alice', 'email' => 'alice@example.com'], 'active' => true] |
| 62 | +``` |
| 63 | + |
| 64 | +### Arrays in JSON |
| 65 | + |
| 66 | +You can also convert JSON arrays into PHP arrays. |
| 67 | + |
| 68 | +```javascript |
| 69 | +const jsonArray = '{"fruits": ["apple", "banana", "cherry"]}'; |
| 70 | + |
| 71 | +console.log(jsonToPhp(jsonArray)); |
| 72 | +// ['fruits' => ['apple', 'banana', 'cherry']] |
| 73 | +``` |
| 74 | + |
| 75 | +### Beautifying Output |
| 76 | + |
| 77 | +You can beautify the output using the `beautify` option, which adds line breaks and indentation to make the PHP array |
| 78 | +more readable. |
| 79 | + |
| 80 | +```javascript |
| 81 | +const jsonString = '{"name": "John", "address": {"city": "New York", "zip": "10001"}}'; |
| 82 | + |
| 83 | +console.log(jsonToPhp(jsonString, {beautify: true})); |
| 84 | +// [ |
| 85 | +// 'name' => 'John', |
| 86 | +// 'address' => [ |
| 87 | +// 'city' => 'New York', |
| 88 | +// 'zip' => '10001' |
| 89 | +// ] |
| 90 | +// ] |
| 91 | +``` |
| 92 | + |
| 93 | +### Indentation Level |
| 94 | + |
| 95 | +The `indentLevel` option allows you to specify the inital indentation level for the PHP array. |
| 96 | + |
| 97 | +```javascript |
| 98 | +console.log(jsonToPhp(jsonString, {beautify: true, indentLevel: 2})); |
| 99 | +// [ |
| 100 | +// 'name' => 'John', |
| 101 | +// 'address' => [ |
| 102 | +// 'city' => 'New York', |
| 103 | +// 'zip' => '10001' |
| 104 | +// ] |
| 105 | +// ] |
| 106 | +``` |
| 107 | + |
| 108 | +You can adjust the indentation size: |
| 109 | + |
| 110 | +```javascript |
| 111 | +console.log(jsonToPhp(jsonString, {beautify: true, indentSize: 2})); |
| 112 | +// [ |
| 113 | +// 'name' => 'John', |
| 114 | +// 'address' => [ |
| 115 | +// 'city' => 'New York', |
| 116 | +// 'zip' => '10001' |
| 117 | +// ] |
| 118 | +// ] |
| 119 | +``` |
| 120 | + |
| 121 | +### Using Tabs for Indentation |
| 122 | + |
| 123 | +You can use tabs for indentation by setting the `useTabs` option to `true`. |
| 124 | + |
| 125 | +```javascript |
| 126 | +console.log(jsonToPhp(jsonString, {beautify: true, useTabs: true})); |
| 127 | +// [ |
| 128 | +// 'name' => 'John', |
| 129 | +// 'address' => [ |
| 130 | +// 'city' => 'New York', |
| 131 | +// 'zip' => '10001' |
| 132 | +// ] |
| 133 | +// ] |
| 134 | +``` |
| 135 | + |
| 136 | +### Handling Different Data Types |
| 137 | + |
| 138 | +The package supports different JSON data types including strings, numbers, booleans, and null values. |
| 139 | + |
| 140 | +```javascript |
| 141 | +const jsonWithTypes = '{"isAdmin": true, "balance": 100.5, "preferences": null}'; |
| 142 | + |
| 143 | +console.log(jsonToPhp(jsonWithTypes)); |
| 144 | +// ['isAdmin' => true, 'balance' => 100.5, 'preferences' => null] |
| 145 | +``` |
| 146 | + |
| 147 | + |
| 148 | +## Edge Cases |
| 149 | + |
| 150 | +### Empty JSON |
| 151 | + |
| 152 | +If you provide an empty JSON string, the package will return an empty PHP array. |
| 153 | + |
| 154 | +```javascript |
| 155 | +console.log(jsonToPhp('{}')); |
| 156 | +// [] |
| 157 | +``` |
| 158 | + |
| 159 | +### Invalid JSON |
| 160 | + |
| 161 | +If the provided string is not valid JSON, it will throw an error. |
| 162 | + |
| 163 | +```javascript |
| 164 | +try { |
| 165 | + jsonToPhp('{invalid}'); |
| 166 | +} catch (error) { |
| 167 | + console.error('Invalid JSON format'); |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +## License |
| 172 | + |
| 173 | +MIT License |
| 174 | + |
| 175 | +Copyright (c) 2024 Andrei Surdu |
| 176 | + |
| 177 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 178 | +of this software and associated documentation files (the "Software"), to deal |
| 179 | +in the Software without restriction, including without limitation the rights |
| 180 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 181 | +copies of the Software, and to permit persons to whom the Software is |
| 182 | +furnished to do so, subject to the following conditions: |
| 183 | + |
| 184 | +The above copyright notice and this permission notice shall be included in all |
| 185 | +copies or substantial portions of the Software. |
| 186 | + |
| 187 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 188 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 189 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 190 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 191 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 192 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 193 | +SOFTWARE. |
0 commit comments