Skip to content

Commit

Permalink
Added backslash to PHP internal functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nilportugues committed Nov 2, 2015
1 parent 5654b23 commit 89bc2a5
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 51 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
"nilportugues/json-api": "~1.0",
"symfony/psr-http-message-bridge": "~0.2"
},
"require-dev": {
"laravel/laravel": "5.*",
"fabpot/php-cs-fixer": "~1.9",
"nilportugues/php_backslasher": "~0.2"
},
"autoload": {
"psr-4": {
"NilPortugues\\Laravel5\\JsonApiSerializer\\" : "src/NilPortugues/Laravel5/JsonApiSerializer/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Laravel5\JsonApiSerializer\Facades;

use Illuminate\Support\Facades\Facade;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Author: Nil Portugués Calderó <[email protected]>
* Date: 8/18/15
* Time: 11:19 PM
* Time: 11:19 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -15,7 +15,8 @@
trait JsonApiResponseTrait
{
/**
* @param \Psr\Http\Message\ResponseInterface $response
* @param \Psr\Http\Message\ResponseInterface $response
*
* @return \Psr\Http\Message\ResponseInterface
*/
protected function addHeaders(\Psr\Http\Message\ResponseInterface $response)
Expand Down
35 changes: 15 additions & 20 deletions src/NilPortugues/Laravel5/JsonApiSerializer/JsonApiSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Laravel5\JsonApiSerializer;

use ErrorException;
Expand Down Expand Up @@ -48,20 +49,19 @@ protected function serializeObject($value)
return [self::MAP_TYPE => 'array', self::SCALAR_VALUE => $items];
}

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

$stdClass = (object) $value->getAttributes();
$data = $this->serializeData($stdClass);
$data[self::CLASS_IDENTIFIER_KEY] = get_class($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),
\get_class($value),
new ReflectionClass($value)
);

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

return $data;
Expand All @@ -79,40 +79,35 @@ protected function serializeObject($value)
*/
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;
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)) {
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')) {

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);
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Laravel5\JsonApiSerializer;

use Illuminate\Support\Facades\Cache;
Expand All @@ -33,21 +34,21 @@ class Laravel5JsonApiSerializerServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->publishes([__DIR__ . self::PATH => config('jsonapi.php')]);
$this->publishes([__DIR__.self::PATH => config('jsonapi.php')]);
}

/**
* Register the service provider.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__ . self::PATH, 'jsonapi');
$this->mergeConfigFrom(__DIR__.self::PATH, 'jsonapi');
$this->app->singleton(
\NilPortugues\Laravel5\JsonApiSerializer\JsonApiSerializer::class,
function ($app) {

$mapping = $app['config']->get('jsonapi');
$key = md5(json_encode($mapping));
$key = \md5(\json_encode($mapping));

return Cache::rememberForever(
$key,
Expand All @@ -67,7 +68,6 @@ function () use ($mapping) {
private static function parseRoutes(Mapper $mapper)
{
foreach ($mapper->getClassMap() as &$mapping) {

$mappingClass = new \ReflectionClass($mapping);

self::setUrlWithReflection($mapping, $mappingClass, 'resourceUrlPattern');
Expand All @@ -76,9 +76,9 @@ private static function parseRoutes(Mapper $mapper)
$mappingProperty->setAccessible(true);

$otherUrls = (array) $mappingProperty->getValue($mapping);
if(!empty($otherUrls)) {
if (!empty($otherUrls)) {
foreach ($otherUrls as &$url) {
$url = urldecode(route($url));
$url = \urldecode(route($url));
}
}
$mappingProperty->setValue($mapping, $otherUrls);
Expand All @@ -88,11 +88,11 @@ private static function parseRoutes(Mapper $mapper)
$mappingProperty->setAccessible(true);

$relationshipSelfUrl = (array) $mappingProperty->getValue($mapping);
if(!empty($relationshipSelfUrl)) {
if (!empty($relationshipSelfUrl)) {
foreach ($relationshipSelfUrl as &$urlMember) {
if(!empty($urlMember)) {
if (!empty($urlMember)) {
foreach ($urlMember as &$url) {
$url = urldecode(route($url));
$url = \urldecode(route($url));
}
}
}
Expand All @@ -103,7 +103,6 @@ private static function parseRoutes(Mapper $mapper)
return $mapper;
}


/**
* @param Mapping $mapping
* @param ReflectionClass $mappingClass
Expand All @@ -115,8 +114,8 @@ private static function setUrlWithReflection(Mapping $mapping, ReflectionClass $
$mappingProperty->setAccessible(true);
$value = $mappingProperty->getValue($mapping);

if(!empty($value)) {
$value = urldecode(route($value));
if (!empty($value)) {
$value = \urldecode(route($value));
$mappingProperty->setValue($mapping, $value);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/NilPortugues/Laravel5/JsonApiSerializer/Mapper/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Author: Nil Portugués Calderó <[email protected]>
* Date: 10/16/15
* Time: 8:59 PM
* Time: 8:59 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -19,7 +19,7 @@ class Mapper extends \NilPortugues\Api\Mapping\Mapper
*/
protected function buildMapping($mappedClass)
{
return (is_string($mappedClass) && class_exists($mappedClass, true)) ?
return (\is_string($mappedClass) && \class_exists($mappedClass, true)) ?
MappingFactory::fromClass($mappedClass) :
MappingFactory::fromArray($mappedClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Author: Nil Portugués Calderó <[email protected]>
* Date: 10/16/15
* Time: 8:59 PM
* Time: 8:59 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -17,8 +17,7 @@
use ReflectionMethod;

/**
* Class MappingFactory
* @package NilPortugues\Laravel5\JsonApiSerializer\Mapper
* Class MappingFactory.
*/
class MappingFactory extends \NilPortugues\Api\Mapping\MappingFactory
{
Expand All @@ -34,12 +33,12 @@ class MappingFactory extends \NilPortugues\Api\Mapping\MappingFactory
*/
protected static function getClassProperties($className)
{
if (class_exists($className, true)) {
if (\class_exists($className, true)) {
$reflection = new ReflectionClass($className);
$value = $reflection->newInstanceWithoutConstructor();
$value = $reflection->newInstanceWithoutConstructor();

if (is_subclass_of($value, Model::class, true)) {
$attributes = array_merge(
if (\is_subclass_of($value, Model::class, true)) {
$attributes = \array_merge(
Schema::getColumnListing($value->getTable()),
self::getRelationshipMethodsAsPropertyName($value, $className, $reflection)
);
Expand All @@ -48,7 +47,6 @@ protected static function getClassProperties($className)

return self::$eloquentClasses[$className];
}

}

return parent::getClassProperties($className);
Expand All @@ -65,25 +63,22 @@ protected static function getRelationshipMethodsAsPropertyName($value, $classNam
{
$methods = [];
foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {

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

$name = $method->name;
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)) {
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')) {
if (false !== \strpos(\get_class($returned), 'Illuminate\Database\Eloquent\Relations')) {
$methods[] = $method->name;
}

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

0 comments on commit 89bc2a5

Please sign in to comment.