Completed
Push — master ( 735fe4...23bc65 )
by Vitaly
03:16
created

ResourceValidator::getWebRelativePath()   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
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 10.07.16 at 16:24
5
 */
6
namespace samsonphp\resource;
7
8
use samsonphp\resource\exception\ResourceNotFound;
9
10
class ResourceValidator
11
{
12
    /** @var string Full path to project web root directory */
13
    public static $webRoot;
14
15
    /** @var string Full path to project root directory */
16
    public static $projectRoot;
17
18
    /** @var string Full path to project cache root directory */
19
    public static $cacheRoot;
20
21
    /**
22
     * Build relative path to static resource relatively to web root path.
23
     *
24
     * @param string $relativePath Relative path to static resource
25
     * @param string $parentPath   Path to parent entity
26
     *
27
     * @return string Validated relative path to static resource relatively to web root path
28
     * @throws ResourceNotFound
29
     */
30
    public static function getWebRelativePath($relativePath, $parentPath = '')
31
    {
32
        return static::getRelativePath($relativePath, $parentPath, static::$webRoot);
33
    }
34
35
    /**
36
     * Build correct relative path to static resource using relative path and parent path.
37
     *
38
     * @param string $relativePath Relative path to static resource
39
     * @param string $parentPath   Path to parent entity
40
     * @param string $rootPath     Root path for relative path building
41
     *
42
     * @return string Validated relative path to static resource
43
     * @throws ResourceNotFound
44
     */
45
    public static function getRelativePath($relativePath, $parentPath = '', $rootPath = '')
46
    {
47
        // If parent path if not passed - use project root path
48
        $parentPath = $parentPath === '' ? static::$projectRoot : $parentPath;
49
50
        // Build full path to resource from given relative path
51
        $fullPath = rtrim($parentPath, DIRECTORY_SEPARATOR)
52
            . DIRECTORY_SEPARATOR
53
            . ltrim($relativePath, DIRECTORY_SEPARATOR);
54
55
        // Make real path with out possible "../"
56
        $realPath = realpath($fullPath);
57
58
        // Output link to static resource handler with relative path to project root
59
        if ($realPath) {
60
            // Build relative path to static resource
61
            return str_replace($rootPath, '', $realPath);
62
        }
63
64
        throw new ResourceNotFound($fullPath);
65
    }
66
67
    /**
68
     * Build relative path to static resource relatively to project root path.
69
     *
70
     * @param string $relativePath Relative path to static resource
71
     * @param string $parentPath   Path to parent entity
72
     *
73
     * @return string Validated relative path to static resource relatively to project root path
74
     * @throws ResourceNotFound
75
     */
76
    public static function getProjectRelativePath($relativePath, $parentPath = '')
77
    {
78
        return static::getRelativePath($relativePath, $parentPath, static::$projectRoot);
79
    }
80
}