Skip to content

Commit 7a6d71d

Browse files
committed
initial commit
1 parent 692251e commit 7a6d71d

16 files changed

+516
-1
lines changed

Diff for: .editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

Diff for: .gitattributes

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Path-based git attributes
2+
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
3+
4+
# Ignore all test and documentation with "export-ignore".
5+
/.gitattributes export-ignore
6+
/.gitignore export-ignore
7+
/.travis.yml export-ignore
8+
/phpunit.xml.dist export-ignore
9+
/.scrutinizer.yml export-ignore
10+
/tests export-ignore

Diff for: .github/FUNDING.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# These are supported funding model platforms
2+
3+
github: [Gummibeer]
4+
patreon: # Replace with a single Patreon username
5+
open_collective: astrotomic
6+
ko_fi: # Replace with a single Ko-fi username
7+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9+
liberapay: # Replace with a single Liberapay username
10+
issuehunt: astrotomic
11+
otechie: # Replace with a single Otechie username
12+
custom: ['https://offset.earth/treeware']

Diff for: .github/workflows/composer-normalize.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: normalize composer.json
2+
3+
on:
4+
push:
5+
paths:
6+
- 'composer.json'
7+
pr:
8+
paths:
9+
- 'composer.json'
10+
11+
jobs:
12+
normalize:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Git checkout
16+
uses: actions/checkout@v2
17+
18+
- name: normalize composer.json
19+
run: |
20+
composer global require ergebnis/composer-normalize
21+
composer normalize
22+
23+
- uses: stefanzweifel/[email protected]
24+
with:
25+
commit_message: normalize composer.json

Diff for: .github/workflows/run-tests.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: run-tests
2+
3+
on:
4+
push:
5+
pr:
6+
schedule:
7+
- cron: '0 0 * * *'
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
fail-fast: true
15+
matrix:
16+
php: [7.4, 7.3, 7.2, 7.1]
17+
18+
name: P${{ matrix.php }} - ${{ matrix.dependency-version }}
19+
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v2
23+
24+
- name: Cache dependencies
25+
uses: actions/cache@v1
26+
with:
27+
path: ~/.composer/cache/files
28+
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
29+
keys: |
30+
dependencies-php-${{ matrix.php }}-composer-
31+
dependencies-php-
32+
33+
- name: Install dependencies
34+
run: composer update --prefer-dist --no-interaction --no-suggest
35+
36+
- name: Execute tests
37+
run: vendor/bin/pest

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/composer.lock
2+
/vendor/

Diff for: .styleci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
preset: laravel
2+
3+
risky: true

Diff for: CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Changelog
2+
3+
All notable changes to `php-conditional-proxy` will be documented in this file
4+
5+
## 0.1.0 - 2020-06-11
6+
7+
- initial release

Diff for: CONTRIBUTING.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Contributing
2+
3+
Contributions are **welcome** and will be fully **credited**.
4+
5+
Please read and understand the contribution guide before creating an issue or pull request.
6+
7+
## Etiquette
8+
9+
This project is open source, and as such, the maintainers give their free time to build and maintain the source code
10+
held within. They make the code freely available in the hope that it will be of use to other developers. It would be
11+
extremely unfair for them to suffer abuse or anger for their hard work.
12+
13+
Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the
14+
world that developers are civilized and selfless people.
15+
16+
It's the duty of the maintainer to ensure that all submissions to the project are of sufficient
17+
quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used.
18+
19+
## Viability
20+
21+
When requesting or submitting new features, first consider whether it might be useful to others. Open
22+
source projects are used by many developers, who may have entirely different needs to your own. Think about
23+
whether or not your feature is likely to be used by other users of the project.
24+
25+
## Procedure
26+
27+
Before filing an issue:
28+
29+
- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident.
30+
- Check to make sure your feature suggestion isn't already present within the project.
31+
- Check the pull requests tab to ensure that the bug doesn't have a fix in progress.
32+
- Check the pull requests tab to ensure that the feature isn't already in progress.
33+
34+
Before submitting a pull request:
35+
36+
- Check the codebase to ensure that your feature doesn't already exist.
37+
- Check the pull requests to ensure that another person hasn't already submitted the feature or fix.
38+
39+
## Requirements
40+
41+
If the project maintainer has any additional requirements, you will find them listed here.
42+
43+
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer).
44+
45+
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
46+
47+
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
48+
49+
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option.
50+
51+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
52+
53+
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
54+
55+
**Happy coding**!

Diff for: LICENSE renamed to LICENSE.md

File renamed without changes.

Diff for: README.md

