Skip to content

Commit

Permalink
Removed unnecessary mapping factory behaviour for Eloquent Models
Browse files Browse the repository at this point in the history
  • Loading branch information
nilportugues committed Dec 4, 2015
1 parent 4c37fe1 commit ec33c81
Showing 1 changed file with 1 addition and 42 deletions.
43 changes: 1 addition & 42 deletions src/NilPortugues/Laravel5/JsonApi/Mapper/MappingFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@

namespace NilPortugues\Laravel5\JsonApi\Mapper;

use ErrorException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Schema;
use ReflectionClass;
use ReflectionMethod;

/**
* Class MappingFactory.
Expand All @@ -38,10 +36,7 @@ protected static function getClassProperties($className)
$value = $reflection->newInstanceWithoutConstructor();

if (\is_subclass_of($value, Model::class, true)) {
$attributes = \array_merge(
Schema::getColumnListing($value->getTable()),
self::getRelationshipMethodsAsPropertyName($value, $className, $reflection)
);
$attributes = Schema::getColumnListing($value->getTable());

self::$eloquentClasses[$className] = $attributes;

Expand All @@ -52,40 +47,4 @@ protected static function getClassProperties($className)
return parent::getClassProperties($className);
}

/**
* @param $value
* @param string $className
* @param ReflectionClass $reflection
*
* @return array
*/
protected static function getRelationshipMethodsAsPropertyName($value, $className, ReflectionClass $reflection)
{
$methods = [];
foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
if (\ltrim($method->class, '\\') === \ltrim($className, '\\')) {
$name = $method->name;
$reflectionMethod = $reflection->getMethod($name);

// Eloquent relations do not include parameters, so we'll be filtering based on this criteria.
if (0 == $reflectionMethod->getNumberOfParameters()) {
try {
$returned = $reflectionMethod->invoke($value);
//All operations (eg: boolean operations) are now filtered out.
if (\is_object($returned)) {

// Only keep those methods as properties if these are returning Eloquent relations.
// But do not run the operation as it is an expensive operation.
if (false !== \strpos(\get_class($returned), 'Illuminate\Database\Eloquent\Relations')) {
$methods[] = $method->name;
}
}
} catch (ErrorException $e) {
}
}
}
}

return $methods;
}
}

0 comments on commit ec33c81

Please sign in to comment.