343 lines
11 KiB
PHP
343 lines
11 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace LDC\DAM\Client\Models;
|
||
|
|
|
||
|
|
use LDC\DAM\Client\Enums\LogLevel;
|
||
|
|
use LDC\DAM\Client\Enums\MediaExtension;
|
||
|
|
use LDC\DAM\Client\Exceptions\AccessError;
|
||
|
|
use LDC\DAM\Client\Exceptions\ValidationException;
|
||
|
|
use LDC\DAM\Client\Transport\ApiInterface;
|
||
|
|
use LDC\DAM\Client\Utils;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @ignore
|
||
|
|
* Base Class for crm client models
|
||
|
|
*
|
||
|
|
* @category Api Models
|
||
|
|
* @package DAMclient
|
||
|
|
* @subpackage models
|
||
|
|
* @author Jérémy Munsch <jeremy.munsch@ldc.fr>
|
||
|
|
*/
|
||
|
|
abstract class DamModel implements \JsonSerializable
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Is true if this model was not filled from crm
|
||
|
|
*
|
||
|
|
* @var boolean
|
||
|
|
*/
|
||
|
|
protected $isNew = true;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The Api object that will handle requests and responses
|
||
|
|
*
|
||
|
|
* @var \LDC\DAM\Client\Transport\ApiInterface
|
||
|
|
*/
|
||
|
|
protected static $apiInterface;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Validation rules
|
||
|
|
*
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
protected static $validation = [];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Model fields
|
||
|
|
*
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
protected $modelFields = [];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The crm fields
|
||
|
|
* that should not be visible
|
||
|
|
*
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
protected $invisibleFields = [];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Model updated values
|
||
|
|
*
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
protected $updatedValues = [];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* CrmClientModel
|
||
|
|
*/
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
self::setUpApiInterface();
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Generic accessor
|
||
|
|
*
|
||
|
|
* @param string $field
|
||
|
|
* @return mixed|null Returns null if the property was not found any way.
|
||
|
|
*/
|
||
|
|
public function __get(string $field)
|
||
|
|
{
|
||
|
|
$props = \array_diff(
|
||
|
|
$this->modelFields,
|
||
|
|
$this->invisibleFields
|
||
|
|
);
|
||
|
|
if (in_array($field, $props)) {
|
||
|
|
return Utils::getRawProperty($this, $field);
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Set prop on the model.
|
||
|
|
*
|
||
|
|
* @param string $field
|
||
|
|
* @param mixed $value
|
||
|
|
* @return void
|
||
|
|
* @throws \LDC\DAM\Client\Exceptions\ValidationException If a field could not be validated.
|
||
|
|
* @throws \LDC\DAM\Client\Exceptions\AccessError If trying to set a value on a unknown property.
|
||
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
|
||
|
|
*/
|
||
|
|
public function __set(string $field, $value): void
|
||
|
|
{
|
||
|
|
// phpcs:enable
|
||
|
|
try {
|
||
|
|
if (in_array($field, $this->modelFields)) {
|
||
|
|
$type = $this->getPropType($field);
|
||
|
|
if (!is_null($value) and !$this->checkFieldType($type, $value)) {
|
||
|
|
throw new ValidationException("Wrong type for $field");
|
||
|
|
}
|
||
|
|
$this->validateValue($field, $value);
|
||
|
|
$this->updatedValues[$field] = $value;
|
||
|
|
Utils::setRawProperty($this, $field, $value);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
|
||
|
|
} catch (\ReflectionException $e) {
|
||
|
|
// Do nothing.
|
||
|
|
}
|
||
|
|
throw new AccessError(\get_class($this), $field);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create the Api interface
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
protected static function setUpApiInterface()
|
||
|
|
{
|
||
|
|
self::$apiInterface = self::$apiInterface ?: new ApiInterface();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Fill values into model using validation rules.
|
||
|
|
*
|
||
|
|
* @param array $properties
|
||
|
|
* @return void
|
||
|
|
* @throws \LDC\DAM\Client\Exceptions\ValidationException If a field could not be validated.
|
||
|
|
*/
|
||
|
|
public function fill(array $properties)
|
||
|
|
{
|
||
|
|
if (count($properties)) {
|
||
|
|
foreach ($properties as $field => $value) {
|
||
|
|
try {
|
||
|
|
$type = $this->getPropType($field);
|
||
|
|
// * Force boolean
|
||
|
|
if ($type === 'bool') {
|
||
|
|
$value = (\is_int($value) and ($value === 1 or $value === 0)) ? (bool)$value : $value;
|
||
|
|
}
|
||
|
|
// * Force DateTime
|
||
|
|
if ($type === 'DateTime') {
|
||
|
|
$value = (
|
||
|
|
\is_string($value) and
|
||
|
|
(\DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $value) !== false)
|
||
|
|
) ? \DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $value) : $value;
|
||
|
|
}
|
||
|
|
if (!is_null($value) and !$this->checkFieldType($type, $value)) {
|
||
|
|
throw new ValidationException("Wrong type for $field");
|
||
|
|
}
|
||
|
|
$this->validateValue($field, $value);
|
||
|
|
$this->updatedValues[$field] = $value;
|
||
|
|
Utils::setRawProperty($this, $field, $value);
|
||
|
|
} catch (AccessError | \ReflectionException $e) {
|
||
|
|
Utils::log(\sprintf(
|
||
|
|
'%s::fill : unknown property access for %s, it does not exist on object.',
|
||
|
|
\get_class($this),
|
||
|
|
$field
|
||
|
|
), LogLevel::DEBUG());
|
||
|
|
continue;
|
||
|
|
}//end try
|
||
|
|
} //end foreach
|
||
|
|
}//end if
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Set all properties to without any validation
|
||
|
|
*
|
||
|
|
* @param array $properties
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
protected function fillRaw(array $properties)
|
||
|
|
{
|
||
|
|
if (count($properties)) {
|
||
|
|
foreach ($properties as $field => $value) {
|
||
|
|
try {
|
||
|
|
Utils::setRawProperty($this, $field, $value);
|
||
|
|
} catch (AccessError $e) {
|
||
|
|
Utils::log(\sprintf(
|
||
|
|
'%s::fillRaw : unknown property access for %s, it does not exist on object.',
|
||
|
|
\get_class($this),
|
||
|
|
$field
|
||
|
|
), LogLevel::DEBUG());
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Validate all model properties
|
||
|
|
*
|
||
|
|
* * All properties are nullable
|
||
|
|
*
|
||
|
|
* @param array $ignore
|
||
|
|
* @return array the validated values
|
||
|
|
* @throws \LDC\DAM\Client\Exceptions\ValidationException If a field could not be validated.
|
||
|
|
*/
|
||
|
|
protected function validateAllValues(array $ignore = []): array
|
||
|
|
{
|
||
|
|
$out = [];
|
||
|
|
foreach (static::$validation as $field => $validationRules) {
|
||
|
|
if (\in_array($field, $ignore) or \is_null(Utils::getRawProperty($this, $field))) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
$this->validateValue($field, Utils::getRawProperty($this, $field));
|
||
|
|
$out[$field] = Utils::getRawProperty($this, $field);
|
||
|
|
}
|
||
|
|
return $out;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Validate a value against its rules
|
||
|
|
*
|
||
|
|
* @param string $field
|
||
|
|
* @param mixed $value
|
||
|
|
* @return boolean
|
||
|
|
* @throws \ErrorException If a field has no validation rule.
|
||
|
|
* @throws \LDC\DAM\Client\Exceptions\ValidationException If a field could not be validated.
|
||
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
|
||
|
|
* @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
|
||
|
|
*/
|
||
|
|
protected static function validateValue(string $field, $value): bool
|
||
|
|
{
|
||
|
|
// phpcs:enable
|
||
|
|
if (!isset(static::$validation[$field])) {
|
||
|
|
throw new \ErrorException("$field has no validation rule");
|
||
|
|
}
|
||
|
|
$validator = Utils::makeValidator(
|
||
|
|
[$field => $value],
|
||
|
|
[$field => static::$validation[$field]]
|
||
|
|
);
|
||
|
|
/** @throws \LDC\DAM\Client\Exceptions\ValidationException */
|
||
|
|
$validator();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check a value against its field type
|
||
|
|
*
|
||
|
|
* @param string $type
|
||
|
|
* @param mixed $value
|
||
|
|
* @return boolean Return true if $type matches $value type.
|
||
|
|
* @throws \ErrorException If type is not handled,
|
||
|
|
* handled types are boolean, string, integer, DateTime.
|
||
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
|
||
|
|
*/
|
||
|
|
protected function checkFieldType(string $type, $value): bool
|
||
|
|
{
|
||
|
|
// phpcs:enable
|
||
|
|
/**
|
||
|
|
* Checks if the value is valid for a specific enum class
|
||
|
|
* @param string $className
|
||
|
|
* @param mixed $value
|
||
|
|
* @return boolean
|
||
|
|
*/
|
||
|
|
$checkValueIsEnum = function (string $className, $value) {
|
||
|
|
try {
|
||
|
|
$value = $className::make($value);
|
||
|
|
} finally {
|
||
|
|
return \is_object($value) and (get_class($value) === $className or
|
||
|
|
\in_array($className, \class_parents($value)));
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
switch ($type) {
|
||
|
|
case '\\' . MediaExtension::class:
|
||
|
|
return $checkValueIsEnum(\ltrim($type, '\\'), $value);
|
||
|
|
case 'DateTime':
|
||
|
|
return \is_object($value) and $value instanceof \DateTime;
|
||
|
|
case 'boolean':
|
||
|
|
return \is_bool(\is_int($value) ? (bool)$value : $value);
|
||
|
|
case 'array':
|
||
|
|
return \is_array($value);
|
||
|
|
case 'string':
|
||
|
|
return \is_string($value);
|
||
|
|
case 'integer':
|
||
|
|
return \is_int($value);
|
||
|
|
}
|
||
|
|
throw new \ErrorException(\sprintf("Unhandled $type for %s", \json_encode($value)));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the type of any of this class property
|
||
|
|
* from doc comment
|
||
|
|
*
|
||
|
|
* @param string $property
|
||
|
|
* @return string|null the type or null if not defined
|
||
|
|
* @throws \ErrorException If the property has no comment to read.
|
||
|
|
* @throws \ErrorException If a type for a property could ne be recognized.
|
||
|
|
* @throws \ReflectionException If the class or property does not exist.
|
||
|
|
* @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
|
||
|
|
*/
|
||
|
|
protected function getPropType(string $property)
|
||
|
|
{
|
||
|
|
$matches = [];
|
||
|
|
$propReflection = new \ReflectionProperty(static::class, $property);
|
||
|
|
$docComment = $propReflection->getDocComment();
|
||
|
|
if (!$docComment) {
|
||
|
|
throw new \ErrorException(\sprintf("%s $property has not comment to parse.", \get_class($this)));
|
||
|
|
}
|
||
|
|
preg_match_all('/@var ([\w_\x5c]+)/', $docComment, $matches);
|
||
|
|
if (
|
||
|
|
isset($matches[1][0]) and \in_array($matches[1][0], [
|
||
|
|
'string', 'integer', 'boolean', 'array', 'DateTime', '\\' . MediaExtension::class
|
||
|
|
], true)
|
||
|
|
) {
|
||
|
|
return $matches[1][0];
|
||
|
|
}
|
||
|
|
throw new \ErrorException("Unknown type for property $property");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Assert primaryKey is valid.
|
||
|
|
*
|
||
|
|
* @param mixed $primaryKey
|
||
|
|
* @return void
|
||
|
|
* @throws LDC\DAM\Client\Exceptions\ValidationException If a primary key is not a valid integer.
|
||
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
|
||
|
|
*/
|
||
|
|
protected static function assertValidPrimaryKey($primaryKey): void
|
||
|
|
{
|
||
|
|
// phpcs:enable
|
||
|
|
$validator = Utils::makeValidator(
|
||
|
|
['id' => $primaryKey],
|
||
|
|
['id' => 'required|int']
|
||
|
|
);
|
||
|
|
if ($validator()) {
|
||
|
|
throw new ValidationException(implode(' ', $validator->errors()->get('id')));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|