class Binary

The binary data type.

The plain value of binary data is a PHP file resource, see http://php.net/manual/en/language.types.resource.php. For setting the value a PHP file resource or a (absolute) stream resource URI may be passed.

Hierarchy

Expanded class hierarchy of Binary

1 string reference to 'Binary'
system_data_type_info in drupal/core/modules/system/system.module
Implements hook_data_type_info().

File

drupal/core/lib/Drupal/Core/TypedData/Type/Binary.php, line 20
Definition of Drupal\Core\TypedData\Type\Binary.

Namespace

Drupal\Core\TypedData\Type
View source
class Binary extends TypedData implements TypedDataInterface {

  /**
   * The file resource URI.
   *
   * @var string
   */
  protected $uri;

  /**
   * A generic file resource handle.
   *
   * @var resource
   */
  public $handle = NULL;

  /**
   * Implements TypedDataInterface::getValue().
   */
  public function getValue() {
    if (!isset($this->handle) && isset($this->uri)) {
      $this->handle = fopen($this->uri, 'rb');
    }
    return $this->handle;
  }

  /**
   * Implements TypedDataInterface::setValue().
   */
  public function setValue($value) {
    if (!isset($value)) {
      $this->handle = NULL;
      $this->uri = NULL;
    }
    elseif (is_resource($value)) {
      $this->handle = $value;
    }
    elseif (is_string($value)) {

      // Note: For performance reasons we store the given URI and access the
      // resource upon request. See Binary::getValue()
      $this->uri = $value;
    }
    else {
      throw new InvalidArgumentException("Invalid value for binary data given.");
    }
  }

  /**
   * Implements TypedDataInterface::getString().
   */
  public function getString() {
    $contents = '';
    while (!feof($this
      ->getValue())) {
      $contents .= fread($this->handle, 8192);
    }
    return $contents;
  }

  /**
   * Implements TypedDataInterface::validate().
   */
  public function validate() {

    // TODO: Implement validate() method.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Binary::$handle public property A generic file resource handle.
Binary::$uri protected property The file resource URI.
Binary::getString public function Implements TypedDataInterface::getString(). Overrides TypedData::getString
Binary::getValue public function Implements TypedDataInterface::getValue(). Overrides TypedData::getValue
Binary::setValue public function Implements TypedDataInterface::setValue(). Overrides TypedData::setValue
Binary::validate public function Implements TypedDataInterface::validate(). Overrides TypedDataInterface::validate
TypedData::$definition protected property The data definition.
TypedData::getDefinition public function Implements TypedDataInterface::getDefinition(). Overrides TypedDataInterface::getDefinition
TypedData::getType public function Implements TypedDataInterface::getType(). Overrides TypedDataInterface::getType
TypedData::__construct public function Constructs a TypedData object given its definition. 3