Version 1
[yaffs-website] / vendor / consolidation / output-formatters / src / Transformations / PropertyParser.php
diff --git a/vendor/consolidation/output-formatters/src/Transformations/PropertyParser.php b/vendor/consolidation/output-formatters/src/Transformations/PropertyParser.php
new file mode 100644 (file)
index 0000000..18b982e
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+namespace Consolidation\OutputFormatters\Transformations;
+
+/**
+ * Transform a string of properties into a PHP associative array.
+ *
+ * Input:
+ *
+ *   one: red
+ *   two: white
+ *   three: blue
+ *
+ * Output:
+ *
+ *   [
+ *      'one' => 'red',
+ *      'two' => 'white',
+ *      'three' => 'blue',
+ *   ]
+ */
+class PropertyParser
+{
+    public static function parse($data)
+    {
+        if (!is_string($data)) {
+            return $data;
+        }
+        $result = [];
+        $lines = explode("\n", $data);
+        foreach ($lines as $line) {
+            list($key, $value) = explode(':', trim($line), 2) + ['', ''];
+            $result[$key] = trim($value);
+        }
+        return $result;
+    }
+}