1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 
<?php
namespace LeanMapper;
use DibiConnection;
use LeanMapper\Exception\InvalidArgumentException;
class Connection extends DibiConnection
{
    const WIRE_ENTITY = 1;
    const WIRE_PROPERTY = 2;
    const WIRE_ENTITY_AND_PROPERTY = 3;
    
    private $filters;
    
    public function registerFilter($name, $callback, $wiringSchema = null)
    {
        if (!preg_match('#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$#', $name)) {
            throw new InvalidArgumentException("Invalid filter name given: '$name'. For filter names apply the same rules as for function names in PHP.");
        }
        if (isset($this->filters[$name])) {
            throw new InvalidArgumentException("Filter with name '$name' was already registered.");
        }
        if (!is_callable($callback, true)) {
            throw new InvalidArgumentException("Callback given for filter '$name' is not callable.");
        }
        $this->filters[$name] = array($callback, $this->translateWiringSchema($wiringSchema));
    }
    
    public function hasFilter($name)
    {
        try {
            $this->checkFilterExistence($name);
        } catch (InvalidArgumentException $e) {
            return false;
        }
        return true;
    }
    
    public function getFilterCallback($name)
    {
        $this->checkFilterExistence($name);
        return $this->filters[$name][0];
    }
    
    public function getWiringSchema($filterName)
    {
        $this->checkFilterExistence($filterName);
        return $this->filters[$filterName][1];
    }
    
    public function command()
    {
        return new Fluent($this);
    }
    
    
    
    private function checkFilterExistence($name)
    {
        if (!isset($this->filters[$name])) {
            throw new InvalidArgumentException("Filter with name '$name' was not found.");
        }
    }
    
    private function translateWiringSchema($wiringSchema)
    {
        if ($wiringSchema === null) {
            return '';
        }
        if (is_int($wiringSchema)) {
            $result = '';
            if ($wiringSchema & self::WIRE_ENTITY) {
                $result .= 'e';
            }
            if ($wiringSchema & self::WIRE_PROPERTY) {
                $result .= 'p';
            }
            $wiringSchema = $result;
        } elseif (!preg_match('#^(?:([pe])(?!.*\1))*$#', $wiringSchema)) {
            throw new InvalidArgumentException("Invalid wiring schema given: '$wiringSchema'. Please use only characters p (Property) and e (Entity) in unique, non-repeating combination.");
        }
        return $wiringSchema;
    }
}