Maknz\Slack\Attachment::addField PHP Method

addField() public method

Add a field to the attachment.
public addField ( mixed $field )
$field mixed
    public function addField($field)
    {
        if ($field instanceof AttachmentField) {
            $this->fields[] = $field;
            return $this;
        } elseif (is_array($field)) {
            $this->fields[] = new AttachmentField($field);
            return $this;
        }
        throw new InvalidArgumentException('The attachment field must be an instance of Maknz\\Slack\\AttachmentField or a keyed array');
    }

Usage Example

 public function testSetFields()
 {
     $a = new Attachment(['fallback' => 'Fallback', 'text' => 'Text']);
     $a->addField(['title' => 'Title 1', 'value' => 'Value 1', 'short' => true])->addField(['title' => 'Title 2', 'value' => 'Value 2', 'short' => true]);
     $this->assertSame(2, count($a->getFields()));
     $a->setFields([]);
     $this->assertSame(0, count($a->getFields()));
 }