Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thiagodp/traits
🔮 Be more productive with PHP
https://github.com/thiagodp/traits
array builder generator getter php phputil setter trait
Last synced: 2 months ago
JSON representation
🔮 Be more productive with PHP
- Host: GitHub
- URL: https://github.com/thiagodp/traits
- Owner: thiagodp
- License: mit
- Created: 2016-08-15T22:26:51.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-10-15T18:30:11.000Z (about 5 years ago)
- Last Synced: 2024-10-06T22:35:09.454Z (3 months ago)
- Topics: array, builder, generator, getter, php, phputil, setter, trait
- Language: PHP
- Homepage:
- Size: 11.7 KB
- Stars: 8
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# phputil\traits
Useful traits for PHP.[![Build Status](https://travis-ci.org/thiagodp/traits.svg?branch=master)](https://travis-ci.org/thiagodp/traits)
We use [semantic version](http://semver.org/). See [our releases](https://github.com/thiagodp/traits/releases).
## Installation
```command
composer require phputil/traits
```## Traits
* [GetterBuilder](https://github.com/thiagodp/traits/blob/master/lib/GetterBuilder.php)
* [WithBuilder](https://github.com/thiagodp/traits/blob/master/lib/WithBuilder.php)
* [GetterSetterWithBuilder](https://github.com/thiagodp/traits/blob/master/lib/GetterSetterWithBuilder.php)
* [FromArray](https://github.com/thiagodp/traits/blob/master/lib/FromArray.php)
* [ToArray](https://github.com/thiagodp/traits/blob/master/lib/ToArray.php)## Examples
Example on `GetterBuilder`:
```php
use phputil\traits\GetterBuilder;class MyClass {
use GetterBuilder; // simulate getters
private $name = '';
private $description = '';
function __construct( $name, $description ) {
$this->name = $name;
$this->description = $description;
}
}
$obj = new MyClass( 'Bob', 'I am Bob' );
echo $obj->getName(); // Bob
echo $obj->getDescription(); // I am Bob
```Example on `WithBuilder`:
```php
use phputil\traits\WithBuilder;class MyClass {
use WithBuilder;
public $name = '';
public $description = '';
}$obj = ( new MyClass() )->withName( 'Bob' )->withDescription( 'I am Bob' );
echo $obj->name; // Bob
echo $obj->description; // I am Bob
```Example on `GetterSetterWithBuilder`:
```php
use phputil\traits\GetterSetterWithBuilder;class MyClass {
use GetterSetterWithBuilder;
private $name = '';
private $description = '';
}
$obj = ( new MyClass() )->withName( 'Bob' )->setDescription( 'I am Bob' );
echo $obj->getName(); // Bob
echo $obj->getDescription(); // I am Bob
$obj->setName( 'Bob Dylan' );
echo $obj->getName(); // Bob Dylan
```Example on `FromArray`:
```php
use phputil\traits\FromArray;class MyClass {
use FromArray;
private $id;
protected $name;
public $age;
}
$obj = new MyClass();
$obj->fromArray( array( 'id' => 10, 'name' => 'Bob', 'age' => 18 ) );
var_dump( $obj ); // the attributes will have the array values
```Example on converting from a dynamic object:
```php
// From a converting from a dynamic object, just use a type casting
$p = new \stdClass;
$p->id = 10;
$p->name = 'Bob';
$p->age = 18;$obj = new MyClass();
$obj->fromArray( (array) $p ); // Just make a type casting to array ;)
```Example on `ToArray`:
```php
use phputil\traits\ToArray;class MyClass {
use ToArray;
private $id = 50;
protected $name = 'Bob';
public $age = 21;
}
$obj = new MyClass();
var_dump( $obj->toArray() ); // array( 'id' => 50, 'name' => 'Bob', 'age' => 21 )
```