Skip to content

Commit

Permalink
Create configuration for OpenGraph. Updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
vumanskyi committed Oct 5, 2020
1 parent dd82538 commit b8764ef
Show file tree
Hide file tree
Showing 17 changed files with 162 additions and 152 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
CHANGE LOG
==========
## v2.1.0 (5/10/2020)

* Added configuration for Open Graph
* Updated README.md file

## v2.0.1 (2/10/2020)

* Add missed fields to TwitterCard
* Added missed fields to TwitterCard
* php7.1 doesn't support

## V2.0.0 (31/03/2019)
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ Add video attribute:
->render();
```

In current version also has the configuration **Twitter Card**
The current version also has a configuration **Twitter Card**

```php
/**
Expand All @@ -252,3 +252,7 @@ In current version also has the configuration **Twitter Card**
->setCreator('Vlad Umanskyi')
->render();
```
If you want to add own configuration or override some tag, you must implement **umanskyi31\opengraph\Configuration** and add to Yii container. Some example you can find here **umanskyi31\opengraph\OpenGraphConfiguration**

--------------------------------------------
If you have any issue please let me know [issue](https://github.com/vumanskyi/yii2-opengraph/issues)
29 changes: 29 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

namespace umanskyi31\opengraph;

/**
* This is an implementation of configuration for open graph
* With this configuration you can override exists tags and set own implmentation
*
* @author Vladyslav Umanskyi <[email protected]>
* @version 2.0.1
*/
interface Configuration
{
const VERSION = '2.0.1';

/**
* Consist all available tags
*
* @return array
*/
public function tags(): array;

/**
* @param array $data
* @return string|array|void
*/
public function render(array $data);
}
160 changes: 30 additions & 130 deletions src/OpenGraph.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
declare(strict_types=1);

namespace umanskyi31\opengraph;

use umanskyi31\opengraph\Exceptions\OpenGraphException;
use umanskyi31\opengraph\Tags\Article;
use umanskyi31\opengraph\Tags\Audio;
use umanskyi31\opengraph\Tags\Basic;
Expand All @@ -13,150 +15,48 @@
use umanskyi31\opengraph\Tags\Video;
use Yii;

/**
* @method Article getArticle()
* @method Image getImage()
* @method Audio getAudio()
* @method Book getBook()
* @method Basic getBasic()
* @method Music getMusic()
* @method Profile getProfile()
* @method Video getVideo()
* @method TwitterCard useTwitterCard()
*/
class OpenGraph
{
/**
* @var Article
*/
protected $article;

/**
* @var Audio
*/
protected $audio;

/**
* @var Basic
*/
protected $basic;

/**
* @var Book
*/
protected $book;

/**
* @var Image
*/
protected $image;

/**
* @var Music
*/
protected $music;

/**
* @var Profile
*/
protected $profile;

/**
* @var Video
*/
protected $video;

/**
* @var TwitterCard
*/
protected $twitterCard;

/**
* @return Basic
*/
public function getBasic(): Basic
{
$this->basic = new Basic($this);

return $this->basic;
}
protected $configuration;

/**
* @return Image
* OpenGraph constructor.
*/
public function getImage(): Image
public function __construct()
{
$this->image = new Image($this);
Yii::$container->setSingletons([
Configuration::class => OpenGraphConfiguration::class,
]);

return $this->image;
$this->configuration = Yii::$container->get(Configuration::class);
}

/**
* @return Music
* @param string $name
* @param array $arguments
* @return object
* @throws OpenGraphException
*/
public function getMusic(): Music
public function __call(string $name, array $arguments = [])
{
$this->music = new Music($this);
$tags = $this->configuration->tags();

return $this->music;
}

/**
* @return Video
*/
public function getVideo(): Video
{
$this->video = new Video($this);
if (!array_key_exists($name, $tags)) {
throw new OpenGraphException(sprintf('The method %s does not exist', $name));
}

return $this->video;
}

/**
* @return Audio
*/
public function getAudio(): Audio
{
$this->audio = new Audio($this);

return $this->audio;
}

/**
* @return Article
*/
public function getArticle(): Article
{
$this->article = new Article($this);

return $this->article;
}

/**
* @return Book
*/
public function getBook(): Book
{
$this->book = new Book($this);

return $this->book;
}

/**
* @return Profile
*/
public function getProfile(): Profile
{
$this->profile = new Profile($this);

return $this->profile;
}

/**
* @return TwitterCard
*/
public function useTwitterCard(): TwitterCard
{
$this->twitterCard = new TwitterCard($this);

return $this->twitterCard;
}

/**
* @codeCoverageIgnore
*
* @param array $data
*/
public function render(array $data)
{
Yii::$app->view->registerMetaTag($data);
return $tags[$name];
}
}
47 changes: 47 additions & 0 deletions src/OpenGraphConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);

