Field.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /*
  3. {
  4. 'type': string,
  5. 'minOccurs': xsd:nonNegativeInteger, default: 1,
  6. 'maxOccurs': xsd:allNNI, default: 1,
  7. 'restrictions': [], // TODO: array of type Restriction
  8. 'appInfo': [], // TODO: define type for appInfo
  9. 'source': p5:enum (values: 'table', 'view', 'backRef'), default: 'table'
  10. }
  11. */
  12. class Type_Field_Simple extends Type_Field {}
  13. class Type_Field_Ref extends Type_Field {}
  14. class Type_Field {
  15. var $_data = [];
  16. static function build($field = []) {
  17. if (!$field) throw new Exception("Missing data in build Type Field");
  18. if (empty($field['type']) && empty($field['xsdType'])) throw new Exception("Missing type in Type Field build");
  19. $type = (!empty($field['type'])) ? $field['type'] : $field['xsdType'];
  20. // TODO: validate $field['type']
  21. $instance = ('ref:' === substr($type, 0, 4))
  22. ? new Type_Field_Ref($field)
  23. : new Type_Field_Simple($field)
  24. ;
  25. $instance->type = $type;
  26. $instance->minOccurs = V::get('minOccurs', 1, $field, 'int');
  27. $instance->maxOccurs = ('unbounded' === V::get('maxOccurs', 1, $field)) ? 'unbounded' : V::get('maxOccurs', 1, $field);
  28. $instance->restrictions = (!empty($field['restrictions']))
  29. ? self::parseRestriction($field['restrictions'])
  30. : []
  31. ;
  32. $instance->appInfo = (!empty($field['appInfo']))
  33. ? self::parseAppInfo($field['appInfo'])
  34. : []
  35. ;
  36. $instance->source = self::getSourceFromAppInfo($instance->appInfo);
  37. return $instance;
  38. }
  39. static function parseRestriction($restriction) {
  40. $ret = [];
  41. if (!empty($restrictions)) {
  42. if (is_string($restrictions)) {
  43. $ret = @json_decode($restrictions, $assoc = true);
  44. if (null === $ret && 0 !== json_last_error()) {
  45. throw new Exception("Parse json error for restrictions: " . json_last_error());
  46. }
  47. } else if (is_array($restrictions)) {
  48. $ret = $restrictions;
  49. } else {
  50. throw new Exception("Not upported restrictions");
  51. }
  52. }
  53. return $ret;
  54. }
  55. static function parseAppInfo($appInfo) {
  56. $ret = [];
  57. if (!empty($appInfo)) {
  58. if (is_string($appInfo)) {
  59. $ret = @json_decode($appInfo, $assoc = true);
  60. if (null === $ret && 0 !== json_last_error()) {
  61. throw new Exception("Parse json error for appInfo: " . json_last_error());
  62. }
  63. } else if (is_array($appInfo)) {
  64. $ret = $appInfo;
  65. } else {
  66. throw new Exception("Not upported appInfo");
  67. }
  68. }
  69. return $ret;
  70. }
  71. static function getSourceFromAppInfo($appInfo) {
  72. if (!$appInfo) return 'table';
  73. // [appInfo] => Array:
  74. // [flat_relation_cache] => Array:
  75. // [@backref_evaluate] => true
  76. // [source] => Array:
  77. // [@ref_engine] => view
  78. $source_view = (!empty($appInfo['flat_relation_cache']['source']) && 'view' === V::get('@ref_engine', '', $appInfo['flat_relation_cache']['source']));
  79. $source_backRef = (!empty($appInfo['flat_relation_cache']) && V::get('@backref_evaluate', false, $appInfo['flat_relation_cache']));
  80. if ($source_backRef) return 'backRef';
  81. if ($source_view) return 'view';
  82. return 'table';
  83. }
  84. function __isset($name) {
  85. return (array_key_exists($name, $this->_data));
  86. }
  87. function __get($name) {
  88. if (array_key_exists($name, $this->_data)) {
  89. return $this->_data[$name];
  90. }
  91. return null;
  92. }
  93. function __set($name, $value) {
  94. $this->_data[$name] = $value;
  95. }
  96. function toArray() {
  97. return $this->_data;
  98. }
  99. function __toString() {
  100. return str_replace('"', '',
  101. str_replace([ '{', '}', '":', ',"' ], [ '{ ', ' }', ': ', ', ' ], json_encode($this->_data))
  102. );
  103. }
  104. }
  105. /*
  106. case: install Ref based on backRef - example namespace=default_db/BI_audit_ENERGA_PRACOWNICY/BI_audit_ENERGA_PRACOWNICY
  107. <xs:element ref="default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object">
  108. <xs:annotation>
  109. <xs:appinfo>
  110. <system_cache__appinfo:flat_relation_cache system_cache__appinfo:backref_evaluate="true">
  111. <system_cache__appinfo:source system_cache__appinfo:ref_engine="view"/>
  112. </system_cache__appinfo:flat_relation_cache>
  113. </xs:appinfo>
  114. </xs:annotation>
  115. </xs:element>
  116. // [appInfo] => Array:
  117. // [flat_relation_cache] => Array:
  118. // [@backref_evaluate] => true
  119. // [source] => Array:
  120. // [@ref_engine] => view
  121. */