Skip to content

Commit e8b5526

Browse files
author
P1ratRuleZZZ
committed
2 parents 998162e + 4c003d1 commit e8b5526

1 file changed

Lines changed: 55 additions & 14 deletions

File tree

README.md

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,71 @@ LazyAccess
44
LazyAccess is a wrapper around any arrays. Provides easy way of getting it's values or get a default value instead.
55

66
Replaces stupid long constructions like
7+
```php
8+
isset($var) ? $var : NULL.
9+
```
10+
# Installation
711

8-
isset($var) ? $var : NULL.
12+
## Using composer
913

10-
#Description
14+
Update your composer.json with following:
15+
```json
16+
"repositories": [
17+
{
18+
"type": "git",
19+
"url": "https://github.com/p1ratrulezzz/LazyAccess-to-PHP-arrays.git"
20+
}
21+
],
22+
require: {
23+
"p1ratrulezzz/lazyaccess": "master"
24+
}
25+
```
26+
and run
27+
```bash
28+
composer install
29+
```
30+
or (recommended)
31+
```bash
32+
composer require p1ratrulezzz/lazyaccess master
33+
```
34+
Second method will allow you to install this package without manual changes in composer.lock file.
1135

36+
## Manual installation
37+
```bash
38+
git clone --branch master https://github.com/p1ratrulezzz/LazyAccess-to-PHP-arrays.git lazyaccess
39+
```
40+
Then in PHP code include the files
41+
```php
42+
require_once 'lazyaccess/src/LazyAccess.php';
43+
require_once 'lazyaccess/src/LazyAccessTyped.php';
44+
```
45+
# Description
1246

1347
For example:
1448
usual PHP code is
15-
16-
$somevar = isset($array[$key]['key2'][0]) ? $array[$key]['key2'][0] : 'some_default_value';
17-
49+
```php
50+
$somevar = isset($array[$key]['key2'][0]) ? $array[$key]['key2'][0] : 'some_default_value';
51+
```
1852
This code is long and duplicates same things.
1953
With LazyAccess same code will be
20-
21-
$wrapper = new LazyAccessTyped($array); //Define it once somewhere in your code
22-
$somevar = $array[$key]->key2[0]->value('some_default_value');
23-
//or
24-
$somevar = $array[$key]['key2'][0]->value('some_default_value'); //the same as the above
25-
//or
26-
$somevar = $array->$key->key2->0->value('some_default_value'); //the same as the above
54+
```php
55+
$wrapper = new LazyAccessTyped($array); //Define it once somewhere in your code
56+
$somevar = $array[$key]->key2[0]->value('some_default_value');
57+
//or
58+
$somevar = $array[$key]['key2'][0]->value('some_default_value'); //the same as the above
59+
//or
60+
$somevar = $array->$key->key2->0->value('some_default_value'); //the same as the above
61+
// Also there are some wrappers with types: asString(), asInteger(), asDouble()
62+
$somevar = $array->{$key}->key2->0->asString('some_default_value');
63+
$somevar = $array->{$key}->key2->0->asInteger(0); // It will perform intval() operation before returning, so you can be sure that there will be an integer value.
64+
// asDouble() also will replace comma "," to a point ".", for example value 1,93 will be converted to 1.93
65+
$floating_point_value = new LazyAccessTyped(['test_float' => ['inner' => '1,93']])->test_float->inner->asDouble(0); // Will return 1.93
66+
```
67+
2768
It provides ability to use array operator ("[]") or object operator ("->") to access nesting array elements!
2869

29-
#Note
70+
# Note
3071

3172
There are two classes LazyAccess and LazyAccessTyped. LazyAccessTyped provides ability to use converters such as asFloat(), asInteger() and etc.
3273

33-
Please, do not use LazyAccess, cause it can behave unpredictible with it's return value. LazyAccessTyped is more better decision.
74+
Please, do not use LazyAccess, cause it can behave unpredictible with it's return value. LazyAccessTyped is much better and safer.

0 commit comments

Comments
 (0)