foldershare-8.x-1.2/src/Entity/FolderShareTraits/GetSetKindTrait.php

src/Entity/FolderShareTraits/GetSetKindTrait.php
<?php

namespace Drupal\foldershare\Entity\FolderShareTraits;

/**
 * Get/set FolderShare entity kind field.
 *
 * This trait includes get methods for FolderShare entity kind field,
 * along with utility functions to test for specific kinds of items.
 *
 * <B>Internal trait</B>
 * This trait is internal to the FolderShare module and used to define
 * features of the FolderShare entity class. It is a mechanism to group
 * functionality to improve code management.
 *
 * @ingroup foldershare
 */
trait GetSetKindTrait {

  /*---------------------------------------------------------------------
   *
   * Kind field.
   *
   *---------------------------------------------------------------------*/

  /**
   * {@inheritdoc}
   */
  public function getKind() {
    return $this->get('kind')->value;
  }

  /**
   * Sets the item kind.
   *
   * <B>This method is internal and strictly for use by the FolderShare
   * module itself.</B>
   *
   * The caller must call save() for the change to take effect.
   *
   * <B>Process locks</B>
   * This method does not lock access. The caller should lock around changes
   * to the entity.
   *
   * @param string $kind
   *   The new kind. The value is not validated but is expected to
   *   be one of the known kind names (e.g. FOLDER_KIND, FILE_KIND,
   *   IMAGE_KIND, or MEDIA_KIND).
   *
   * @see ::getKind()
   * @see ::isFile()
   * @see ::isFolder()
   * @see ::isImage()
   * @see ::isMedia()
   */
  private function setKind(string $kind) {
    $this->kind->setValue($kind);
  }

  /*---------------------------------------------------------------------
   *
   * Test kind field.
   *
   *---------------------------------------------------------------------*/

  /**
   * {@inheritdoc}
   */
  public function isFile() {
    return ($this->get('kind')->value === self::FILE_KIND);
  }

  /**
   * {@inheritdoc}
   */
  public function isFolder() {
    return ($this->get('kind')->value === self::FOLDER_KIND);
  }

  /**
   * {@inheritdoc}
   */
  public function isImage() {
    return ($this->get('kind')->value === self::IMAGE_KIND);
  }

  /**
   * {@inheritdoc}
   */
  public function isMedia() {
    return ($this->get('kind')->value === self::MEDIA_KIND);
  }

  /*--------------------------------------------------------------------
   *
   * Translations.
   *
   *-------------------------------------------------------------------*/

  /**
   * Returns a translation of the entity kind in singular form.
   *
   * Standard entity kinds are mapped to their corresponding terms.
   *
   * @param string $kind
   *   The kind of a FolderShare entity.
   * @param int $caseMode
   *   (optional, default = MB_CASE_LOWER) The mix of case for the returned
   *   value. One of MB_CASE_UPPER, MB_CASE_LOWER, or MB_CASE_TITLE.
   *
   * @return string
   *   The user-visible term for the singular kind.
   *
   * @see ::translateKinds()
   */
  public static function translateKind(
    string $kind,
    int $caseMode = MB_CASE_LOWER) {

    $term = '';
    switch ($kind) {
      case self::FILE_KIND:
        $term = t('file');
        break;

      case self::IMAGE_KIND:
        $term = t('image');
        break;

      case self::MEDIA_KIND:
        $term = t('media');
        break;

      case self::FOLDER_KIND:
        $term = t('folder');
        break;

      case 'rootlist':
        $term = t('top-level items');
        break;

      default:
      case 'item':
      case 'items':
        $term = t('item');
        break;
    }

    return mb_convert_case($term, $caseMode);
  }

  /**
   * Returns a translation of the entity kind in plural form.
   *
   * Standard entity kinds are mapped to their corresponding terms.
   *
   * @param string $kind
   *   The kind of a FolderShare entity.
   * @param int $caseMode
   *   (optional, default = MB_CASE_LOWER) The mix of case for the returned
   *   value. One of MB_CASE_UPPER, MB_CASE_LOWER, or MB_CASE_TITLE.
   *
   * @return string
   *   The user-visible term for the plural kind.
   *
   * @see ::translateKind()
   */
  public static function translateKinds(
    string $kind,
    int $caseMode = MB_CASE_LOWER) {

    $term = '';
    switch ($kind) {
      case self::FILE_KIND:
        $term = t('files');
        break;

      case self::IMAGE_KIND:
        $term = t('images');
        break;

      case self::MEDIA_KIND:
        $term = t('media');
        break;

      case self::FOLDER_KIND:
        $term = t('folders');
        break;

      case 'rootlist':
        $term = t('top-level items');
        break;

      default:
      case 'item':
      case 'items':
        $term = t('items');
        break;
    }

    return mb_convert_case($term, $caseMode);
  }

}

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

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