<?php
// namespace ISO8583;

/**
 * 定义协议
 */
class Protocol 
{
	protected $schema = [];

	public function __construct($schema = null)
	{
        $schema = $schema === null ? implode(DIRECTORY_SEPARATOR, [__DIR__, 'Schema.json']) : $schema;
        if (!file_exists($schema)) {
            throw new \Exception('未知Schema定义文件: ' . $schema);
        }

		$schemaJSON    = json_decode(file_get_contents($schema), true);
        if ($schemaJSON === null) {
            throw new \Exception('Schema定义文件非标准JSON文件: ' . $schema);
        }

		foreach($schemaJSON as $field => $data) 
		{
			$this->setFieldData((int)$field, $data);
		}
	}

	public function getFieldData($field)
	{
		 if (!isset($this->schema[$field])) 
		 {
			 throw new \Exception('Schema定义文件中找不到字段：' . $field);
		 }

		 return $this->schema[$field];
	}

	public function setFieldData($field, $data)
	{
		$this->schema[$field] = $data;
	}
}
