| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- /*
- {
- 'type': string,
- 'minOccurs': xsd:nonNegativeInteger, default: 1,
- 'maxOccurs': xsd:allNNI, default: 1,
- 'restrictions': [], // TODO: array of type Restriction
- 'appInfo': [], // TODO: define type for appInfo
- 'source': p5:enum (values: 'table', 'view', 'backRef'), default: 'table'
- }
- */
- class Type_Field_Simple extends Type_Field {}
- class Type_Field_Ref extends Type_Field {}
- class Type_Field {
- var $_data = [];
- static function build($field = []) {
- if (!$field) throw new Exception("Missing data in build Type Field");
- if (empty($field['type']) && empty($field['xsdType'])) throw new Exception("Missing type in Type Field build");
- $type = (!empty($field['type'])) ? $field['type'] : $field['xsdType'];
- // TODO: validate $field['type']
- $instance = ('ref:' === substr($type, 0, 4))
- ? new Type_Field_Ref($field)
- : new Type_Field_Simple($field)
- ;
- $instance->type = $type;
- $instance->minOccurs = V::get('minOccurs', 1, $field, 'int');
- $instance->maxOccurs = ('unbounded' === V::get('maxOccurs', 1, $field)) ? 'unbounded' : V::get('maxOccurs', 1, $field);
- $instance->restrictions = (!empty($field['restrictions']))
- ? self::parseRestriction($field['restrictions'])
- : []
- ;
- $instance->appInfo = (!empty($field['appInfo']))
- ? self::parseAppInfo($field['appInfo'])
- : []
- ;
- $instance->source = self::getSourceFromAppInfo($instance->appInfo);
- return $instance;
- }
- static function parseRestriction($restriction) {
- $ret = [];
- if (!empty($restrictions)) {
- if (is_string($restrictions)) {
- $ret = @json_decode($restrictions, $assoc = true);
- if (null === $ret && 0 !== json_last_error()) {
- throw new Exception("Parse json error for restrictions: " . json_last_error());
- }
- } else if (is_array($restrictions)) {
- $ret = $restrictions;
- } else {
- throw new Exception("Not upported restrictions");
- }
- }
- return $ret;
- }
- static function parseAppInfo($appInfo) {
- $ret = [];
- if (!empty($appInfo)) {
- if (is_string($appInfo)) {
- $ret = @json_decode($appInfo, $assoc = true);
- if (null === $ret && 0 !== json_last_error()) {
- throw new Exception("Parse json error for appInfo: " . json_last_error());
- }
- } else if (is_array($appInfo)) {
- $ret = $appInfo;
- } else {
- throw new Exception("Not upported appInfo");
- }
- }
- return $ret;
- }
- static function getSourceFromAppInfo($appInfo) {
- if (!$appInfo) return 'table';
- // [appInfo] => Array:
- // [flat_relation_cache] => Array:
- // [@backref_evaluate] => true
- // [source] => Array:
- // [@ref_engine] => view
- $source_view = (!empty($appInfo['flat_relation_cache']['source']) && 'view' === V::get('@ref_engine', '', $appInfo['flat_relation_cache']['source']));
- $source_backRef = (!empty($appInfo['flat_relation_cache']) && V::get('@backref_evaluate', false, $appInfo['flat_relation_cache']));
- if ($source_backRef) return 'backRef';
- if ($source_view) return 'view';
- return 'table';
- }
- function __isset($name) {
- return (array_key_exists($name, $this->_data));
- }
- function __get($name) {
- if (array_key_exists($name, $this->_data)) {
- return $this->_data[$name];
- }
- return null;
- }
- function __set($name, $value) {
- $this->_data[$name] = $value;
- }
- function toArray() {
- return $this->_data;
- }
- function __toString() {
- return str_replace('"', '',
- str_replace([ '{', '}', '":', ',"' ], [ '{ ', ' }', ': ', ', ' ], json_encode($this->_data))
- );
- }
- }
- /*
- case: install Ref based on backRef - example namespace=default_db/BI_audit_ENERGA_PRACOWNICY/BI_audit_ENERGA_PRACOWNICY
- <xs:element ref="default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object">
- <xs:annotation>
- <xs:appinfo>
- <system_cache__appinfo:flat_relation_cache system_cache__appinfo:backref_evaluate="true">
- <system_cache__appinfo:source system_cache__appinfo:ref_engine="view"/>
- </system_cache__appinfo:flat_relation_cache>
- </xs:appinfo>
- </xs:annotation>
- </xs:element>
- // [appInfo] => Array:
- // [flat_relation_cache] => Array:
- // [@backref_evaluate] => true
- // [source] => Array:
- // [@ref_engine] => view
- */
|