1 | <?php |
||
16 | class SchemaMapper |
||
17 | { |
||
18 | |||
19 | |||
20 | public function convert(JsonDefinition $definition) |
||
87 | |||
88 | |||
89 | /* |
||
90 | * |
||
91 | |||
92 | { |
||
93 | |||
94 | "x-documentClass": {{ (base ~ 'Document\\' ~ document) | json_encode() }}, |
||
95 | |||
96 | {% if json is defined %} |
||
97 | "description": {{ json.getDescription()|json_encode() }}, |
||
98 | {% else %} |
||
99 | "description": "@todo replace me", |
||
100 | {% endif %} |
||
101 | |||
102 | "x-versioning": {{ isVersioning|json_encode() }}, |
||
103 | |||
104 | {% if noIdField is not defined %} |
||
105 | "x-id-in-post-allowed": false, |
||
106 | {% else %} |
||
107 | "x-id-in-post-allowed": true, |
||
108 | {% endif %} |
||
109 | |||
110 | {% if json is defined %} |
||
111 | "title": "{{ json.getTitle() }}", |
||
112 | {% endif %} |
||
113 | |||
114 | "properties": { |
||
115 | {% set requiredFields = [] %} |
||
116 | {% set searchableFields = [] %} |
||
117 | {% set readOnlyFields = [] %} |
||
118 | |||
119 | {% if idField is defined %} |
||
120 | {% if idField.required is defined and idField.required == true %} |
||
121 | {% set requiredFields = requiredFields|merge(['id']) %} |
||
122 | {% endif %} |
||
123 | {% endif %} |
||
124 | |||
125 | |||
126 | |||
127 | {% for field in fields %} |
||
128 | "{{ field.fieldName }}": { |
||
129 | "title": {{ field.title|json_encode() }}, |
||
130 | |||
131 | {% if field.collection is defined and field.type == 'extref' %} |
||
132 | "collection": {{ field.collection|json_encode() }}, |
||
133 | {% endif %} |
||
134 | |||
135 | {% if field.readOnly is defined and field.readOnly == true %} |
||
136 | "readOnly": {{ field.readOnly|json_encode() }}, |
||
137 | {% endif %} |
||
138 | |||
139 | {% if field.recordOriginException is defined and field.recordOriginException == true %} |
||
140 | "recordOriginException": {{ field.recordOriginException|json_encode() }}, |
||
141 | {% endif %} |
||
142 | |||
143 | {% if field.xDynamicKey is defined and field.xDynamicKey != null %} |
||
144 | "x-dynamic-key": { |
||
145 | "document-id": "{{ field.xDynamicKey.documentId }}", |
||
146 | "repository-method": "{{ field.xDynamicKey.repositoryMethod }}", |
||
147 | "ref-field": "{{ field.xDynamicKey.refField }}" |
||
148 | }, |
||
149 | {% endif %} |
||
150 | |||
151 | {% if field.constraints is defined and field.constraints != null %} |
||
152 | "x-constraints": {{ field.constraints|json_encode() }}, |
||
153 | {% endif %} |
||
154 | |||
155 | {% if field.description is defined and field.description != '' %} |
||
156 | "description": {{ field.description|json_encode() }} |
||
157 | {% else %} |
||
158 | "description": "@todo replace me" |
||
159 | {% endif %} |
||
160 | |||
161 | {% if field.required is defined and field.required == true %} |
||
162 | {% set requiredFields = requiredFields|merge([field.fieldName]) %} |
||
163 | {% endif %} |
||
164 | |||
165 | {% if field.searchable is defined and field.searchable > 0 %} |
||
166 | {% set searchableFields = searchableFields|merge([field.fieldName]) %} |
||
167 | {% endif %} |
||
168 | |||
169 | {% if field.readOnly is defined and field.readOnly == true %} |
||
170 | {% set readOnlyFields = readOnlyFields|merge([field.fieldName]) %} |
||
171 | {% endif %} |
||
172 | |||
173 | }, |
||
174 | {% endfor %} |
||
175 | |||
176 | |||
177 | |||
178 | "id": { |
||
179 | "title": "ID", |
||
180 | "description": "Unique identifier" |
||
181 | {% if isrecordOriginFlagSet %} |
||
182 | }, |
||
183 | "recordOrigin": { |
||
184 | "title": "record origin", |
||
185 | "description": "A small string like 'core' to determine the record origin. Documents from some sources must not be modified. The 'core' value is defined as readonly by default." |
||
186 | {% endif %} |
||
187 | } |
||
188 | }, |
||
189 | |||
190 | {# |
||
191 | the whole recordOrigin thing is kinda messed up as you need 2 vars to correctly detect what should be done. |
||
192 | for schema, we condense it so recordOriginModifiable holds the whole truth in one.. |
||
193 | #} |
||
194 | {% if (recordOriginModifiable is defined and recordOriginModifiable == "false") and |
||
195 | (isrecordOriginFlagSet is defined and isrecordOriginFlagSet == true) %} |
||
196 | "recordOriginModifiable": false, |
||
197 | {% else %} |
||
198 | "recordOriginModifiable": true, |
||
199 | {% endif %} |
||
200 | "required": {{ requiredFields|json_encode() }}, |
||
201 | "searchable": {{ searchableFields|json_encode() }}, |
||
202 | "readOnlyFields": {{ readOnlyFields|json_encode() }} |
||
203 | } |
||
204 | |||
205 | |||
206 | * |
||
207 | * |
||
208 | * |
||
209 | * |
||
210 | */ |
||
211 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: