class OperationData

Same name in other branches
  1. 9 composer/Plugin/Scaffold/Operations/OperationData.php \Drupal\Composer\Plugin\Scaffold\Operations\OperationData
  2. 10 composer/Plugin/Scaffold/Operations/OperationData.php \Drupal\Composer\Plugin\Scaffold\Operations\OperationData
  3. 11.x composer/Plugin/Scaffold/Operations/OperationData.php \Drupal\Composer\Plugin\Scaffold\Operations\OperationData

Holds parameter data for operation objects during operation creation only.

@internal

Hierarchy

Expanded class hierarchy of OperationData

1 file declares its use of OperationData
Handler.php in composer/Plugin/Scaffold/Handler.php

File

composer/Plugin/Scaffold/Operations/OperationData.php, line 10

Namespace

Drupal\Composer\Plugin\Scaffold\Operations
View source
class OperationData {
    const MODE = 'mode';
    const PATH = 'path';
    const OVERWRITE = 'overwrite';
    const PREPEND = 'prepend';
    const APPEND = 'append';
    const DEFAULT = 'default';
    const FORCE_APPEND = 'force-append';
    
    /**
     * The parameter data.
     *
     * @var array
     */
    protected $data;
    
    /**
     * The destination path
     *
     * @var string
     */
    protected $destination;
    
    /**
     * OperationData constructor.
     *
     * @param mixed $data
     *   The raw data array to wrap.
     */
    public function __construct($destination, $data) {
        $this->destination = $destination;
        $this->data = $this->normalizeScaffoldMetadata($destination, $data);
    }
    
    /**
     * Gets the destination path that this operation data is associated with.
     *
     * @return string
     *   The destination path for the scaffold result.
     */
    public function destination() {
        return $this->destination;
    }
    
    /**
     * Gets operation mode
     *
     * @return string
     *   Operation mode.
     */
    public function mode() {
        return $this->data[self::MODE];
    }
    
    /**
     * Checks if path exists
     *
     * @return bool
     *   Returns true if path exists
     */
    public function hasPath() {
        return isset($this->data[self::PATH]);
    }
    
    /**
     * Gets path
     *
     * @return string
     *   The path.
     */
    public function path() {
        return $this->data[self::PATH];
    }
    
    /**
     * Determines overwrite.
     *
     * @return bool
     *   Returns true if overwrite mode was selected.
     */
    public function overwrite() {
        return !empty($this->data[self::OVERWRITE]);
    }
    
    /**
     * Determines whether 'force-append' has been set.
     *
     * @return bool
     *   Returns true if 'force-append' mode was selected.
     */
    public function forceAppend() {
        if ($this->hasDefault()) {
            return TRUE;
        }
        return !empty($this->data[self::FORCE_APPEND]);
    }
    
    /**
     * Checks if prepend path exists.
     *
     * @return bool
     *   Returns true if prepend exists.
     */
    public function hasPrepend() {
        return isset($this->data[self::PREPEND]);
    }
    
    /**
     * Gets prepend path.
     *
     * @return string
     *   Path to prepend data
     */
    public function prepend() {
        return $this->data[self::PREPEND];
    }
    
    /**
     * Checks if append path exists.
     *
     * @return bool
     *   Returns true if prepend exists.
     */
    public function hasAppend() {
        return isset($this->data[self::APPEND]);
    }
    
    /**
     * Gets append path.
     *
     * @return string
     *   Path to append data
     */
    public function append() {
        return $this->data[self::APPEND];
    }
    
    /**
     * Checks if default path exists.
     *
     * @return bool
     *   Returns true if there is default data available.
     */
    public function hasDefault() {
        return isset($this->data[self::DEFAULT]);
    }
    
    /**
     * Gets default path.
     *
     * @return string
     *   Path to default data
     */
    public function default() {
        return $this->data[self::DEFAULT];
    }
    
    /**
     * Normalizes metadata by converting literal values into arrays.
     *
     * Conversions performed include:
     *   - Boolean 'false' means "skip".
     *   - A string means "replace", with the string value becoming the path.
     *
     * @param string $destination
     *   The destination path for the scaffold file.
     * @param mixed $value
     *   The metadata for this operation object, which varies by operation type.
     *
     * @return array
     *   Normalized scaffold metadata with default values.
     */
    protected function normalizeScaffoldMetadata($destination, $value) {
        $defaultScaffoldMetadata = [
            self::MODE => ReplaceOp::ID,
            self::PREPEND => NULL,
            self::APPEND => NULL,
            self::DEFAULT => NULL,
            self::OVERWRITE => TRUE,
        ];
        return $this->convertScaffoldMetadata($destination, $value) + $defaultScaffoldMetadata;
    }
    
    /**
     * Performs the conversion-to-array step in normalizeScaffoldMetadata.
     *
     * @param string $destination
     *   The destination path for the scaffold file.
     * @param mixed $value
     *   The metadata for this operation object, which varies by operation type.
     *
     * @return array
     *   Normalized scaffold metadata.
     */
    protected function convertScaffoldMetadata($destination, $value) {
        if (is_bool($value)) {
            if (!$value) {
                return [
                    self::MODE => SkipOp::ID,
                ];
            }
            throw new \RuntimeException("File mapping {$destination} cannot be given the value 'true'.");
        }
        if (empty($value)) {
            throw new \RuntimeException("File mapping {$destination} cannot be empty.");
        }
        if (is_string($value)) {
            $value = [
                self::PATH => $value,
            ];
        }
        // If there is no 'mode', but there is an 'append' or a 'prepend' path,
        // then the mode is 'append' (append + prepend).
        if (!isset($value[self::MODE]) && (isset($value[self::APPEND]) || isset($value[self::PREPEND]))) {
            $value[self::MODE] = AppendOp::ID;
        }
        return $value;
    }

}

Members

Title Sort descending Modifiers Object type Summary
OperationData::$data protected property The parameter data.
OperationData::$destination protected property The destination path
OperationData::APPEND constant
OperationData::append public function Gets append path.
OperationData::convertScaffoldMetadata protected function Performs the conversion-to-array step in normalizeScaffoldMetadata.
OperationData::DEFAULT constant
OperationData::default public function Gets default path.
OperationData::destination public function Gets the destination path that this operation data is associated with.
OperationData::forceAppend public function Determines whether 'force-append' has been set.
OperationData::FORCE_APPEND constant
OperationData::hasAppend public function Checks if append path exists.
OperationData::hasDefault public function Checks if default path exists.
OperationData::hasPath public function Checks if path exists
OperationData::hasPrepend public function Checks if prepend path exists.
OperationData::MODE constant
OperationData::mode public function Gets operation mode
OperationData::normalizeScaffoldMetadata protected function Normalizes metadata by converting literal values into arrays.
OperationData::overwrite public function Determines overwrite.
OperationData::OVERWRITE constant
OperationData::path public function Gets path
OperationData::PATH constant
OperationData::prepend public function Gets prepend path.
OperationData::PREPEND constant
OperationData::__construct public function OperationData constructor.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.