Issues (9)

Branch: chinese

src/Url.php (1 issue)

Severity
1
<?php
2
3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cecil;
15
16
use Cecil\Asset;
17
use Cecil\Builder;
18
use Cecil\Collection\Menu\Entry as MenuEntry;
19
use Cecil\Collection\Page\Page;
20
use Cecil\Config;
21
use Cecil\Renderer\Page as PageRenderer;
22
use Cecil\Util;
23
24
/**
25
 * URL class.
26
 *
27
 * Builds an URL from a Page, a Menu Entry, an Asset or a string.
28
 */
29
class Url
30
{
31
    /** @var Builder */
32
    protected $builder;
33
34
    /** @var Config */
35
    protected $config;
36
37
    /** @var string */
38
    protected $url;
39
40
    /**
41
     * Creates an URL from a Page, a Menu Entry, an Asset or a string.
42
     *
43
     * @param Builder                          $builder
44
     * @param Page|MenuEntry|Asset|string|null $value
45
     * @param array|null                       $options Rendering options, e.g.: ['canonical' => true, 'format' => 'html', 'language' => 'fr']
46
     */
47
    public function __construct(Builder $builder, $value, ?array $options = null)
48
    {
49
        $this->builder = $builder;
50
        $this->config = $builder->getConfig();
51 1
52
        // handles options
53 1
        $canonical = null; // if true prefix url with baseurl config
54 1
        $format = null;    // output format
55 1
        $language = null;  // force language
56 1
        extract(\is_array($options) ? $options : [], EXTR_IF_EXISTS);
57
58
        // base URL
59
        $base = '';
60 1
        // enable canonical URL
61 1
        if ($this->config->isEnabled('canonicalurl') || $canonical === true) {
62 1
            $base = rtrim((string) $this->config->get('baseurl'), '/');
63 1
        }
64
        // disable canonical URL by option
65
        if ($canonical === false) {
0 ignored issues
show
The condition $canonical === false is always true.
Loading history...
66 1
            $base = '';
67
        }
68 1
        // use URL path as base if exists
69 1
        if ($base == '' && '/' != $basepath = parse_url((string) $this->config->get('baseurl'), PHP_URL_PATH)) {
70
            if (\is_string($basepath)) {
71
                $base = '/' . trim($basepath, '/');
72 1
            }
73 1
        }
74
75
        // if value is empty returns base URL
76 1
        if (\is_null($value) || empty($value) || $value == '/') {
77
            $this->url = $base;
78
79
            return;
80
        }
81
82
        switch (true) {
83 1
            case $value instanceof Page: // $value is a Page
84 1
                /** @var Page $value */
85
                if (!$format) {
86 1
                    $format = $value->getVariable('output');
87
                    if (\is_array($value->getVariable('output'))) {
88
                        $default = array_search('html', $value->getVariable('output')) ?: 0;
89
                        $format = $value->getVariable('output')[$default];
90 1
                    }
91
                    if (!$format) {
92 1
                        $format = 'html';
93 1
                    }
94 1
                }
95 1
                $this->url = $base . '/' . ltrim((new PageRenderer($this->builder, $value))->getPath($format), '/');
96 1
                if ($canonical && $value->hasVariable('canonical') && $value->getVariable('canonical')['url']) { // canonical URL
97
                    $this->url = $value->getVariable('canonical')['url'];
98 1
                }
99 1
                break;
100
            case $value instanceof MenuEntry: // $value is a Menu Entry
101
                /** @var MenuEntry $value */
102 1
                if (Util\File::isRemote($value['url'])) {
103 1
                    $this->url = $value['url'];
104
                    break;
105
                }
106 1
                $this->url = $base . '/' . ltrim($value['url'], '/');
107 1
                break;
108
            case $value instanceof Asset: // $value is an Asset
109
                /** @var Asset $value */
110
                $this->url = $base . '/' . ltrim($value['path'], '/');
111
                if ($value->isImageInCdn()) {
112
                    $this->url = (string) $value;
113
                }
114
                break;
115 1
            case \is_string($value): // others cases
116
                /** @var non-falsy-string $value */
117 1
                // $value is a potential Page ID
118 1
                $pageId = Page::slugify($value);
119
                // should force language?
120
                $lang = '';
121 1
                if ($language !== null && $language != $this->config->getLanguageDefault()) {
122 1
                    $pageId = "$language/$pageId";
123
                    $lang = "$language/";
124
                }
125 1
                switch (true) {
126
                    case Util\File::isRemote($value): // $value is an external URL
127 1
                        $this->url = $value;
128 1
                        break;
129 1
                    case $this->builder->getPages()->has($pageId): // $pageId exists in pages collection
130 1
                        $this->url = (string) new self($this->builder, $this->builder->getPages()->get($pageId), $options);
131
                        break;
132
                    default:
133 1
                        // remove double language prefix
134 1
                        if ($lang && Util\Str::startsWith($value, $lang)) {
135 1
                            $value = substr($value, \strlen($lang));
136 1
                        }
137 1
                        $this->url = $base . '/' . $lang . ltrim($value, '/');
138 1
                }
139
        }
140
    }
141 1
142 1
    /**
143
     * If called like a string returns built URL.
144 1
     */
145
    public function __toString(): string
146
    {
147
        return $this->getUrl();
148
    }
149
150
    /**
151
     * Returns built URL.
152 1
     */
153
    public function getUrl(): string
154 1
    {
155
        return (string) $this->url ?: '/';
156
    }
157
}
158