Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chillerlan/php-traits
A collection of (more or less) useful traits for PHP7.2+
https://github.com/chillerlan/php-traits
array dotenv helper magic php-spl-iterators php7 trait
Last synced: about 2 months ago
JSON representation
A collection of (more or less) useful traits for PHP7.2+
- Host: GitHub
- URL: https://github.com/chillerlan/php-traits
- Owner: chillerlan
- License: mit
- Archived: true
- Created: 2017-11-13T16:20:28.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-21T02:38:43.000Z (over 5 years ago)
- Last Synced: 2024-09-26T01:38:08.326Z (about 2 months ago)
- Topics: array, dotenv, helper, magic, php-spl-iterators, php7, trait
- Language: PHP
- Homepage:
- Size: 1.16 MB
- Stars: 18
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# chillerlan/php-traits
A collection of (more or less) useful traits for PHP7.2+
[![version][packagist-badge]][packagist]
[![license][license-badge]][license]
[![Travis][travis-badge]][travis]
[![Coverage][coverage-badge]][coverage]
[![Scrunitizer][scrutinizer-badge]][scrutinizer]
[![Packagist downloads][downloads-badge]][downloads]
[![PayPal donate][donate-badge]][donate][packagist-badge]: https://img.shields.io/packagist/v/chillerlan/php-traits.svg?style=flat-square
[packagist]: https://packagist.org/packages/chillerlan/php-traits
[license-badge]: https://img.shields.io/github/license/chillerlan/php-traits.svg?style=flat-square
[license]: https://github.com/chillerlan/php-traits/blob/master/LICENSE
[travis-badge]: https://img.shields.io/travis/chillerlan/php-traits.svg?style=flat-square
[travis]: https://travis-ci.org/chillerlan/php-traits
[coverage-badge]: https://img.shields.io/codecov/c/github/chillerlan/php-traits.svg?style=flat-square
[coverage]: https://codecov.io/github/chillerlan/php-traits
[scrutinizer-badge]: https://img.shields.io/scrutinizer/g/chillerlan/php-traits.svg?style=flat-square
[scrutinizer]: https://scrutinizer-ci.com/g/chillerlan/php-traits
[downloads-badge]: https://img.shields.io/packagist/dt/chillerlan/php-traits.svg?style=flat-square
[downloads]: https://packagist.org/packages/chillerlan/php-traits/stats
[donate-badge]: https://img.shields.io/badge/donate-paypal-ff33aa.svg?style=flat-square
[donate]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=WLYUNAT9ZTJZ4## Features
- `ClassLoader` - invokes objects of a given class and interface/type with an arbitrary count of constructor arguments
- `Magic` - turns methods into magic properties
- `Enumerable` - provides some of [prototype's enumerable methods](http://api.prototypejs.org/language/Enumerable/), implements `EnumerableInterface`
- `ArrayHelpers`
- `ByteArray` - useful for byte/bit-flipping purposes, extends [`SplFixedArray`](http://php.net/manual/class.splfixedarray.php)
- `ByteArrayDispenser` - creates `ByteArray` from several data types (hex, base64, binary, json etc.)
- `DotArray` - adds dot key notation functionality
- `SearchableArray` - deep search arrays using [`RecursiveIteratorIterator`](http://php.net/manual/class.recursiveiteratoriterator.php)
- `Interfaces`
- `ArrayAccessTrait` - implements [`ArrayAccess`](http://php.net/manual/class.arrayaccess.php)
- `IteratorTrait` - implements [`Iterator`](http://php.net/manual/class.iterator.php)
- `SPL`
- `CountableTrait` - implements [`Countable`](http://php.net/manual/class.countable.php)
- `SeekableIteratorTrait` - implements [`SeekableIterator`](http://php.net/manual/class.seekableiterator.php)
## Documentation### Installation
**requires [composer](https://getcomposer.org)***composer.json* (note: replace `dev-master` with a version boundary)
```json
{
"require": {
"php": "^7.2",
"chillerlan/php-traits": "dev-master"
}
}
```#### Manual installation
Download the desired version of the package from [master](https://github.com/chillerlan/php-traits/archive/master.zip) or
[release](https://github.com/chillerlan/php-traits/releases) and extract the contents to your project folder. After that:
- run `composer install` to install the required dependencies and generate `/vendor/autoload.php`.
- if you use a custom autoloader, point the namespace `chillerlan\Traits` to the folder `src` of the packageProfit!
### Usage
#### `ClassLoader`
Simple usage:
```php
class MyClass{
use ClassLoader;
protected function doStuff(string $class){
$obj = $this->loadClass(__NAMESPACE__.'\\Whatever\\'.$class);
// do stuff
}
}
```Let's assume we have several classes that implement the same interface, but their constructors have different parameter counts, like so:
```php
class SomeClass implements MyInterface{
public funtion __construct($param_foo){}
}class OtherClass implements MyInterface{
public funtion __construct($param_foo, $param_bar){}
}
```Initialize an object based on a selction
```php
class MyClass{
use ClassLoader;
protected $classes = [
'foo' => SomeClass::class,
'bar' => OtherClass::class
];
protected funtion initInterface(string $whatever, $foo, $bar = null):MyInterface{
foreach($this->classes as $what => $class){
if($whatever === $what){
return $this->loadClass($class, MyInterface::class, $foo, $bar);
}
}
}
}
```#### `Magic`
`Magic` allows to access internal methods like as properties.
```php
class MyMagicContainer{
use Magic;protected $foo;
protected function magic_get_foo(){
// do whatever...
return 'foo: '.$this->foo;
}protected function magic_set_foo($value){
// do stuff with $value
// ...
$this->foo = $value.'bar';
}
}
``````php
$magic = new MyMagicContainer;$magic->foo = 'foo';
var_dump($magic->foo); // -> foo: foobar
```
#### `Enumerable`
```php
class MyEnumerableContainer implements EnumerableInterface{
use Enumerable;public function __construct(array $data){
$this->array = $data;
}
}
``````php
$enum = new MyEnumerableContainer($data);$enum
->__each(function($value, $index){
// do stuff
$this->array[$index] = $stuff;
})
->__reverse()
->__to_array()
;$arr = $enum->__map(function($value, $index){
// do stuff
return $stuff;
});$enum;
```