You are here

Number.php in SCSS Compiler 1.0.x

File

src/Config/Schema/Number.php
View source
<?php

namespace Drupal\compiler_scss\Config\Schema;

use Drupal\Core\Config\Schema\ArrayElement;
use Drupal\compiler_scss\Type\Number as IntermediateNumber;

/**
 * A config schema type used to represent a Sass number.
 *
 * Copyright (C) 2021  Library Solutions, LLC (et al.).
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */
class Number extends ArrayElement {

  /**
   * {@inheritdoc}
   */
  protected function getElementDefinition($key) {
    $value = $this->value[$key] ?? NULL;
    $definition = [];
    $properties = $this
      ->getDataDefinition()
      ->getPropertyDefinitions();
    if (array_key_exists($key, $properties)) {
      $definition = $properties[$key]
        ->toArray();
    }
    return $this
      ->buildDataDefinition($definition, $value, $key);
  }

  /**
   * {@inheritdoc}
   */
  public function getValue() {
    $value = $this
      ->toArray() + [
      'value' => NULL,
      'unit' => NULL,
    ];
    if (is_numeric($value['value'])) {
      if (is_string($value['unit']) && strlen($value['unit']) > 0) {
        return new IntermediateNumber($value['value'], $value['unit']);
      }
      return floatval($value['value']);
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function setValue($value, $notify = TRUE) {
    if ($value instanceof IntermediateNumber) {
      $value = [
        'value' => $value
          ->value(),
        'unit' => $value
          ->unit(),
      ];
    }
    elseif (is_numeric($value)) {
      $value = [
        'value' => $value,
      ];
    }
    parent::setValue($value, $notify);
  }

}

Classes

Namesort descending Description
Number A config schema type used to represent a Sass number.