AttributeArray example


class AttributeTest extends UnitTestCase {

  /** * Tests the constructor of the attribute class. */
  public function testConstructor() {
    $attribute = new Attribute(['class' => ['example-class']]);
    $this->assertTrue(isset($attribute['class']));
    $this->assertEquals(new AttributeArray('class', ['example-class'])$attribute['class']);

    // Test adding boolean attributes through the constructor.     $attribute = new Attribute(['selected' => TRUE, 'checked' => FALSE]);
    $this->assertTrue($attribute['selected']->value());
    $this->assertFalse($attribute['checked']->value());

    // Test that non-array values with name "class" are cast to array.     $attribute = new Attribute(['class' => 'example-class']);
    $this->assertTrue(isset($attribute['class']));
    $this->assertEquals(new AttributeArray('class', ['example-class'])$attribute['class']);

    

    // An array value or 'class' attribute name are forced to always be an     // AttributeArray value for consistency.     if ($name == 'class' && !is_array($value)) {
      // Cast the value to string in case it implements MarkupInterface.       $value = [(string) $value];
    }
    if (is_array($value)) {
      // Cast the value to an array if the value was passed in as a string.       // @todo Decide to fix all the broken instances of class as a string       // in core or cast them.       $value = new AttributeArray($name$value);
    }
    elseif (is_bool($value)) {
      $value = new AttributeBoolean($name$value);
    }
    // As a development aid, we allow the value to be a safe string object.     elseif ($value instanceof MarkupInterface) {
      // Attributes are not supposed to display HTML markup, so we just convert       // the value to plain text.       $value = PlainTextOutput::renderFromHtml($value);
      $value = new AttributeString($name$value);
    }
    
Home | Imprint | This part of the site doesn't use cookies.