Skip to content

Commit

Permalink
Merge pull request #57 from samuelhgf/master
Browse files Browse the repository at this point in the history
Implements a method that returns only the built link of the social network
  • Loading branch information
jorenvh authored Sep 11, 2020
2 parents c82c43b + 4350274 commit ee60b9c
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,46 @@ This will generate the following html
</ul>
</div>
```

### Getting the raw links

In some cases you may only need the raw links without any html, you can get these by calling the `getRawLinks` method.

**A single link**
```php
Share::page('http://jorenvanhocht.be', 'Share title')
->facebook()
->getRawLinks();
```

Outputs:

```html
https://www.facebook.com/sharer/sharer.php?u=http://jorenvanhocht.be
```

**Multiple links**

```php
Share::page('http://jorenvanhocht.be', 'Share title')
->facebook()
->twitter()
->linkedin('Extra linkedin summary can be passed here')
->whatsapp()
->onlyLink();
```

Outputs:

```
[
"facebook" => "https://www.facebook.com/sharer/sharer.php?u=http://jorenvanhocht.be",
"twitter" => "https://twitter.com/intent/tweet?text=Share+title&url=http://jorenvanhocht.be",
"linkedin" => "http://www.linkedin.com/shareArticle?mini=true&url=http://jorenvanhocht.be&title=Share+title&summary=Extra+linkedin+summary+can+be+passed+here",
"whatsapp" => "https://wa.me/?text=http://jorenvanhocht.be",
]
```

### Optional parameters

#### Add extra classes, id's or titles to the social buttons
Expand Down
32 changes: 32 additions & 0 deletions src/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ class Share
*/
protected $url;

/**
* The generated urls
*
* @var string
*/
protected $generatedUrls = [];

/**
* Optional text for Twitter
* and Linkedin title
Expand Down Expand Up @@ -210,6 +217,20 @@ public function pinterest()
return $this;
}

/**
* Get the raw generated links.
*
* @return string|array
*/
public function getRawLinks()
{
if(count($this->generatedUrls) === 1) {
return array_first($this->generatedUrls);
}

return $this->generatedUrls;
}

/**
* Build a single link
*
Expand All @@ -220,6 +241,8 @@ protected function buildLink($provider, $url)
{
$fontAwesomeVersion = config('laravel-share.fontAwesomeVersion', 4);

$this->rememberRawLink($provider, $url);

$this->html .= trans("laravel-share::laravel-share-fa$fontAwesomeVersion.$provider", [
'url' => $url,
'class' => key_exists('class', $this->options) ? $this->options['class'] : '',
Expand All @@ -245,4 +268,13 @@ protected function setPrefixAndSuffix($prefix, $suffix)
$this->suffix = $suffix;
}
}

/**
* @param $provider
* @param $socialNetworkUrl
*/
protected function rememberRawLink($provider, $socialNetworkUrl)
{
$this->generatedUrls[$provider] = $socialNetworkUrl;
}
}
113 changes: 113 additions & 0 deletions tests/RawLinksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php


namespace Jorenvh\Share\Test;


use Jorenvh\Share\ShareFacade;

class RawLinksTest extends TestCase
{
/** @test */
public function it_can_return_only_facebook_built_link()
{
$expected = 'https://www.facebook.com/sharer/sharer.php?u=https://codeswitch.be';
$result = ShareFacade::page('https://codeswitch.be', 'My share title')
->facebook()
->getRawLinks();

$this->assertEquals($expected, $result);
}

/** @test */
public function it_can_return_only_twitter_built_link()
{
$expected = 'https://twitter.com/intent/tweet?text=My+share+title&url=https://codeswitch.be';
$result = ShareFacade::page('https://codeswitch.be', 'My share title')
->twitter()
->getRawLinks();

$this->assertEquals($expected, $result);
}

/** @test */
public function it_can_return_only_linkedin_built_link()
{
$expected = 'http://www.linkedin.com/shareArticle?mini=true&url=https://codeswitch.be&title=My+share+title&summary=';
$result = ShareFacade::page('https://codeswitch.be', 'My share title')
->linkedin()
->getRawLinks();

$this->assertEquals($expected, $result);
}

/** @test */
public function it_can_return_only_whatsapp_built_link()
{
$expected = 'https://wa.me/?text=https://codeswitch.be';
$result = ShareFacade::page('https://codeswitch.be', 'My share title')
->whatsapp()
->getRawLinks();

$this->assertEquals($expected, $result);
}

/** @test */
public function it_can_return_only_pinterest_built_link()
{
$expected = 'http://pinterest.com/pin/create/button/?url=https://codeswitch.be';
$result = ShareFacade::page('https://codeswitch.be', 'My share title')
->pinterest()
->getRawLinks();

$this->assertEquals($expected, $result);
}

/** @test */
public function it_can_return_only_reddit_built_link()
{
$expected = 'https://www.reddit.com/submit?title=My+share+title&url=https://codeswitch.be';
$result = ShareFacade::page('https://codeswitch.be', 'My share title')
->reddit()
->getRawLinks();

$this->assertEquals($expected, $result);
}

/** @test */
public function it_can_return_only_telegram_built_link()
{
$expected = 'https://telegram.me/share/url?url=https://codeswitch.be&text=My+share+title';
$result = ShareFacade::page('https://codeswitch.be', 'My share title')
->telegram()
->getRawLinks();

$this->assertEquals($expected, $result);
}

/** @test */
public function it_can_return_multiple_built_links_at_once()
{
$result = ShareFacade::page('https://codeswitch.be', 'My share title')
->facebook()
->twitter()
->linkedin()
->whatsapp()
->pinterest()
->reddit()
->telegram()
->getRawLinks();

$expected = [
'facebook' => 'https://www.facebook.com/sharer/sharer.php?u=https://codeswitch.be',
'twitter' => 'https://twitter.com/intent/tweet?text=My+share+title&url=https://codeswitch.be',
'linkedin' => 'http://www.linkedin.com/shareArticle?mini=true&url=https://codeswitch.be&title=My+share+title&summary=',
'whatsapp' => 'https://wa.me/?text=https://codeswitch.be',
'pinterest' => 'http://pinterest.com/pin/create/button/?url=https://codeswitch.be',
'reddit' => 'https://www.reddit.com/submit?title=My+share+title&url=https://codeswitch.be',
'telegram' => 'https://telegram.me/share/url?url=https://codeswitch.be&text=My+share+title',
];

$this->assertEquals($expected, $result);
}
}

0 comments on commit ee60b9c

Please sign in to comment.