1: <?php
2:
3: namespace Charcoal\Object;
4:
5: use \DateTime;
6: use \DateTimeInterface;
7: use \InvalidArgumentException;
8:
9:
10: use \Pimple\Container;
11:
12: use \Charcoal\Factory\FactoryInterface;
13:
14: // From `charcoal-core`
15: use \Charcoal\Model\AbstractModel;
16:
17: use \Charcoal\Object\ContentInterface;
18: use \Charcoal\Object\RevisionableInterface;
19: use \Charcoal\Object\RevisionableTrait;
20:
21: /**
22: *
23: */
24: class Content extends AbstractModel implements
25: ContentInterface,
26: RevisionableInterface
27: {
28: use RevisionableTrait;
29:
30: /**
31: * Objects are active by default
32: * @var boolean $Active
33: */
34: private $active = true;
35:
36: /**
37: * The position is used for ordering lists
38: * @var integer $Position
39: */
40: private $position = 0;
41:
42: /**
43: * Object creation date (set automatically on save)
44: * @var DateTime $Created
45: */
46: private $created;
47:
48: /**
49: * @var mixed
50: */
51: private $createdBy;
52:
53: /**
54: * Object last modified date (set automatically on save and update)
55: * @var DateTime $LastModified
56: */
57: private $lastModified;
58:
59: /**
60: * @var mixed
61: */
62: private $lastModifiedBy;
63:
64: /**
65: * @var FactoryInterface $modelFactory
66: */
67: private $modelFactory;
68:
69: /**
70: * Dependencies
71: * @param Container $container DI Container.
72: * @return void
73: */
74: public function setDependencies(Container $container)
75: {
76: parent::setDependencies($container);
77:
78: $this->setModelFactory($container['model/factory']);
79: }
80:
81: /**
82: * @param FactoryInterface $factory The factory used to create models.
83: * @return AdminScript Chainable
84: */
85: protected function setModelFactory(FactoryInterface $factory)
86: {
87: $this->modelFactory = $factory;
88: return $this;
89: }
90:
91: /**
92: * @return FactoryInterface The model factory.
93: */
94: protected function modelFactory()
95: {
96: return $this->modelFactory;
97: }
98:
99: /**
100: * @param boolean $active The active flag.
101: * @return Content Chainable
102: */
103: public function setActive($active)
104: {
105: $this->active = !!$active;
106: return $this;
107: }
108:
109: /**
110: * @return boolean
111: */
112: public function active()
113: {
114: return $this->active;
115: }
116:
117: /**
118: * @param integer $position The position (for ordering purpose).
119: * @throws InvalidArgumentException If the position is not an integer (or numeric integer string).
120: * @return Content Chainable
121: */
122: public function setPosition($position)
123: {
124: if ($position === null) {
125: $this->position = null;
126: return $this;
127: }
128: if (!is_numeric($position)) {
129: throw new InvalidArgumentException(
130: sprintf('Position must be an integer (%s given).', gettype($position))
131: );
132: }
133: $this->position = (int)$position;
134: return $this;
135: }
136:
137: /**
138: * @return integer
139: */
140: public function position()
141: {
142: return $this->position;
143: }
144:
145: /**
146: * @param \DateTimeInterface|string|null $created The date/time at object's creation.
147: * @throws InvalidArgumentException If the date/time is invalid.
148: * @return Content Chainable
149: */
150: public function setCreated($created)
151: {
152: if ($created === null) {
153: $this->created = null;
154: return $this;
155: }
156: if (is_string($created)) {
157: $created = new DateTime($created);
158: }
159: if (!($created instanceof DateTimeInterface)) {
160: throw new InvalidArgumentException(
161: 'Invalid "Created" value. Must be a date/time string or a DateTime object.'
162: );
163: }
164: $this->created = $created;
165: return $this;
166: }
167:
168: /**
169: * @return DateTimeInterface|null
170: */
171: public function created()
172: {
173: return $this->created;
174: }
175:
176: /**
177: * @param mixed $createdBy The creator of the content object.
178: * @return Content Chainable
179: */
180: public function setCreatedBy($createdBy)
181: {
182: $this->createdBy = $createdBy;
183: return $this;
184: }
185:
186: /**
187: * @return mixed
188: */
189: public function createdBy()
190: {
191: return $this->createdBy;
192: }
193:
194: /**
195: * @param \DateTimeInterface|string|null $lastModified The last modified date/time.
196: * @throws InvalidArgumentException If the date/time is invalid.
197: * @return Content Chainable
198: */
199: public function setLastModified($lastModified)
200: {
201: if ($lastModified === null) {
202: $this->lastModified = null;
203: return $this;
204: }
205: if (is_string($lastModified)) {
206: $lastModified = new DateTime($lastModified);
207: }
208: if (!($lastModified instanceof DateTimeInterface)) {
209: throw new InvalidArgumentException(
210: 'Invalid "Last Modified" value. Must be a date/time string or a DateTime object.'
211: );
212: }
213: $this->lastModified = $lastModified;
214: return $this;
215: }
216:
217: /**
218: * @return DateTimeInterface
219: */
220: public function lastModified()
221: {
222: return $this->lastModified;
223: }
224:
225: /**
226: * @param mixed $lastModifiedBy The last modification's username.
227: * @return Content Chainable
228: */
229: public function setLastModifiedBy($lastModifiedBy)
230: {
231: $this->lastModifiedBy = $lastModifiedBy;
232: return $this;
233: }
234:
235: /**
236: * @return mixed
237: */
238: public function lastModifiedBy()
239: {
240: return $this->lastModifiedBy;
241: }
242:
243: /**
244: * StorableTrait > preSavÆ’e(): Called automatically before saving the object to source.
245: * For content object, set the `created` and `lastModified` properties automatically
246: * @return boolean
247: */
248: public function preSave()
249: {
250: parent::preSave();
251:
252: $this->setCreated('now');
253: $this->setLastModified('now');
254:
255: return true;
256: }
257:
258: /**
259: * StorableTrait > preUpdate(): Called automatically before updating the object to source.
260: * For content object, set the `lastModified` property automatically.
261: *
262: * @param array $properties The properties (ident) set for update.
263: * @return boolean
264: */
265: public function preUpdate(array $properties = null)
266: {
267: parent::preUpdate($properties);
268:
269: // Content is revisionable
270: if ($this->revisionEnabled()) {
271: $this->generateRevision();
272: }
273:
274: $this->setLastModified('now');
275:
276: return true;
277: }
278: }
279: