<?php

namespace PortalPeople\netsuite\models;

use yii\base\BaseObject;

/**
 * Base NetSuite Record Model
 * 
 * This class provides a base for all NetSuite record models
 * 
 * @package PortalPeople\netsuite\models
 */
abstract class NetSuiteRecord extends BaseObject
{
    /**
     * @var int Internal ID of the record
     */
    public $internalId;
    
    /**
     * @var string External ID of the record
     */
    public $externalId;
    
    /**
     * @var array Record data
     */
    protected $data = [];
    
    /**
     * Get the NetSuite record type
     * 
     * @return string Record type
     */
    abstract public static function getRecordType();
    
    /**
     * Get record data as array
     * 
     * @return array Record data
     */
    public function toArray()
    {
        $result = [];
        
        if ($this->internalId !== null) {
            $result['internalId'] = $this->internalId;
        }
        
        if ($this->externalId !== null) {
            $result['externalId'] = $this->externalId;
        }
        
        return array_merge($result, $this->data);
    }
    
    /**
     * Load data from array
     * 
     * @param array $data Record data
     */
    public function loadData($data)
    {
        if (isset($data['internalId'])) {
            $this->internalId = $data['internalId'];
            unset($data['internalId']);
        }
        
        if (isset($data['externalId'])) {
            $this->externalId = $data['externalId'];
            unset($data['externalId']);
        }
        
        $this->data = $data;
    }
    
    /**
     * Get a field value
     * 
     * @param string $field Field name
     * @return mixed Field value
     */
    public function getField($field)
    {
        return isset($this->data[$field]) ? $this->data[$field] : null;
    }
    
    /**
     * Set a field value
     * 
     * @param string $field Field name
     * @param mixed $value Field value
     */
    public function setField($field, $value)
    {
        $this->data[$field] = $value;
    }
}