Passed
Push — fix/nikic-iter-2 ( e84dbe...0caef1 )
by Arnaud
03:16
created

Url::getUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 $url;
33
    /** @var Slugify */
34
    private static $slugifier;
35
36
    /**
37
     * Creates an URL from string, Page or Asset.
38
     *
39
     * $options[
40
     *     'canonical' => true,
41
     *     'format'    => 'json',
42
     * ];
43
     *
44
     * @param Builder                $builder
45
     * @param Page|Asset|string|null $value
46
     * @param array|null             $options
47
     */
48
    public function __construct(Builder $builder, $value, array $options = null)
49
    {
50
        $this->builder = $builder;
51
        $this->config = $builder->getConfig();
52
        if (!self::$slugifier instanceof Slugify) {
53
            self::$slugifier = Slugify::create(['regexp' => Page::SLUGIFY_PATTERN]);
54
        }
55
        $this->baseurl = (string) $this->config->get('baseurl');
56
57
        // handles options
58
        $canonical = null;
59
        $format = null;
60
        extract(is_array($options) ? $options : [], EXTR_IF_EXISTS);
61
62
        // canonical URL?
63
        if ((bool) $this->config->get('canonicalurl') || $canonical === true) {
64
            $base = rtrim($this->baseurl, '/');
65
        }
66
        if ($canonical === false) {
67
            $base = '';
68
        }
69
70
        // value is empty (ie: `url()`)
71
        if (is_null($value) || empty($value) || $value == '/') {
72
            $this->url = $base.'/';
73
74
            return;
75
        }
76
77
        // potential page id
78
        $pageId = null;
79
        if (is_string($value)) {
80
            $pageId = self::$slugifier->slugify($value);
81
        }
82
83
        switch (true) {
84
            // Page
85
            case $value instanceof Page:
86
                if (!$format) {
87
                    $format = $value->getVariable('output');
88
                    if (is_array($value->getVariable('output'))) {
89
                        $format = $value->getVariable('output')[0];
90
                    }
91
                    if (!$format) {
92
                        $format = 'html';
93
                    }
94
                }
95
                $this->url = $base.'/'.ltrim($value->getUrl($format, $this->config), '/');
96
                break;
97
            // Asset
98
            case $value instanceof Asset:
99
                $asset = $value;
100
                $this->url = $base.'/'.ltrim($asset['path'], '/');
101
                /** @var Asset $asset */
102
                $asset->save();
103
                break;
104
            // External URL
105
            case Util::isExternalUrl($value):
106
                $this->url = $value;
107
                break;
108
            // asset as string
109
            case false !== strpos($value, '.') ? true : false:
110
                $this->url = $base.'/'.ltrim($value, '/');
111
                break;
112
            // Page ID as string
113
            case $this->builder->getPages()->has($pageId):
114
                $page = $this->builder->getPages()->get($pageId);
115
                $this->url = new self($this->builder, $page, $options);
116
                break;
117
            // others cases?
118
            default:
119
                // others cases
120
                $this->url = $base.'/'.$value;
121
        }
122
    }
123
124
    /**
125
     * Returns URL.
126
     *
127
     * @return string
128
     */
129
    public function __toString(): string
130
    {
131
        return $this->url;
132
    }
133
134
    /**
135
     * Returns URL.
136
     *
137
     * @return string
138
     */
139
    public function getUrl(): string
140
    {
141
        return $this->__toString();
142
    }
143
}
144