Passed
Push — url ( b41b6a...f657fd )
by Arnaud
04:07
created

Url   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
eloc 58
c 5
b 1
f 1
dl 0
loc 123
rs 10
wmc 21

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A getUrl() 0 3 1
D __construct() 0 75 19
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Assets;
15
16
use Cecil\Builder;
17
use Cecil\Collection\Page\Page;
18
use Cecil\Config;
19
use Cecil\Renderer\Page as PageRenderer;
20
use Cecil\Util;
21
use Cocur\Slugify\Slugify;
22
23
class Url
24
{
25
    /** @var Builder */
26
    protected $builder;
27
28
    /** @var Config */
29
    protected $config;
30
31
    /** @var Page|Asset|string|null */
32
    protected $value;
33
34
    /** @var array */
35
    protected $options;
36
37
    /** @var string */
38
    protected $baseurl;
39
40
    /** @var string */
41
    protected $url;
42
43
    /** @var Slugify */
44
    private static $slugifier;
45
46
    /**
47
     * Creates an URL from a string, a Page or an Asset.
48
     *
49
     * @param Builder                $builder
50
     * @param Page|Asset|string|null $value
51
     * @param array|null             $options Render options, e.g.: ['canonical' => true, 'format' => 'json']
52
     */
53
    public function __construct(Builder $builder, $value, array $options = null)
54
    {
55
        $this->builder = $builder;
56
        $this->config = $builder->getConfig();
57
        if (!self::$slugifier instanceof Slugify) {
58
            self::$slugifier = Slugify::create(['regexp' => Page::SLUGIFY_PATTERN]);
59
        }
60
        $this->baseurl = (string) $this->config->get('baseurl');
61
62
        // handles options
63
        $canonical = null;
64
        $format = null;
65
        $language = null;
66
        extract(is_array($options) ? $options : [], EXTR_IF_EXISTS);
67
68
        // canonical URL?
69
        $base = '';
70
        if ((bool) $this->config->get('canonicalurl') || $canonical === true) {
71
            $base = rtrim($this->baseurl, '/');
72
        }
73
        if ($canonical === false) {
74
            $base = '';
75
        }
76
77
        // value is empty (ie: `url()`)
78
        if (is_null($value) || empty($value) || $value == '/') {
79
            $this->url = $base.'/';
80
81
            return;
82
        }
83
84
        switch (true) {
85
            // Page
86
            case $value instanceof Page:
87
                if (!$format) {
88
                    $format = $value->getVariable('output');
89
                    if (is_array($value->getVariable('output'))) {
90
                        $format = $value->getVariable('output')[0]; // first format by default
91
                    }
92
                    if (!$format) {
93
                        $format = 'html'; // 'html' format by default
94
                    }
95
                }
96
                $this->url = $base.'/'.ltrim((new PageRenderer($this->config))->getUrl($value, $format), '/');
97
                break;
98
            // Asset
99
            case $value instanceof Asset:
100
                $this->url = $base.'/'.ltrim($value['path'], '/');
101
                break;
102
            // string
103
            case is_string($value):
104
                // potential Page ID
105
                $pageId = self::$slugifier->slugify($value);
106
                // force language?
107
                $lang = '';
108
                if ($language !== null && $language != $this->config->getLanguageDefault()) {
109
                    $pageId = "$pageId.$language";
110
                    $lang = "$language/";
111
                }
112
                switch (true) {
113
                    // External URL
114
                    case Util\Url::isUrl($value):
115
                        $this->url = $value;
116
                        break;
117
                    // Page ID as string
118
                    case $this->builder->getPages()->has($pageId):
119
                        $this->url = (string) new self(
120
                            $this->builder,
121
                            $this->builder->getPages()->get($pageId),
122
                            $options
123
                        );
124
                        break;
125
                    // default case
126
                    default:
127
                        $this->url = $base.'/'.$lang.ltrim($value, '/');
128
                }
129
        }
130
    }
131
132
    /**
133
     * Returns URL.
134
     */
135
    public function __toString(): string
136
    {
137
        return (string) $this->url;
138
    }
139
140
    /**
141
     * Returns URL.
142
     */
143
    public function getUrl(): string
144
    {
145
        return $this->__toString();
146
    }
147
}
148