1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @jenschude <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Commercetools\Core\Model\Common; |
7
|
|
|
|
8
|
|
|
use DateTime; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @package Commercetools\Core\Model\Common |
12
|
|
|
* @method getId() |
13
|
|
|
*/ |
14
|
|
|
abstract class Resource extends JsonObject implements ReferenceObjectInterface |
15
|
|
|
{ |
16
|
|
|
public function fieldDefinitions() |
17
|
|
|
{ |
18
|
|
|
return [ |
19
|
|
|
'id' => [static::TYPE => 'string'], |
20
|
|
|
'version' => [static::TYPE => 'int'], |
21
|
|
|
'createdAt' => [ |
22
|
|
|
static::TYPE => DateTime::class, |
23
|
|
|
static::DECORATOR => DateTimeDecorator::class |
24
|
|
|
], |
25
|
|
|
'lastModifiedAt' => [ |
26
|
|
|
static::TYPE => DateTime::class, |
27
|
|
|
static::DECORATOR => DateTimeDecorator::class |
28
|
|
|
], |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
|
32
|
335 |
|
public function getReferenceIdentifier() |
33
|
|
|
{ |
34
|
335 |
|
return $this->getId(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return Reference |
39
|
|
|
*/ |
40
|
335 |
|
public function getReference() |
41
|
|
|
{ |
42
|
335 |
|
$className = trim(get_called_class(), '\\'); |
43
|
335 |
|
$referenceClass = $className . 'Reference'; |
44
|
335 |
|
if (class_exists($referenceClass)) { |
45
|
334 |
|
$reference = call_user_func_array( |
46
|
334 |
|
$referenceClass . '::ofId', |
47
|
334 |
|
[$this->getReferenceIdentifier(), $this->getContextCallback()] |
48
|
|
|
); |
49
|
|
|
} else { |
50
|
1 |
|
$classParts = explode('\\', $className); |
51
|
1 |
|
$class = lcfirst(array_pop($classParts)); |
52
|
1 |
|
$type = strtolower(preg_replace('/([A-Z])/', '-$1', $class)); |
53
|
|
|
|
54
|
1 |
|
$reference = Reference::ofTypeAndId($type, $this->getReferenceIdentifier(), $this->getContextCallback()); |
55
|
|
|
} |
56
|
335 |
|
$reference->setObj($this); |
57
|
|
|
|
58
|
335 |
|
return $reference; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|