Passed
Push — analysis-ajLxYo ( 606ccb )
by Arnaud
08:49 queued 03:37
created

Url::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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