+119-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,119 @@
1-
# php-conditional-proxy
1+
# PHP ConditionalProxy
2+
3+
[![Latest Version](http://img.shields.io/packagist/v/astrotomic/php-conditional-proxy.svg?label=Release&style=for-the-badge)](https://packagist.org/packages/astrotomic/php-conditional-proxy)
4+
[![MIT License](https://img.shields.io/github/license/Astrotomic/php-conditional-proxy.svg?label=License&color=blue&style=for-the-badge)](https://github.com/Astrotomic/php-conditional-proxy/blob/master/LICENSE)
5+
[![Offset Earth](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-green?style=for-the-badge)](https://plant.treeware.earth/Astrotomic/php-conditional-proxy)
6+
7+
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/Astrotomic/php-conditional-proxy/run-tests?style=flat-square&logoColor=white&logo=github&label=Tests)](https://github.com/Astrotomic/php-conditional-proxy/actions?query=workflow%3Arun-tests)
8+
[![StyleCI](https://styleci.io/repos/271375912/shield)](https://styleci.io/repos/271375912)
9+
[![Total Downloads](https://img.shields.io/packagist/dt/astrotomic/php-conditional-proxy.svg?label=Downloads&style=flat-square)](https://packagist.org/packages/astrotomic/php-conditional-proxy)
10+
11+
This package provides a trait and class to allow calling methods based on a condition without breaking the method chain.
12+
This is useful if you want to call a method only if the variable you insert has a value.
13+
14+
## Installation
15+
16+
You can install the package via composer:
17+
18+
```bash
19+
composer require astrotomic/php-conditional-proxy
20+
```
21+
22+
## Usage
23+
24+
The easiest will be to use the `\Astrotomic\ConditionalProxy\HasConditionalCalls` trait which adds a `when()` method.
25+
The `\Astrotomic\ConditionalProxy\ConditionalProxy` is made to work with methods returning `$this` because otherwise you can get unexpected results and wrong code-completion.
26+
27+
```php
28+
use Astrotomic\ConditionalProxy\HasConditionalCalls;
29+
30+
class MyClass
31+
{
32+
use HasConditionalCalls;
33+
}
34+
```
35+
36+
### Conditional chained Method
37+
38+
You can call the `when()` method by only passing the condition and chain the method to call if the condition is `true`.
39+
40+
```php
41+
// foo() bar() baz() will be called
42+
$class->foo()->when(true)->bar()->baz();
43+
44+
// foo() baz() will be called
45+
$class->foo()->when(false)->bar()->baz();
46+
```
47+
48+
### Conditional Callback
49+
50+
If you want you can also pass a callback as second parameter to the `when()` method.
51+
52+
```php
53+
// foo() bar() baz() will be called
54+
$class->foo()->when(true, fn($self) => $self->bar())->baz();
55+
56+
// foo() baz() will be called
57+
$class->foo()->when(false, fn($self) => $self->bar())->baz();
58+
```
59+
60+
### Advanced Usage
61+
62+
If you already have an `if()` or `when()` method and want to advance it or implement this behavior in any other method you can initialize and return the `\Astrotomic\ConditionalProxy\ConditionalProxy` yourself.
63+
64+
```php
65+
use Astrotomic\ConditionalProxy\ConditionalProxy;
66+
67+
class MyClass
68+
{
69+
public function if($condition)
70+
{
71+
return new ConditionalProxy($this, $condition);
72+
}
73+
74+
public function foo($foo)
75+
{
76+
$this->foo = $foo;
77+
78+
return $this;
79+
}
80+
}
81+
```
82+
83+
## Testing
84+
85+
``` bash
86+
composer test
87+
```
88+
89+
## Changelog
90+
91+
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
92+
93+
## Contributing
94+
95+
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
96+
97+
### Security
98+
99+
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
100+
101+
## Credits
102+
103+
- [Tom Witkowski](https://github.com/Gummibeer)
104+
- [All Contributors](../../contributors)
105+
106+
## License
107+
108+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
109+
110+
## Treeware
111+
112+
You're free to use this package, but if it makes it to your production environment I would highly appreciate you buying the world a tree.
113+
114+
It’s now common knowledge that one of the best tools to tackle the climate crisis and keep our temperatures from rising above 1.5C is to [plant trees](https://www.bbc.co.uk/news/science-environment-48870920). If you contribute to my forest you’ll be creating employment for local families and restoring wildlife habitats.
115+
116+
You can buy trees at [offset.earth/treeware](https://plant.treeware.earth/Astrotomic/php-conditional-proxy)
117+
118+
Read more about Treeware at https://treeware.earth
119+

Diff for: composer.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "astrotomic/php-conditional-proxy",
3+
"description": "Easily generate Open Graph tags",
4+
"keywords": [],
5+
"homepage": "https://github.com/Astrotomic/php-conditional-proxy",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Tom Witkowski",
10+
"email": "[email protected]",
11+
"homepage": "https://astrotomic.info",
12+
"role": "Developer"
13+
}
14+
],
15+
"require": {
16+
"php": "^7.1"
17+
},
18+
"require-dev": {
19+
"pestphp/pest": "^0.1.5"
20+
},
21+
"config": {
22+
"sort-packages": true
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"Astrotomic\\ConditionalProxy\\": "src"
27+
}
28+
},
29+
"minimum-stability": "dev",
30+
"prefer-stable": true,
31+
"scripts": {
32+
"test": "vendor/bin/pest"
33+
}
34+
}

Diff for: phpunit.xml.dist

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

Diff for: src/ConditionalProxy.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Astrotomic\ConditionalProxy;
4+
5+
class ConditionalProxy
6+
{
7+
protected $object;
8+
protected $condition;
9+
10+
/**
11+
* @param object $object
12+
* @param $condition
13+
*/
14+
public function __construct($object, $condition)
15+
{
16+
$this->object = $object;
17+
$this->condition = $condition;
18+
}
19+
20+
public function __call($name, $arguments)
21+
{
22+
if ($this->condition) {
23+
return call_user_func_array([$this->object, $name], $arguments);
24+
}
25+
26+
return $this->object;
27+
}
28+
}

0 commit comments

Comments
 (0)