forked from romka-chev/yii2-swiper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlide.php
140 lines (118 loc) · 4.22 KB
/
Slide.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace bestyii\swiper;
use bestyii\swiper\helpers\SwiperCssHelper;
use yii\base\BaseObject;
use yii\helpers\ArrayHelper;
/**
* Slide is representation of each slide for Swiper widget.
* Do not use it directly if you don't really know what
* you are doing.
*
* @package bestyii\swiper
*/
class Slide extends BaseObject
{
/**
* @see \bestyii\swiper\Slide::$content
*/
const CONTENT = 'content';
/**
* @see \bestyii\swiper\Slide::$background
*/
const BACKGROUND = 'background';
/**
* @see \bestyii\swiper\Slide::$hash
*/
const HASH = 'hash';
/**
* @var string content part, which will be applied in [[\yii\helpers\Html::tag()]].
*/
public $content;
/**
* @var string the shorthand alias which will be converted
* to [[background-image:url({$background})]] and them
* merged into other [[$options]]
*/
public $background;
/**
* @var string the shorthand alias, which will be moved to
* [[$options['data']['hash']]]
*/
public $hash;
/**
* @var mixed[] options, which will be applied in [[\yii\helpers\Html::tag()]]
*
* @see \bestyii\swiper\Slide::$background
* @see \bestyii\swiper\Slide::$hash
*/
public $options = [ ];
/**
* @param string|mixed[] $config the configuration of [[\bestyii\swiper\Slide]]
* You can create slide just from string
* For example:
*
* ~~~
* $slide = new \bestyii\swiper\Slide('slide content');
* ~~~
*
*
* Also you can create slide from array or strings and
* they will be merged into one string
* For example:
*
* ~~~
* $slide = new \bestyii\swiper\Slide([
* 'content' => [
* '<h1>Title</h1>',
* '<h3>Subtitle</h3>',
* '<p>Main content</p>'
* ]
* ]);
* ~~~
*
* @see \bestyii\swiper\Slide::$background
* @see \bestyii\swiper\Slide::$hash
* @see \bestyii\swiper\Slide::$content
*/
public function __construct( $config = [ ] )
{
$config = is_string( $config )
? [ self::CONTENT => $config ]
: $config;
$config[self::CONTENT] = ArrayHelper::getValue( $config, self::CONTENT, null );
$config[self::CONTENT] = is_array( $config[self::CONTENT] )
? implode( '', $config[self::CONTENT] )
: $config[self::CONTENT];
parent::__construct( $config );
}
/**
* @inheritdoc
*/
public function init()
{
$this->normalizeOptions();
}
/**
* This function sets default values to
* options for further usage
*/
protected function normalizeOptions()
{
$this->options['data'] = ArrayHelper::getValue( $this->options, 'data', [ ] );
$this->options['style'] = ArrayHelper::getValue( $this->options, 'style', '' );
$this->options['data']['hash'] = $this->hash ?: ArrayHelper::getValue( $this->options['data'], 'hash', null );
$this->hash = $this->hash ?: ArrayHelper::getValue( $this->options['data'], 'hash', null );
if ($this->background) {
$this->options['style'] = SwiperCssHelper::mergeStyleAndBackground(
$this->background,
ArrayHelper::getValue( $this->options, 'style', '' )
);
} elseif (ArrayHelper::getValue( $this->options, 'style' )) {
$this->background = SwiperCssHelper::getBackgroundUrl(
$this->options['style']
);
}
$this->options = array_filter( $this->options );
$this->options['data'] = array_filter( $this->options['data'] );
}
}