2017 © Pedro Peláez
 

library witchcraft

Opionated magic methods as traits

image

tuupola/witchcraft

Opionated magic methods as traits

  • Saturday, January 28, 2017
  • by tuupola
  • Repository
  • 2 Watchers
  • 20 Stars
  • 4,466 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 9 Versions
  • 2 % Grown

The README.md

Witchcraft

Opionated PHP magic methods as traits., (*1)

Author Software License Build Status HHVM Status Coverage, (*2)

Install

You can install latest version using composer., (*3)

$ composer require tuupola/witchcraft

Usage

You have your usual class with boilerplate accessors and mutators., (*4)

class Unicorn
{
    private $color;
    private $birthday;

    public function __construct($color = "white", $birthday = null)
    {
        $this->color = $color;
        $this->birthday = $birthday;
    }

    public function getColor()
    {
        return $this->color;
    }

    public function setColor($color)
    {
        $this->color = $color;
        return $this;
    }

    public function getBirthday()
    {
        return $this->birthday;
    }

    public function setBirthday($birthday)
    {
        $this->birthday = DateTime::createFromFormat("Y-m-d", $birthday);
        return $this;
    }

    public function getAge()
    {
        $now = new DateTime();
        return $this->birthday->diff($now)->format("%y years");
    }
}

It all works really nice with ide autocompletes and everything. Problem is your code looks quite ugly., (*5)

$unicorn = new Unicorn();
$unicorn->setBirthday("1930-24-12")->setColor("rainbow");
print $unicorn->getAge();

Magic methods

Witchcraft to the resque. If you add Witchcraft\MagicMethods trait you can use pretty methods., (*6)

class Unicorn
{
    use \Witchcraft\MagicMethods;

    /* Rest of the code stays exactly the same. */
}
$unicorn = new Unicorn();
$unicorn->birthday("1930-24-12")->color("rainbow");
print $unicorn->age();

Magic properties

If you add Witchcraft\MagicProperties trait you can use pretty properties., (*7)

class Unicorn
{
    use \Witchcraft\MagicProperties;

    /* Rest of the code stays exactly the same. */
}
$unicorn = new Unicorn();
$unicorn->birthday = "1930-24-12";
$unicorn->color = "rainbow";
print $unicorn->age;

Dynamic methods

As a bonus you can dynamically assing methods to the object., (*8)

$unicorn->something(function ($input) {
    return "Got {$input}!";
});

$unicorn->something("milk");

/* Got milk! */
$unicorn->something = function ($input) {
    return "No {$input} :(";
};

$unicorn->something("beer");

/* No beer :() */

Why?

Because I think getFoo() and setFoo("bar") are ugly., (*9)

Testing

You can run tests either manually..., (*10)

``` bash $ composer test, (*11)


... or automatically on every code change. You will need [entr](http://entrproject.org/) for this to work. ``` bash $ brew install entr $ composer watch

Contributing

Please see CONTRIBUTING for details., (*12)

Security

If you discover any security related issues, please email tuupola@appelsiini.net instead of using the issue tracker., (*13)

License

The MIT License (MIT). Please see License File for more information., (*14)

The Versions

28/01 2017

dev-master

9999999-dev https://github.com/tuupola/witchcraft

Opionated magic methods as traits

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

magic methods witchcraft

08/10 2016

1.1.0

1.1.0.0 https://github.com/tuupola/witchcraft

Opionated magic methods as traits

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

magic methods witchcraft

02/03 2016

1.0.1

1.0.1.0 https://github.com/tuupola/witchcraft

Opionated magic methods as traits

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

magic methods witchcraft

29/02 2016

1.0.0

1.0.0.0 https://github.com/tuupola/witchcraft

Opionated magic methods as traits

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

magic methods witchcraft

06/02 2016

0.3.0

0.3.0.0 https://github.com/tuupola/witchcraft

Opionated magic methods as traits

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

magic methods witchcraft

06/02 2016

dev-development

dev-development https://github.com/tuupola/witchcraft

Opionated magic methods as traits

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

magic methods witchcraft

31/03 2015

0.2.1

0.2.1.0 https://github.com/tuupola/witchcraft

Opionated magic methods as traits

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

magic methods witchcraft

16/03 2015

0.2.0

0.2.0.0 https://github.com/tuupola/witchcraft

Opionated magic methods as traits

  Sources   Download

MIT

The Requires

  • php ~5.4

 

The Development Requires

magic methods witchcraft

17/10 2014

0.1.0

0.1.0.0 https://github.com/tuupola/witchcraft

Opionated magic methods as traits

  Sources   Download

MIT

The Requires

  • php ~5.4

 

The Development Requires

magic methods witchcraft