class BinaryData
Same name in other branches
- 9 core/lib/Drupal/Core/TypedData/Plugin/DataType/BinaryData.php \Drupal\Core\TypedData\Plugin\DataType\BinaryData
- 8.9.x core/lib/Drupal/Core/TypedData/Plugin/DataType/BinaryData.php \Drupal\Core\TypedData\Plugin\DataType\BinaryData
- 11.x core/lib/Drupal/Core/TypedData/Plugin/DataType/BinaryData.php \Drupal\Core\TypedData\Plugin\DataType\BinaryData
The binary data type.
The plain value of binary data is a PHP file resource, see http://php.net/manual/language.types.resource.php. For setting the value a PHP file resource or an (absolute) stream resource URI may be passed.
Hierarchy
- class \Drupal\Core\TypedData\Plugin\DataType\BinaryData extends \Drupal\Core\TypedData\PrimitiveBase implements \Drupal\Core\TypedData\Type\BinaryInterface
Expanded class hierarchy of BinaryData
File
-
core/
lib/ Drupal/ Core/ TypedData/ Plugin/ DataType/ BinaryData.php, line 17
Namespace
Drupal\Core\TypedData\Plugin\DataTypeView source
class BinaryData extends PrimitiveBase implements BinaryInterface {
/**
* The file resource URI.
*
* @var string
*/
protected $uri;
/**
* A generic file resource handle.
*
* @var resource
*/
public $handle = NULL;
/**
* {@inheritdoc}
*/
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 an (absolute) stream resource URI as value.
*/
public function setValue($value, $notify = TRUE) {
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 BinaryData::getValue()
$this->uri = $value;
$this->handle = NULL;
}
else {
$this->handle = $value;
}
// Notify the parent of any changes.
if ($notify && isset($this->parent)) {
$this->parent
->onChange($this->name);
}
}
/**
* {@inheritdoc}
*/
public function getString() {
// Return the file content.
$contents = '';
while (!feof($this->getValue())) {
$contents .= fread($this->handle, 8192);
}
return $contents;
}
/**
* {@inheritdoc}
*/
public function getCastedValue() {
return $this->getValue();
}
}
Members
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.