Overview

Namespaces

  • LeanMapper
    • Bridges
      • Nette
        • DI
    • Exception
    • Reflection
    • Relationship

Classes

  • LeanMapper\Bridges\Nette\DI\LeanMapperExtension
  • LeanMapper\Caller
  • LeanMapper\Connection
  • LeanMapper\DataDifference
  • LeanMapper\DefaultEntityFactory
  • LeanMapper\DefaultMapper
  • LeanMapper\Entity
  • LeanMapper\EntityDataDecoder
  • LeanMapper\Events
  • LeanMapper\Filtering
  • LeanMapper\FilteringResult
  • LeanMapper\FilteringResultDecorator
  • LeanMapper\Fluent
  • LeanMapper\ImplicitFilters
  • LeanMapper\Reflection\Aliases
  • LeanMapper\Reflection\AliasesBuilder
  • LeanMapper\Reflection\AliasesParser
  • LeanMapper\Reflection\AnnotationsParser
  • LeanMapper\Reflection\EntityReflection
  • LeanMapper\Reflection\Property
  • LeanMapper\Reflection\PropertyFactory
  • LeanMapper\Reflection\PropertyFilters
  • LeanMapper\Reflection\PropertyMethods
  • LeanMapper\Reflection\PropertyPasses
  • LeanMapper\Reflection\PropertyType
  • LeanMapper\Reflection\PropertyValuesEnum
  • LeanMapper\Relationship\BelongsTo
  • LeanMapper\Relationship\BelongsToMany
  • LeanMapper\Relationship\BelongsToOne
  • LeanMapper\Relationship\HasMany
  • LeanMapper\Relationship\HasOne
  • LeanMapper\Repository
  • LeanMapper\Result
  • LeanMapper\ResultProxy
  • LeanMapper\Row

Interfaces

  • LeanMapper\IEntityFactory
  • LeanMapper\IMapper

Exceptions

  • LeanMapper\Exception\Exception
  • LeanMapper\Exception\InvalidAnnotationException
  • LeanMapper\Exception\InvalidArgumentException
  • LeanMapper\Exception\InvalidMethodCallException
  • LeanMapper\Exception\InvalidStateException
  • LeanMapper\Exception\InvalidValueException
  • LeanMapper\Exception\MemberAccessException
  • LeanMapper\Exception\UtilityClassException
  • Overview
  • Namespace
  • Class
  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: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 
<?php

/**
 * This file is part of the Lean Mapper library (http://www.leanmapper.com)
 *
 * Copyright (c) 2013 Vojtěch Kohout (aka Tharos)
 *
 * For the full copyright and license information, please view the file
 * license.md that was distributed with this source code.
 */

namespace LeanMapper;

use LeanMapper\Exception\InvalidArgumentException;

/**
 * \Dibi\Connection with filter support
 *
 * @author Vojtěch Kohout
 */
class Connection extends \Dibi\Connection
{

    const WIRE_ENTITY = 1;

    const WIRE_PROPERTY = 2;

    const WIRE_ENTITY_AND_PROPERTY = 3;

    /** @var array */
    private $filters;



    /**
     * Registers new filter
     *
     * @param string $name
     * @param mixed $callback
     * @param string|int|null $wiringSchema
     * @throws InvalidArgumentException
     */
    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] = [$callback, $this->translateWiringSchema($wiringSchema)];
    }



    /**
     * @param string $name
     * @return bool
     */
    public function hasFilter($name)
    {
        try {
            $this->checkFilterExistence($name);
        } catch (InvalidArgumentException $e) {
            return false;
        }
        return true;
    }



    /**
     * Gets callable filter's callback
     *
     * @param string $name
     * @return callable
     */
    public function getFilterCallback($name)
    {
        $this->checkFilterExistence($name);
        return $this->filters[$name][0];
    }



    /**
     * Gets wiring schema
     *
     * @param string $filterName
     * @return string
     */
    public function getWiringSchema($filterName)
    {
        $this->checkFilterExistence($filterName);
        return $this->filters[$filterName][1];
    }



    /**
     * Creates new instance of Fluent
     *
     * @return Fluent
     */
    public function command()
    {
        return new Fluent($this);
    }

    ////////////////////
    ////////////////////

    /**
     * @param string $name
     * @throws InvalidArgumentException
     */
    private function checkFilterExistence($name)
    {
        if (!isset($this->filters[$name])) {
            throw new InvalidArgumentException("Filter with name '$name' was not found.");
        }
    }



    /**
     * @param string|int|null $wiringSchema
     * @return string
     * @throws InvalidArgumentException
     */
    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;
    }

}
tharos/leanmapper v3.1.0 API documentation API documentation generated by ApiGen