You are here

abstract class BaseMapping in Flags 8

Provides generic mapping service to map values to flags using config entities.

Class BaseMapping

Hierarchy

Expanded class hierarchy of BaseMapping

1 file declares its use of BaseMapping
Language.php in src/Mapping/Language.php

File

src/Mapping/BaseMapping.php, line 14

Namespace

Drupal\flags\Mapping
View source
abstract class BaseMapping implements FlagMappingInterface {

  /**
   * @var ImmutableConfig[]
   */
  protected $config;

  /**
   * @var string[]
   */
  protected $extraClasses = [];

  /**
   * Gets config key that holds list of mapping entities.
   *
   * @return string
   */
  protected abstract function getConfigKey();

  /**
   * Creates new instance of BaseMapping class.
   *
   * @param ConfigFactoryInterface $config
   */
  public function __construct(ConfigFactoryInterface $config) {
    $ids = $config
      ->listAll($this
      ->getConfigKey());
    $this->config = $config
      ->loadMultiple($ids);
  }

  /**
   * {@inheritdoc}
   */
  function map($value) {

    // Unify input data
    $code = trim(strtolower($value));
    $key = $this
      ->getConfigKey() . '.' . $code;
    if (isset($this->config[$key])) {

      // We make sure that flag is lowercase to match our CSS.
      return strtolower($this->config[$key]
        ->get('flag'));
    }
    return $code;
  }

  /**
   * {@inheritDoc}
   */
  public function getOptionAttributes(array $options = []) {
    $attributes = [];
    foreach ($options as $key) {
      $classes = [
        'flag',
        'flag-' . strtolower($this
          ->map($key)),
      ];
      $classes = array_merge($this
        ->getExtraClasses(), $classes);
      $attributes[$key] = new Attribute([
        'data-class' => $classes,
      ]);
    }
    return $attributes;
  }

  /**
   * {@inheritDoc}
   */
  public function getExtraClasses() {
    return $this->extraClasses;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BaseMapping::$config protected property
BaseMapping::$extraClasses protected property 2
BaseMapping::getConfigKey abstract protected function Gets config key that holds list of mapping entities. 2
BaseMapping::getExtraClasses public function Returns array of extra classes for specific mapper. Overrides FlagMappingInterface::getExtraClasses
BaseMapping::getOptionAttributes public function Gets array with attributes for each option element. Overrides FlagMappingInterface::getOptionAttributes
BaseMapping::map function Maps provided string to a flag code. Returned string should be lower case flag code. Overrides FlagMappingInterface::map
BaseMapping::__construct public function Creates new instance of BaseMapping class.