https://github.com/giann/trunk
🪵 A safe way to query data from a PHP array inspired by SwiftyJSON
https://github.com/giann/trunk
Last synced: about 1 year ago
JSON representation
🪵 A safe way to query data from a PHP array inspired by SwiftyJSON
- Host: GitHub
- URL: https://github.com/giann/trunk
- Owner: giann
- License: mit
- Created: 2022-04-28T14:56:32.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-08-20T09:58:56.000Z (almost 2 years ago)
- Last Synced: 2024-10-28T16:18:16.726Z (over 1 year ago)
- Language: PHP
- Homepage:
- Size: 25.4 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Trunk
A safe way to query data from a PHP array inspired by SwiftyJSON
```php
$trunk = new Trunk($data);
$this->assertTrue($trunk['astring'] instanceof Trunk);
$this->assertEquals($trunk['astring']->string(), 'hello world');
$this->assertEquals($trunk['anint']->int(), 12);
$this->assertEquals($trunk['alist']->listValue()[3]->string(), 'hello');
$this->assertEquals($trunk['amap']['hello']->string(), 'world');
$this->assertEquals($trunk['amap']['hello']['doesnexists']->data, null);
$this->assertTrue($trunk['anobject']->ofClass(Person::class) instanceof Person);
$this->assertEquals($trunk['anobject']->ofClass(Person::class)->name, 'joe');
$this->assertTrue($trunk['listofobject']->listOfClass(Person::class)[0] instanceof Person);
$this->assertEquals($trunk['listofobject']->listOfClass(Person::class)[0]->name, 'joe');
$this->assertTrue($trunk['mapofobject']->mapOfClass(Person::class)['joe'] instanceof Person);
$this->assertEquals($trunk['mapofobject']->mapOfClass(Person::class)['joe']->name, 'joe');
$this->assertTrue(
$trunk['transformlist']
->listOfClass(
Person::class,
fn ($el) => new Person($el)
)[0] instanceof Person
);
$this->assertTrue(
$trunk['transformmap']
->mapOfClass(
Person::class,
fn ($el) => new Person($el)
)['joe'] instanceof Person
);
```