public function testTime() { /* This tests multiple formats. Ideally, all the format checking * should be done testing the date helper, but we'll do this * here for now. */ $forum = new Forum(); // Test #1, relative time $forumTime = '7 minutes ago'; $verifyTime = new \DateTime('NOW'); $verifyTime->add(\DateInterval::createFromDateString($forumTime)); $forum->setTime($forumTime); $this->assertEquals($verifyTime->format('Y-m-d\\TH:iO'), $forum->getTime()); // Test #2, partial date (same year) $forumTime = 'Jun 17, 1:01 PM'; $currentTime = new \DateTime('NOW'); $currentYear = $currentTime->format('Y'); $verifyTime = new \DateTime($currentYear . '-06-17 13:01'); $forum->setTime($forumTime); $this->assertEquals($verifyTime->format('Y-m-d\\TH:iO'), $forum->getTime()); // Test #3, full date $forumTime = ' May 1, 2014 1:19 PM'; $verifyTime = new \DateTime('2014-05-01 13:19'); $forum->setTime($forumTime); $this->assertEquals($verifyTime->format('Y-m-d\\TH:iO'), $forum->getTime()); }
private static function parseTopicDetails($item) { $crawler = new Crawler($item); $topic = new Forum(); $topic->profile = new Profile(); $topic->setTime($crawler->filter('div[style="padding-left: 3px;"]')->text()); # message id. # Example: # <div class="forum_border_around" id="forumMsg30902219">...</div> $topic->setid(str_replace('forumMsg', '', $crawler->attr('id'))); # image url. # Example: # <img src="http://cdn.myanimelist.net/images/useravatars/1901304.jpg" vspace="2" border="0"> //Note: Some MAL users do not have any avatars in the forum! try { $topic->profile->setAvatarUrl($crawler->filter('img')->attr('src')); } catch (\InvalidArgumentException $e) { //do nothing } $details = explode("\n\t\t ", $crawler->filter('td[class="forum_boardrow2"]')->text()); $topic->setUsername($details[0]); $topic->profile->details->setForumPosts(str_replace('Posts: ', '', $details[6])); if ($details[1] == '') { $topic->profile->details->setAccessRank('Member'); } else { $topic->profile->details->setAccessRank($details[1]); } if ($topic->profile->details->getForumPosts() == '') { $topic->profile->details->setStatus($details[3]); $topic->profile->details->setJoinDate(str_replace('Joined: ', '', $details[4])); $topic->profile->details->setForumPosts(str_replace('Posts: ', '', $details[5])); } else { $topic->profile->details->setStatus($details[4]); $topic->profile->details->setJoinDate(str_replace('Joined: ', '', $details[5])); } //to force json array and !objects. $topic->profile->manga_stats = null; $topic->profile->anime_stats = null; # comment. # Example: # <div id="message25496275">...</div> $topic->setComment($crawler->filter('div[id="message' . $topic->getId() . '"]')->html()); return $topic; }