Skip to content

Commit 563f70e

Browse files
authored
Merge pull request #16 from josantonius/release/2.0.0
Release/2.0.0
2 parents e1c1854 + 8fdf66e commit 563f70e

17 files changed

+852
-427
lines changed

.github/lang/es-ES/README.md

Lines changed: 87 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ Biblioteca PHP para la gestión de archivos JSON.
2020
- [Cómo empezar](#cómo-empezar)
2121
- [Uso](#uso)
2222
- [Tests](#tests)
23-
- [Manejo de excepciones](#manejo-de-excepciones)
2423
- [Tareas pendientes](#☑-tareas-pendientes)
2524
- [Registro de cambios](#registro-de-cambios)
2625
- [Contribuir](#contribuir)
@@ -33,9 +32,14 @@ Biblioteca PHP para la gestión de archivos JSON.
3332

3433
Esta biblioteca es compatible desde la versión **8.0** de PHP hasta la versión **8.1** de PHP.
3534

36-
Para versiones anteriores de PHP (desde la **5.6** hasta la **7.4**) puedes utilizar la
35+
Para seguir utilizando la versión con métodos estáticos sin las nuevas características:
36+
37+
- Para versiones anteriores de PHP (desde la **5.6** hasta la **7.4**), puedes utilizar la
3738
[versión 1.1.9](https://github.com/josantonius/php-json/tree/1.1.9) de esta biblioteca.
3839

40+
- Para las versiones **8.0** y **8.1** de PHP, puedes utilizar la
41+
[version 1.2.0](https://github.com/josantonius/php-json/tree/1.2.0) de esta biblioteca.
42+
3943
## Instalación
4044

4145
La mejor forma de instalar esta extensión es a través de [Composer](http://getcomposer.org/download/).
@@ -59,68 +63,49 @@ También puedes **clonar el repositorio** completo con Git:
5963
git clone https://github.com/josantonius/php-json.git
6064
```
6165

62-
O **instalarlo manualmente**:
63-
64-
Descargar [Json.php](https://raw.githubusercontent.com/josantonius/php-json/main/src/Json.php),
65-
[JsonLastError.php](https://raw.githubusercontent.com/josantonius/php-json/main/src/JsonLastError.php) y
66-
[JsonException.php](https://raw.githubusercontent.com/josantonius/php-json/main/src/Exception/JsonException.php):
67-
68-
```console
69-
wget https://raw.githubusercontent.com/josantonius/php-json/main/src/Json.php
70-
```
71-
72-
```console
73-
wget https://raw.githubusercontent.com/josantonius/php-json/main/src/JsonLastError.php
74-
```
75-
76-
```console
77-
wget https://raw.githubusercontent.com/josantonius/php-json/main/src/Exception/JsonException.php
78-
```
79-
8066
## Métodos disponibles
8167

8268
Métodos disponibles en esta biblioteca:
8369

84-
### Crear archivo JSON desde array
70+
### Obtener el contenido del archivo JSON
8571

8672
```php
87-
Json::arrayToFile($array, $file);
73+
$json->get();
8874
```
8975

90-
| Atributo | Descripción | Tipo | Requerido | Predeterminado
91-
| --- | --- | --- | --- | --- |
92-
| $array | Array a guardar en archivo JSON. | array || |
93-
| $file | Ruta hacia el archivo. | string || |
76+
**@throws** _CreateDirectoryException_ | _CreateFileException_ | _JsonErrorException_
9477

95-
**# Return** (boolean)
78+
**@Return** `array` - _Contenido del archivo_
9679

97-
### Guardar en array el contenido de archivo JSON
80+
### Establecer el contenido del archivo JSON
9881

9982
```php
100-
Json::fileToArray($file);
83+
$json->set(array|object $content);
10184
```
10285

103-
| Atributo | Descripción | Tipo | Requerido | Predeterminado
104-
| --- | --- | --- | --- | --- |
105-
| $file | Ruta o URL externa al archivo JSON. | string || |
86+
**@throws** _CreateFileException_ | _JsonErrorException_ | _UnavailableMethodException_
10687

107-
**# Return** (array|false)
88+
**@Return** `void`
10889

109-
### Comprobar si hay errores
90+
### Fusionar en el archivo JSON
11091

11192
```php
112-
JsonLastError::check();
93+
$json->merge(array|object $content);
11394
```
11495

115-
**# Return** (array|null) → Null si no hay errores o array con código de estado y mensaje de error.
96+
**@throws** _CreateFileException_ | _GetFileException_ | _JsonErrorException_ | _UnavailableMethodException_
11697

117-
### Obtener colección de errores JSON
98+
**@Return** `array` - _Array resultante_
99+
100+
### Incluir en el archivo JSON
118101

119102
```php
120-
JsonLastError::getCollection();
103+
$json->push(array|object $content);
121104
```
122105

123-
**# Return** (array) → Recopilación de posibles errores.
106+
**@throws** _CreateFileException_ | _GetFileException_ | _JsonErrorException_ | _UnavailableMethodException_
107+
108+
**@Return** `array` - _Array resultante_
124109

125110
## Cómo empezar
126111

@@ -132,56 +117,94 @@ require __DIR__ . '/vendor/autoload.php';
132117
use josantonius\Json\Json;
133118
```
134119

135-
Si la instalaste **manualmente**, utiliza:
120+
```php
121+
$json = new Json('path/to/file.json');
122+
123+
# Si el archivo no existe, se creará.
124+
```
125+
126+
O
136127

137128
```php
138-
require_once __DIR__ . '/Json.php';
139-
require_once __DIR__ . '/JsonLastError.php';
140-
require_once __DIR__ . '/JsonException.php';
129+
$json = new Json('https://site.com/file.json');
141130

142-
use josantonius\Json\Json;
131+
# Cuando el archivo JSON se obtiene desde una URL, sólo estará disponible el método "get".
143132
```
144133

145134
## Uso
146135

147136
Ejemplo de uso para esta biblioteca:
148137

149-
### Crear un archivo JSON desde un array
138+
### Obtener el contenido del archivo
150139

151-
```php
152-
$array = [
153-
'name' => 'josantonius',
154-
'email' => 'info@josantonius.com',
155-
'url' => 'https://github.com/josantonius/php-json'
156-
];
140+
```json
141+
{
142+
"foo": "bar"
143+
}
144+
```
157145

158-
$pathfile = __DIR__ . '/filename.json';
146+
```php
147+
$json->get();
148+
```
159149

160-
Json::arrayToFile($array, $pathfile);
150+
```php
151+
['foo' => 'bar']
161152
```
162153

163-
### Guardar en un array el contenido de un archivo JSON
154+
### Establecer el contenido del archivo
164155

165156
```php
166-
$pathfile = __DIR__ . '/filename.json';
157+
$json->set(['foo' => 'bar']);
158+
```
167159

168-
$array = Json::fileToArray($pathfile);
160+
```json
161+
{
162+
"foo": "bar"
163+
}
169164
```
170165

171-
### Comprobar si hubo errores
166+
### Fusionar en el archivo
167+
168+
```json
169+
{
170+
"foo": "bar"
171+
}
172+
```
172173

173174
```php
174-
$lastError = JsonLastError::check();
175+
$json->merge(['bar' => 'foo']);
176+
```
175177

176-
if (!is_null($lastError)) {
177-
var_dump($lastError);
178+
```json
179+
{
180+
"foo": "bar",
181+
"bar": "foo"
178182
}
179183
```
180184

181-
### Obtener colección con errores JSON
185+
### Incluir en el archivo
186+
187+
```json
188+
[
189+
{
190+
"name": "foo"
191+
}
192+
]
193+
```
182194

183195
```php
184-
$jsonLastErrorCollection = JsonLastError::getCollection();
196+
$json->push(['name' => 'bar']);
197+
```
198+
199+
```json
200+
[
201+
{
202+
"name": "foo"
203+
},
204+
{
205+
"name": "bar"
206+
}
207+
]
185208
```
186209

187210
## Tests
@@ -227,10 +250,6 @@ Ejecutar todas las pruebas anteriores:
227250
composer tests
228251
```
229252

230-
## Manejo de excepciones
231-
232-
Esta biblioteca utiliza [control de excepciones](src/Exception) que puedes personalizar a tu gusto.
233-
234253
## ☑ Tareas pendientes
235254

236255
- [ ] Añadir nueva funcionalidad.
@@ -254,8 +273,8 @@ _pull request_, comenzar una discusión o reportar un _issue_.
254273

255274
## Patrocinar
256275

257-
Si este proyecto te ayuda a reducir el tiempo de desarrollo,
258-
[¡puedes patrocinarme!](https://github.com/josantonius/lang/es-ES/README.md#patrocinar) :blush:
276+
Si este proyecto te ayuda a reducir el tiempo de desarrollo y quieres agradecérmelo,
277+
[¡podrías patrocinarme!](https://github.com/josantonius/lang/es-ES/README.md#patrocinar) :blush:
259278

260279
## Licencia
261280

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
with:
5757
php-version: '${{ matrix.php }}'
5858
coverage: 'none'
59-
tools: 'composer:v6'
59+
tools: 'composer:v2'
6060

6161
- name: 'Install dependencies'
6262
run: |
@@ -77,7 +77,7 @@ jobs:
7777
uses: 'shivammathur/setup-php@v2'
7878
with:
7979
php-version: '8.1'
80-
tools: 'composer:v6'
80+
tools: 'composer:v2'
8181

8282
- name: 'Install dependencies'
8383
run: |

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
vendor/
2+
coverage/
23
.vscode/
34
composer.phar
45
composer.lock

CHANGELOG.md

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,78 @@
11
# CHANGELOG
22

3+
## [2.0.0](https://github.com/josantonius/php-json/releases/tag/2.0.0) (2022-06-16)
4+
5+
* The library was completely refactored.
6+
7+
* Static methods are no longer used.
8+
9+
* New methods were added to merge and push content in JSON files.
10+
11+
* The JSON error handling class was removed in preference to a single method.
12+
13+
* To continue using the version with static methods without the new features:
14+
15+
* For older versions of PHP (from **5.6** to **7.4**),
16+
[version 1.1.9](https://github.com/josantonius/php-json/tree/1.1.9) of this library can be used.
17+
18+
* For PHP versions **8.0** and **8.1**,
19+
[version 1.2.0](https://github.com/josantonius/php-json/tree/1.2.0) of this library can be used.
20+
21+
* Deprecated `Josantonius\Json\Json::arrayToFile` method.
22+
* Deprecated `Josantonius\Json\Json::fileToArray` method.
23+
24+
* Deleted `Josantonius\Json\Exception\JsonException` class.
25+
* Deleted `Josantonius\Json\JsonLastError` class.
26+
* Deleted `Josantonius\Json\Tests\JsonLastErrorTest` class.
27+
28+
* Added `Josantonius\Json\Exception\CreateDirectoryException` class.
29+
* Added `Josantonius\Json\Exception\CreateFileException` class.
30+
* Added `Josantonius\Json\Exception\GetFileException` class.
31+
* Added `Josantonius\Json\Exception\JsonErrorException` class.
32+
* Added `Josantonius\Json\Exception\UnavailableMethodException` class.
33+
34+
* Deleted `Josantonius\Json\Json::createDirectory` method.
35+
* Deleted `Josantonius\Json\Json::saveFile` method.
36+
37+
* Added `Josantonius\Json\Json->__construct()` method.
38+
* Added `Josantonius\Json\Json->get()` method.
39+
* Added `Josantonius\Json\Json->set()` method.
40+
* Added `Josantonius\Json\Json->merge()` method.
41+
* Added `Josantonius\Json\Json->push()` method.
42+
* Added `Josantonius\Json\Json->createFileIfNotExists()` private method.
43+
* Added `Josantonius\Json\Json->createDirIfNotExists()` private method.
44+
* Added `Josantonius\Json\Json->getFileContents()` private method.
45+
* Added `Josantonius\Json\Json->saveToJsonFile()` private method.
46+
* Added `Josantonius\Json\Json->checkJsonLastError()` private method.
47+
48+
* Deleted `JsonTest->testGetCollection()` method.
49+
* Deleted `JsonTest->testArrayToFileCreateFileException()` method.
50+
* Deleted `JsonTest->testFileToArray()` method.
51+
* Deleted `JsonTest->testFileToArrayCreateFileException()` method.
52+
* Deleted `JsonTest->testExternalFileToArray()` method.
53+
* Deleted `JsonTest->testExternalFileNonExistentToArray()` method.
54+
55+
* Added `JsonTest->itShouldReturnValidInstance()` method.
56+
* Added `JsonTest->constructorShouldCreateTheFileIfNotExist()` method.
57+
* Added `JsonTest->constructorShouldThrowExceptionIfPathIsWrong()` method.
58+
* Added `JsonTest->constructorShouldThrowExceptionIfFilenameIsWrong()` method.
59+
* Added `JsonTest->itShouldGetFileContents()` method.
60+
* Added `JsonTest->itShouldGetRemoteFileContents()` method.
61+
* Added `JsonTest->itShouldSetArrayOnJsonFile()` method.
62+
* Added `JsonTest->itShouldSetObjectOnJsonFile()` method.
63+
* Added `JsonTest->itShouldThrowExceptionIfSetMethodIsUsedWithRemoteFile()` method.
64+
* Added `JsonTest->itShouldMergeArrayOnJsonFile()` method.
65+
* Added `JsonTest->itShouldMergeObjectOnJsonFile()` method.
66+
* Added `JsonTest->itShouldThrowExceptionIfMergeMethodIsUsedWithRemoteFile()` method.
67+
* Added `JsonTest->itShouldPushArrayOnJsonFile()` method.
68+
* Added `JsonTest->itShouldPushObjectOnJsonFile()` method.
69+
* Added `JsonTest->itShouldThrowExceptionIfPushMethodIsUsedWithRemoteFile()` method.
70+
* Added `JsonTest->itShouldThrowExceptionIfFileCannotBeObtained()` method.
71+
* Added `JsonTest->itShouldThrowExceptionIfRemoteFileCannotBeObtained()` method.
72+
* Added `JsonTest->itShouldThrowExceptionWhenThereAreJsonErrorsInTheFile()` method.
73+
* Added `JsonTest->arrayToFileStaticMethodShouldBehaveLikeTheSetMethod()` method.
74+
* Added `JsonTest->fileToArrayStaticMethodShouldBehaveLikeTheGetMethod()` method.
75+
376
## [1.2.0](https://github.com/josantonius/php-json/releases/tag/1.2.0) (2022-06-13)
477

578
* Support for PHP version **8.0** and higher.
@@ -128,7 +201,8 @@
128201

129202
## [1.1.2](https://github.com/josantonius/php-json/releases/tag/1.1.2) (2017-05-31)
130203

131-
* The file exception not found in the `fileToArray()` method was deleted. Now if it does not exist the file will create it with an empty array.
204+
* The file exception not found in the `fileToArray()` method was deleted.
205+
Now if it does not exist the file will create it with an empty array.
132206

133207
* `JSON_PRETTY_PRINT` was added at time to create the json file.
134208

@@ -142,7 +216,8 @@
142216

143217
## [1.0.0](https://github.com/josantonius/php-json/releases/tag/1.0.0) (2016-12-14)
144218

145-
* Compatible only with PHP 7.0 or higher. In the next versions, the library will be modified to make it compatible with PHP 5.6 or higher.
219+
* Compatible only with PHP 7.0 or higher.
220+
In the next versions, the library will be modified to make it compatible with PHP 5.6 or higher.
146221

147222
* Added `Josantonius\Json\Json` class.
148223
* Added `Josantonius\Json\Json::arrayToFile()` method.

0 commit comments

Comments
 (0)