1<?php
2
3declare(strict_types=1);
4
5namespace League\Flysystem;
6
7class DirectoryAttributes implements StorageAttributes
8{
9    use ProxyArrayAccessToProperties;
10
11    /**
12     * @var string
13     */
14    private $type = StorageAttributes::TYPE_DIRECTORY;
15
16    /**
17     * @var string
18     */
19    private $path;
20
21    /**
22     * @var string|null
23     */
24    private $visibility;
25
26    /**
27     * @var int|null
28     */
29    private $lastModified;
30
31    /**
32     * @var array
33     */
34    private $extraMetadata;
35
36    public function __construct(string $path, ?string $visibility = null, ?int $lastModified = null, array $extraMetadata = [])
37    {
38        $this->path = $path;
39        $this->visibility = $visibility;
40        $this->lastModified = $lastModified;
41        $this->extraMetadata = $extraMetadata;
42    }
43
44    public function path(): string
45    {
46        return $this->path;
47    }
48
49    public function type(): string
50    {
51        return StorageAttributes::TYPE_DIRECTORY;
52    }
53
54    public function visibility(): ?string
55    {
56        return $this->visibility;
57    }
58
59    public function lastModified(): ?int
60    {
61        return $this->lastModified;
62    }
63
64    public function extraMetadata(): array
65    {
66        return $this->extraMetadata;
67    }
68
69    public function isFile(): bool
70    {
71        return false;
72    }
73
74    public function isDir(): bool
75    {
76        return true;
77    }
78
79    public function withPath(string $path): StorageAttributes
80    {
81        $clone = clone $this;
82        $clone->path = $path;
83
84        return $clone;
85    }
86
87    public static function fromArray(array $attributes): StorageAttributes
88    {
89        return new DirectoryAttributes(
90            $attributes[StorageAttributes::ATTRIBUTE_PATH],
91            $attributes[StorageAttributes::ATTRIBUTE_VISIBILITY] ?? null,
92            $attributes[StorageAttributes::ATTRIBUTE_LAST_MODIFIED] ?? null,
93            $attributes[StorageAttributes::ATTRIBUTE_EXTRA_METADATA] ?? []
94        );
95    }
96
97    /**
98     * @inheritDoc
99     */
100    public function jsonSerialize(): array
101    {
102        return [
103            StorageAttributes::ATTRIBUTE_TYPE => $this->type,
104            StorageAttributes::ATTRIBUTE_PATH => $this->path,
105            StorageAttributes::ATTRIBUTE_VISIBILITY => $this->visibility,
106            StorageAttributes::ATTRIBUTE_LAST_MODIFIED => $this->lastModified,
107            StorageAttributes::ATTRIBUTE_EXTRA_METADATA => $this->extraMetadata,
108        ];
109    }
110}
111