openquestions-1.0.x-dev/src/Service/OQUtils.php

src/Service/OQUtils.php
<?php

namespace Drupal\openquestions\Service;

use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\StringTranslation\TranslationManager;

/**
 * Our utils.
 */
class OQUtils implements OQUtilsInterface {

  use StringTranslationTrait;

  /**
   * For t().
   *
   * @var \Drupal\Core\StringTranslation\TranslationManager
   */
  protected $stringTranslation;

  /**
   * Our logger.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;

  /**
   * OQUtils constructor.
   *
   * @param \Drupal\Core\StringTranslation\TranslationManager $stringTranslation
   *   For t().
   * @param \Drupal\Core\Logger\LoggerChannelInterface $logger
   *   Our logger.
   */
  public function __construct(TranslationManager $stringTranslation, LoggerChannelInterface $logger) {
    $this->stringTranslation = $stringTranslation;
    $this->logger = $logger;
  }

  /**
   * {@inheritdoc}
   */
  public function stringListToOptions(array $items) {
    $ret = [];

    foreach ($items as $item) {
      $value = $item['value'];
      if ($item['label'] && $item['modifier'] == 'LABEL') {
        $ret[$value] = $item['label'];
        continue;
      }

      if ($item['label']) {
        $ret[$value] = $value . ' (' . $item['label'] . ')';
        continue;
      }

      $ret[$value] = $value;
    }

    return $ret;
  }

  /**
   * {@inheritdoc}
   */
  public function parseStringList($s, $modifiers = []) {
    $ret = [
      'items' => [],
      'errors' => [],
    ];

    $list = preg_split("/\r\n|\n|\r/", $s);
    $list = array_map('trim', $list);
    $list = array_filter($list, 'strlen');
    if (!$list || count($list) < 2) {
      $ret['errors'][] = $this->t('Too few items');
      return $ret;
    }

    foreach ($list as $item) {
      $components = explode('|', $item);
      if (!$components) {
        $ret['errors'][] = $this->t('Bad row: @row', ['@row' => $item]);
        return $ret;
      }

      if (!is_numeric($components[0])) {
        $ret['errors'][] = $this->t('Not numeric: @component', ['@component' => $components[0]]);
        return $ret;
      }

      if (!empty($components[2]) && !in_array($components[2], $modifiers)) {
        $ret['errors'][] = $this->t('Bad modifier @component', ['@component' => $components[2]]);
        return $ret;
      }

      $ret['items'][] = [
        'value' => $components[0],
        'label' => !empty($components[1]) ? $components[1] : NULL,
        'modifier' => !empty($components[2]) ? $components[2] : NULL,
      ];
    }

    return $ret;
  }

  /**
   * {@inheritdoc}
   */
  public function getFirstReference($entity, $fieldName) {
    if (!$entity || !$fieldName) {
      return NULL;
    }

    $temp = $entity->get($fieldName);
    if (!$temp) {
      return NULL;
    }

    $temp = $temp->first();
    if (!$temp) {
      return NULL;
    }

    $temp = $temp->get('entity');
    if (!$temp) {
      return NULL;
    }

    $temp = $temp->getTarget();
    if (!$temp) {
      return NULL;
    }

    return $temp->getValue();
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc