<commit_before><?php
/**
* Atarashii MAL API
*
* @author    <NAME> <<EMAIL>>
* @author    <NAME> <<EMAIL>>
* @copyright 2014-2015 <NAME> and <NAME>
* @license   http://www.apache.org/licenses/LICENSE-2.0 Apache Public License 2.0
*/

namespace Atarashii\APIBundle\Parser;

use Atarashii\APIBundle\Model\Actor;
use Symfony\Component\DomCrawler\Crawler;
use Atarashii\APIBundle\Model\Cast;

class CastParser
{
    public static function parse($contents)
    {
        $crawler = new Crawler();
        $crawler->addHTMLContent($contents, 'UTF-8');

        $items = $crawler->filter('td[style="padding-left: 5px;"] table[width="100%"]');

        //Bypass Undefined variable error.
        $staff = null;
        $characters = null;

        foreach ($items as $item) {
            //Bypass to determine if the last table contains the staff members
            $crawler = new Crawler($item);
            if ($crawler->filter('td[width="27"]')->count() != 1) {
                $staffitems = $crawler->children();
                foreach ($staffitems as $staffitem) {
                    $staff[] = self::parseStaff($staffitem);
                }
            } else {
                $characters[] = self::parseCharacters($crawler);
            }
        }

        return array('Characters' => $characters, 'Staff' => $staff);
    }

    private static function parseCharacters($item)
    {
        $cast = new Cast();

        $cast->setId(explode('/', $item->filter('a')->attr('href'))[2]);
        $cast->setName($item->filter('a')->eq(1)->text());

        $result = preg_match('/rs\/(.*?)\?/', $item->filter('img')->attr('src'), $imageURL);
        if ($result > 0)
            $cast->setImage('http://cdn.myanimelist.net/images/characters/' . $imageURL[1]);
        else
            $cast->setImage($item->filter('img')->attr('src'));

        $cast->setRole($item->filter('small')->text());

            foreach ($item->filter('table[class="space_table"] tr') as $actorItem) {
                $actor = new Actor();
                $crawler = new Crawler($actorItem);

                if ($crawler->filter('td')->count() > 1) {
                    $actor->setId(explode('/', $crawler->filter('a')->attr('href'))[2]);
                    $actor->setName($crawler->filter('a')->text());
                    $actor->setLanguage($crawler->filter('small')->last()->text());

                    $result = preg_match('/rs\/(.*?)\?/', $crawler->filter('img')->last()->attr('src'), $imageURL);
                    if ($result > 0)
                        $actor->setImage('http://cdn.myanimelist.net/images/voiceactors/' . $imageURL[1]);
                    else
                        $actor->setImage($crawler->filter('img')->last()->attr('src'));

                    $cast->setActors($actor);
                }
        }

        return $cast;
    }

    private static function parseStaff($item)
    {
        $crawler = new Crawler($item);
        $cast = new Cast();

        $cast->setId(explode('/', $crawler->filter('a')->attr('href'))[2]);
        $cast->setName($crawler->filter('a')->eq(1)->text());
        $cast->setRank($crawler->filter('small')->last()->text());

        $result = preg_match('/rs\/(.*?)\?/', $crawler->filter('img')->last()->attr('src'), $imageURL);
        if ($result > 0)
            $cast->setImage('http://cdn.myanimelist.net/images/voiceactors/' . $imageURL[1]);
        else
            $cast->setImage($crawler->filter('img')->last()->attr('src'));<commit_msg>Fix Cast Parser for PHP 5.3<commit_after><?php
/**
* Atarashii MAL API
*
* @author    <NAME> <<EMAIL>>
* @author    <NAME> <<EMAIL>>
* @copyright 2014-2015 <NAME> and <NAME>
* @license   http://www.apache.org/licenses/LICENSE-2.0 Apache Public License 2.0
*/

namespace Atarashii\APIBundle\Parser;

use Atarashii\APIBundle\Model\Actor;
use Symfony\Component\DomCrawler\Crawler;
use Atarashii\APIBundle\Model\Cast;

class CastParser
{
    public static function parse($contents)
    {
        $crawler = new Crawler();
        $crawler->addHTMLContent($contents, 'UTF-8');

        $items = $crawler->filter('td[style="padding-left: 5px;"] table[width="100%"]');

        //Bypass Undefined variable error.
        $staff = null;
        $characters = null;

        foreach ($items as $item) {
            //Bypass to determine if the last table contains the staff members
            $crawler = new Crawler($item);
            if ($crawler->filter('td[width="27"]')->count() != 1) {
                $staffitems = $crawler->children();
                foreach ($staffitems as $staffitem) {
                    $staff[] = self::parseStaff($staffitem);
                }
            } else {
                $characters[] = self::parseCharacters($crawler);
            }
        }

        return array('Characters' => $characters, 'Staff' => $staff);
    }

    private static function parseCharacters($item)
    {
        $cast = new Cast();

        $characterIds = explode('/', $item->filter('a')->attr('href'));

        $cast->setId($characterIds[2]);
        $cast->setName($item->filter('a')->eq(1)->text());

        $result = preg_match('/rs\/(.*?)\?/', $item->filter('img')->attr('src'), $imageURL);
        if ($result > 0)
            $cast->setImage('http://cdn.myanimelist.net/images/characters/' . $imageURL[1]);
        else
            $cast->setImage($item->filter('img')->attr('src'));

        $cast->setRole($item->filter('small')->text());

            foreach ($item->filter('table[class="space_table"] tr') as $actorItem) {
                $actor = new Actor();
                $crawler = new Crawler($actorItem);

                if ($crawler->filter('td')->count() > 1) {
                    $actorIds = explode('/', $crawler->filter('a')->attr('href'));

                    $actor->setId($actorIds[2]);
                    $actor->setName($crawler->filter('a')->text());
                    $actor->setLanguage($crawler->filter('small')->last()->text());

                    $result = preg_match('/rs\/(.*?)\?/', $crawler->filter('img')->last()->attr('src'), $imageURL);
                    if ($result > 0)
                        $actor->setImage('http://cdn.myanimelist.net/images/voiceactors/' . $imageURL[1]);
                    else
                        $actor->setImage($crawler->filter('img')->last()->attr('src'));

                    $cast->setActors($actor);
                }
        }

        return $cast;
    }

    private static function parseStaff($item)
    {
        $crawler = new Crawler($item);
        $cast = new Cast();

        $castId = explode('/', $crawler->filter('a')->attr('href'));

        $cast->setId($castId[2]);
        $cast->setName($crawler->filter('a')->eq(1)->text());
        $cast->setRank($crawler->filter('small')->last()->text());

        $result = preg_match('/rs\/(.*?)\?/', $crawler->filter('img')->last()->attr('src'), $imageURL);
        if ($result > 0)
            $cast->setImage('http://cdn.myanimelist.net/images/voiceactors/' . $imageURL[1]);
        else
            $cast->setImage($crawler->filter('img')->last()->attr('src'));
{'fraction_non_alphanumeric': 0.20592363840539024, 'fraction_numerical': 0.009264458169567658, 'mean_word_length': 2.219611387257117, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 155, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3425090', 'n_tokens_mistral': 2253, 'n_tokens_neox': 2121, 'n_words': 346}