Skip to content

Commit

Permalink
Add TraitUtils::contain and is_contain_trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladislav Lyshenko committed Sep 15, 2015
0 parents commit d3ce001
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# phpstorm project files
.idea

# netbeans project files
nbproject

# zend studio for eclipse project files
.buildpath
.project
.settings

# windows thumbnail cache
Thumbs.db

# composer vendor dir
/vendor

# composer itself is not needed
/composer.phar

# Mac DS_Store Files
.DS_Store

# phpunit itself is not needed
phpunit.phar
# local phpunit config
/phpunit.xml
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changelog

All Notable changes to `trait-utils` will be documented in this file.

## 1.0.0 - 2015-09-15

- Initial release
- Add TraitUtils::contain and is_contain_trait
32 changes: 32 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

We accept contributions via Pull Requests on [Github](https://github.com/vladdnepr/trait-utils).


## Pull Requests

- **[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).

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.

- **Create feature branches** - Don't ask us to pull from your master branch.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **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.


## Running Tests

``` bash
$ phpunit
```


**Happy coding**!
25 changes: 25 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org>

38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# PHP Trait Contain

PHP Trait Utilities - contain PHP trait utilities

For example - check class contain some trait with check parent classes

- [Github Project](https://github.com/vladdnepr/trait-utils)

## Installation

If you do not have [Composer](http://getcomposer.org/), you may install it by following the instructions
at [getcomposer.org](http://getcomposer.org/doc/00-intro.md#installation-nix).

You can then install this package using the following command:

```php
php composer.phar require "vladdnepr/trait-utils" "*"
```
or add

```json
"vladdnepr/trait-utils": "*"
```

to the require section of your application's `composer.json` file.

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## Credits

- [Vladislav Lyshenko](https://github.com/vladdnepr)
- [All Contributors](../../contributors)

## License

Public domain. Please see [License File](LICENSE.md) for more information.
28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "vladdnepr/trait-utils",
"description": "PHP Trait Utilities",
"keywords": ["php", "trait", "contain", "instanceof", "utils", "utilities"],
"homepage": "https://github.com/vladdnepr/trait-utils",
"license": "public domain",
"authors": [
{
"name": "Vladislav Lyshenko",
"email": "vladdnepr1989@gmail.com"
}
],
"support": {
"issues": "https://github.com/vladdnepr/trait-utils/issues?state=open",
"source": "https://github.com/vladdnepr/trait-utils"
},
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "~4.5"
},
"autoload": {
"psr-4": {
"VladDnepr\\TraitUtils\\": "src"
}
}
}
44 changes: 44 additions & 0 deletions src/TraitUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace VladDnepr\TraitUtils;

class TraitUtils
{
/**
* Cache results of contain method
* @var array
*/
protected static $cache = [];

/**
* Return is the trait used by the given class or object
*
* @param mixed $object The tested object or class name
* @param string $trait_class Trait name
* @return bool
*/
public static function contain($object, $trait_class)
{
$cache_key = md5((is_object($object) ? get_class($object) : $object) . $trait_class);

if (!isset(self::$cache[$cache_key])) {
$traits = [];

do {
$traits = array_merge($traits, class_uses($object));
} while ($object = get_parent_class($object));

self::$cache[$cache_key] = isset($traits[$trait_class]);
}

return self::$cache[$cache_key];
}
}

/**
* @see TraitContain::contain()
*/
function is_contain_trait($object, $trait_class)
{
return TraitUtils::contain($object, $trait_class);
}

0 comments on commit d3ce001

Please sign in to comment.