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