1<?php
2
3declare(strict_types=1);
4
5namespace Sabre\DAV\FS;
6
7use Sabre\DAV\Exception\Forbidden;
8use Sabre\DAV\INode;
9use Sabre\Uri;
10
11/**
12 * Base node-class.
13 *
14 * The node class implements the method used by both the File and the Directory classes
15 *
16 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
17 * @author Evert Pot (http://evertpot.com/)
18 * @license http://sabre.io/license/ Modified BSD License
19 */
20abstract class Node implements INode
21{
22    /**
23     * The path to the current node.
24     *
25     * @var string
26     */
27    protected $path;
28
29    /**
30     * The overridden name of the node.
31     *
32     * @var string
33     */
34    protected $overrideName;
35
36    /**
37     * Sets up the node, expects a full path name.
38     *
39     * If $overrideName is set, this node shows up in the tree under a
40     * different name. In this case setName() will be disabled.
41     *
42     * @param string $path
43     * @param string $overrideName
44     */
45    public function __construct($path, $overrideName = null)
46    {
47        $this->path = $path;
48        $this->overrideName = $overrideName;
49    }
50
51    /**
52     * Returns the name of the node.
53     *
54     * @return string
55     */
56    public function getName()
57    {
58        if ($this->overrideName) {
59            return $this->overrideName;
60        }
61
62        list(, $name) = Uri\split($this->path);
63
64        return $name;
65    }
66
67    /**
68     * Renames the node.
69     *
70     * @param string $name The new name
71     */
72    public function setName($name)
73    {
74        if ($this->overrideName) {
75            throw new Forbidden('This node cannot be renamed');
76        }
77
78        list($parentPath) = Uri\split($this->path);
79        list(, $newName) = Uri\split($name);
80
81        $newPath = $parentPath.'/'.$newName;
82        rename($this->path, $newPath);
83
84        $this->path = $newPath;
85    }
86
87    /**
88     * Returns the last modification time, as a unix timestamp.
89     *
90     * @return int
91     */
92    public function getLastModified()
93    {
94        return filemtime($this->path);
95    }
96}
97