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: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199:
<?php
namespace LeanMapper\Reflection;
use LeanMapper\Exception\InvalidAnnotationException;
use LeanMapper\Exception\UtilityClassException;
use LeanMapper\Relationship;
use LeanMapper\Result;
class PropertyFactory
{
public function __construct()
{
throw new UtilityClassException('Cannot instantiate utility class ' . get_called_class() . '.');
}
public static function createFromAnnotation($annotationType, $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+\(([^)]+)\))?
(?:\s+m:enum\(([^)]+)\))?
(?:\s+m:(?:(hasOne|hasMany|belongsToOne|belongsToMany)(?:\(([^)]+)\))?))?
(?:\s+m:filter\(([^)]+)\))?
(?:\s+m:extra\(([^)]+)\))?
~xi', $annotation, $matches);
if (!$matched) {
throw new InvalidAnnotationException("Invalid property annotation given: @$annotationType $annotation");
}
$propertyType = new PropertyType($matches[2], $aliases);
$containsCollection = $matches[3] !== '';
if ($propertyType->isBasicType() and $containsCollection) {
throw new InvalidAnnotationException("Unsupported property annotation given: @$annotationType $annotation");
}
$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: @$annotationType $annotation");
}
$name = substr($matches[5], 1);
$column = (isset($matches[6]) and $matches[6] !== '') ? $matches[6] : $name;
$propertyValuesEnum = null;
if (isset($matches[7]) and $matches[7] !== '') {
if (!$propertyType->isBasicType() or $propertyType->getType() === 'array') {
throw new InvalidAnnotationException("Invalid property annotation given: values of {$propertyType->getType()} property cannot be enumerated");
}
$propertyValuesEnum = new PropertyValuesEnum($matches[7], $reflection);
}
$propertyFilters = null;
if (isset($matches[10]) and $matches[10] !== '') {
if ($propertyType->isBasicType()) {
throw new InvalidAnnotationException("Invalid property annotation given: {$propertyType->getType()} property cannot be filtered");
}
$propertyFilters = new PropertyFilters($matches[10], $aliases);
}
$relationship = null;
if (isset($matches[8]) and $matches[8] !== '') {
$relationship = self::createRelationship(
$reflection->getName(),
$propertyType,
$containsCollection,
$matches[8],
(isset($matches[9]) and $matches[9] !== null) ? $matches[9] : null
);
}
if ($relationship !== null) {
if (isset($matches[6]) and $matches[6] !== '') {
throw new InvalidAnnotationException("All special column and table names must be specified in relationship definition when property holds relationship: @$annotationType $annotation");
}
$column = null;
if ($relationship instanceof Relationship\HasOne) {
$column = $relationship->getColumnReferencingTargetTable();
}
}
$extra = isset($matches[11]) ? $matches[11] : null;
return new Property(
$name,
$column,
$propertyType,
$annotationType === 'property',
$isNullable,
$containsCollection,
$relationship,
$propertyFilters,
$propertyValuesEnum,
$extra
);
}
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.");
}
}
if ($relationshipType !== 'hasOne') {
$strategy = Result::STRATEGY_IN;
if ($definition !== null and substr($definition, -6) === '#union') {
$strategy = Result::STRATEGY_UNION;
$definition = substr($strategy, 0, strlen($definition) - 6);
}
}
$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,
$strategy
);
case 'belongsToOne':
return new Relationship\BelongsToOne($pieces[0] ? : $sourceTable . '_id', $pieces[1] ? : $targetTable, $strategy);
case 'belongsToMany':
return new Relationship\BelongsToMany($pieces[0] ? : $sourceTable . '_id', $pieces[1] ? : $targetTable, $strategy);
}
return null;
}
private static function trimNamespace($class)
{
$class = explode('\\', $class);
return end($class);
}
}