gocobachi /
compressy
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of Compressy. |
||
| 5 | * |
||
| 6 | * (c) Alchemy <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace Gocobachi\Compressy\Resource; |
||
| 13 | |||
| 14 | class Resource |
||
| 15 | { |
||
| 16 | private $original; |
||
| 17 | private $target; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Constructor |
||
| 21 | * |
||
| 22 | * @param string $original |
||
| 23 | * @param string $target |
||
| 24 | */ |
||
| 25 | public function __construct($original, $target) |
||
| 26 | { |
||
| 27 | $this->original = $original; |
||
| 28 | $this->target = $target; |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Returns the target |
||
| 33 | * |
||
| 34 | * @return string |
||
| 35 | */ |
||
| 36 | public function getTarget() |
||
| 37 | { |
||
| 38 | return $this->target; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Returns the original path |
||
| 43 | * |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | public function getOriginal() |
||
| 47 | { |
||
| 48 | return $this->original; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Returns whether the resource can be processed in place given a context or not. |
||
| 53 | * |
||
| 54 | * For example : |
||
| 55 | * - /path/to/file1 can be processed to file1 in /path/to context |
||
| 56 | * - /path/to/subdir/file2 can be processed to subdir/file2 in /path/to context |
||
| 57 | * |
||
| 58 | * @param string $context |
||
| 59 | * |
||
| 60 | * @return bool |
||
| 61 | */ |
||
| 62 | public function canBeProcessedInPlace($context) |
||
| 63 | { |
||
| 64 | if (!is_string($this->original)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 65 | return false; |
||
| 66 | } |
||
| 67 | |||
| 68 | if (!$this->isLocal()) { |
||
| 69 | return false; |
||
| 70 | } |
||
| 71 | |||
| 72 | $data = parse_url($this->original); |
||
| 73 | |||
| 74 | return sprintf('%s/%s', rtrim($context, '/'), $this->target) === $data['path']; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Returns a context for computing this resource in case it is possible. |
||
| 79 | * |
||
| 80 | * Useful to avoid teleporting. |
||
| 81 | * |
||
| 82 | * @return null|string |
||
| 83 | */ |
||
| 84 | public function getContextForProcessInSinglePlace() |
||
| 85 | { |
||
| 86 | if (!is_string($this->original)) { |
||
|
0 ignored issues
–
show
|
|||
| 87 | return null; |
||
| 88 | } |
||
| 89 | |||
| 90 | if (!$this->isLocal()) { |
||
| 91 | return null; |
||
| 92 | } |
||
| 93 | |||
| 94 | if (PathUtil::basename($this->original) === $this->target) { |
||
| 95 | return dirname($this->original); |
||
| 96 | } |
||
| 97 | |||
| 98 | return null; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Returns true if the resource is local. |
||
| 103 | * |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | private function isLocal() |
||
| 107 | { |
||
| 108 | if (!is_string($this->original)) { |
||
|
0 ignored issues
–
show
|
|||
| 109 | return false; |
||
| 110 | } |
||
| 111 | |||
| 112 | $data = parse_url($this->original); |
||
| 113 | |||
| 114 | return isset($data['path']); |
||
| 115 | } |
||
| 116 | } |
||
| 117 |