Skip to content

Commit

Permalink
Merge pull request #4 from juampi92/feature/tagcollection-magic-getter
Browse files Browse the repository at this point in the history
Support `->twitter()->title()`
  • Loading branch information
juampi92 authored Sep 8, 2022
2 parents 7770c73 + 01c63b8 commit 95af4ea
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Tags/TagCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ public function get(string $property)
return $this->metadata[$property] ?? null;
}

/**
* @return string|array<string>|null
*/
public function __call(string $property, array $arguments)
{
return $this->get($property);
}

/**
* @return string|array<string>|null
*/
public function __get(string $property)
{
return $this->get($property);
}

public function toArray(): array
{
return $this->metadata;
Expand Down
33 changes: 33 additions & 0 deletions tests/Tags/TagCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,37 @@ public function test_it_should_convert_to_array(): void
],
], $og->toArray());
}

public function test_magic_getter_and_call_work(): void
{
// Arrange
$metadata = [
'og:title' => 'My Title',
'og:image' => [
'https://image.url/here.jpg',
'https://image.url/here-2.jpg',
],
];

// Act
$og = new TagCollection('og:', $metadata);

// Assert
$this->assertEquals('My Title', $og->title);
$this->assertEquals('My Title', $og->title());
$this->assertEquals('My Title', $og->get('title'));

$this->assertEquals([
'https://image.url/here.jpg',
'https://image.url/here-2.jpg',
], $og->image());
$this->assertEquals([
'https://image.url/here.jpg',
'https://image.url/here-2.jpg',
], $og->image);
$this->assertEquals([
'https://image.url/here.jpg',
'https://image.url/here-2.jpg',
], $og->get('image'));
}
}

0 comments on commit 95af4ea

Please sign in to comment.