Skip to content
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
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
language: php

php:
- 5.6
- 7.0
- 7.1
- hhvm

Expand All @@ -24,7 +22,7 @@ matrix:
allow_failures:
- php: hhvm
include:
- php: 5.6
- php: 7.1
env: deps=low

sudo: false
85 changes: 59 additions & 26 deletions Attribute/SpecializedAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,75 +12,108 @@
class SpecializedAttribute extends Attribute implements SpecializedAttributeInterface
{
/**
* @var AttributeSpecializationInterface
* @var AttributeSpecializationInterface[]
*/
protected $specialization;
protected $specializations;

/**
* @var AttributeSpecializationContextInterface
* @var AttributeSpecializationContextInterface[]
*/
protected $context;
protected $contexts;

/**
* @param AttributeSpecializationInterface $specialization
* @param AttributeSpecializationInterface[] $specializations
* @param string $name
* @param string $key
* @param string $displayName
* @param callable|null $valueDisplayStrategy
*/
public function __construct(
AttributeSpecializationInterface $specialization,
$name,
$key = null,
$displayName = null,
array $specializations,
string $name,
string $key = null,
string $displayName = null,
callable $valueDisplayStrategy = null
) {
$this->specialization = $specialization;
$this->specializations = $specializations;
$this->contexts = [];
parent::__construct($name, $key, $displayName, $valueDisplayStrategy);
}

/**
* {@inheritDoc}
*/
public function getSpecialization()
public function getSpecializations()
{
return $this->specialization;
return $this->specializations;
}

/**
* {@inheritDoc}
*/
public function setContext(AttributeSpecializationContextInterface $context)
{
$this->context = $context;
public function setContext(
AttributeSpecializationContextInterface $context,
string $specialization
) {
$this->contexts[$specialization] = $context;
}

/**
* {@inheritDoc}
*/
public function getContext()
public function getContext(string $specialization)
{
return $this->context;
if (!array_key_exists($specialization, $this->contexts)) {
throw new \LogicException(
'Cannot get context for specialization `%s` on attribute `%s` as it has not been set',
$specialization,
$this->getName()
);
}

return $this->contexts[$specialization];
}

/**
* {@inheritDoc}
*/
public function getSearchKey(array $options = [])
{
if (!$this->context) {
throw new \LogicException(sprintf('Cannot get a search key for a specialized attribute without a context where name is %s', $this->getName()));
foreach ($this->specializations as $specialization) {
if (!array_key_exists($specialization->getName(), $this->contexts)) {
throw new \LogicException(
sprintf(
'Cannot get context for specialization `%s` on attribute `%s` as it has not been set',
$specialization->getName(),
$this->getName()
)
);
}
}

if ($this->context instanceof AttributeSpecializationNullContextInterface) {
return parent::getSearchKey();
ksort($this->contexts);
$contextValues = [];

foreach ($this->contexts as $context) {
if ($context instanceof AttributeSpecializationNullContextInterface) {
continue;
}

try {
$contextValues[] = $context->getValue();
} catch (IllegalContextValueException $e) {
throw new UnformableSearchKeyException(
sprintf(
'The attribute "%s" cannot form a search key due to incomplete context data.',
$this->getName())
);
}
}
try {
$contextValue = $this->context->getValue();
} catch (IllegalContextValueException $e) {
throw new UnformableSearchKeyException(sprintf('The attribute "%s" cannot form a search key due to incomplete context data.', $this->getName()));

if (empty($contextValues)) {
return parent::getSearchKey();
}

return sprintf('%s_%s', parent::getSearchKey(), $contextValue);
return sprintf('%s_%s', parent::getSearchKey(), implode('_', $contextValues));
}
}
18 changes: 10 additions & 8 deletions Attribute/SpecializedAttributeDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract class SpecializedAttributeDecorator implements SpecializedAttributeInte
/**
* The attribute being decorated.
*
* @var SpecializedAttributeInterface
* @var SpecializedAttributeInterface|AttributeInterface
**/
private $attribute;

Expand Down Expand Up @@ -58,25 +58,27 @@ public function getAttribute()
/**
* {@inheritDoc}
*/
public function getSpecialization()
public function getSpecializations()
{
return $this->attribute->getSpecialization();
return $this->attribute->getSpecializations();
}

/**
* {@inheritDoc}
*/
public function setContext(AttributeSpecializationContextInterface $context)
{
$this->attribute->setContext($context);
public function setContext(
AttributeSpecializationContextInterface $context,
string $specialization
) {
$this->attribute->setContext($context, $specialization);
}

/**
* {@inheritDoc}
*/
public function getContext()
public function getContext(string $specialization)
{
return $this->attribute->getContext();
return $this->attribute->getContext($specialization);
}

public function __toString()
Expand Down
12 changes: 7 additions & 5 deletions Attribute/SpecializedAttributeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@
namespace Markup\NeedleBundle\Attribute;

/**
* An attribute implementation that has a specialization set on it
* An attribute implementation that has one or more specializations set on it
* allowing the search key to be changed by adding a context
*/
interface SpecializedAttributeInterface
{
/**
* @return AttributeSpecialization
* @return AttributeSpecialization[]
*/
public function getSpecialization();
public function getSpecializations();

/**
* @param AttributeSpecializationContextInterface
* @param string
*/
public function setContext(AttributeSpecializationContextInterface $context);
public function setContext(AttributeSpecializationContextInterface $context, string $specialization);

/**
* @param string
* @return AttributeSpecializationContextInterface
*/
public function getContext();
public function getContext(string $specialization);
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"php": ">=5.6",
"php": ">=7.1",
"nelmio/solarium-bundle": "^2.0.4",
"solarium/solarium": "^3.1.0",
"pagerfanta/pagerfanta": "~1.0.1",
Expand Down