Update README because I forgot how to develop this app.
[Marketplaylister.git] / mpfuncs.php
index 870acaf..d5be144 100644 (file)
@@ -1,48 +1,16 @@
-<?php\r
-\r
-    const DATE_FORM = 'mdY';\r
-\r
-    \r
-    function parseEpisodePage(DomNodeList $divs): array {\r
-        $episodePage = [];\r
-        foreach ($divs as $div) {\r
-            if ($div->hasAttribute('class') && $div->getAttribute('class') === 'episode-music') {\r
-                $songs = [];\r
-                foreach ($div->childNodes as $row) {\r
-                    $children = $row->childNodes[0]->childNodes;\r
-                    $songs[] = [\r
-                        'title' => $children[0]->nodeValue,\r
-                        'artist' => $children[1]->nodeValue\r
-                    ];\r
-                }\r
-                $episodePage[] = $songs;\r
-            }\r
-            \r
-        }\r
-        \r
-        return $episodePage;\r
-        \r
-    }\r
-    \r
-    /**\r
-     * Go through the DOM elements provided and pull out the Dates of all marketplace\r
-     * pod episodes in the provided list.\r
-     * \r
-     * @param DomNodeList $headers The elements with a header tag from the DOM\r
-     * @param DateTime $lastDate The date of the most recent episode from the DB\r
-     */\r
-    function parseEpisodeDate(DomNodeList $headers, DateTime $lastDate): array {\r
-      $episodeDates = [];\r
-      foreach ($headers as $header) {\r
-        if ($header->hasAttribute('class') && $header->getAttribute('class') === 'river--hed') {\r
-          $episodeAnchorHref = $header->firstChild->getAttribute('href');\r
-          $dateString = explode('/', $episodeAnchorHref)[3];\r
-          $episodeDate = DateTime::createFromFormat(DATE_FORM, $dateString);\r
-          if ($episodeDate < $lastDate) {\r
-            break;\r
-          }\r
-          $episodeDates[] = $episodeDate;\r
-        }\r
-      }\r
-      return $episodeDates;\r
-    }\r
+<?php
+
+const DATE_FORM = 'mdY';
+
+function findChildWithClass(/** iterable */ $children, string $class)/**: array*/ {
+    if ($children instanceof Traversable) {
+        $children = iterator_to_array($children);
+    }
+    return array_filter(
+        $children,
+        function($child) use ($class) {
+            return $child->hasAttributes()
+                && $child->attributes->getNamedItem('class')->value == $class;
+        }
+    );
+}