Скажем, у меня есть класс PHP, унаследованный от класса DomDocument, и похоже, что код ниже, я пытаюсь реализовать некоторые методы класса. Я думаю, что у меня есть один для подсчета, но я чувствую себя немного потерянным для фильтрации. Некоторые указатели были бы хороши относительно того, как правильно понимать и использовать класс
PHP Код класса
class MovieXML extends \DOMDocument
{
/**
* Returns number of movies in document.
*
* @return int
*/
public function count(): int
{
/**
* @todo Implement it
*/
$document = new static('1.0', 'UTF-8');
$movies = $document->getElementsByTagName('movie');
return $movies->length;
}
/**
* Returns new MovieXML object with only movies created in given year.
*
* @param int $year
* @return MovieXML
*/
public function filterByYear(int $year): MovieXML
{
/**
* @todo Implement it
*/
$movies = [];
return $this->createNewDocumentWithMovies($movies);
}
/**
* Returns new MovieXML object with only movies with actors containing substring $name in their names.
*
* @param string $name
* @return MovieXML
*/
public function filterByActor(string $name): MovieXML
{
/**
* @todo Implement it
*/
$movies = [];
return $this->createNewDocumentWithMovies($movies);
}
/**
* Returns new MovieXML object with only movies marked as series.
*
* @return MovieXML
*/
public function filterSeries(): MovieXML
{
/**
* @todo Implement it
*/
$movies = [];
return $this->createNewDocumentWithMovies($movies);
}
/**
* @param array $movies
* @return MovieXML
*/
protected function createNewDocumentWithMovies(array $movies): MovieXML
{
$document = new static('1.0', 'UTF-8');
$moviesElement = $document->createElement('movies');
foreach ($movies as $movie) {
$singleMovieElement = $document->createElement('movie');
$moviesElement->appendChild($singleMovieElement);
}
$document->saveXML();
return $document;
// $document->loadXML('<movies></movies>');
// return $document;
/**
* @todo Implement it, import movies to document
*/
// $xml = new \SimpleXMLElement('<movies/>');
// array_walk_recursive($movies, array($xml, 'addChild'));
// return $xml->asXML();
}
protected function Array2Dom($array, $doc = null)
{
if ($doc == null) {
$doc = new \DOMDocument();
$doc->formatOutput = true;
$currentNode = $doc;
} else {
if ($array['_type'] == '_text')
$currentNode = $doc->createTextNode($array['_content']);
else
$currentNode = $doc->createElement($array['_type']);
}
if ($array['_type'] != '_text') {
if (isset($array['_attributes'])) {
foreach ($array['_attributes'] as $name => $value) {
$currentNode->setAttribute($name, $value);
}
}
if (isset($array['_children'])) {
foreach ($array['_children'] as $child) {
$childNode = $this->Array2Dom($child, $doc);
$childNode = $currentNode->appendChild($childNode);
}
}
}
return $currentNode;
}
}
XML код для генерации или работы с
<?xml version="1.0" encoding="UTF-8" ?>
<movies>
<movie>
<name>Guardians of the Galaxy</name>
<year>2014</year>
<actors>
<actor>
<name>Chris Pratt</name>
</actor>
<actor>
<name>Vin Diesel</name>
</actor>
<actor>
<name>Bradley Cooper</name>
</actor>
</actors>
</movie>
<movie>
<name>Interstellar</name>
<year>2014</year>
<actors>
<actor>
<name>Matthew McConaughey</name>
</actor>
<actor>
<name>Anne Hathaway</name>
</actor>
<actor>
<name>Jessica Chastain</name>
</actor>
</actors>
</movie>
<movie>
<name>Sniper</name>
<year>2014</year>
<actors>
<actor>
<name>Bradley Cooper</name>
</actor>
<actor>
<name>Sienna Miller</name>
</actor>
<actor>
<name>Kyle Gallner</name>
</actor>
</actors>
</movie>
<movie serie="yes">
<name>Hobbit: Battle of five armies</name>
<year>2014</year>
<actors>
<actor>
<name>Ian McKellen</name>
</actor>
<actor>
<name>Martin Freeman</name>
</actor>
<actor>
<name>Richard Armitage</name>
</actor>
</actors>
</movie>
<movie serie="yes">
<name>X-Men: Future</name>
<year>2014</year>
<actors>
<actor>
<name>Patrick Stewart</name>
</actor>
<actor>
<name>Ian McKellen</name>
</actor>
<actor>
<name>Hugh Jackman</name>
</actor>
</actors>
</movie>
<movie serie="yes">
<name>Spectre</name>
<year>2015</year>
<actors>
<actor>
<name>Daniel Craig</name>
</actor>
<actor>
<name>Christoph Waltz</name>
</actor>
</actors>
</movie>
<movie serie="yes">
<name>Star Wars: The force awakens</name>
<year>2015</year>
<actors>
<actor>
<name>Harrison Ford</name>
</actor>
<actor>
<name>Mark Hammill</name>
</actor>
<actor>
<name>Carrie Fisher</name>
</actor>
</actors>
</movie>
<movie serie="no">
<name>Pixels</name>
<year>2015</year>
<actors>
<actor>
<name>Adam Sandler</name>
</actor>
<actor>
<name>Andy Samberg</name>
</actor>
<actor>
<name>Selena Gomez</name>
</actor>
</actors>
</movie>
</movies>