1<?php
2
3declare(strict_types=1);
4
5namespace Sabre\DAV;
6
7/**
8 * Collection class.
9 *
10 * This is a helper class, that should aid in getting collections classes setup.
11 * Most of its methods are implemented, and throw permission denied exceptions
12 *
13 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
14 * @author Evert Pot (http://evertpot.com/)
15 * @license http://sabre.io/license/ Modified BSD License
16 */
17abstract class Collection extends Node implements ICollection
18{
19    /**
20     * Returns a child object, by its name.
21     *
22     * This method makes use of the getChildren method to grab all the child
23     * nodes, and compares the name.
24     * Generally its wise to override this, as this can usually be optimized
25     *
26     * This method must throw Sabre\DAV\Exception\NotFound if the node does not
27     * exist.
28     *
29     * @param string $name
30     *
31     * @throws Exception\NotFound
32     *
33     * @return INode
34     */
35    public function getChild($name)
36    {
37        foreach ($this->getChildren() as $child) {
38            if ($child->getName() === $name) {
39                return $child;
40            }
41        }
42        throw new Exception\NotFound('File not found: '.$name);
43    }
44
45    /**
46     * Checks is a child-node exists.
47     *
48     * It is generally a good idea to try and override this. Usually it can be optimized.
49     *
50     * @param string $name
51     *
52     * @return bool
53     */
54    public function childExists($name)
55    {
56        try {
57            $this->getChild($name);
58
59            return true;
60        } catch (Exception\NotFound $e) {
61            return false;
62        }
63    }
64
65    /**
66     * Creates a new file in the directory.
67     *
68     * Data will either be supplied as a stream resource, or in certain cases
69     * as a string. Keep in mind that you may have to support either.
70     *
71     * After successful creation of the file, you may choose to return the ETag
72     * of the new file here.
73     *
74     * The returned ETag must be surrounded by double-quotes (The quotes should
75     * be part of the actual string).
76     *
77     * If you cannot accurately determine the ETag, you should not return it.
78     * If you don't store the file exactly as-is (you're transforming it
79     * somehow) you should also not return an ETag.
80     *
81     * This means that if a subsequent GET to this new file does not exactly
82     * return the same contents of what was submitted here, you are strongly
83     * recommended to omit the ETag.
84     *
85     * @param string          $name Name of the file
86     * @param resource|string $data Initial payload
87     *
88     * @return string|null
89     */
90    public function createFile($name, $data = null)
91    {
92        throw new Exception\Forbidden('Permission denied to create file (filename '.$name.')');
93    }
94
95    /**
96     * Creates a new subdirectory.
97     *
98     * @param string $name
99     *
100     * @throws Exception\Forbidden
101     */
102    public function createDirectory($name)
103    {
104        throw new Exception\Forbidden('Permission denied to create directory');
105    }
106}
107