PHP code example of miqwit / dedex

1. Go to this page and download the library: Download miqwit/dedex library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

miqwit / dedex example snippets


use DedexBundle\Controller\ErnParserController;
use DedexBundle\Simplifiers\SimpleAlbum;

$xml_path = "tests/Ern382/samples/with_assets/complete_flac/1199119911991.xml";
$parser = new ErnParserController();
$ern = $parser->parse($xml_path);
$album = new SimpleAlbum($ern);

// Then you can access high level property of this album
$release_date = $album->getOriginalReleaseDate();
$artists_at_album_level = $album->getArtists();
$tracks_for_cd_1 = $album->getTracksPerCd()[1];

// You can also access exact XML mapping by getting 
// ddex objects at album or track level
$ddex_release = $album->getDdexRelease();
$ddex_release->getReleaseId()[0]->getICPN()->value(); // direct access
$ddex_track = $tracks_for_cd_1[1]->getDdexSoundRecording()->getDuration(); // direct access

use DedexBundle\Controller\ErnParserController;

$xml_path = "tests/Ern382/samples/audioalbum_complete.xml";
$parser = new ErnParserController();
$ern = $parser->parse($xml_path);

// Then you can access properties of the ERN
$created_at = $ern->getMessageHeader()->getMessageCreatedDateTime();

use DedexBundle\Controller\ErnParserController;

$xml_path = "tests/Ern382/samples/audioalbum_complete.xml";
$parser = new ErnParserController();
$parser->setXsdValidation(true);
$ern = $parser->parse($xml_path);

use DedexBundle\Controller\ErnParserController;
use DedexBundle\Rule\AtLeastOneImage;
use DedexBundle\Exception\RuleValidationException;

$xml_path = "tests/Ern382/samples/audioalbum_complete.xml";
$parser = new ErnParserController();
$parser->addRule(new AtLeastOneImage(Rule::LEVEL_ERROR));
// ... can add multiple rules one by one
// ... or multiple rules with $parser->addRuleSet([])
$ern = $parser->parse($xml_path);  // will raise an RuleValidationException if rule is broken

use DedexBundle\Controller\ErnParserController;
use DedexBundle\Rule\RuleSet;

$parser = new ErnParserController();
$parser->setThrowOnRuleError(false);  // parse() returns the message even if an ERROR rule fails
$parser->addRuleSet(RuleSet::MUSICAL_ALBUM("path/to/assets"));
$ern = $parser->parse($xml_path);

foreach ($parser->getRuleResults() as $result) {
  echo $result->getRule()->getName(), ": ", $result->getStatus(), "\n";  // passed, failed or skipped
  foreach ($result->getViolations() as $violation) {
    echo "  ", $violation, "\n";                 // WARNING: Resource A9 is not used in any release
    echo "  ", $violation->getSubject(), "\n";   // A9
    echo "  ", $violation->getPath(), "\n";      // ResourceList/Image[2]
  }
}

use DedexBundle\Rule\Rule;

class AllSoundRecordingsHaveDuration extends Rule {
  protected $message = "All SoundRecordings must have a duration.";

  public function validates($newReleaseMessage): bool {
    $this->resetMessage();
    foreach (array_values($newReleaseMessage->getResourceList()->getSoundRecording()) as $i => $sr) {
      if ($sr->getDuration() === null) {
        $ref = $sr->getResourceReference();
        $this->fail("SoundRecording $ref has no duration", $ref, sprintf("ResourceList/SoundRecording[%d]", $i + 1));
      }
    }
    return count($this->getViolations()) === 0;
  }
}

$parser->setDisplayLog(true);

$parser->setXsdValidation(true);

$parser->getRuleMessages();

$parser->getRuleResults();

$parser->setThrowOnRuleError(false);

$parser->addRuleSet();