Commit b1f0104f authored by e0ipso's avatar e0ipso
Browse files

ci: fix coding standards

parent bf60401c
......@@ -18,6 +18,9 @@ final class ArticleRepository extends TypedEntityRepositoryBase {
*/
const FIELD_TAGS_NAME = 'field_tags';
/**
* {@inheritdoc}
*/
public function __construct(ContainerInterface $container) {
parent::__construct($container);
$this->variantConditions = [
......
......@@ -7,6 +7,14 @@ namespace Drupal\typed_entity_example\WrappedEntities;
*/
final class BakingArticle extends Article {
/**
* An example method that is specific for articles about baking.
*
* This is not useful at all, but used only as an example.
*
* @return string
* Either yeast or baking soda.
*/
public function yeastOrBakingSoda(): string {
return mt_rand(0, 1) ? 'yeast' : 'baking soda';
}
......
<?php
namespace Drupal\typed_entity\WrappedEntityVariants;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\typed_entity\InvalidValueException;
/**
* Implementors will have support for context.
*/
interface ContextAwareInterface {
/**
* Gets the context value filed under the provided name.
*
* @param string $name
* The context name.
*
* @return mixed
* The context value.
*/
public function getContext(string $name);
/**
* Sets a context value under a specific name.
*
* @param string $name
* The context name.
* @param mixed $data
* The value to store.
*/
public function setContext(string $name, $data): void;
/**
......
<?php
namespace Drupal\typed_entity\WrappedEntityVariants;
/**
* Trait for classes implementing ContextAwareInterface.
*/
trait ContextAwareTrait {
/**
* The in-memory key/value store.
*
* @var array
*/
protected $contexts = [];
/**
* {@inheritdoc}
*/
public function getContext(string $name) {
return $this->contexts[$name] ?? NULL;
}
/**
* {@inheritdoc}
*/
public function setContext(string $name, $data): void {
$this->contexts[$name] = $data;
}
......
......@@ -8,35 +8,77 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\typed_entity\InvalidValueException;
/**
* Configurable variant condition that checks for a given value in a field.
*/
class FieldValueVariantCondition implements VariantConditionInterface, ContextAwareInterface {
use ContextAwareTrait;
use StringTranslationTrait;
/**
* Inverse the result of the evaluation.
*
* @var bool
*/
protected $isNegated = FALSE;
/**
* Name of the field that contains the data.
*
* @var string
*/
protected $fieldName = '';
/**
* The value to check for.
*
* @var null
*/
protected $value = NULL;
/**
* The FQN of the wrapper class for the variant.
*
* @var string
*/
protected $variant;
/**
* FieldValueVariantCondition constructor.
*
* @param bool $is_negated
* @param string $field_name
* @param null $value
* @param $variant
* Name of the field that contains the data.
* @param mixed $value
* The value to check for.
* @param string $variant
* The FQN of the wrapper class for the variant.
* @param bool $is_negated
* Inverse the result of the evaluation.
*
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function __construct(string $field_name, $value, $variant, bool $is_negated = FALSE) {
public function __construct(string $field_name, $value, string $variant, bool $is_negated = FALSE) {
$this->isNegated = $is_negated;
$this->fieldName = $field_name;
$this->value = $value;
$this->variant = $variant;
}
/**
* {@inheritdoc}
*/
public function isNegated(): bool {
return $this->isNegated;
}
/**
* {@inheritdoc}
*
* @throws \Drupal\typed_entity\InvalidValueException
*
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function evaluate(): bool {
$this->validateContext();
$entity = $this->getContext('entity');
......@@ -55,6 +97,9 @@ class FieldValueVariantCondition implements VariantConditionInterface, ContextAw
return $this->isNegated() ? !$result : $result;
}
/**
* {@inheritdoc}
*/
public function summary(): string {
return $this->t('Active when the %field is %value.', [
'%field' => $this->fieldName,
......@@ -62,6 +107,9 @@ class FieldValueVariantCondition implements VariantConditionInterface, ContextAw
]);
}
/**
* {@inheritdoc}
*/
public function variant(): string {
return $this->variant;
}
......
......@@ -2,11 +2,43 @@
namespace Drupal\typed_entity\WrappedEntityVariants;
/**
* Interface for the variant conditions.
*/
interface VariantConditionInterface {
/**
* Checks if the condition is negated.
*
* @return bool
* TRUE if the condition is negated.
*/
public function isNegated(): bool;
/**
* Evaluates the condition.
*
* @return bool
* TRUE if the condition is fulfilled.
*
* @throws \Drupal\typed_entity\InvalidValueException
*/
public function evaluate(): bool;
/**
* A human readable summary of the condition. Used for interface purposes.
*
* @return string
* The summary.
*/
public function summary(): string;
/**
* Gets the FQN of the class for the wrapped entity variant.
*
* @return string
* The variant class.
*/
public function variant(): string;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment