Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace opensrs\backwardcompatibility\dataconversion\domains\provisioning;

use opensrs\backwardcompatibility\dataconversion\DataConversion;
use opensrs\Exception;

class SWRegister extends DataConversion
{
Expand Down Expand Up @@ -52,10 +53,11 @@ class SWRegister extends DataConversion
// if data->custom_nameservers == 1, nameserver_list
// is populated with data->name1, data->name2 etc
// up to max potential data->name10
// 'nameserver_list' => 'data->nameserver_list',
'nameserver_list' => 'data->nameserver_list',
'owner_confirm_address' => 'data->owner_confirm_address',
'period' => 'data->period',
'premium_price_to_verify' => 'data->premium_price_to_verify',
'auth_info' => 'data->auth_info',
'reg_domain' => 'data->reg_domain',
'reg_username' => 'data->reg_username',
'reg_password' => 'data->reg_password',
Expand All @@ -77,41 +79,74 @@ public function convertDataObject($dataObject, $newStructure = null)

$newDataObject = $p->convertDataObject($dataObject, $newStructure);

// run customizations required by this particular class

// set custom nameservers to nameserver_list
// Check if we need to handle custom nameservers
if (isset($dataObject->data)) {
if ($dataObject->data->custom_nameservers == 1) {
$newDataObject->attributes->nameserver_list = array();

for ($j = 1; $j <= 10; ++$j) {
$tns = 'name'.$j;
$tso = 'sortorder'.$j;

if (
isset($dataObject->data->$tns) &&
$dataObject->data->$tns != '' &&
isset($dataObject->data->$tso) &&
$dataObject->data->$tso
) {
$nameserver = new \stdClass();
$nameserver->name = $dataObject->data->{$tns};
$nameserver->sortorder = $dataObject->data->{$tso};

$newDataObject->attributes->nameserver_list[] = $nameserver;
if (
(isset($dataObject->data->custom_nameservers) && (int)$dataObject->data->custom_nameservers === 1) ||
(isset($dataObject->data->custom_transfer_nameservers) && (int)$dataObject->data->custom_transfer_nameservers === 1)
) {
// ✅ Normalize keys first
$dataObject->data->nameserver_list = array_values((array)$dataObject->data->nameserver_list);

if (empty($dataObject->data->nameserver_list)) {
throw new Exception("nameserver_list is required when custom_nameservers or custom_transfer_nameservers is set to 1 ");
}

// ✅ Set normalized list into attributes
$newDataObject->attributes->nameserver_list = $dataObject->data->nameserver_list;

if (!empty($newDataObject->attributes->nameserver_list)) {
$normalizedList = [];
foreach ($dataObject->data->nameserver_list as $ns) {
$nsArray = (array) $ns;
if (!empty($nsArray['name'])) {
$normalizedList[] = [
'name' => $nsArray['name'],
'sortorder' => (int) $nsArray['sortorder']
];
}
}
$dataObject->data->nameserver_list = array_values($normalizedList);

if (empty($normalizedList)) {
throw new Exception('nameserver_list is invalid — contains only empty names');
}

$newDataObject->attributes->nameserver_list = array_values($normalizedList);
}

// ✅ Optional: legacy fallback logic if you still support name1..10
if (empty($newDataObject->attributes->nameserver_list)) {
for ($j = 1; $j <= 10; ++$j) {
$tns = 'name' . $j;
$tso = 'sortorder' . $j;

if (
!empty($dataObject->data->$tns) &&
!empty($dataObject->data->$tso)
) {
$nameserver = new \stdClass();
$nameserver->name = $dataObject->data->{$tns};
$nameserver->sortorder = $dataObject->data->{$tso};

$newDataObject->attributes->nameserver_list[] = $nameserver;
}
}
}
}
}

if (isset($dataObject->personal)) {
$newDataObject->attributes->contact_set = new \stdClass();
$newDataObject->attributes->contact_set->owner = $dataObject->personal;
$newDataObject->attributes->contact_set->admin = $dataObject->personal;
$newDataObject->attributes->contact_set->billing = $dataObject->personal;
$newDataObject->attributes->contact_set->tech = $dataObject->personal;
if (!isset($newDataObject->attributes->contact_set) || empty((array)$newDataObject->attributes->contact_set)) {
if (isset($dataObject->data->contact_set)) {
$newDataObject->attributes->contact_set = $dataObject->data->contact_set;
} elseif (isset($dataObject->personal)) {
$newDataObject->attributes->contact_set = new \stdClass();
$newDataObject->attributes->contact_set->owner = $dataObject->personal;
$newDataObject->attributes->contact_set->admin = $dataObject->personal;
$newDataObject->attributes->contact_set->billing = $dataObject->personal;
$newDataObject->attributes->contact_set->tech = $dataObject->personal;
}
}
// end customizations

return $newDataObject;
}
Expand Down
74 changes: 67 additions & 7 deletions opensrs/domains/provisioning/SWRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace opensrs\domains\provisioning;

use opensrs\Base;
use opensrs\Exception;

class SWRegister extends Base
{
Expand All @@ -15,17 +16,38 @@ class SWRegister extends Base
public $resultFullFormatted;
public $resultFormatted;

public $requiredFields = array(
'attributes' => array(
// Dynamic required fields per reg_type
public $requiredFieldsByType = array(
'new' => array(
'domain',
'custom_nameservers',
'custom_tech_contact',
'period',
'reg_username',
'reg_password',
'reg_type',
),
);
),
'transfer' => array(
'domain',
'auth_info',
'reg_username',
'reg_password',
'reg_type',
//'custom_transfer_nameservers' //is optional in API, enforced as domains existing if not set
),
'assign' => array(
'domain',
'reg_username',
'reg_password',
'reg_type',
),
'owner_change' => array(
'domain',
'reg_username',
'reg_password',
'reg_type',
),
);

public function __construct($formatString, $dataObject, $returnFullResponse = true)
{
Expand All @@ -35,6 +57,8 @@ public function __construct($formatString, $dataObject, $returnFullResponse = tr

$this->_validateObject($dataObject);

$this->validateAttributes((array)$dataObject->attributes);

$this->send($dataObject, $returnFullResponse);
}

Expand All @@ -43,13 +67,49 @@ public function __destruct()
parent::__destruct();
}

private function validateAttributes($attributes)
{
if (!isset($attributes['reg_type'])) {
throw new Exception('SWRegister Error - Missing required field: reg_type');
}

$regType = strtolower($attributes['reg_type']);

if (!isset($this->requiredFieldsByType[$regType])) {
throw new Exception("SWRegister Error - Unsupported reg_type: {$regType}");
}

foreach ($this->requiredFieldsByType[$regType] as $field) {
if (!isset($attributes[$field])) {
throw new Exception("SWRegister Error - Missing required field for '{$regType}': {$field}");
}
}

// Special handling for transfer - ensure custom_transfer_nameservers is always present as an array
if ($regType === 'transfer') {
if (!isset($attributes['custom_transfer_nameservers'])) {
$attributes['custom_transfer_nameservers'] = 0;
}
}

// Example for .EU domain validation
if (isset($attributes['domain']) && preg_match('/\.eu$/i', $attributes['domain'])) {
if (empty($attributes['lang_pref'])) {
throw new Exception("SWRegister Error - .EU domains require 'lang_pref'.");
}
if (empty($attributes['eu_country_of_residence']) && empty($attributes['eu_country_of_citizenship'])) {
throw new Exception("SWRegister Error - .EU domains require 'eu_country_of_residence' or 'eu_country_of_citizenship'.");
}
}

// You can extend here for other TLDs like .CA or .DE
}

public function customResponseHandling($arrayResult, $returnFullResponse = true)
{
/* Added by BC : NG : 16-7-2014 : To set error message for Insufficient Funds */
if (isset($arrayResult['attributes']['forced_pending']) and $arrayResult['attributes']['forced_pending'] != '' and $arrayResult['is_success'] == 1) {
if (isset($arrayResult['attributes']['forced_pending']) && $arrayResult['attributes']['forced_pending'] != '' && $arrayResult['is_success'] == 1) {
$arrayResult['is_success'] = 0;

// Get Resonse Text 'Registration successful' when insufficient fund
if ($arrayResult['response_text'] == 'Registration successful') {
$arrayResult['response_text'] = 'Insufficient Funds';
}
Expand Down