Skip to content

Commit

Permalink
Enh: added slider method into Content helper
Browse files Browse the repository at this point in the history
  • Loading branch information
sokolnikov911 committed Sep 2, 2018
1 parent bcddaa3 commit 7912ea4
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Dev-master
* Enh: added rating method into Content helper
* Enh: added ownVideo method into Content helper
* Enh: added adBlockPosition method into Content helper
* Enh: added slider method into Content helper

1.1.1 July 10, 2018
--------------------
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ At this time you can use helpers for generate next elements:
* rating;
* accordion;
* video from own server;
* ad block position element.
* ad block position element;
* media-content with slider.

Examples of using helpers you can find in `examples/content_helpers.php`.

Expand Down
3 changes: 2 additions & 1 deletion README_RU.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ $header = Content::header('Main title', 'Second title',
* рейтинг;
* аккордеон;
* видео со своего сервера;
* елемент-указатель на место размещения рекламного блока.
* елемент-указатель на место размещения рекламного блока;
* галереия медиа-контента со слайдером.

Примеры использования хелпера для генерирования контента смотрите в `examples/content_helpers.php`.

Expand Down
11 changes: 11 additions & 0 deletions examples/content_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,14 @@

// generate Ad block position element
$adBlockPosition = Content::adBlockPosition('first_ad_place');



// generate slider block
$items = [
['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''],
['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''],
['url' => 'http://example.com/image3.jpg'],
['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1']
];
$slider = Content::slider($items);
48 changes: 48 additions & 0 deletions src/sokolnikov911/YandexTurboPages/helpers/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ class Content
const SHARE_TYPE_TWITTER = 'twitter';
const SHARE_TYPE_VKONTAKTE = 'vkontakte';

const SLIDER_DATA_VIEW_SQUARE = 'square';
const SLIDER_DATA_VIEW_PORTRAIT = 'portrait';
const SLIDER_DATA_VIEW_LANDSCAPE = 'landscape';

const SLIDER_DATA_ITEM_VIEW_COVER = 'cover';
const SLIDER_DATA_ITEM_VIEW_CONTAIN = 'contain';

const OWN_VIDEO_TYPE_MP4 = 'video/mp4';

/**
Expand Down Expand Up @@ -89,6 +96,47 @@ public static function gallery(array $imagesArray, string $header = null): strin
return '<div data-block="gallery">' . $galleryString . '</div>';
}

/**
* Generate media slider
* @param array $itemsArray Array of items with data
* [
* ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''],
* ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''],
* ['url' => 'http://example.com/image3.jpg'],
* ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1']
* ]
* @param string|null $header
* @param string $dataView
* @param string $dataItemView
* @return string
*/
public static function slider(array $itemsArray, string $header = null,
string $dataView = self::SLIDER_DATA_VIEW_SQUARE,
string $dataItemView = self::SLIDER_DATA_ITEM_VIEW_COVER): string
{
$sliderString = $header ? '<header>' . $header . '</header>' : '';

foreach ($itemsArray as $item) {
if (isset($item['url']) || (isset($item['href']) && isset($item['text']))) {
$sliderString .= '<figure>';
$sliderString .= isset($item['title'])
? '<figcaption>' . $item['title'] . '</figcaption>'
: '';

if (isset($item['url'])) {
$sliderString .= '<img src="' . $item['url'] . '" />';
} elseif (isset($item['href']) && isset($item['text'])) {
$sliderString .= '<a href="' . $item['href'] . '">' . $item['text'] . '</a>';
}

$sliderString .= '</figure>';
}
}

return '<div data-block="slider" data-view="' . $dataView . '" data-item-view="'
. $dataItemView . '">' . $sliderString . '</div>';
}

/**
* Generate share block
* @param array|null $networks Array of network names
Expand Down
65 changes: 65 additions & 0 deletions tests/sokolnikov911/YandexTurboPages/helpers/ContentSliderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace sokolnikov911\YandexTurboPages\helpers;

use PHPUnit\Framework\TestCase;

class ContentSliderTest extends TestCase
{
private $header = 'Some slider description';
private $items = [
['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''],
['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''],
['url' => 'http://example.com/image3.jpg'],
['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1']
];

public function testBaseSlider()
{
$slider = Content::slider($this->items);
$baseSlider = "<div data-block=\"slider\" data-view=\"square\" data-item-view=\"cover\">
<figure>
<figcaption>Image title 1</figcaption>
<img src=\"http://example.com/image1.jpg\" />
</figure>
<figure>
<figcaption>Image title 2</figcaption>
<img src=\"http://example.com/image2.jpg\" />
</figure>
<figure>
<img src=\"http://example.com/image3.jpg\" />
</figure>
<figure>
<figcaption>Link title 1</figcaption>
<a href=\"http://example.com/page1.html\">Link text 1</a>
</figure>
</div>";

$this->assertXmlStringEqualsXmlString($baseSlider, $slider);
}

public function testSliderWithHeader()
{
$slider = Content::slider($this->items, 'Slider header', Content::SLIDER_DATA_VIEW_PORTRAIT, Content::SLIDER_DATA_ITEM_VIEW_CONTAIN);
$baseSlider = "<div data-block=\"slider\" data-view=\"portrait\" data-item-view=\"contain\">
<header>Slider header</header>
<figure>
<figcaption>Image title 1</figcaption>
<img src=\"http://example.com/image1.jpg\" />
</figure>
<figure>
<figcaption>Image title 2</figcaption>
<img src=\"http://example.com/image2.jpg\" />
</figure>
<figure>
<img src=\"http://example.com/image3.jpg\" />
</figure>
<figure>
<figcaption>Link title 1</figcaption>
<a href=\"http://example.com/page1.html\">Link text 1</a>
</figure>
</div>";

$this->assertXmlStringEqualsXmlString($baseSlider, $slider);
}
}

0 comments on commit 7912ea4

Please sign in to comment.