InstanceConfig.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. class Type_InstanceConfig {
  3. var $_data = []; // { id, namespace, rootNamespace, _createdAt, SOURCE, VERSION, A_STATUS, tableName }
  4. static function build($instanceRow = []) {
  5. if (!$instanceRow) throw new Exception("Missing data in build Type InstanceConfig");
  6. // if (!$instanceRow['ID']) throw new Exception("Missing ID in Type RefConfig build"); // TODO: allow missing ID?
  7. $instanceConfig = new Type_InstanceConfig();
  8. $instanceConfig->id = (int)$instanceRow['id'];
  9. $instanceConfig->namespace = $instanceRow['namespace'];
  10. $instanceConfig->rootNamespace = $instanceRow['rootNamespace'];
  11. $instanceConfig->source = $instanceRow['SOURCE'];
  12. $instanceConfig->status = $instanceRow['A_STATUS'];
  13. $instanceConfig->version = $instanceRow['VERSION'];
  14. $instanceConfig->tableName = self::generateTableName($instanceConfig->id, $instanceConfig->source);
  15. return $instanceConfig;
  16. }
  17. static function generateTableName($id, $source) { // @return string | null
  18. switch ($source) {
  19. case 'table': return "CRM__#INSTANCE_TABLE__{$id}";
  20. case 'view': return "CRM__#INSTANCE_TABLE__{$id}_VIEW";
  21. default: return null;
  22. }
  23. }
  24. function __isset($name) {
  25. return (array_key_exists($name, $this->_data));
  26. }
  27. function __get($name) {
  28. if (array_key_exists($name, $this->_data)) {
  29. return $this->_data[$name];
  30. }
  31. return null;
  32. }
  33. function __set($name, $value) {
  34. $this->_data[$name] = $value;
  35. }
  36. function toArray() {
  37. return $this->_data;
  38. }
  39. function __toString() {
  40. return str_replace('"', '',
  41. str_replace([ '{', '}', '":', ',"' ], [ '{ ', ' }', ': ', ', ' ], json_encode($this->_data))
  42. );
  43. }
  44. }