1: <?php
2:
3: namespace Charcoal\Cms;
4:
5: // From 'charcoal-object'
6: use Charcoal\Object\Content;
7: use Charcoal\Object\CategorizableInterface;
8: use Charcoal\Object\CategorizableTrait;
9:
10: // From 'charcoal-translator'
11: use Charcoal\Translator\Translation;
12:
13: // From 'charcoal-cms'
14: use Charcoal\Cms\DocumentInterface;
15:
16: /**
17: * Base document class.
18: */
19: abstract class AbstractDocument extends Content implements
20: CategorizableInterface,
21: DocumentInterface
22: {
23: use CategorizableTrait;
24:
25: /**
26: * @var Translation|string|null $name
27: */
28: private $name;
29:
30: /**
31: * @var string $file
32: */
33: private $file;
34:
35: /**
36: * @var string $basePath
37: */
38: private $basePath;
39:
40: /**
41: * @var string $baseUrl
42: */
43: private $baseUrl;
44:
45: /**
46: * @param mixed $name The document name.
47: * @return self
48: */
49: public function setName($name)
50: {
51: $this->name = $this->translator()->translation($name);
52: return $this;
53: }
54:
55: /**
56: * @return Translation|string|null
57: */
58: public function name()
59: {
60: return $this->name;
61: }
62:
63: /**
64: * @param string $file The file relative path / url.
65: * @return self
66: */
67: public function setFile($file)
68: {
69: $this->file = $file;
70: return $this;
71: }
72:
73: /**
74: * @return string
75: */
76: public function file()
77: {
78: return $this->file;
79: }
80:
81: /**
82: * @param string $path The document base path.
83: * @return self
84: */
85: public function setBasePath($path)
86: {
87: $this->basePath = $path;
88: return $this;
89: }
90:
91: /**
92: * Get the base path, with a trailing slash.
93: *
94: * @return string
95: */
96: public function basePath()
97: {
98: if (!$this->basePath) {
99: return '';
100: }
101: return rtrim($this->basePath, '/').'/';
102: }
103:
104: /**
105: * @param string $url The document base URL.
106: * @return self
107: */
108: public function setBaseUrl($url)
109: {
110: $this->baseUrl = $url;
111: return $this;
112: }
113:
114: /**
115: * Get the base url, with a trailing slash.
116: *
117: * @return string
118: */
119: public function baseUrl()
120: {
121: return rtrim($this->baseUrl, '/').'/';
122: }
123:
124: /**
125: * @return string
126: */
127: public function path()
128: {
129: return $this->basePath().$this->file();
130: }
131:
132: /**
133: * @return string
134: */
135: public function url()
136: {
137: return $this->baseUrl().$this->file();
138: }
139:
140: /**
141: * @return string
142: */
143: public function mimetype()
144: {
145: $p = $this->property('file');
146: return $p->mimetype($this->path());
147: }
148:
149: /**
150: * Get the fiqlename (basename; without any path segment).
151: *
152: * @return string
153: */
154: public function filename()
155: {
156: return basename($this->file());
157: }
158:
159: /**
160: * Get the document's file size, in bytes.
161: *
162: * @return integer
163: */
164: public function filesize()
165: {
166: return filesize($this->path());
167: }
168: }
169: