Skip to content

Commit

Permalink
Fetch all data from eloquent
Browse files Browse the repository at this point in the history
  • Loading branch information
nilportugues committed Oct 16, 2015
1 parent 47126a0 commit ea601fa
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/NilPortugues/Laravel5/JsonApiSerializer/JsonApiSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
*/
namespace NilPortugues\Laravel5\JsonApiSerializer;

use ErrorException;
use Illuminate\Database\Eloquent\Model;
use NilPortugues\Api\JsonApi\JsonApiTransformer;
use NilPortugues\Serializer\DeepCopySerializer;
use ReflectionClass;
use ReflectionMethod;

/**
* Class JsonApiSerializer.
Expand Down Expand Up @@ -46,13 +49,76 @@ protected function serializeObject($value)
}

if (is_subclass_of($value, Model::class, true)) {


$stdClass = (object) $value->getAttributes();
$data = $this->serializeData($stdClass);
$data[self::CLASS_IDENTIFIER_KEY] = get_class($value);

$methods = $this->getRelationshipMethodsAsPropertyName($value, get_class($value), new ReflectionClass($value));

if(!empty($methods)) {
$data = array_merge($data, $methods);
}

return $data;
}

return parent::serializeObject($value);
}


/**
* @param $value
* @param string $className
* @param ReflectionClass $reflection
*
* @return array
*/
protected 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')) {

$items = [];
foreach($returned->getResults() as $model) {

if(is_object($model)) {
$stdClass = (object) $model->getAttributes();
$data = $this->serializeData($stdClass);
$data[self::CLASS_IDENTIFIER_KEY] = get_class($model);

$items[] = $data;
}
}
if(!empty($items)) {
$methods[$name] = [self::MAP_TYPE => 'array', self::SCALAR_VALUE => $items];
}

}
}
} catch(ErrorException $e) {}
}
}
}


return $methods;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ protected static function getRelationshipMethodsAsPropertyName($value, $classNam
{
$methods = [];
foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {

if (ltrim($method->class, "\\") === ltrim($className, "\\")) {

$name = $method->name;
Expand Down

0 comments on commit ea601fa

Please sign in to comment.