Completed
Push — master ( 785c2a...666091 )
by Peter
06:01
created

ApiUrl::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Zamm;
10
11
use Maslosoft\Zamm\Interfaces\SourceAccessorInterface;
12
use UnexpectedValueException;
13
14
/**
15
 * Api Url generator
16
 * 
17
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
18
 */
19
class ApiUrl implements SourceAccessorInterface
20
{
21
22
	use Traits\SourceMagic;
23
24
	private static $sources = [];
25
	private $dotName = '';
26
	private $source = '';
27
28
	public function __construct($className = null, $text = '')
29
	{
30
		$this->dotName = str_replace('\\', '.', $className);
31
32
33
		// Many source found, search for proper api source
34
		$url = '';
35
//		var_dump(self::$sources);
36
		$search = self::normalize($className);
37
		foreach (self::$sources as $url => $ns)
38
		{
39
			if (empty($ns))
40
			{
41
				continue;
42
			}
43
			$pos = strpos($search, $ns);
44
45
			if ($pos === false)
46
			{
47
				continue;
48
			}
49
			if ($pos === 0)
50
			{
51
				$this->source = $url;
1 ignored issue
show
Documentation Bug introduced by
It seems like $url can also be of type integer. However, the property $source is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
52
				break;
53
			}
54
		}
55
56
		// Last resort url assigning
57
		if (empty($this->source))
58
		{
59
			$this->source = $url;
60
		}
61
	}
62
63
	/**
64
	 * Set root url of current project API. This can be relative or absolute url.
65
	 *
66
	 * Set url for many projects with namespaces - this allows cross-linking different projects:
67
	 *
68
	 * ```
69
	 * ApiUrl::setRoot([
70
	 * 		'/mangan/api' => 'Maslosoft\\Mangan\\'
71
	 * 		'/addendum/api' => 'Maslosoft\\Addendum\\'
72
	 * ]);
73
	 * ```
74
	 *
75
	 * Could also be used for one project, but might result in wrong url if
76
	 * used on classes outside of project:
77
	 *
78
	 * ```
79
	 * ApiUrl::setRoot('/mangan/api);
80
	 * ```
81
	 *
82
	 * 
83
	 * @param string $apiUrl
84
	 */
85
	public static function setRoot($apiUrl)
86
	{
87
		if (is_string($apiUrl))
88
		{
89
			self::$sources[rtrim($apiUrl, '/')] = '';
90
		}
91
		elseif (is_array($apiUrl))
92
		{
93
			$urls = [];
94
			foreach ($apiUrl as $url => $ns)
95
			{
96
				$urls[rtrim($url, '/')] = self::normalize($ns);
97
			}
98
			self::$sources = array_merge(self::$sources, $urls);
99
		}
100
		else
101
		{
102
			throw new UnexpectedValueException(sprintf('Expected `apiUrl` to be string or array, got `%s`', gettype($apiUrl)));
103
		}
104
	}
105
106
	public function method($name)
107
	{
108
		// https://df.home/zamm/api/class-Maslosoft.Zamm.Decorators.AbstractDecorator.html#_decorate
109
		return sprintf('%s/class-%s.html#_%s', $this->source, $this->dotName, $name);
110
	}
111
112
	public function property($name)
113
	{
114
		// https://df.home/zamm/api/class-Maslosoft.Zamm.Zamm.html#$decorators
115
		return sprintf('%s/class-%s.html#$%s', $this->source, $this->dotName, $name);
116
	}
117
118
	public static function __callStatic($name, $arguments)
119
	{
120
		
121
	}
122
123
	public function __toString()
124
	{
125
		return sprintf('%s/class-%s.html', $this->source, $this->dotName);
126
	}
127
128
	/**
129
	 * Ensure that name starts and ends with slash
130
	 * @param string $name
131
	 * @return string
132
	 */
133
	private static function normalize($name)
134
	{
135
		return sprintf('\\%s\\', trim($name, '\\'));
136
	}
137
138
}
139