diff --git a/core/lib/Drupal/Core/TypedData/AccessibleInterface.php b/core/lib/Drupal/Core/TypedData/AccessibleInterface.php new file mode 100644 index 0000000..3c42f7a --- /dev/null +++ b/core/lib/Drupal/Core/TypedData/AccessibleInterface.php @@ -0,0 +1,27 @@ +handle) && isset($this->uri)) { + $this->handle = fopen($this->uri, 'rb'); + } + return $this->handle; + } + + /** + * Implements WrapperInterface::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)) { + $this->uri = $value; + } + else { + throw new InvalidArgumentException("Invalid value for binary data given."); + } + } + + /** + * Implements WrapperInterface::getString(). + */ + public function getString() { + $contents = ''; + while (!feof($this->getValue())) { + $contents .= fread($this->handle, 8192); + } + return $contents; + } + + /** + * Implements WrapperInterface::validate(). + */ + public function validate() { + // TODO: Implement validate() method. + } +} diff --git a/core/lib/Drupal/Core/TypedData/Type/Boolean.php b/core/lib/Drupal/Core/TypedData/Type/Boolean.php new file mode 100644 index 0000000..de67e06 --- /dev/null +++ b/core/lib/Drupal/Core/TypedData/Type/Boolean.php @@ -0,0 +1,39 @@ +value = isset($value) ? (bool) $value : $value; + } + + /** + * Implements WrapperInterface::validate(). + */ + public function validate() { + // TODO: Implement validate() method. + } +} diff --git a/core/lib/Drupal/Core/TypedData/Type/Date.php b/core/lib/Drupal/Core/TypedData/Type/Date.php new file mode 100644 index 0000000..a60c20d --- /dev/null +++ b/core/lib/Drupal/Core/TypedData/Type/Date.php @@ -0,0 +1,68 @@ +value; + } + + /** + * Implements WrapperInterface::setValue(). + */ + public function setValue($value) { + if ($value instanceof DateTime || !isset($value)) { + $this->value = $value; + } + elseif (is_numeric($value)) { + // Value is a timestamp. + $this->value = new DateTime('@' . $value); + } + elseif (is_string($value)) { + $this->value = new DateTime($value); + } + else { + throw new InvalidArgumentException("Invalid date format given."); + } + } + + /** + * Implements WrapperInterface::getString(). + */ + public function getString() { + return (string) $this->getValue()->format(DateTime::ISO8601); + } + + /** + * Implements WrapperInterface::validate(). + */ + public function validate() { + // TODO: Implement validate() method. + } +} diff --git a/core/lib/Drupal/Core/TypedData/Type/Decimal.php b/core/lib/Drupal/Core/TypedData/Type/Decimal.php new file mode 100644 index 0000000..3bede7e --- /dev/null +++ b/core/lib/Drupal/Core/TypedData/Type/Decimal.php @@ -0,0 +1,39 @@ +value = isset($value) ? (float) $value : $value; + } + + /** + * Implements WrapperInterface::validate(). + */ + public function validate() { + // TODO: Implement validate() method. + } +} diff --git a/core/lib/Drupal/Core/TypedData/Type/Duration.php b/core/lib/Drupal/Core/TypedData/Type/Duration.php new file mode 100644 index 0000000..5dc8fc9 --- /dev/null +++ b/core/lib/Drupal/Core/TypedData/Type/Duration.php @@ -0,0 +1,66 @@ +value = $value; + } + elseif (is_numeric($value)) { + // Value is a time span in seconds. + $this->value = new DateInterval('PT' . $value . 'S'); + } + elseif (is_string($value)) { + // @todo: Add support for negative intervals on top of the DateInterval + // constructor. + $this->value = new DateInterval($value); + } + else { + throw new InvalidArgumentException("Invalid duration format given."); + } + } + + /** + * Implements WrapperInterface::getString(). + */ + public function getString() { + // Generate an ISO 8601 formatted string as supported by + // DateInterval::__construct() and setValue(). + return (string) $this->getValue()->format('%rP%yY%mM%dDT%hH%mM%sS'); + } + + /** + * Implements WrapperInterface::validate(). + */ + public function validate() { + // TODO: Implement validate() method. + } +} diff --git a/core/lib/Drupal/Core/TypedData/Type/Integer.php b/core/lib/Drupal/Core/TypedData/Type/Integer.php new file mode 100644 index 0000000..1d6bf3f --- /dev/null +++ b/core/lib/Drupal/Core/TypedData/Type/Integer.php @@ -0,0 +1,39 @@ +value = isset($value) ? (int) $value : $value; + } + + /** + * Implements WrapperInterface::validate(). + */ + public function validate() { + // TODO: Implement validate() method. + } +} diff --git a/core/lib/Drupal/Core/TypedData/Type/Language.php b/core/lib/Drupal/Core/TypedData/Type/Language.php new file mode 100644 index 0000000..068a32e --- /dev/null +++ b/core/lib/Drupal/Core/TypedData/Type/Language.php @@ -0,0 +1,93 @@ +definition = $definition; + + if (isset($context['parent']) && !empty($this->definition['settings']['langcode source'])) { + $this->langcode = $context['parent']->get($this->definition['settings']['langcode source']); + } + else { + // No context given, so just initialize an langcode property for storing + // the code. + $this->langcode = drupal_wrap_data(array('type' => 'string')); + } + + if (isset($value)) { + $this->setValue($value); + } + } + + /** + * Implements WrapperInterface::getValue(). + */ + public function getValue() { + $langcode = $this->langcode->getValue(); + return $langcode ? language_load($langcode) : NULL; + } + + /** + * Implements WrapperInterface::setValue(). + * + * Both the langcode and the language object may be passed as value. + */ + public function setValue($value) { + if (!isset($value)) { + $this->langcode->setValue(NULL); + } + elseif (is_scalar($value)) { + $this->langcode->setValue($value); + } + elseif (is_object($value)) { + $this->langcode->setValue($value->langcode); + } + else { + throw new InvalidArgumentException('Value is no valid langcode or language object.'); + } + } + + /** + * Implements WrapperInterface::getString(). + */ + public function getString() { + $language = $this->getValue(); + return $language ? $language->name : ''; + } + + /** + * Implements WrapperInterface::validate(). + */ + public function validate() { + // TODO: Implement validate() method. + } +} diff --git a/core/lib/Drupal/Core/TypedData/Type/String.php b/core/lib/Drupal/Core/TypedData/Type/String.php new file mode 100644 index 0000000..2adce82 --- /dev/null +++ b/core/lib/Drupal/Core/TypedData/Type/String.php @@ -0,0 +1,39 @@ +value = isset($value) ? (string) $value : $value; + } + + /** + * Implements WrapperInterface::validate(). + */ + public function validate() { + // TODO: Implement validate() method. + } +} diff --git a/core/lib/Drupal/Core/TypedData/Type/Uri.php b/core/lib/Drupal/Core/TypedData/Type/Uri.php new file mode 100644 index 0000000..9b933ea --- /dev/null +++ b/core/lib/Drupal/Core/TypedData/Type/Uri.php @@ -0,0 +1,38 @@ +value = isset($value) ? (string) $value : $value; + } + + /** + * Implements WrapperInterface::validate(). + */ + public function validate() { + // TODO: Implement validate() method. + } +} diff --git a/core/lib/Drupal/Core/TypedData/Type/WrapperBase.php b/core/lib/Drupal/Core/TypedData/Type/WrapperBase.php new file mode 100644 index 0000000..6411cb6 --- /dev/null +++ b/core/lib/Drupal/Core/TypedData/Type/WrapperBase.php @@ -0,0 +1,86 @@ +definition = $definition; + if (isset($value)) { + $this->setValue($value); + } + } + + /** + * Implements WrapperInterface::getType(). + * + * @return string + */ + public function getType() { + return $this->definition['type']; + } + + /** + * Implements WrapperInterface::getDefinition(). + * + * @return array + */ + public function getDefinition() { + return $this->definition; + } + + /** + * Implements WrapperInterface::getValue(). + * + * @return mixed + */ + public function getValue() { + return $this->value; + } + + /** + * Implements WrapperInterface::setValue(). + * + * @param mixed $value + */ + public function setValue($value) { + $this->value = $value; + } + + /** + * Implements WrapperInterface::getString(). + * + * @return string + */ + public function getString() { + return (string) $this->getValue(); + } +} diff --git a/core/lib/Drupal/Core/TypedData/WrapperInterface.php b/core/lib/Drupal/Core/TypedData/WrapperInterface.php new file mode 100644 index 0000000..75acaf5 --- /dev/null +++ b/core/lib/Drupal/Core/TypedData/WrapperInterface.php @@ -0,0 +1,83 @@ + 'Test data wrappers', + 'description' => 'Tests the functionality of all core data wrappers.', + 'group' => 'Typed Data API', + ); + } + + /** + * Tests the basics around constructing and working with data wrappers. + */ + public function testGetAndSet() { + // Boolean type. + $wrapper = $this->drupalWrapData(array('type' => 'boolean'), TRUE); + $this->assertTrue($wrapper->getValue() === TRUE, 'Boolean value was fetched.'); + $wrapper->setValue(FALSE); + $this->assertTrue($wrapper->getValue() === FALSE, 'Boolean value was changed.'); + $this->assertTrue(is_string($wrapper->getString()), 'Boolean value was converted to string'); + $wrapper->setValue(NULL); + $this->assertNull($wrapper->getValue(), 'Boolean wrapper is null-able.'); + + // String type. + $value = $this->randomString(); + $wrapper = $this->drupalWrapData(array('type' => 'string'), $value); + $this->assertTrue($wrapper->getValue() === $value, 'String value was fetched.'); + $new_value = $this->randomString(); + $wrapper->setValue($new_value); + $this->assertTrue($wrapper->getValue() === $new_value, 'String value was changed.'); + // Funky test. + $this->assertTrue(is_string($wrapper->getString()), 'String value was converted to string'); + $wrapper->setValue(NULL); + $this->assertNull($wrapper->getValue(), 'String wrapper is null-able.'); + + // Integer type. + $value = rand(); + $wrapper = $this->drupalWrapData(array('type' => 'integer'), $value); + $this->assertTrue($wrapper->getValue() === $value, 'Integer value was fetched.'); + $new_value = rand(); + $wrapper->setValue($new_value); + $this->assertTrue($wrapper->getValue() === $new_value, 'Integer value was changed.'); + $this->assertTrue(is_string($wrapper->getString()), 'Integer value was converted to string'); + $wrapper->setValue(NULL); + $this->assertNull($wrapper->getValue(), 'Integer wrapper is null-able.'); + + // Decimal type. + $value = 123.45; + $wrapper = $this->drupalWrapData(array('type' => 'decimal'), $value); + $this->assertTrue($wrapper->getValue() === $value, 'Decimal value was fetched.'); + $new_value = 678.90; + $wrapper->setValue($new_value); + $this->assertTrue($wrapper->getValue() === $new_value, 'Decimal value was changed.'); + $this->assertTrue(is_string($wrapper->getString()), 'Decimal value was converted to string'); + $wrapper->setValue(NULL); + $this->assertNull($wrapper->getValue(), 'Decimal wrapper is null-able.'); + + // Date type. + $value = new DateTime('@' . REQUEST_TIME); + $wrapper = $this->drupalWrapData(array('type' => 'date'), $value); + $this->assertTrue($wrapper->getValue() === $value, 'Date value was fetched.'); + $new_value = REQUEST_TIME + 1; + $wrapper->setValue($new_value); + $this->assertTrue($wrapper->getValue()->getTimestamp() === $new_value, 'Date value was changed and set by timestamp.'); + $wrapper->setValue('2000-01-01'); + $this->assertTrue($wrapper->getValue()->format('Y-m-d') == '2000-01-01', 'Date value was changed and set by date string.'); + $this->assertTrue(is_string($wrapper->getString()), 'Date value was converted to string'); + $wrapper->setValue(NULL); + $this->assertNull($wrapper->getValue(), 'Date wrapper is null-able.'); + + // Duration type. + $value = new DateInterval('PT20S'); + $wrapper = $this->drupalWrapData(array('type' => 'duration'), $value); + $this->assertTrue($wrapper->getValue() === $value, 'Duration value was fetched.'); + $wrapper->setValue(10); + $this->assertTrue($wrapper->getValue()->s == 10, 'Duration value was changed and set by time span in seconds.'); + $wrapper->setValue('P40D'); + $this->assertTrue($wrapper->getValue()->d == 40, 'Duration value was changed and set by duration string.'); + $this->assertTrue(is_string($wrapper->getString()), 'Duration value was converted to string'); + // Test getting the string and passing it back as value. + $duration = $wrapper->getString(); + $wrapper->setValue($duration); + $this->assertEqual($wrapper->getString(), $duration, 'Duration formatted as string can be used to set the duration value.'); + $wrapper->setValue(NULL); + $this->assertNull($wrapper->getValue(), 'Duration wrapper is null-able.'); + + // Generate some files that will be used to test the URI and the binary + // data types. + $files = $this->drupalGetTestFiles('image'); + + // URI type. + $wrapper = $this->drupalWrapData(array('type' => 'uri'), $files[0]->uri); + $this->assertTrue($wrapper->getValue() === $files[0]->uri, 'URI value was fetched.'); + $wrapper->setValue($files[1]->uri); + $this->assertTrue($wrapper->getValue() === $files[1]->uri, 'URI value was changed.'); + $this->assertTrue(is_string($wrapper->getString()), 'URI value was converted to string'); + $wrapper->setValue(NULL); + $this->assertNull($wrapper->getValue(), 'URI wrapper is null-able.'); + + // Binary type. + $wrapper = $this->drupalWrapData(array('type' => 'binary'), $files[0]->uri); + $this->assertTrue(is_resource($wrapper->getValue()), 'Binary value was fetched.'); + // Try setting by URI. + $wrapper->setValue($files[1]->uri); + $this->assertEqual(is_resource($wrapper->getValue()), fopen($files[1]->uri, 'r'), 'Binary value was changed.'); + $this->assertTrue(is_string($wrapper->getString()), 'Binary value was converted to string'); + // Try setting by resource. + $wrapper->setValue(fopen($files[2]->uri, 'r')); + $this->assertEqual(is_resource($wrapper->getValue()), fopen($files[2]->uri, 'r'), 'Binary value was changed.'); + $this->assertTrue(is_string($wrapper->getString()), 'Binary value was converted to string'); + $wrapper->setValue(NULL); + $this->assertNull($wrapper->getValue(), 'Binary wrapper is null-able.'); + } +}