Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotations are now allowed to put camel or underscore case properties #147

Merged
merged 1 commit into from
Jan 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Annotation/AbstractProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchBundle\Annotation;

use ONGR\ElasticsearchBundle\Mapping\AbstractAnnotationCamelizer;
use ONGR\ElasticsearchBundle\Mapping\DumperInterface;

/**
* Makes sure thats annotations are well formated.
*/
abstract class AbstractProperty extends AbstractAnnotationCamelizer implements DumperInterface
{
/**
* {@inheritdoc}
*/
public function dump(array $exclude = [])
{
$array = array_diff_key(
array_filter(get_object_vars($this)),
array_flip(array_merge(['name', 'objectName', 'multiple'], $exclude))
);

return array_combine(
array_map(
function ($key) {
return $this->underscore($key);
},
array_keys($array)
),
array_values($array)
);
}
}
2 changes: 1 addition & 1 deletion Annotation/Inherit.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class Inherit
*
* @throws \InvalidArgumentException
*/
public function __constructor(array $values)
public function __construct(array $values)
{
if (is_string($values['value'])) {
$this->value = [$values['value']];
Expand Down
19 changes: 3 additions & 16 deletions Annotation/MultiField.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @Annotation
* @Target("ANNOTATION")
*/
final class MultiField
final class MultiField extends AbstractProperty
{
/**
* @var string
Expand Down Expand Up @@ -48,23 +48,10 @@ final class MultiField
/**
* @var string
*/
public $index_analyzer;
public $indexAnalyzer;

/**
* @var string
*/
public $search_analyzer;

/**
* Filters object values.
*
* @return array
*/
public function filter()
{
return array_diff_key(
array_filter(get_object_vars($this)),
array_flip(['name'])
);
}
public $searchAnalyzer;
}
19 changes: 3 additions & 16 deletions Annotation/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @Annotation
* @Target("PROPERTY")
*/
final class Property
final class Property extends AbstractProperty
{
/**
* @var string
Expand Down Expand Up @@ -48,12 +48,12 @@ final class Property
/**
* @var string
*/
public $index_analyzer;
public $indexAnalyzer;

/**
* @var string
*/
public $search_analyzer;
public $searchAnalyzer;

/**
* @var float
Expand Down Expand Up @@ -84,17 +84,4 @@ final class Property
* @var bool OneToOne or OneToMany.
*/
public $multiple;

/**
* Filters object null values and name.
*
* @return array
*/
public function filter()
{
return array_diff_key(
array_filter(get_object_vars($this)),
array_flip(['name', 'objectName', 'multiple'])
);
}
}
2 changes: 1 addition & 1 deletion Annotation/Skip.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class Skip
*
* @throws \InvalidArgumentException
*/
public function __constructor(array $values)
public function __construct(array $values)
{
if (is_string($values['value'])) {
$this->value = [$values['value']];
Expand Down
28 changes: 7 additions & 21 deletions Annotation/Suggester/AbstractSuggesterProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
namespace ONGR\ElasticsearchBundle\Annotation\Suggester;

use Doctrine\Common\Annotations\Annotation\Required;
use ONGR\ElasticsearchBundle\Annotation\AbstractProperty;

/**
* Abstract class for various suggester annotations.
*/
abstract class AbstractSuggesterProperty
abstract class AbstractSuggesterProperty extends AbstractProperty
{
/**
* @var string
Expand All @@ -40,45 +41,30 @@ abstract class AbstractSuggesterProperty
/**
* @var string
*/
public $index_analyzer;
public $indexAnalyzer;

/**
* @var string
*/
public $search_analyzer;
public $searchAnalyzer;

/**
* @var int
*/
public $preserve_separators;
public $preserveSeparators;

/**
* @var bool
*/
public $preserve_position_increments;
public $preservePositionIncrements;

/**
* @var int
*/
public $max_input_length;
public $maxInputLength;

/**
* @var bool
*/
public $payloads;

/**
* Returns required properties.
*
* @param array $extraExclude Extra object variables to exclude.
*
* @return array
*/
public function filter($extraExclude = [])
{
return array_diff_key(
array_filter(get_object_vars($this)),
array_flip(array_merge(['name', 'objectName', 'classObjectName'], $extraExclude))
);
}
}
9 changes: 4 additions & 5 deletions Annotation/Suggester/Context/AbstractContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
namespace ONGR\ElasticsearchBundle\Annotation\Suggester\Context;

use Doctrine\Common\Annotations\Annotation\Required;
use ONGR\ElasticsearchBundle\Mapping\DumperInterface;

/**
* Abstract class for various context annotations.
*/
abstract class AbstractContext
abstract class AbstractContext implements DumperInterface
{
/**
* @var array
Expand All @@ -43,11 +44,9 @@ abstract class AbstractContext
abstract public function getType();

/**
* Returns filtered object data.
*
* @return array
* {@inheritdoc}
*/
public function filter()
public function dump(array $exclude = [])
{
$vars = array_diff_key(
array_filter(get_object_vars($this)),
Expand Down
6 changes: 3 additions & 3 deletions Annotation/Suggester/ContextSuggesterProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ class ContextSuggesterProperty extends AbstractSuggesterProperty
/**
* {@inheritdoc}
*/
public function filter($extraExclude = [])
public function dump(array $exclude = [])
{
$data = parent::filter(['context']);
$data = parent::dump(['context']);

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

return $data;
Expand Down
5 changes: 3 additions & 2 deletions DataCollector/ElasticsearchDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace ONGR\ElasticsearchBundle\DataCollector;

use Monolog\Logger;
use ONGR\ElasticsearchBundle\Common\JsonFormatter;
use ONGR\ElasticsearchBundle\Logger\Handler\CollectionHandler;
use ONGR\ElasticsearchBundle\Service\JsonFormatter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
Expand Down Expand Up @@ -174,9 +174,10 @@ private function handleRecords($route, $records)
private function addQuery($route, $record, $queryBody)
{
parse_str(parse_url($record['context']['uri'], PHP_URL_QUERY), $httpParameters);
$body = json_decode(trim($queryBody, "'"), true);
$this->queries[$route][] = array_merge(
[
'body' => JsonFormatter::prettify(trim($queryBody, "'")),
'body' => $body !== null ? json_encode($body, JSON_PRETTY_PRINT) : '',
'method' => $record['context']['method'],
'httpParameters' => $httpParameters,
'time' => $record['context']['duration'] * 100,
Expand Down
47 changes: 47 additions & 0 deletions Mapping/AbstractAnnotationCamelizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchBundle\Mapping;

use Doctrine\Common\Inflector\Inflector;

/**
* Transforms document properties from underscore case to camel case.
*/
abstract class AbstractAnnotationCamelizer
{
/**
* Camelizes properties when reading from document.
*
* @param array $values Key-value for properties to be defined in this class.
*/
public function __construct(array $values)
{
foreach ($values as $key => $value) {
$this->{Inflector::camelize($key)} = $value;
}
}

/**
* Converts string from camel into underscore case (port from rails).
*
* @param string $word
*
* @return string
*/
protected function underscore($word)
{
$word = preg_replace('#([A-Z\d]+)([A-Z][a-z])#', '\1_\2', $word);
$word = preg_replace('#([a-z\d])([A-Z])#', '\1_\2', $word);

return strtolower(strtr($word, '-', '_'));
}
}
4 changes: 2 additions & 2 deletions Mapping/DocumentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ private function getProperties(\ReflectionClass $reflectionClass, $properties =
continue;
}

$maps = $type->filter();
$maps = $type->dump();

// Object.
if (in_array($type->type, ['object', 'nested']) && !empty($type->objectName)) {
Expand All @@ -305,7 +305,7 @@ private function getProperties(\ReflectionClass $reflectionClass, $properties =
$fieldsMap = [];
/** @var MultiField $field */
foreach ($maps['fields'] as $field) {
$fieldsMap[$field->name] = $field->filter();
$fieldsMap[$field->name] = $field->dump();
}
$maps['fields'] = $fieldsMap;
}
Expand Down
27 changes: 27 additions & 0 deletions Mapping/DumperInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchBundle\Mapping;

/**
* DumperInterface is the interface implemented by elasticsearch document annotations.
*/
interface DumperInterface
{
/**
* Dumps properties into array.
*
* @param array $exclude Properties array to exclude from dump.
*
* @return array
*/
public function dump(array $exclude = []);
}
Loading