3 namespace Drupal\Core\TypedData\Plugin\DataType;
5 use Drupal\Core\TypedData\PrimitiveBase;
6 use Drupal\Core\TypedData\Type\BinaryInterface;
9 * The binary data type.
11 * The plain value of binary data is a PHP file resource, see
12 * http://php.net/manual/language.types.resource.php. For setting the value
13 * a PHP file resource or a (absolute) stream resource URI may be passed.
17 * label = @Translation("Binary")
20 class BinaryData extends PrimitiveBase implements BinaryInterface {
23 * The file resource URI.
30 * A generic file resource handle.
34 public $handle = NULL;
39 public function getValue() {
40 // If the value has been set by (absolute) stream resource URI, access the
42 if (!isset($this->handle) && isset($this->uri)) {
43 $this->handle = is_readable($this->uri) ? fopen($this->uri, 'rb') : FALSE;
49 * Overrides TypedData::setValue().
51 * Supports a PHP file resource or a (absolute) stream resource URI as value.
53 public function setValue($value, $notify = TRUE) {
58 elseif (is_string($value)) {
59 // Note: For performance reasons we store the given URI and access the
60 // resource upon request. See BinaryData::getValue()
65 $this->handle = $value;
67 // Notify the parent of any changes.
68 if ($notify && isset($this->parent)) {
69 $this->parent->onChange($this->name);
76 public function getString() {
77 // Return the file content.
79 while (!feof($this->getValue())) {
80 $contents .= fread($this->handle, 8192);
88 public function getCastedValue() {
89 return $this->getValue();