Skip to content

Commit faa7a6c

Browse files
committed
Merge pull request #147 from martiis/issue-75
Annotations are now allowed to put camel or underscore case properties
2 parents 543708f + 5b18af7 commit faa7a6c

20 files changed

+227
-283
lines changed

Annotation/AbstractProperty.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\Annotation;
13+
14+
use ONGR\ElasticsearchBundle\Mapping\AbstractAnnotationCamelizer;
15+
use ONGR\ElasticsearchBundle\Mapping\DumperInterface;
16+
17+
/**
18+
* Makes sure thats annotations are well formated.
19+
*/
20+
abstract class AbstractProperty extends AbstractAnnotationCamelizer implements DumperInterface
21+
{
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function dump(array $exclude = [])
26+
{
27+
$array = array_diff_key(
28+
array_filter(get_object_vars($this)),
29+
array_flip(array_merge(['name', 'objectName', 'multiple'], $exclude))
30+
);
31+
32+
return array_combine(
33+
array_map(
34+
function ($key) {
35+
return $this->underscore($key);
36+
},
37+
array_keys($array)
38+
),
39+
array_values($array)
40+
);
41+
}
42+
}

Annotation/Inherit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class Inherit
3131
*
3232
* @throws \InvalidArgumentException
3333
*/
34-
public function __constructor(array $values)
34+
public function __construct(array $values)
3535
{
3636
if (is_string($values['value'])) {
3737
$this->value = [$values['value']];

Annotation/MultiField.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @Annotation
2020
* @Target("ANNOTATION")
2121
*/
22-
final class MultiField
22+
final class MultiField extends AbstractProperty
2323
{
2424
/**
2525
* @var string
@@ -48,23 +48,10 @@ final class MultiField
4848
/**
4949
* @var string
5050
*/
51-
public $index_analyzer;
51+
public $indexAnalyzer;
5252

5353
/**
5454
* @var string
5555
*/
56-
public $search_analyzer;
57-
58-
/**
59-
* Filters object values.
60-
*
61-
* @return array
62-
*/
63-
public function filter()
64-
{
65-
return array_diff_key(
66-
array_filter(get_object_vars($this)),
67-
array_flip(['name'])
68-
);
69-
}
56+
public $searchAnalyzer;
7057
}

Annotation/Property.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @Annotation
2020
* @Target("PROPERTY")
2121
*/
22-
final class Property
22+
final class Property extends AbstractProperty
2323
{
2424
/**
2525
* @var string
@@ -48,12 +48,12 @@ final class Property
4848
/**
4949
* @var string
5050
*/
51-
public $index_analyzer;
51+
public $indexAnalyzer;
5252

5353
/**
5454
* @var string
5555
*/
56-
public $search_analyzer;
56+
public $searchAnalyzer;
5757

5858
/**
5959
* @var float
@@ -84,17 +84,4 @@ final class Property
8484
* @var bool OneToOne or OneToMany.
8585
*/
8686
public $multiple;
87-
88-
/**
89-
* Filters object null values and name.
90-
*
91-
* @return array
92-
*/
93-
public function filter()
94-
{
95-
return array_diff_key(
96-
array_filter(get_object_vars($this)),
97-
array_flip(['name', 'objectName', 'multiple'])
98-
);
99-
}
10087
}

Annotation/Skip.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class Skip
3131
*
3232
* @throws \InvalidArgumentException
3333
*/
34-
public function __constructor(array $values)
34+
public function __construct(array $values)
3535
{
3636
if (is_string($values['value'])) {
3737
$this->value = [$values['value']];

Annotation/Suggester/AbstractSuggesterProperty.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
namespace ONGR\ElasticsearchBundle\Annotation\Suggester;
1313

1414
use Doctrine\Common\Annotations\Annotation\Required;
15+
use ONGR\ElasticsearchBundle\Annotation\AbstractProperty;
1516

1617
/**
1718
* Abstract class for various suggester annotations.
1819
*/
19-
abstract class AbstractSuggesterProperty
20+
abstract class AbstractSuggesterProperty extends AbstractProperty
2021
{
2122
/**
2223
* @var string
@@ -40,45 +41,30 @@ abstract class AbstractSuggesterProperty
4041
/**
4142
* @var string
4243
*/
43-
public $index_analyzer;
44+
public $indexAnalyzer;
4445

4546
/**
4647
* @var string
4748
*/
48-
public $search_analyzer;
49+
public $searchAnalyzer;
4950

5051
/**
5152
* @var int
5253
*/
53-
public $preserve_separators;
54+
public $preserveSeparators;
5455

5556
/**
5657
* @var bool
5758
*/
58-
public $preserve_position_increments;
59+
public $preservePositionIncrements;
5960

6061
/**
6162
* @var int
6263
*/
63-
public $max_input_length;
64+
public $maxInputLength;
6465

6566
/**
6667
* @var bool
6768
*/
6869
public $payloads;
69-
70-
/**
71-
* Returns required properties.
72-
*
73-
* @param array $extraExclude Extra object variables to exclude.
74-
*
75-
* @return array
76-
*/
77-
public function filter($extraExclude = [])
78-
{
79-
return array_diff_key(
80-
array_filter(get_object_vars($this)),
81-
array_flip(array_merge(['name', 'objectName', 'classObjectName'], $extraExclude))
82-
);
83-
}
8470
}

Annotation/Suggester/Context/AbstractContext.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
namespace ONGR\ElasticsearchBundle\Annotation\Suggester\Context;
1313

1414
use Doctrine\Common\Annotations\Annotation\Required;
15+
use ONGR\ElasticsearchBundle\Mapping\DumperInterface;
1516

1617
/**
1718
* Abstract class for various context annotations.
1819
*/
19-
abstract class AbstractContext
20+
abstract class AbstractContext implements DumperInterface
2021
{
2122
/**
2223
* @var array
@@ -43,11 +44,9 @@ abstract class AbstractContext
4344
abstract public function getType();
4445

4546
/**
46-
* Returns filtered object data.
47-
*
48-
* @return array
47+
* {@inheritdoc}
4948
*/
50-
public function filter()
49+
public function dump(array $exclude = [])
5150
{
5251
$vars = array_diff_key(
5352
array_filter(get_object_vars($this)),

Annotation/Suggester/ContextSuggesterProperty.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ class ContextSuggesterProperty extends AbstractSuggesterProperty
2020
/**
2121
* {@inheritdoc}
2222
*/
23-
public function filter($extraExclude = [])
23+
public function dump(array $exclude = [])
2424
{
25-
$data = parent::filter(['context']);
25+
$data = parent::dump(['context']);
2626

2727
/** @var AbstractContext $singleContext */
2828
foreach ($this->context as $singleContext) {
29-
$data['context'][$singleContext->name] = $singleContext->filter();
29+
$data['context'][$singleContext->name] = $singleContext->dump();
3030
}
3131

3232
return $data;

DataCollector/ElasticsearchDataCollector.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
namespace ONGR\ElasticsearchBundle\DataCollector;
1313

1414
use Monolog\Logger;
15+
use ONGR\ElasticsearchBundle\Common\JsonFormatter;
1516
use ONGR\ElasticsearchBundle\Logger\Handler\CollectionHandler;
16-
use ONGR\ElasticsearchBundle\Service\JsonFormatter;
1717
use Symfony\Component\HttpFoundation\Request;
1818
use Symfony\Component\HttpFoundation\Response;
1919
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
@@ -174,9 +174,10 @@ private function handleRecords($route, $records)
174174
private function addQuery($route, $record, $queryBody)
175175
{
176176
parse_str(parse_url($record['context']['uri'], PHP_URL_QUERY), $httpParameters);
177+
$body = json_decode(trim($queryBody, "'"), true);
177178
$this->queries[$route][] = array_merge(
178179
[
179-
'body' => JsonFormatter::prettify(trim($queryBody, "'")),
180+
'body' => $body !== null ? json_encode($body, JSON_PRETTY_PRINT) : '',
180181
'method' => $record['context']['method'],
181182
'httpParameters' => $httpParameters,
182183
'time' => $record['context']['duration'] * 100,
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\Mapping;
13+
14+
use Doctrine\Common\Inflector\Inflector;
15+
16+
/**
17+
* Transforms document properties from underscore case to camel case.
18+
*/
19+
abstract class AbstractAnnotationCamelizer
20+
{
21+
/**
22+
* Camelizes properties when reading from document.
23+
*
24+
* @param array $values Key-value for properties to be defined in this class.
25+
*/
26+
public function __construct(array $values)
27+
{
28+
foreach ($values as $key => $value) {
29+
$this->{Inflector::camelize($key)} = $value;
30+
}
31+
}
32+
33+
/**
34+
* Converts string from camel into underscore case (port from rails).
35+
*
36+
* @param string $word
37+
*
38+
* @return string
39+
*/
40+
protected function underscore($word)
41+
{
42+
$word = preg_replace('#([A-Z\d]+)([A-Z][a-z])#', '\1_\2', $word);
43+
$word = preg_replace('#([a-z\d])([A-Z])#', '\1_\2', $word);
44+
45+
return strtolower(strtr($word, '-', '_'));
46+
}
47+
}

0 commit comments

Comments
 (0)