<?php
namespace App\ExportData\Output; abstract class DbEntityAbstract { protected $xml = NULL; protected $dbTableName = NULL; public $operation = NULL; protected $primaryKeyName = NULL; protected $primaryKeyValue = NULL; protected $fields = []; public function __construct(\SimpleXMLElement $xml) { $this->xml = $xml; $this->setTableName(); $this->setPrimaryKeyName(); $this->setPrimaryKeyValue(); } abstract protected function setTableName(); abstract protected function setPrimaryKeyName(); abstract protected function setPrimaryKeyValue(); abstract public function populate(); public function getDbTableName() { return $this->dbTableName; } public function getOperation() { return $this->operation; } public function getPrimaryKeyName() { return $this->primaryKeyName; } public function getPrimaryKeyValue() { return $this->primaryKeyValue; } public function getFields() { return $this->fields; } public function validate() { $msgs = new \App\MessagesCollector(); if ($this->dbTableName == NULL) { throw new \App\MessagesException($msgs->addError(sprintf('The flag `dbTableName` is not set for class `%s`', get_called_class()))); } if ($this->operation == NULL) { throw new \App\MessagesException($msgs->addError(sprintf('The operation must be set as insert/update/delete for class `%s`', get_called_class()))); } if (!in_array($this->operation, ['insert', 'update', 'delete'])) { throw new \App\MessagesException($msgs->addError(sprintf("The operation `%s` is not valid for class `%s`", $this->operation, get_called_class()))); } if (($this->operation == 'update' or $this->operation == 'delete') and (!$this->primaryKeyName or !$this->primaryKeyValue)) { throw new \App\MessagesException($msgs->addError(sprintf("The primary key value and primary key name must be set for operation `%s` for class `%s`", $this->operation, get_called_class()))); } if (($this->operation == 'insert' or $this->operation == 'update') and !$this->fields) { throw new \App\MessagesException($msgs->addError(sprintf("The array with fields must be set for operation `%s` for class `%s`", $this->operation, get_called_class()))); } $this->validateFields($msgs); if ($msgs->hasErrorOrWarning()) { throw new \App\MessagesException($msgs); } } abstract protected function validateFields(\App\MessagesCollector $msgs); } 