<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media\Trait;

/**
 * Provides type-safe access to plugin definition values.
 *
 * Drupal plugin definitions can be either an array or a
 * PluginDefinitionInterface object. This trait centralizes the repeated
 * is_array() guard into a single helper, eliminating boilerplate in every
 * getter method of plugin base classes.
 *
 * Classes using this trait must have a $pluginDefinition property
 * (provided by PluginBase).
 */
trait PluginDefinitionAccessTrait {

  /**
   * Gets a value from the plugin definition array.
   *
   * @param string $key
   *   The definition key to look up.
   * @param mixed $default
   *   Default value if the key is missing or definition is not an array.
   *
   * @return mixed
   *   The definition value or the default.
   */
  protected function getDefinitionValue(string $key, mixed $default = NULL): mixed {
    if (!is_array($this->pluginDefinition)) {
      return $default;
    }
    return $this->pluginDefinition[$key] ?? $default;
  }

}
