<?php
/** 
 * @package     VikAppointments
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

/**
 * Populate the options array with the existing states,
 * in order to support the correct placeholders while
 * importing and exporting the records.
 *
 * @since 1.7.3
 */
class ImportColumnState extends ImportColumn
{
	/**
	 * States cache.
	 *
	 * @var array
	 */
	private static $states = null;

	/**
	 * Binds the internal properties with the given array/object.
	 *
	 * @param 	mixed  $data  Either an array or an object.
	 *
	 * @return 	void
	 */
	protected function setup($data)
	{
		// use parent to set up data
		parent::setup($data);

		foreach (static::getStates() as $state)
		{
			// register state as option
			$this->options[$state['id']] = $state['state_name'];
		}
	}

	/**
	 * Loads a list of available states.
	 *
	 * @return 	array
	 */
	protected static function getStates()
	{
		// load states only once
		if (is_null(static::$states))
		{
			// sort states list by name ASC
			static::$states = VAPLocations::getStates(0, 'state_name');
		}

		return static::$states;
	}
}
