All Projects → tuupola → witchcraft

tuupola / witchcraft

Licence: MIT license
Opionated PHP magic methods as traits for PHP 5.4+

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to witchcraft

php-traits
A collection of (more or less) useful traits for PHP7.2+
Stars: ✭ 17 (-26.09%)
Mutual labels:  magic, trait
PsData
Flexible data model plugin for Unreal Engine 4
Stars: ✭ 34 (+47.83%)
Mutual labels:  magic
Z.js
🦄 Hide text via Unicode's ZW(N)Js
Stars: ✭ 161 (+600%)
Mutual labels:  magic
discover-videos
This app is a clone of Netflix app, check out the course http://bit.ly/nextjs-udemy-ankita on how to build this
Stars: ✭ 81 (+252.17%)
Mutual labels:  magic
Yagmail
Send email in Python conveniently for gmail using yagmail
Stars: ✭ 2,169 (+9330.43%)
Mutual labels:  magic
docker-credential-magic
A magic shim for Docker credential helpers 🪄
Stars: ✭ 56 (+143.48%)
Mutual labels:  magic
Opendominion
A text-based, persistent browser-based strategy game (PBBG) in a fantasy war setting
Stars: ✭ 155 (+573.91%)
Mutual labels:  magic
docker-compose-laravel
A Docker Compose setup for Laravel projects.
Stars: ✭ 23 (+0%)
Mutual labels:  magic
ava-rethinkdb
🔗 RethinkDB helpers for AVA
Stars: ✭ 24 (+4.35%)
Mutual labels:  magic
Openram
An open-source static random access memory (SRAM) compiler.
Stars: ✭ 239 (+939.13%)
Mutual labels:  magic
Magic
Create your .Net Core/Angular/Database CRUD Web apps by simply clicking a button
Stars: ✭ 214 (+830.43%)
Mutual labels:  magic
Mtgjson
MTGJSON build scripts for Magic: the Gathering
Stars: ✭ 191 (+730.43%)
Mutual labels:  magic
Unicopia
The pony powers mod to power in your pony pony pony
Stars: ✭ 42 (+82.61%)
Mutual labels:  magic
File Type
Detect the file type of a Buffer/Uint8Array/ArrayBuffer
Stars: ✭ 2,386 (+10273.91%)
Mutual labels:  magic
jquery-stars
jQuery "Magic" animation plugin
Stars: ✭ 15 (-34.78%)
Mutual labels:  magic
Hitrava
Convert your Huawei Health sport activities and import them in Strava.
Stars: ✭ 156 (+578.26%)
Mutual labels:  magic
Abracadabra
Automated refactorings for VS Code (JS & TS) ✨ It's magic ✨
Stars: ✭ 204 (+786.96%)
Mutual labels:  magic
go-traits
A concept package that helps implement mixin behavior using embedded structs and hook interfaces.
Stars: ✭ 21 (-8.7%)
Mutual labels:  trait
behavior-trait
Allows handling events via inline declared methods, which can be added by traits
Stars: ✭ 18 (-21.74%)
Mutual labels:  trait
yii2-render-many
Trait for Yii Framework 2
Stars: ✭ 14 (-39.13%)
Mutual labels:  trait

Witchcraft

Opionated PHP magic methods as traits.

Author Software License Build Status HHVM Status Coverage

Install

You can install latest version using composer.

$ composer require tuupola/witchcraft

Usage

You have your usual class with boilerplate accessors and mutators.

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.

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

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.

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.

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

Testing

You can run tests either manually...

$ composer test

... or automatically on every code change. You will need entr for this to work.

$ brew install entr
$ composer watch

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].