Skip to content

Commit d3ce001

Browse files
author
Vladislav Lyshenko
committed
Add TraitUtils::contain and is_contain_trait
0 parents  commit d3ce001

File tree

7 files changed

+202
-0
lines changed

7 files changed

+202
-0
lines changed

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# phpstorm project files
2+
.idea
3+
4+
# netbeans project files
5+
nbproject
6+
7+
# zend studio for eclipse project files
8+
.buildpath
9+
.project
10+
.settings
11+
12+
# windows thumbnail cache
13+
Thumbs.db
14+
15+
# composer vendor dir
16+
/vendor
17+
18+
# composer itself is not needed
19+
/composer.phar
20+
21+
# Mac DS_Store Files
22+
.DS_Store
23+
24+
# phpunit itself is not needed
25+
phpunit.phar
26+
# local phpunit config
27+
/phpunit.xml

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changelog
2+
3+
All Notable changes to `trait-utils` will be documented in this file.
4+
5+
## 1.0.0 - 2015-09-15
6+
7+
- Initial release
8+
- Add TraitUtils::contain and is_contain_trait

CONTRIBUTING.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Contributing
2+
3+
Contributions are **welcome** and will be fully **credited**.
4+
5+
We accept contributions via Pull Requests on [Github](https://github.com/vladdnepr/trait-utils).
6+
7+
8+
## Pull Requests
9+
10+
- **[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](http://pear.php.net/package/PHP_CodeSniffer).
11+
12+
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
13+
14+
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
15+
16+
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
17+
18+
- **Create feature branches** - Don't ask us to pull from your master branch.
19+
20+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
21+
22+
- **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 before submitting.
23+
24+
25+
## Running Tests
26+
27+
``` bash
28+
$ phpunit
29+
```
30+
31+
32+
**Happy coding**!

LICENSE.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
This is free and unencumbered software released into the public domain.
2+
3+
Anyone is free to copy, modify, publish, use, compile, sell, or
4+
distribute this software, either in source code form or as a compiled
5+
binary, for any purpose, commercial or non-commercial, and by any
6+
means.
7+
8+
In jurisdictions that recognize copyright laws, the author or authors
9+
of this software dedicate any and all copyright interest in the
10+
software to the public domain. We make this dedication for the benefit
11+
of the public at large and to the detriment of our heirs and
12+
successors. We intend this dedication to be an overt act of
13+
relinquishment in perpetuity of all present and future rights to this
14+
software under copyright law.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.
23+
24+
For more information, please refer to <http://unlicense.org>
25+

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# PHP Trait Contain
2+
3+
PHP Trait Utilities - contain PHP trait utilities
4+
5+
For example - check class contain some trait with check parent classes
6+
7+
- [Github Project](https://github.com/vladdnepr/trait-utils)
8+
9+
## Installation
10+
11+
If you do not have [Composer](http://getcomposer.org/), you may install it by following the instructions
12+
at [getcomposer.org](http://getcomposer.org/doc/00-intro.md#installation-nix).
13+
14+
You can then install this package using the following command:
15+
16+
```php
17+
php composer.phar require "vladdnepr/trait-utils" "*"
18+
```
19+
or add
20+
21+
```json
22+
"vladdnepr/trait-utils": "*"
23+
```
24+
25+
to the require section of your application's `composer.json` file.
26+
27+
## Contributing
28+
29+
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
30+
31+
## Credits
32+
33+
- [Vladislav Lyshenko](https://github.com/vladdnepr)
34+
- [All Contributors](../../contributors)
35+
36+
## License
37+
38+
Public domain. Please see [License File](LICENSE.md) for more information.

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "vladdnepr/trait-utils",
3+
"description": "PHP Trait Utilities",
4+
"keywords": ["php", "trait", "contain", "instanceof", "utils", "utilities"],
5+
"homepage": "https://github.com/vladdnepr/trait-utils",
6+
"license": "public domain",
7+
"authors": [
8+
{
9+
"name": "Vladislav Lyshenko",
10+
"email": "vladdnepr1989@gmail.com"
11+
}
12+
],
13+
"support": {
14+
"issues": "https://github.com/vladdnepr/trait-utils/issues?state=open",
15+
"source": "https://github.com/vladdnepr/trait-utils"
16+
},
17+
"require": {
18+
"php": ">=5.4.0"
19+
},
20+
"require-dev": {
21+
"phpunit/phpunit": "~4.5"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"VladDnepr\\TraitUtils\\": "src"
26+
}
27+
}
28+
}

src/TraitUtils.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace VladDnepr\TraitUtils;
4+
5+
class TraitUtils
6+
{
7+
/**
8+
* Cache results of contain method
9+
* @var array
10+
*/
11+
protected static $cache = [];
12+
13+
/**
14+
* Return is the trait used by the given class or object
15+
*
16+
* @param mixed $object The tested object or class name
17+
* @param string $trait_class Trait name
18+
* @return bool
19+
*/
20+
public static function contain($object, $trait_class)
21+
{
22+
$cache_key = md5((is_object($object) ? get_class($object) : $object) . $trait_class);
23+
24+
if (!isset(self::$cache[$cache_key])) {
25+
$traits = [];
26+
27+
do {
28+
$traits = array_merge($traits, class_uses($object));
29+
} while ($object = get_parent_class($object));
30+
31+
self::$cache[$cache_key] = isset($traits[$trait_class]);
32+
}
33+
34+
return self::$cache[$cache_key];
35+
}
36+
}
37+
38+
/**
39+
* @see TraitContain::contain()
40+
*/
41+
function is_contain_trait($object, $trait_class)
42+
{
43+
return TraitUtils::contain($object, $trait_class);
44+
}

0 commit comments

Comments
 (0)