I have a class that should be started with a% of properties.
$subscription = new CheckRenew([
'custom_2_id' => 13,
'email' => '[email protected]',
'zip' => '90530000',
'number' => '1234'
]);
Inside it has a array
method, which defines these resources. However, this __construct()
can come without one of the elements, so before assigning I check if each element exists.
namespace App\SubscriptionHelpers;
use App\SubscriptionModels\Subscription;
class CheckRenew
{
public $zip;
public $email;
public $addressNumber;
protected $custom2Id; // service customer id (magento)
public function __construct($info)
{
$this->custom2Id = null;
if (isset($info['custom_2_id'])) {
$this->custom2Id = $info['custom_2_id'];
}
$this->email = null;
if (isset($info['email'])) {
$this->email = $info['email'];
}
$this->email = null;
if (isset($info['email'])) {
$this->email = $info['email'];
}
$this->zip = null;
if (isset($info['zip'])) {
$this->zip = $info['zip'];
}
$this->addressNumber = null;
if (isset($info['number'])) {
$this->addressNumber = $info['number'];
}
}
}
Pretty basic. And it works, the problem is that as you can see, I've repeated a lot of code. As I am sick of DRY (do not repeat yourself), I would like, if possible, that someone would help me find a cleaner method of doing these assignments.
TL; DR
I would like to know in a cleaner, cleaner way, to check and assign my class properties.