summaryrefslogtreecommitdiffstats
path: root/lib/Controller/ApiPayloadTrait.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Controller/ApiPayloadTrait.php')
-rw-r--r--lib/Controller/ApiPayloadTrait.php36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/Controller/ApiPayloadTrait.php b/lib/Controller/ApiPayloadTrait.php
new file mode 100644
index 000000000..2bb31784e
--- /dev/null
+++ b/lib/Controller/ApiPayloadTrait.php
@@ -0,0 +1,36 @@
+<?php
+
+
+namespace OCA\News\Controller;
+
+use OCA\News\Db\IAPI;
+
+trait ApiPayloadTrait
+{
+ /**
+ * Serialize all data
+ *
+ * @param mixed $data IAPI or array,
+ * anything else will return an empty array
+ *
+ * @return array
+ */
+ public function serialize($data): array
+ {
+ $return = [];
+ if ($data instanceof IAPI) {
+ return [$data->toAPI()];
+ }
+
+ if (!is_array($data)) {
+ return $return;
+ }
+
+ foreach ($data as $entity) {
+ if ($entity instanceof IAPI) {
+ $return[] = $entity->toAPI();
+ }
+ }
+ return $return;
+ }
+}