You are here

class JQueryColorpickerService in Jquery Colorpicker 8

The jQuery Colorpicker service.

Hierarchy

Expanded class hierarchy of JQueryColorpickerService

1 file declares its use of JQueryColorpickerService
jQueryColorpickerServiceTest.php in tests/src/Unit/Service/jQueryColorpickerServiceTest.php
1 string reference to 'JQueryColorpickerService'
jquery_colorpicker.services.yml in ./jquery_colorpicker.services.yml
jquery_colorpicker.services.yml

File

src/Service/JQueryColorpickerService.php, line 10

Namespace

Drupal\jquery_colorpicker\Service
View source
class JQueryColorpickerService implements JQueryColorpickerServiceInterface {
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function formatColor($color) {
    if (is_scalar($color)) {
      $color = (string) $color;
      if (strlen($color)) {
        if (preg_match('/^#/', $color)) {
          $color = substr($color, 1);
        }
      }
    }
    else {
      $color = '';
    }
    return $color;
  }

  /**
   * {@inheritdoc}
   */
  public function validateColor($color) {
    $error = FALSE;
    if (is_string($color) || is_int($color)) {
      if (strlen($color) != 6) {
        $error = $this
          ->t('Color values must be exactly six characters in length');
      }
      elseif (!preg_match('/^[0-9a-fA-F]{6}$/i', $color)) {
        $error = $this
          ->t("You entered an invalid value for the color. Colors must be hexadecimal, and can only contain the characters '0-9', 'a-f' and/or 'A-F'.");
      }
    }
    else {
      $error = $this
        ->t('Color must be a string or an integer');
    }
    return $error;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JQueryColorpickerService::formatColor public function Formats colors into a consistent format for database storage. Overrides JQueryColorpickerServiceInterface::formatColor
JQueryColorpickerService::validateColor public function Validates a hecidecimal color string. Overrides JQueryColorpickerServiceInterface::validateColor
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.