namespace umanskyi31\opengraph;


use umanskyi31\opengraph\Tags\Article;
use umanskyi31\opengraph\Tags\Audio;
use umanskyi31\opengraph\Tags\Basic;
use umanskyi31\opengraph\Tags\Book;
use umanskyi31\opengraph\Tags\Image;
use umanskyi31\opengraph\Tags\Music;
use umanskyi31\opengraph\Tags\Profile;
use umanskyi31\opengraph\Tags\TwitterCard;
use umanskyi31\opengraph\Tags\Video;
use Yii;

class OpenGraphConfiguration implements Configuration
{
/**
* @return array
*/
public function tags(): array
{
return [
'getBasic' => new Basic($this),
'getImage' => new Image($this),
'getMusic' => new Music($this),
'getVideo' => new Video($this),
'getAudio' => new Audio($this),
'getArticle' => new Article($this),
'getBook' => new Book($this),
'getProfile' => new Profile($this),
'useTwitterCard' => new TwitterCard($this),
];
}

/**
* @codeCoverageIgnore
* @param array $data
* @return array|string|void
*/
public function render(array $data)
{
Yii::$app->view->registerMetaTag($data);
}
}
17 changes: 9 additions & 8 deletions src/Tags/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace umanskyi31\opengraph\Tags;

use umanskyi31\opengraph\Configuration;
use umanskyi31\opengraph\OpenGraph;

abstract class Tag
Expand All @@ -12,26 +13,26 @@ abstract class Tag
const OG_PREFIX = 'og:';

/**
* @var OpenGraph
* @var Configuration
*/
private $openGraph;
private $configuration;

/**
* TwitterCard constructor.
*
* @param OpenGraph $openGraph
* @param Configuration $configuration
*/
public function __construct(OpenGraph $openGraph)
public function __construct(Configuration $configuration)
{
$this->openGraph = $openGraph;
$this->configuration = $configuration;
}

/**
* @return OpenGraph
* @return Configuration
*/
public function getOpenGraph(): OpenGraph
public function getOpenGraph(): Configuration
{
return $this->openGraph;
return $this->configuration;
}

/**
Expand Down
16 changes: 15 additions & 1 deletion tests/Unit/src/OpenGraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace umanskyi31\opengraph\test\Unit\src;

use PHPUnit\Framework\TestCase;
use umanskyi31\opengraph\Exceptions\OpenGraphException;
use umanskyi31\opengraph\OpenGraph;
use umanskyi31\opengraph\Tags\Article;
use umanskyi31\opengraph\Tags\Audio;
Expand All @@ -27,7 +28,10 @@ protected function setUp(): void
$this->opengraph = new OpenGraph();
}

public function testCheckGetters()
/**
* @test
*/
public function checkGetters()
{
$this->assertInstanceOf(OpenGraph::class, $this->opengraph);
$this->assertInstanceOf(Basic::class, $this->opengraph->getBasic());
Expand All @@ -40,4 +44,14 @@ public function testCheckGetters()
$this->assertInstanceOf(Profile::class, $this->opengraph->getProfile());
$this->assertInstanceOf(TwitterCard::class, $this->opengraph->useTwitterCard());
}

/**
* @test
*/
public function notExistsMethod()
{
$this->expectException(OpenGraphException::class);
$this->expectExceptionMessage('The method getTest does not exist');
$this->opengraph->getTest();
}
}
3 changes: 2 additions & 1 deletion tests/Unit/src/Tags/ArticleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use PHPUnit\Framework\TestCase;
use umanskyi31\opengraph\OpenGraph;
use umanskyi31\opengraph\OpenGraphConfiguration;
use umanskyi31\opengraph\Tags\Article;

class ArticleTest extends TestCase
Expand All @@ -16,7 +17,7 @@ protected function setUp(): void
{
parent::setUp();

$this->opengraph = new OpenGraph();
$this->opengraph = new OpenGraphConfiguration();
}

/**
Expand Down
Loading

0 comments on commit b8764ef

Please sign in to comment.