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
namespace LeanMapper\Reflection;
use LeanMapper\Exception\InvalidAnnotationException;
use LeanMapper\Exception\UtilityClassException;
use LeanMapper\Relationship;
class PropertyFactory
{
public function __construct()
{
throw new UtilityClassException('Cannot instantiate utility class ' . get_called_class() . '.');
}
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
);
}
private static function createRelationship($sourceClass, PropertyType $propertyType, $containsCollection, $relationshipType, $definition = null)
{
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;
}
private static function trimNamespace($class)
{
$class = explode('\\', $class);
return end($class);
}
}