2017 © Pedro Peláez
 

library gandalf

image

cloudson/gandalf

  • Sunday, January 26, 2014
  • by cloudson
  • Repository
  • 0 Watchers
  • 9 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Gandalf Build Status

Gandalf is a (crazy) php library that handles functions., (*1)

Where use?

  • php5.4+
  • Where there are courageous people

Why use?

As we know, our prefer language has object semi opened, I mean, we can define instance fields in execution time, see below:, (*2)

<?php

class Baggin
{

}

$bilbo = new Baggin;
$bilbo->hasRing =  true; 

var_dump($bilbo->hasRing); // true

But, we couldn't insert methods as functional programming languages (javascript, ruby ...), (*3)

<?php

class Elf
{

}

$legolas = new Elf;
$legolas->attack = function () {
    echo 'Goooo!';
};

$legolas->attack(); // Fatal error: Call to undefined method Elf::attack()

How use it

Use our trait:, (*4)

<?php

class Elf
{
    use Gandalf\Entity\Caller;
}

$legolas = new Elf;
$legolas->attack = function () {
    echo 'Goooo!';
};

$legolas->attack(); // Goooo! =) 

Define function pattern

In Doctrine\ORM exists a method dynamic for search entities:, (*5)

<?php

$repository->findOneByName('bar');
$repository->findByPlace('Middle earth');

with Gandalf you can write similar methods that use regex pattern, see below:, (*6)

<?php

$legolas = new Elf;
$legolas->def('findBy([A-Z][a-z]+)', function($value){
    return "Find by {$this->_1}";
});

$legolas->findByName('bilbo'); // "return 'Find by Name'"

note that $this->_1 is a group var regex. You could too use var $this->matches[1]., (*7)

Important: $this don't is the current context, (*8)

<?php 
$legolas = new Elf;
$legolas->def('find(One){0,1}By([A-Z][a-z]+)', function($value){
    var_dump($this->matches);
});

$legolas->findByName('bilbo'); // "['findByName', null, 'Name']"
$legolas->findOneByFamily('bilbo'); // "['findOneByFamily', null, 'Family']"

Creating shortcuts

Many times, we need write compound calls like, (*9)

<?php
return str_replace(' ', '-', strtolower(trim($foo)));

with Gandalf you can write this, (*10)

<?php
$foo = new Elf;
$foo->short('getSlug', [
            ['trim', ":param1"],
            ['strtolower', ":return1"],
            ['str_replace',' ', '-',":return2"],
        ]);
$foo->getSlug('How use crazy Gandalf lib!'); // how-use-crazy-gandalf-lib

Why you should be careful before uses this library on production.

  • Galdalf is a wizard, and wizards do magic. So, this library uses many php magic methods. The performance of the Galdalf can be a problem to your application.
  • For now, it's possible create a function with invalid name. E.g; a regex '/[a-z]#[a-z]/' is acceptable, but you never will call $foo->a#a() .

And...

Contact-me on twitter or cloudson@outlook.com if you want talk about this project. It would be awesome!, (*11)

Bitdeli Badge, (*12)

The Versions

26/01 2014

dev-master

9999999-dev

  Sources   Download

by Claudson Oliveira

26/01 2014

0.7.0

0.7.0.0

  Sources   Download

by Claudson Oliveira