Overview

Namespaces

  • LeanMapper
    • Exception
    • Reflection
    • Relationship

Classes

  • LeanMapper\Entity
  • 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\PropertyType
  • LeanMapper\Relationship\BelongsTo
  • LeanMapper\Relationship\BelongsToMany
  • LeanMapper\Relationship\BelongsToOne
  • LeanMapper\Relationship\HasMany
  • LeanMapper\Relationship\HasOne
  • LeanMapper\Repository
  • LeanMapper\Result
  • LeanMapper\Row

Exceptions

  • LeanMapper\Exception\InvalidAnnotationException
  • LeanMapper\Exception\InvalidArgumentException
  • LeanMapper\Exception\InvalidMethodCallException
  • LeanMapper\Exception\InvalidStateException
  • LeanMapper\Exception\InvalidValueException
  • LeanMapper\Exception\MemberAccessException
  • LeanMapper\Exception\RuntimeException
  • 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: 
<?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-mit.txt that was distributed with this source code.
 */

namespace LeanMapper\Reflection;

use LeanMapper\Exception\InvalidAnnotationException;
use LeanMapper\Exception\UtilityClassException;
use LeanMapper\Relationship;

/**
 * Property factory
 *
 * @author Vojtěch Kohout
 */
class PropertyFactory
{

    /**
     * @throws UtilityClassException
     */
    public function __construct()
    {
        throw new UtilityClassException('Cannot instantiate utility class ' . get_called_class() . '.');
    }

    /**
     * Creates new LeanMapper\Reflection\Property instance from given annotation
     *
     * @param string $annotation
     * @param EntityReflection $reflection
     * @return Property
     * @throws InvalidAnnotationException
     */
    public static function createFromAnnotation($annotation, EntityReflection $reflection)
    {
        $aliases = $reflection->getAliases();

        $matches = array();
        $matched = preg_match('~
            ^(null\|)?
            ((?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+)
            (\[\])?
            (\|null)? \s+
            (\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)
            (?:\s+m:(?:(hasOne|hasMany|belongsToOne|belongsToMany)(?:\(([^)]+)\))?))?
            (?:\s+m:filter\(([^)]+)\))?
        ~xi', $annotation, $matches);

        if (!$matched) {
            throw new InvalidAnnotationException("Invalid property annotation given: @property $annotation");
        }
        $containsCollection = $matches[3] !== '';
        $isNullable = ($matches[1] !== '' or $matches[4] !== '');

        if ($containsCollection and $isNullable) {
            throw new InvalidAnnotationException("It doesn't make sense to have a property containing collection nullable: @property $annotation");
        }
        $propertyType = new PropertyType($matches[2], $aliases);
        $propertyFilters = null;
        if (isset($matches[8])) {
            if ($propertyType->isBasicType()) {
                throw new InvalidAnnotationException("Invalid property annotation given: {$propertyType->getType()} property cannot be filtered");
            }
            $propertyFilters =  new PropertyFilters($matches[8], $aliases);
        }

        $relationship = null;
        if (isset($matches[6])) {
            $relationship = self::createRelationship(
                $reflection->getName(),
                $propertyType,
                $containsCollection,
                $matches[6],
                isset($matches[7]) ? $matches[7] : null
            );
        }

        return new Property(
            substr($matches[5], 1),
            $propertyType,
            $isNullable,
            $containsCollection,
            $relationship,
            $propertyFilters
        );
    }

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

    /**
     * @param string $sourceClass
     * @param PropertyType $propertyType
     * @param bool $containsCollection
     * @param string $relationshipType
     * @param string $definition
     * @return Relationship\BelongsToMany|Relationship\BelongsToOne|Relationship\HasMany|Relationship\HasOne|null
     * @throws InvalidAnnotationException
     */
    private static function createRelationship($sourceClass, PropertyType $propertyType, $containsCollection, $relationshipType, $definition = null)
    {
        // logic validation
        if ($propertyType->isBasicType()) {
            throw new InvalidAnnotationException("Invalid property annotation given: {$propertyType->getType()} property cannot have relationship.");
        }
        if ($relationshipType === 'hasMany' or $relationshipType === 'belongsToMany') {
            if (!$containsCollection) {
                throw new InvalidAnnotationException("Invalid property annotation given: property with '$relationshipType' relationship type must contain collection.");
            }
        } else {
            if ($containsCollection) {
                throw new InvalidAnnotationException("Invalid property annotation given: property with '$relationshipType' relationship type must not contain collection.");
            }
        }

        $pieces = array_replace(array_fill(0, 6, ''), $definition !== null ? explode(':', $definition) : array());

        $sourceTable = strtolower(self::trimNamespace($sourceClass));
        $targetTable = strtolower(self::trimNamespace($propertyType->getType()));

        switch ($relationshipType) {
            case 'hasOne':
                return new Relationship\HasOne($pieces[0] ? : $targetTable . '_id', $pieces[1] ? : $targetTable);
            case 'hasMany':
                return new Relationship\HasMany(
                    $pieces[0] ? : $sourceTable . '_id',
                    $pieces[1] ? : $sourceTable . '_' . $targetTable,
                    $pieces[2] ? : $targetTable . '_id',
                    $pieces[3] ? : $targetTable
                );
            case 'belongsToOne':
                return new Relationship\BelongsToOne($pieces[0] ? : $sourceTable . '_id', $pieces[1] ? : $targetTable);
            case 'belongsToMany':
                return new Relationship\BelongsToMany($pieces[0] ? : $sourceTable . '_id', $pieces[1] ? : $targetTable);
        }
        return null;
    }

    /**
     * @param string $class
     * @return string
     */
    private static function trimNamespace($class)
    {
        $class = explode('\\', $class);
        return end($class);
    }

}
tharos/leanmapper v1.3.0 API documentation API documentation generated by ApiGen