Skip to content
This repository was archived by the owner on Jul 27, 2020. It is now read-only.

Commit ea2b642

Browse files
committed
- Atualização namespace lib doctrine-json-functions
- Nova opções para retorno de dados
1 parent 2e54d66 commit ea2b642

File tree

7 files changed

+216
-23
lines changed

7 files changed

+216
-23
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ class UserDocRepository extends DoctrineBaseRepository implements UserRepository
169169
*/
170170
public function create(array $params): object
171171
{
172+
//$this->setReturn('doctrine'); //optional - default: array
172173
return $this->setCreateArray([
173174
'fieldTest1',
174175
'fieldTest2:fk=classNameFk', //foreign key
@@ -186,6 +187,7 @@ class UserDocRepository extends DoctrineBaseRepository implements UserRepository
186187
*/
187188
public function update(array $params, int $id): object
188189
{
190+
//$this->setReturn('doctrine'); //optional - default: array
189191
return $this->setUpdateArray([
190192
'fieldTest1',
191193
'fieldTest2:fk=classNameFk', //foreign key
@@ -233,6 +235,8 @@ class UserDocRepository extends DoctrineBaseRepository implements UserRepository
233235
//set hidration (optional) - more information see the documentation.
234236
//$this->addHydrateObject();
235237
//$this->addCustomHydrationMode('ObjectAndScalarHydrator');
238+
239+
//$this->setReturn('doctrine'); //optional - default: array
236240
}
237241

238242
/*-------------------------------------------------------------------------------------
@@ -640,6 +644,7 @@ file: `app/Repositories/User/UserDocRepository.php`
640644
*/
641645
public function create(array $params): object
642646
{
647+
//$this->setReturn('doctrine'); //optional - default: array
643648
return $this->setCreateArray([
644649
'firstName',
645650
'lastName',
@@ -661,6 +666,7 @@ Do not automatically inserted `createdAt`
661666
*/
662667
public function create(array $params): object
663668
{
669+
//$this->setReturn('doctrine'); //optional - default: array
664670
return $this->setCreateArray([
665671
'firstName',
666672
'lastName',
@@ -682,6 +688,8 @@ Dealing with the object of return:
682688
*/
683689
public function create(array $params): object
684690
{
691+
//$this->setReturn('doctrine'); //optional - default: array
692+
685693
//Ex 1: The return object will return only the firstName and lastName fields
686694
return $this->setCreateArray([
687695
'firstName',
@@ -745,6 +753,7 @@ file: `app/Repositories/User/UserDocRepository.php`
745753
*/
746754
public function update(array $params, int $id): object
747755
{
756+
//$this->setReturn('doctrine'); //optional - default: array
748757
return $this->setUpdateArray([
749758
'firstName',
750759
'lastName',
@@ -767,6 +776,7 @@ Do not automatically update `updatedAt`
767776
*/
768777
public function update(array $params, int $id): object
769778
{
779+
//$this->setReturn('doctrine'); //optional - default: array
770780
return $this->setUpdateArray([
771781
'firstName',
772782
'lastName',
@@ -789,6 +799,8 @@ Dealing with the object of return:
789799
*/
790800
public function update(array $params, int $id): object
791801
{
802+
//$this->setReturn('doctrine'); //optional - default: array
803+
792804
//Ex 1: The return object will return only the firstName and lastName fields
793805
return $this->setUpdateArray([
794806
'firstName',
@@ -1052,6 +1064,8 @@ public function selectAll()
10521064
//$this->addHydrateSingleScalar();
10531065
//$this->addCustomHydrationMode('ObjectAndScalarHydrator');
10541066
//$this->addCustomHydrationMode('ArrayHydratorCustom');
1067+
1068+
//$this->setReturn('doctrine'); //optional - default: array
10551069
}
10561070

10571071
/*-------------------------------------------------------------------------------------
@@ -1367,6 +1381,8 @@ public function selectDql(array $params): object
13671381
$this->setParameterDql(0, $params['id']); //set parameter
13681382
$this->paginatorDql($params['firstResult'], $params['maxResults']); //paginator
13691383

1384+
//$this->setReturn('doctrine'); //optional - default: array
1385+
13701386
//result
13711387
return $this->getResultDql();
13721388

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"beberlei/DoctrineExtensions": "^1.1",
1616
"symfony/property-access": "^4.1",
1717
"symfony/serializer": "^4.1",
18-
"syslogic/doctrine-json-functions": "~2.0"
18+
"scienta/doctrine-json-functions": "~4.0"
1919
},
2020
"require-dev": {
2121
"phpunit/phpunit": "5.5.*",

config/doctrine.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
'updatedAtFieldName' => 'updatedAt',
100100
'indexNameResultsArray' => 'data',
101101
'indexNamePaginationArray' => 'meta',
102+
'return' => 'array', //stdClass | array | doctrine
102103
],
103104
//------------------------------------------------------------------------
104105
/*
@@ -163,16 +164,17 @@
163164
'CONCAT_WS' => DoctrineExtensions\Query\Mysql\ConcatWs::class,
164165
'ROUND' => DoctrineExtensions\Query\Mysql\Round::class,
165166
'CAST' => DoctrineExtensions\Query\Mysql\Cast::class,
166-
'JSON_OBJECT' => Syslogic\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonObject::class,
167-
'JSON_ARRAY' => Syslogic\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonArray::class,
168-
'JSON_QUOTE' => Syslogic\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonQuote::class,
169-
'JSON_UNQUOTE' => Syslogic\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonUnquote::class,
170-
'JSON_EXTRACT' => Syslogic\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonExtract::class,
171-
'JSON_REPLACE' => Syslogic\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonReplace::class,
172-
'JSON_INSERT' => Syslogic\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonInsert::class,
173-
'JSON_SET' => Syslogic\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonSet::class,
174-
'JSON_ARRAY_APPEND' => Syslogic\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonArrayAppend::class,
175-
'JSON_ARRAY_INSERT' => Syslogic\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonArrayInsert::class,
167+
'JSON_OBJECT' => Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonObject::class,
168+
'JSON_ARRAY' => Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonArray::class,
169+
'JSON_QUOTE' => Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonQuote::class,
170+
'JSON_UNQUOTE' => Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonUnquote::class,
171+
'JSON_EXTRACT' => Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonExtract::class,
172+
'JSON_REPLACE' => Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonReplace::class,
173+
'JSON_INSERT' => Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonInsert::class,
174+
'JSON_SET' => Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonSet::class,
175+
'JSON_ARRAY_APPEND' => Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonArrayAppend::class,
176+
'JSON_ARRAY_INSERT' => Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonArrayInsert::class,
177+
'JSON_CONTAINS' => Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonContains::class,
176178
],
177179
/*
178180
|--------------------------------------------------------------------------

src/Console/Stubs/baseRepository.stub

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class {{nameclass}} extends DoctrineBaseRepository {{interface}}
5757
*/
5858
public function create(array $params): object
5959
{
60+
//$this->setReturn('doctrine'); //optional - default: array
6061
return $this->setCreateArray([
6162
'fieldTest1',
6263
'fieldTest2:fk=classNameFk', //foreign key
@@ -74,6 +75,7 @@ class {{nameclass}} extends DoctrineBaseRepository {{interface}}
7475
*/
7576
public function update(array $params, int $id): object
7677
{
78+
//$this->setReturn('doctrine'); //optional - default: array
7779
return $this->setUpdateArray([
7880
'fieldTest1',
7981
'fieldTest2:fk=classNameFk', //foreign key
@@ -121,6 +123,8 @@ class {{nameclass}} extends DoctrineBaseRepository {{interface}}
121123
//set hidration (optional) - more information see the documentation.
122124
//$this->addHydrateObject();
123125
//$this->addCustomHydrationMode('ObjectAndScalarHydrator');
126+
127+
//$this->setReturn('doctrine'); //optional - default: array
124128
}
125129

126130
/*-------------------------------------------------------------------------------------

src/Core/DoctrineBaseRepository.php

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,28 @@ public function __construct($conn = 'default')
126126
*/
127127
private $configLdr = [];
128128

129+
/**
130+
* Return
131+
*
132+
* @var string
133+
*/
134+
protected $return = 'array';
135+
136+
/**
137+
* Set return object
138+
*
139+
* @param string $return array | stdClass | doctrine
140+
* @return void
141+
*/
142+
protected function setReturn(string $return)
143+
{
144+
$perm = ['array', 'stdClass', 'doctrine'];
145+
if (!in_array($return, $perm)) {
146+
$return = 'doctrine';
147+
}
148+
$this->return = $return;
149+
}
150+
129151
/**
130152
* Set hint null.
131153
*
@@ -223,8 +245,12 @@ private function getConfigLdr()
223245
'updatedAtFieldName' => 'updatedAt',
224246
'indexNameResultsArray' => 'data',
225247
'indexNamePaginationArray' => 'meta',
248+
'return' => 'array'
226249
]");
227250
}
251+
252+
//set return default
253+
$this->setReturn(config('doctrine.LdrConfig.return'));
228254
}
229255

230256
/**
@@ -1778,9 +1804,16 @@ private function instanceSerializer($objectNormalizer = null)
17781804
*/
17791805
private function serializerNormalize($obj, array $atributes): object
17801806
{
1781-
return json_decode(json_encode($this->serializer->normalize($obj, null, [
1782-
'attributes' => $atributes,
1783-
])));
1807+
if ($this->return == 'array') {
1808+
return json_decode(json_encode($this->serializer->normalize($obj, null, [
1809+
'attributes' => $atributes,
1810+
])), true);
1811+
}
1812+
if ($this->return == 'stdClass') {
1813+
return json_decode(json_encode($this->serializer->normalize($obj, null, [
1814+
'attributes' => $atributes,
1815+
])));
1816+
}
17841817
}
17851818

17861819
/**
@@ -1797,7 +1830,12 @@ private function serializerIgnoreAttributes($obj, array $values): object
17971830
$normalizer = $normalizer->setIgnoredAttributes($values);
17981831
$this->instanceSerializer($normalizer);
17991832

1800-
return json_decode($this->serializer->serialize($obj, 'json'));
1833+
if ($this->return == 'array') {
1834+
return json_decode($this->serializer->serialize($obj, 'json'), true);
1835+
}
1836+
if ($this->return == 'stdClass') {
1837+
return json_decode($this->serializer->serialize($obj, 'json'));
1838+
}
18011839
}
18021840

18031841
/**
@@ -1820,7 +1858,15 @@ protected function treatObject($obj, string $type, array $values): object
18201858
return $this->serializerIgnoreAttributes($obj, $values);
18211859
}
18221860
} else {
1823-
return json_decode($this->serializer->serialize($obj, 'json'));
1861+
if ($this->return == 'array') {
1862+
return json_decode($this->serializer->serialize($obj, 'json'), true);
1863+
}
1864+
if ($this->return == 'stdClass') {
1865+
return json_decode($this->serializer->serialize($obj, 'json'));
1866+
}
1867+
if ($this->return == 'doctrine') {
1868+
return $obj;
1869+
}
18241870
}
18251871
}
18261872
}

0 commit comments

Comments
 (0)