Completed
Push — master ( a8898a...5c0dbc )
by Nazar
04:15
created

Meta   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 88.37%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 170
ccs 76
cts 86
cp 0.8837
rs 9
wmc 35
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
C __call() 0 31 8
C render() 0 29 8
C fill_required_properties() 0 27 8
B fill_required_properties_multilingual() 0 20 7
A image() 0 17 4
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2014-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\Page;
9
use
10
	cs\Config,
11
	cs\Language,
12
	cs\Page,
13
	cs\Request,
14
	cs\Singleton,
15
	h;
16
17
/**
18
 * Meta class for generation of various meta tags
19
 *
20
 * @method $this og(string $property, string|string[] $content)
21
 */
22
class Meta {
23
	use
24
		Singleton;
25
	/**
26
	 * Is used as <head prefix="$head_prefix">
27
	 * @var string
28
	 */
29
	public $head_prefix = '';
30
	/**
31
	 * If false - &lt;head&gt; will not be added automatically, and should be in template if needed
32
	 * @var bool
33
	 */
34
	public    $no_head   = false;
35
	protected $links     = '';
36
	protected $og_data   = [];
37
	protected $og_type   = '';
38
	protected $image_src = false;
39
	/**
40
	 * Common wrapper to add all necessary meta tags with images
41
	 *
42
	 * @param string|string[] $images
43
	 *
44
	 * @return Meta
45
	 */
46 2
	function image ($images) {
47 2
		if (!$images) {
48 2
			return $this;
49
		}
50 2
		$images = (array)$images;
51 2
		if (!$this->image_src && $images[0]) {
52 2
			$this->image_src = true;
53 2
			$this->links .= h::link(
54
				[
55 2
					'href' => $images[0],
56 2
					'rel'  => 'image_src'
57
				]
58
			);
59
		}
60 2
		$this->__call('og', ['image', $images]);
61 2
		return $this;
62
	}
63
	/**
64
	 * Common wrapper for generation of various Open Graph protocol meta tags
65
	 *
66
	 * @param string  $type
67
	 * @param mixed[] $params
68
	 *
69
	 * @return Meta
70
	 */
71 8
	function __call ($type, $params) {
72 8
		if (!$params) {
73 4
			$this->og_type         = $type;
74 4
			$this->og_data['type'] = h::meta(
75
				[
76 4
					'property' => "og:type",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal og:type does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
77 4
					'content'  => $type
78
				]
79
			);
80 4
			return $this;
81
		}
82 8
		if (!$params[0]) {
83
			return $this;
84
		}
85 8
		if (is_array($params[1])) {
86 2
			foreach ($params[1] as $p) {
87 2
				$this->__call($type, [$params[0], $p]);
88
			}
89 8
		} elseif ($params[1] || $params[1] === 0) {
90 8
			if (!isset($this->og_data[$params[0]])) {
91 8
				$this->og_data[$params[0]] = '';
92
			}
93 8
			$this->og_data[$params[0]] .= h::meta(
94
				[
95 8
					'property' => "$type:$params[0]",
96 8
					'content'  => $params[1]
97
				]
98
			);
99
		}
100 8
		return $this;
101
	}
102
	/**
103
	 * Generates Open Graph protocol information, and puts it into HTML
104
	 *
105
	 * Usually called by system itself, there is no need to call it manually
106
	 */
107 8
	function render () {
108 8
		$og = &$this->og_data;
109 8
		$this->fill_required_properties($og);
110 8
		$prefix = 'og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#';
111 8
		$type   = explode('.', $this->og_type, 2)[0];
112
		switch ($type) {
113 8
			case 'article':
114 4
			case 'blog':
115 4
			case 'book':
116 4
			case 'profile':
117 4
			case 'video':
118 4
			case 'website':
119 4
				$prefix .= " $type: http://ogp.me/ns/$type#";
120 4
				break;
121
		}
122 8
		$Page       = Page::instance();
123 8
		$Page->Head =
124 8
			$Page->Head.
125 8
			implode('', $og).
126 8
			$this->links;
127 8
		if (!$this->no_head) {
128 8
			$Page->Head = h::head(
129 8
				$Page->Head,
130
				[
131 8
					'prefix' => $prefix.$this->head_prefix
132
				]
133
			);
134
		}
135 8
	}
136
	/**
137
	 * If type, title and other important properties were not specified - try to guess and fill them automatically
138
	 *
139
	 * @param array $og
140
	 */
141 8
	protected function fill_required_properties (&$og) {
142 8
		$Page = Page::instance();
143 8
		if (!@$og['title']) {
144 6
			$this->og('title', $Page->Title);
145
		}
146 8
		if (!@$og['description']) {
147 6
			$this->og('description', $Page->Description);
148
		}
149 8
		$Config = Config::instance();
150 8
		if (!@$og['url']) {
151 8
			$Request = Request::instance();
152
			/** @noinspection NestedTernaryOperatorInspection */
153 8
			$this->og(
154 8
				'url',
155 8
				$Request->home_page
156 6
					? $Config->base_url()
157 8
					: ($Page->canonical_url ?: $Config->base_url().'/'.$Request->path_normalized)
158
			);
159
		}
160 8
		if (!@$og['site_name']) {
161 8
			$this->og('site_name', get_core_ml_text('name'));
162
		}
163 8
		if (!@$og['type']) {
164 4
			$this->og('type', 'website');
165
		}
166 8
		$this->fill_required_properties_multilingual($og);
167 8
	}
168
	/*
169
	 * @param array $og
170
	 */
171 8
	protected function fill_required_properties_multilingual (&$og) {
172 8
		$Config = Config::instance();
173 8
		if (!$Config->core['multilingual']) {
174 8
			return;
175
		}
176
		$L = Language::instance();
177
		if (!@$og['locale']) {
178
			$this->og('locale', $L->clocale);
179
		}
180
		if (
181
			!@$og['locale:alternate'] &&
182
			count($Config->core['active_languages']) > 1
183
		) {
184
			foreach ($Config->core['active_languages'] as $lang) {
185
				if ($lang != $L->clanguage) {
186
					$this->og('locale:alternate', $L->get('clocale', $lang));
187
				}
188
			}
189
		}
190
	}
191
}
192