Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
root = true

[*]
indent_style = space
indent_size = 4
Expand Down
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/.github export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/composer.lock export-ignore
/Makefile export-ignore
/phpcs.xml.dist export-ignore
/phpstan.neon.dist export-ignore
Expand Down
36 changes: 24 additions & 12 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,39 @@ name: tests

on: [push, pull_request]

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
tests:
name: Test PHP ${{ matrix.php }}
runs-on: ubuntu-latest
continue-on-error: ${{ matrix.experimental }}
strategy:
fail-fast: false
matrix:
php: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5']
experimental: [false]
php: ["7.4", "8.0", "8.1", "8.2", "8.3", "8.4", "8.5"]
include:
- php: '8.5'
- php: "8.5"
analysis: true

steps:
- name: Checkout
uses: actions/checkout@v6
uses: actions/checkout@v7

- name: Set up PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: xdebug
coverage: ${{ matrix.analysis && 'xdebug' || 'none' }}

- name: Install dependencies with Composer
run: composer install --no-progress --prefer-dist --optimize-autoloader
uses: ramsey/composer-install@v4
with:
composer-options: --optimize-autoloader

- name: Coding standards
if: matrix.analysis
Expand All @@ -37,12 +45,16 @@ jobs:
run: vendor/bin/phpstan

- name: Tests
if: ${{ !matrix.analysis }}
run: vendor/bin/phpunit

- name: Tests with coverage
if: matrix.analysis
run: vendor/bin/phpunit --coverage-clover clover.xml

- name: Upload coverage results to Coveralls
if: matrix.analysis
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
composer require php-coveralls/php-coveralls -n -W
vendor/bin/php-coveralls --coverage_clover=clover.xml -v
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
file: clover.xml
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
.idea
.phpunit.cache
.phpunit.result.cache
.vscode
clover.xml
Expand Down
24 changes: 17 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
.PHONY: install test phpunit phpcs phpcbf phpstan coverage help

PHP_BIN := vendor/bin

install:
composer install

test: phpunit phpcs phpstan

phpunit:
./vendor/bin/phpunit
$(PHP_BIN)/phpunit

phpcs:
./vendor/bin/phpcs
$(PHP_BIN)/phpcs

phpcbf:
$(PHP_BIN)/phpcbf

phpstan:
./vendor/bin/phpstan
$(PHP_BIN)/phpstan

coverage:
$(PHP_BIN)/phpunit --coverage-html coverage

test:
make phpunit
make phpcs
make phpstan
help:
@echo "Targets: install, test, phpunit, phpcs, phpcbf, phpstan, coverage"
32 changes: 19 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ $array = [];
<a name="count"></a>
### count()

Returns the number of items in a given key:
Returns the number of items in a given key. A non-countable value counts as
one item and a missing key counts as zero items:
```php
$dot->count('user.siblings');
```
Expand Down Expand Up @@ -223,10 +224,7 @@ echo $dot->get('user.name');
// ArrayAccess
echo $dot['user.name'];

// Equivalent vanilla PHP < 7.0
echo isset($array['user']['name']) ? $array['user']['name'] : null;

// Equivalent vanilla PHP >= 7.0
// Equivalent vanilla PHP
echo $array['user']['name'] ?? null;
```

Expand Down Expand Up @@ -342,11 +340,7 @@ Returns the value of a given key and deletes the key:
```php
echo $dot->pull('user.name');

// Equivalent vanilla PHP < 7.0
echo isset($array['user']['name']) ? $array['user']['name'] : null;
unset($array['user']['name']);

// Equivalent vanilla PHP >= 7.0
// Equivalent vanilla PHP
echo $array['user']['name'] ?? null;
unset($array['user']['name']);
```
Expand Down Expand Up @@ -380,6 +374,9 @@ $dot->push('John');
$array[] = 'John';
```

If the given key already holds a non-array, non-null value, the value is not
pushed and the Dot object is left unchanged.

<a name="replace"></a>
### replace()

Expand All @@ -393,7 +390,7 @@ array_replace($originalArray, $array);

Replaces the values with values having the same keys in the given array or Dot object with the given key:
```php
$dot->merge('user', $array);
$dot->replace('user', $array);

// Equivalent vanilla PHP
array_replace($originalArray['user'], $array);
Expand All @@ -418,7 +415,7 @@ Multiple key / value pairs:
```php
$dot->set([
'user.name' => 'John',
'page.title' => 'Home'
'page.title' => 'Home'
]);
```

Expand Down Expand Up @@ -451,6 +448,15 @@ Returns all the stored items as JSON:
echo $dot->toJson();
```

You can also pass [JSON encoding options](https://www.php.net/manual/en/json.constants.php):
```php
// For a given key
echo $dot->toJson('user', JSON_PRETTY_PRINT);

// For all the stored items
echo $dot->toJson(JSON_PRETTY_PRINT);
```

## Contributing

### Pull Requests
Expand All @@ -470,7 +476,7 @@ All pull requests must be accompanied by passing unit tests and complete code co

### Static Analysis

All pull requests must pass static analysis using [PHPStan](https://github.com/sebastianbergmann/phpunit/).
All pull requests must pass static analysis using [PHPStan](https://github.com/phpstan/phpstan).

## License

Expand Down
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
"require-dev": {
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.7",
"phpstan/phpstan": "^2.1"
"phpstan/phpstan": "^2.1",
"phpstan/phpstan-phpunit": "^2",
"phpcompatibility/php-compatibility": "^9.3",
"dealerdirect/phpcodesniffer-composer-installer": "^1.0"
},
"autoload": {
"files": [
Expand All @@ -26,5 +29,10 @@
"psr-4": {
"Adbar\\": "src"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
Loading