diff --git a/src/Extensions/OpenGraphElement.php b/src/Extensions/OpenGraphElement.php index 9e6a71b..413688e 100755 --- a/src/Extensions/OpenGraphElement.php +++ b/src/Extensions/OpenGraphElement.php @@ -6,10 +6,14 @@ class OpenGraphElement implements JsonSerializable { - /** @var string */ + /** + * @var string + */ protected $url; - /** @var object */ + /** + * @var array + */ protected $buttons; /** @@ -22,9 +26,10 @@ public static function create() /** * @param string $url + * * @return $this */ - public function url($url) + public function url(string $url): self { $this->url = $url; @@ -33,9 +38,10 @@ public function url($url) /** * @param ElementButton $button + * * @return $this */ - public function addButton(ElementButton $button) + public function addButton(ElementButton $button): self { $this->buttons[] = $button->toArray(); @@ -44,15 +50,14 @@ public function addButton(ElementButton $button) /** * @param array $buttons + * * @return $this */ - public function addButtons(array $buttons) + public function addButtons(array $buttons): self { - if (isset($buttons) && is_array($buttons)) { - foreach ($buttons as $button) { - if ($button instanceof ElementButton) { - $this->buttons[] = $button->toArray(); - } + foreach ($buttons as $button) { + if ($button instanceof ElementButton) { + $this->buttons[] = $button->toArray(); } } @@ -62,7 +67,7 @@ public function addButtons(array $buttons) /** * @return array */ - public function toArray() + public function toArray(): array { return [ 'url' => $this->url, @@ -73,7 +78,7 @@ public function toArray() /** * @return array */ - public function jsonSerialize() + public function jsonSerialize(): array { return $this->toArray(); } diff --git a/src/Extensions/OpenGraphTemplate.php b/src/Extensions/OpenGraphTemplate.php index 8f4f162..c36e06c 100755 --- a/src/Extensions/OpenGraphTemplate.php +++ b/src/Extensions/OpenGraphTemplate.php @@ -7,10 +7,14 @@ class OpenGraphTemplate implements JsonSerializable, WebAccess { - /** @var string */ + /** + * @var string + */ protected $mediaType; - /** @var array */ + /** + * @var array + */ protected $elements = []; /** @@ -22,12 +26,29 @@ public static function create() } /** - * @param $element + * @param OpenGraphElement $element + * + * @return $this + */ + public function addElement(OpenGraphElement $element): self + { + $this->elements[] = $element->toArray(); + + return $this; + } + + /** + * @param OpenGraphElement[] $elements + * * @return $this */ - public function element($element) + public function addElements(array $elements): self { - $this->elements = [$element->toArray()]; + foreach ($elements as $element) { + if ($element instanceof OpenGraphElement) { + $this->elements[] = $element->toArray(); + } + } return $this; } @@ -35,7 +56,7 @@ public function element($element) /** * @return array */ - public function toArray() + public function toArray(): array { return [ 'attachment' => [ @@ -51,7 +72,7 @@ public function toArray() /** * @return array */ - public function jsonSerialize() + public function jsonSerialize(): array { return $this->toArray(); } @@ -62,7 +83,7 @@ public function jsonSerialize() * * @return array */ - public function toWebDriver() + public function toWebDriver(): array { return [ 'type' => $this->mediaType, diff --git a/tests/Extensions/OpenGraphElementTest.php b/tests/Extensions/OpenGraphElementTest.php index b712bf8..8ff3b34 100755 --- a/tests/Extensions/OpenGraphElementTest.php +++ b/tests/Extensions/OpenGraphElementTest.php @@ -12,29 +12,19 @@ class OpenGraphElementTest extends PHPUnit_Framework_TestCase /** @test */ public function it_can_be_created() { - $button = new OpenGraphElement('Rick Roll'); - $this->assertInstanceOf(Element::class, $button); + $element = new OpenGraphElement(); + $this->assertInstanceOf(OpenGraphElement::class, $element); } /** * @test **/ - public function it_can_set_title() + public function it_can_set_an_url() { - $element = new Element('Rick Roll'); + $element = new OpenGraphElement(); + $element->url('https://example.com'); - $this->assertSame('Rick Roll', Arr::get($element->toArray(), 'title')); - } - - /** - * @test - **/ - public function it_can_set_url() - { - $element = new Element('Rick Roll'); - $element->url('https://open.spotify.com/track/7GhIk7Il098yCjg4BQjzvb'); - - $this->assertSame('https://open.spotify.com/track/7GhIk7Il098yCjg4BQjzvb', Arr::get($element->toArray(), 'url')); + $this->assertSame('https://example.com', Arr::get($element->toArray(), 'url')); } /** @@ -42,12 +32,10 @@ public function it_can_set_url() **/ public function it_can_add_a_button() { - $template = new Element('Here are some buttons'); - $template->addButton(ElementButton::create('button1') - ->url('https://en.wikipedia.org/wiki/Rickrolling') - ->removeHeightRatio()); + $element = new OpenGraphElement(); + $element->addButton(ElementButton::create('button1')->url('https://en.wikipedia.org/wiki/Rickrolling')->removeHeightRatio()); - $this->assertSame('button1', Arr::get($template->toArray(), 'buttons.0.title')); + $this->assertSame('button1', Arr::get($element->toArray(), 'buttons.0.title')); } /** @@ -55,15 +43,13 @@ public function it_can_add_a_button() **/ public function it_can_add_multiple_buttons() { - $template = new Element('Here are some buttons'); - $template->addButtons([ElementButton::create('button1') - ->url('https://en.wikipedia.org/wiki/Rickrolling') - ->removeHeightRatio(), - ElementButton::create('button2') - ->url('https://en.wikipedia.org/') - ->removeHeightRatio(), ]); - - $this->assertSame('button1', Arr::get($template->toArray(), 'buttons.0.title')); - $this->assertSame('button2', Arr::get($template->toArray(), 'buttons.1.title')); + $element = new OpenGraphElement(); + $element->addButtons([ + ElementButton::create('button1')->url('https://en.wikipedia.org/wiki/Rickrolling')->removeHeightRatio(), + ElementButton::create('button2')->url('https://en.wikipedia.org/')->removeHeightRatio(), + ]); + + $this->assertSame('button1', Arr::get($element->toArray(), 'buttons.0.title')); + $this->assertSame('button2', Arr::get($element->toArray(), 'buttons.1.title')); } } diff --git a/tests/Extensions/OpenGraphTemplateTest.php b/tests/Extensions/OpenGraphTemplateTest.php index 86bbf33..8f47a1b 100755 --- a/tests/Extensions/OpenGraphTemplateTest.php +++ b/tests/Extensions/OpenGraphTemplateTest.php @@ -22,9 +22,23 @@ public function it_can_be_created() public function it_can_add_an_element() { $template = new OpenGraphTemplate; - $template->element(OpenGraphElement::create('Rick Roll')); + $template->addElement(OpenGraphElement::create()->url('https://example.com')); - $this->assertSame('Rick Roll', - Arr::get($template->toArray(), 'attachment.payload.elements.0.title')); + $this->assertSame('https://example.com', Arr::get($template->toArray(), 'attachment.payload.elements.0.url')); + } + + /** + * @test + **/ + public function it_can_add_multiple_elements() + { + $template = new OpenGraphTemplate; + $template->addElements([ + OpenGraphElement::create()->url('https://example.com'), + OpenGraphElement::create()->url('https://example.com'), + ]); + + $this->assertSame('https://example.com', Arr::get($template->toArray(), 'attachment.payload.elements.0.url')); + $this->assertSame('https://example.com', Arr::get($template->toArray(), 'attachment.payload.elements.1.url')); } }