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
Contains \Drupal\Core\TypedData\Type\Binary.

Namespace

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

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

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

  /**
   * Overrides TypedData::getValue().
   */
  public function getValue() {

    // If the value has been set by (absolute) stream resource URI, access the
    // resource now.
    if (!isset($this->handle) && isset($this->uri)) {
      $this->handle = is_readable($this->uri) ? fopen($this->uri, 'rb') : FALSE;
    }
    return $this->handle;
  }

  /**
   * Overrides TypedData::setValue().
   *
   * Supports a PHP file resource or a (absolute) stream resource URI as value.
   */
  public function setValue($value, $notify = TRUE) {

    // Notify the parent of any changes to be made.
    if ($notify && isset($this->parent)) {
      $this->parent
        ->onChange($this->name);
    }
    if (!isset($value)) {
      $this->handle = NULL;
      $this->uri = NULL;
    }
    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;
      $this->handle = NULL;
    }
    else {
      $this->handle = $value;
    }
  }

  /**
   * Overrides TypedData::getString().
   */
  public function getString() {

    // Return the file content.
    $contents = '';
    while (!feof($this
      ->getValue())) {
      $contents .= fread($this->handle, 8192);
    }
    return $contents;
  }

}

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 Overrides TypedData::getString(). Overrides TypedData::getString
Binary::getValue public function Overrides TypedData::getValue(). Overrides TypedData::getValue
Binary::setValue public function Overrides TypedData::setValue(). Overrides TypedData::setValue
TypedData::$definition protected property The data definition.
TypedData::$name protected property The property name.
TypedData::$parent protected property The parent typed data object.
TypedData::getConstraints public function Implements \Drupal\Core\TypedData\TypedDataInterface::getConstraints(). Overrides TypedDataInterface::getConstraints 2
TypedData::getDefinition public function Implements \Drupal\Core\TypedData\TypedDataInterface::getDefinition(). Overrides TypedDataInterface::getDefinition
TypedData::getName public function Implements \Drupal\Core\TypedData\TypedDataInterface::getName(). Overrides TypedDataInterface::getName
TypedData::getParent public function Implements \Drupal\Core\TypedData\TypedDataInterface::getParent(). Overrides TypedDataInterface::getParent
TypedData::getPropertyPath public function Implements \Drupal\Core\TypedData\TypedDataInterface::getPropertyPath(). Overrides TypedDataInterface::getPropertyPath
TypedData::getRoot public function Implements \Drupal\Core\TypedData\TypedDataInterface::getRoot(). Overrides TypedDataInterface::getRoot
TypedData::getType public function Implements \Drupal\Core\TypedData\TypedDataInterface::getType(). Overrides TypedDataInterface::getType
TypedData::setContext public function Implements \Drupal\Core\TypedData\TypedDataInterface::setContext(). Overrides TypedDataInterface::setContext 1
TypedData::validate public function Implements \Drupal\Core\TypedData\TypedDataInterface::validate(). Overrides TypedDataInterface::validate 3
TypedData::__construct public function Constructs a TypedData object given its definition and context. 5