https://github.com/ixnode/php-container
A collection of various PHP container classes like JSON, File, etc.
https://github.com/ixnode/php-container
Last synced: 4 months ago
JSON representation
A collection of various PHP container classes like JSON, File, etc.
- Host: GitHub
- URL: https://github.com/ixnode/php-container
- Owner: ixnode
- License: mit
- Created: 2022-12-30T15:01:39.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-25T21:37:47.000Z (over 2 years ago)
- Last Synced: 2024-04-26T21:20:22.046Z (about 2 years ago)
- Language: PHP
- Size: 103 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP Container
[](https://github.com/ixnode/php-container/releases)
[](https://github.com/twelvepics-com/php-calendar-builder/releases)
[](https://www.php.net/supported-versions.php)
[](https://phpstan.org/user-guide/rule-levels)
[](https://phpunit.de)
[](https://www.php-fig.org/psr/psr-12/)
[](https://github.com/phpmd/phpmd)
[](https://github.com/rectorphp/rector)
[](https://github.com/ixnode/php-container/blob/master/LICENSE)
> A collection of various PHP container classes like JSON, File, etc.
## 1) Installation
```bash
composer require ixnode/php-container
```
```bash
vendor/bin/php-container -V
```
```bash
php-container 0.1.0 (12-19-2022 01:17:26) - Björn Hempel
```
## 2) Usage
### 2.1) File
```php
use Ixnode\PhpContainer\File;
```
#### 2.1.1) Check if file exists
```php
$exists = (new File('path-to-file'))->exist();
```
```php
true || false
```
#### 2.1.2) Get the filesize (`integer` value)
```php
$fileSize = (new File('path-to-file'))->getFileSize();
```
```php
1523943
```
#### 2.1.3) Get the filesize (human readable)
```php
$fileSizeHuman = (new File('path-to-file'))->getFileSizeHuman();
```
```php
1.45 MB
```
#### 2.1.4) Get the file content
```php
$content = (new File('path-to-file'))->getContentAsText();
```
```php
line 1
line 2
line 3
...
```
#### 2.1.5) Get the file content as JSON object
```php
$content = (new File('path-to-json-file'))->getJson()->getJsonStringFormatted();
```
```php
{
"data": "Content of file 'path-to-json-file'."
}
```
### 2.2) JSON
```php
use Ixnode\PhpContainer\Json;
```
#### 2.2.1) Convert `array` to JSON
```php
$json = (new Json(['data' => 'json']))->getJsonStringFormatted();
```
```json
{
"data": "json"
}
```
#### 2.2.2) Convert JSON to `array`
```php
$array = (new Json('{"data": "json"}'))->getArray();
```
```php
[
'data' => 'json',
]
```
#### 2.2.3) Convert JSON file to `array`
```php
$array = (new Json(new File('path-to-json-file')))->getArray();
```
#### 2.2.4) Access to JSON object
```php
$json = (new Json([
'key1' => 'value1',
'key2' => [
'associative' => [
'name' => 'Test',
'id' => 123
],
'indexed' => [1, 2, 3],
],
'key3' => 'value3',
'key4' => 'value4',
'key5' => 'value5',
]));
```
```php
print $json->getKeyString(['key2', 'associative', 'name']);
// return value: (string) 'Test'
```
```php
print $json->getKeyInteger(['key2', 'indexed', 0]);
// return value: (int) 1
```
```php
print_r($json->getKeyArray(['key2', 'indexed']));
// return value: (array) [1, 2, 3]
```
#### 2.2.5) Build a new `array` from JSON
```php
$array = (new Json('[{"key1": 111, "key2": "222"},{"key1": 333, "key2": "444"}]'))->buildArray(
[
/* path []['key1'] as area1 */
'area1' => [['key1']],
/* path []['key2'] as area2 */
'area2' => [['key2']],
]
);
```
```php
[
'area1' => [111, 333],
'area2' => ['222', '444'],
]
```
### 2.3) CSV
```php
use Ixnode\PhpContainer\Csv;
```
#### 2.3.1) Parse CSV file to array
```php
$array = (new Csv(new File('path-to-csv-file')))->getArray();
```
Content of "path-to-csv-file":
```text
"headerLine1Cell1";"headerLine1Cell2"
"valueLine2Cell1";"valueLine2Cell2"
"valueLine3Cell1";"valueLine3Cell2"
```
Response:
```php
[
[
'headerLine1Cell1' => 'valueLine2Cell1',
'headerLine1Cell2' => 'valueLine2Cell2',
],
[
'headerLine1Cell1' => 'valueLine3Cell1',
'headerLine1Cell2' => 'valueLine3Cell2',
],
...
]
```
### 2.4) Curl
```php
use Ixnode\PhpContainer\Curl;
```
#### 2.4.1) Return the response value from 'URL'
```php
$text = (new Curl('URL')->getContentAsText();
```
### 2.5) Image
```php
use Ixnode\PhpContainer\Image;
```
#### 2.5.1) Return width of given image.
```php
$imageWidth = (new Image(new File('path-to-json-file')))->getWidth();
```
#### 2.5.2) Returns a resized image.
```php
$imageString = (new Image(new File('path-to-json-file')))->getImageString(1000, Image::FORMAT_JPG, 85);
```
## 3.) Development
```bash
git clone git@github.com:ixnode/php-container.git && cd php-container
```
```bash
composer install
```
```bash
composer test
```
## 4.) License
This tool is licensed under the MIT License - see the [LICENSE](/LICENSE) file for details