ldc-crm/clients/WPCrmUser.php
2023-06-23 17:18:59 +02:00

148 lines
3.1 KiB
PHP

<?php
/**
* LDCCRM For Wordpress usage
* php version 5
*
* @category API
* @package None
* @author Jérémy Munsch <jeremy.munsch@ldc.fr>
* @license MIT https://opensource.org/licenses/MIT
* @version GIT: 0.0.1
* @link /
*/
namespace LDCCRM;
use Exception;
use JsonSerializable;
/**
* Class de gestion des users en session avec Wordpress
* Compatible avec php5
*
* @category API
* @package None
* @author Jérémy Munsch <jeremy.munsch@ldc.fr>
* @license MIT https://opensource.org/licenses/MIT
* @link /
*/
class WPCrmUser implements JsonSerializable
{
/**
* The CRM ID
*
* @var int
*/
protected $crm_id = null;
/**
* The CRM hashcode (UUID)
*
* @var string
*/
protected $hashcode = null;
/**
* The CRM customer email
*
* @var string
*/
protected $email = null;
/**
* The CRM customer info (the api response)
*
* @var object
*/
protected $info = null;
/**
* WPCrmUser construct
*
* @param integer $crm_id The crm id.
* @param string $hashcode The customer hashcode (UUID).
* @param string $email The customer email.
*/
public function __construct($crm_id, $hashcode, $email)
{
$this->crm_id = $crm_id;
$this->hashcode = $hashcode;
$this->email = $email;
}
/**
* Magic getter
*
* @param string $prop The prop to get.
*
* @return integer|string
*/
public function __get($prop)
{
if (is_null($this->info)) {
/**
* Object response
*
* @var object
*/
WPCrm::getUserFromApi($this->email);
}
if (!array_key_exists($prop, (array)$this->info)) {
throw new Exception('This value does not exists');
}
return $this->info->{$prop};
}
/**
* Set the customer info
* with api response
*
* @param object $info The Api object reponse (user)
*
* @return void
*/
public function setInfo($info)
{
if (is_null($info) or is_null($info->email)) {
throw new Exception('Info cannot be null');
}
$this->info = $info;
$this->crm_id = $info->crm_id;
$this->email = $info->email;
$this->hashcode = $info->hashcode;
}
/**
* Generate a user from session data
*
* @param string $string The json data stored in session
*
* @return \LDCCRM\WPCrmUser
*/
public static function parseFromJson($string)
{
$a = json_decode($string, true);
return new self(
$a['crm_id'],
$a['hashcode'],
$a['email'],
$a['info']
);
}
/**
* Serialize this object to an array
*
* @return array
*/
public function jsonSerialize()
{
return [
'crm_id' => $this->crm_id,
'hashcode' => $this->hashcode,
'email' => $this->email,
'info' => $this->info
];
}
}