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

Add initial functional tests #1

Merged
merged 17 commits into from
Oct 24, 2017
47 changes: 47 additions & 0 deletions src/Extension/Conversion/DateConversion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <s.stok@rollerscapes.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Rollerworks\Component\Search\Elasticsearch\Extension\Conversion;

use Rollerworks\Component\Search\Elasticsearch\ValueConversion;
use Rollerworks\Component\Search\Extension\Core\DataTransformer\DateTimeToStringTransformer;

/**
* Class DateConversion.
*/
class DateConversion implements ValueConversion
{
/**
* @var DateTimeToStringTransformer
*/
private $transformer;

public function __construct()
{
$this->transformer = new DateTimeToStringTransformer(null, 'UTC', 'Y-m-d');
}

/**
* Returns the converted value as a valid Elasticsearch value.
*
* @param \DateTime $value
*
* @throws \Rollerworks\Component\Search\Exception\TransformationFailedException
*
* @return string
*/
public function convertValue($value): string
{
return $this->transformer->transform($value);
}
}
33 changes: 33 additions & 0 deletions src/Extension/ElasticsearchExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <s.stok@rollerscapes.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Rollerworks\Component\Search\Elasticsearch\Extension;

use Rollerworks\Component\Search\AbstractExtension;

/**
* Class ElasticsearchExtension.
*/
class ElasticsearchExtension extends AbstractExtension
{
/**
* {@inheritdoc}
*/
protected function loadTypesExtensions(): array
{
return [
new Type\FieldTypeExtension(),
new Type\DateTypeExtension(new Conversion\DateConversion()),
];
}
}
63 changes: 63 additions & 0 deletions src/Extension/Type/DateTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <s.stok@rollerscapes.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Rollerworks\Component\Search\Elasticsearch\Extension\Type;

use Rollerworks\Component\Search\Elasticsearch\Extension\Conversion\DateConversion;
use Rollerworks\Component\Search\Extension\Core\Type\DateType;
use Rollerworks\Component\Search\Field\AbstractFieldTypeExtension;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Class DateTypeExtension.
*/
class DateTypeExtension extends AbstractFieldTypeExtension
{
/**
* @var DateConversion
*/
private $conversion;

/**
* @param DateConversion $conversion
*/
public function __construct(DateConversion $conversion)
{
$this->conversion = $conversion;
}

/**
* {@inheritdoc}
*
* @throws \Symfony\Component\OptionsResolver\Exception\AccessException
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(
[
'elasticsearch_convert_to_range' => true,
'elasticsearch_conversion' => $this->conversion
]
);
}

/**
* Returns the name of the type being extended.
*
* @return string
*/
public function getExtendedType(): string
{
return DateType::class;
}
}
45 changes: 45 additions & 0 deletions src/Extension/Type/FieldTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Rollerworks\Component\Search\Elasticsearch\Extension\Type;

use Rollerworks\Component\Search\Elasticsearch\ValueConversion;
use Rollerworks\Component\Search\Extension\Core\Type\SearchFieldType;
use Rollerworks\Component\Search\Field\AbstractFieldTypeExtension;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Class FieldTypeExtension.
*/
class FieldTypeExtension extends AbstractFieldTypeExtension
{
/**
* {@inheritdoc}
* @throws \Symfony\Component\OptionsResolver\Exception\AccessException
* @throws \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(
[
'elasticsearch_conversion' => null,
'elasticsearch_convert_to_range' => false
]
);
$resolver->setAllowedTypes(
'elasticsearch_conversion',
[
'null',
ValueConversion::class,
]
);
}
/**
* {@inheritdoc}
*/
public function getExtendedType(): string
{
return SearchFieldType::class;
}
}
11 changes: 10 additions & 1 deletion src/FieldMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Rollerworks\Component\Search\Elasticsearch;

use Rollerworks\Component\Search\Field\FieldConfig;

final class FieldMapping
{
public $fieldName;
Expand All @@ -22,14 +24,21 @@ final class FieldMapping
public $boost;
public $options; // special options (reserved)

public function __construct(string $fieldName, string $property)
/**
* @var ValueConversion
*/
public $valueConversion;

public function __construct(string $fieldName, string $property, FieldConfig $fieldConfig)
{
$this->fieldName = $fieldName;

$mapping = $this->parseProperty($property);
$this->indexName = $mapping['indexName'];
$this->typeName = $mapping['typeName'];
$this->propertyName = $mapping['propertyName'];

$this->valueConversion = $fieldConfig->getOption('elasticsearch_conversion');
}

/**
Expand Down
Loading