<?php

namespace Neag\UtilitesBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Response;

use Tims\DebriBundle\Entity\Afisha;
use Vhabare\NewsBundle\Cache\Cache;

class ParsingController extends Controller
{
    protected function getInnerHtml($node)
    {
        if (is_null($node))
        {
            return '';
        }
        return $node->ownerDocument->saveXML($node);
    }

    protected function findFirstNode($code, $parent, $query)
    {
        $doc = new \DOMDocument();
        $doc->loadHTML("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=" . $code . "\"></head><body>" . $this->getInnerHtml($parent)) . "</body></html>";
        $xpath = new \DOMXpath($doc);
        $objects = $xpath->query($query);
        foreach ($objects as $obj) {
            return $obj;
        }
        return null;
    }

    protected function getAttributeValue($node, $name)
    {
        if (is_null($node)) {
            return null;
        }
        $attribute = $node->attributes->getNamedItem($name);
        if (is_null($attribute)) {
            return null;
        }
        return $attribute->value;
    }

}
