ldc-crm-client/src/Models/Answer.php

115 lines
2.5 KiB
PHP
Raw Normal View History

2021-08-11 17:26:30 +02:00
<?php
namespace LDC\LDCCRM\Models;
use LDC\LDCCRM\Enums\ApiStatus;
use LDC\LDCCRM\Utils;
/**
* This class represents an answer
* It has a method to submit answers
*
* @property string $id The answer id
* @property string $question The actual question as a string
* @property \LDC\LDCCRM\Enums\QuestionType $type
* The question type to display,
* it also hints wether it can accept one or multiple responses,
* so only checkbox allows multiple responses
* @property \LDC\LDCCRM\Models\Answer[] $answers An array with answers
*
* @category Api Models
* @package LDCCrmClient
* @subpackage models
*/
class Answer extends CrmClientModel
{
/**
* Is true if this model was not filled from crm
*
* @var boolean
*/
private $isNew = true;
/**
* The question id
*
* Required
* Field Type bigInt
*
* @var int
*/
private $id;
/**
* The actual answer as a string
*
* Required
* Max length 255
* Field Type varchar
*
* @var string
*/
private $answer = null;
/**
* The crm customer fields
*
* @var array
*/
protected $modelFields = [
'id', 'answer'
];
/**
* Validation rules
*
* @var array
*/
protected static $validation = [
'id' => 'required|int',
'answer' => 'required|string|max:255'
];
public function __construct(array $properties = [])
{
parent::__construct();
$this->fill($properties);
return $this;
}
// PUBLIC METHODS
/**
* Get unanswered question for a specific customer
*
* @param \LDC\LDCCRM\Models\Customer $customer
* @return bool
*/
public static function setQuestionAnswers(string $hashcode, int $questionId, array $answersId): bool
{
self::assertValidHascode($hashcode);
self::assertValidPrimaryKey($questionId);
if (!count($answersId)) {
return true;
}
foreach ($answersId as $answerId) {
self::assertValidPrimaryKey($answerId);
}
static::setUpApiInterface(); // set apiInterface
$request = Utils::getResquest(
'apiV2.customers.answers.store',
[
'customer' => $hashcode
],
[
'question' => $questionId,
'answers' => $answersId
]
);
$response = static::$apiInterface->call($request);
return $response->status->equals(ApiStatus::OK());
}
}