Lndj\Traits\Parser::parserSchedule PHP Method

parserSchedule() public method

Paser the schedule data.
public parserSchedule ( Object $body ) : Array
$body Object
return Array
    public function parserSchedule($body)
    {
        $crawler = new Crawler((string) $body);
        $crawler = $crawler->filter('#Table1');
        $page = $crawler->children();
        //delete line 1、2;
        $page = $page->reduce(function (Crawler $node, $i) {
            if ($i == 0 || $i == 1) {
                return false;
            }
        });
        //to array
        $array = $page->each(function (Crawler $node, $i) {
            return $node->children()->each(function (Crawler $node, $j) {
                $span = (int) $node->attr('rowspan') ?: 0;
                return [$node->html(), $span];
            });
        });
        //If there are some classes in the table is in two or more lines,
        //insert it into the next lines in $array.
        //Thanks for @CheukFung
        $line_count = count($array);
        $schedule = [];
        for ($i = 0; $i < $line_count; $i++) {
            //lines
            for ($j = 0; $j < 9; $j++) {
                //rows
                if (isset($array[$i][$j])) {
                    $k = $array[$i][$j][1];
                    while (--$k > 0) {
                        // insert element to next line
                        //Set the span 0
                        $array[$i][$j][1] = 0;
                        $array[$i + $k] = array_merge(array_slice($array[$i + $k], 0, $j), [$array[$i][$j]], array_splice($array[$i + $k], $j));
                    }
                }
                $schedule[$i][$j] = isset($array[$i][$j][0]) ? $array[$i][$j][0] : '';
            }
        }
        return $schedule;
    }