https://github.com/alrik11es/object-dot-notation
Object dot notation getter
https://github.com/alrik11es/object-dot-notation
composer dot dot-notation laravel notation object php5 symfony
Last synced: 10 months ago
JSON representation
Object dot notation getter
- Host: GitHub
- URL: https://github.com/alrik11es/object-dot-notation
- Owner: alrik11es
- License: mit
- Created: 2017-06-06T11:22:28.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-02-21T15:05:31.000Z (almost 7 years ago)
- Last Synced: 2025-03-23T22:25:23.079Z (10 months ago)
- Topics: composer, dot, dot-notation, laravel, notation, object, php5, symfony
- Language: PHP
- Homepage:
- Size: 84 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
 [](https://travis-ci.org/alrik11es/object-dot-notation)
The idea behind this library is to allow the access through
`config.port` dot notation to object data.
### Why?
My main problem was when accessing API data.
```json
{
"hits":{
"products": [
{
"name": "Shoe"
}
]
}
}
```
Imagine this is the result from an API. Usually to be sure that the data is what I want I'm gonna need to do:
```php
hits) && property_exists($result->hits, 'products')){
$whatiwant = $result->hits->products;
}
}
```
This is really time consuming. I just needed a way to do something like:
```php
get('hits.products');
```
**Note:** In PHP7 you can just use `$var = $something ?? $something2;` but if you need to do this dynamically it becomes harder to do.
## Demo
[You can try a demo here](http://demo.dotnotation.net)
## Installation
Requires composer and PHP5.6+
> $ composer require alrik11es/object-dot-notation
## Usage
When the property is not found a `null` will be returned.
Take this object as example:
```json
{
"config": {
"port": "1234",
"url": "testurl.com"
}
}
```
Then:
```php
get('config.port'); // 1234
echo $d->{'config.port'};
echo $d->config; // ['port'=>1234 ...]
```
### Array and array search
For other kind of uses you're gonna need to get a position of an array or search
and get the first value of array.
Take this object as example:
```json
{
"config": [{
"port": "80",
"url": "aurl.com"
},{
"port": "90",
"url": "burl.com"
}]
}
```
You can use this way to access the information:
```php
get('config[0].port'); // 80
echo $d->{'config[port=90|first].url'}; // burl.com
```
### Filters
You can use filters for the array selection process.
`[port=90|first]`
> IMPORTANT NOTE: Actually you can only use `|first` filter, use it always because it's going to be needed on future versions to avoid compatibility problems.
#### Advanced filters
***TODO***