Skip to content
This repository
branch: master
Nicholas Humfrey March 04, 2013
file 92 lines (81 sloc) 2.557 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
<?php
    /**
* Mapping an RDF class type to a PHP Class
*
* This example fetches and displays artist information from the
* BBC Music website. The artist object is an instance of the
* Model_MusicArtist class, so it is possible to call custom PHP
* methods on the object.
*
* It also demonstrates setting new namespaces.
*
* @package EasyRdf
* @copyright Copyright (c) 2009-2013 Nicholas J Humfrey
* @license http://unlicense.org/
*/

    set_include_path(get_include_path() . PATH_SEPARATOR . '../lib/');
    require_once "EasyRdf.php";
    require_once "html_tag_helpers.php";


    class Model_MusicArtist extends EasyRdf_Resource
    {
        function birthEvent()
        {
            foreach ($this->all('bio:event') as $event) {
                if (in_array('bio:Birth', $event->types())) {
                    return $event;
                }
            }
            return null;
        }

        function age()
        {
            $birth = $this->birthEvent();
            if ($birth) {
                $year = substr($birth->get('bio:date'), 0, 4);
                if ($year) {
                    return date('Y') - $year;
                }
            }
            return 'unknown';
        }
    }

    ## Add namespaces
    EasyRdf_Namespace::set('mo', 'http://purl.org/ontology/mo/');
    EasyRdf_Namespace::set('bio', 'http://purl.org/vocab/bio/0.1/');
    EasyRdf_TypeMapper::set('mo:MusicArtist', 'Model_MusicArtist');
?>
<html>
<head><title>EasyRdf Artist Info Example</title></head>
<body>
<h1>EasyRdf Artist Info Example</h1>

<?= form_tag() ?>
<?= text_field_tag('uri', 'http://www.bbc.co.uk/music/artists/70248960-cb53-4ea4-943a-edb18f7d336f.rdf', array('size'=>50)) ?>
<?= submit_tag() ?>
<?= form_end_tag() ?>

<?php
    if (isset($_REQUEST['uri'])) {
        $graph = EasyRdf_Graph::newAndLoad($_REQUEST['uri']);
        $artist = $graph->primaryTopic();
    }

    if (isset($artist)) {
?>

<dl>
<dt>Artist Name:</dt><dd><?= $artist->get('foaf:name') ?></dd>
<dt>Type:</dt><dd><?= join(', ', $artist->types()) ?></dd>
<dt>Homepage:</dt><dd><?= link_to($artist->get('foaf:homepage')) ?></dd>
<dt>Wikipedia page:</dt><dd><?= link_to($artist->get('mo:wikipedia')) ?></dd>
<?php
        if ($artist->isA('mo:SoloMusicArtist')) {
            echo " <dt>Age:</dt>";
            echo " <dd>".$artist->age()."</dd>\n";
        }
    ?>
</dl>
<?php
    }

    if (isset($graph)) {
        echo $graph->dump();
    }
?>
</body>
</html>
Something went wrong with that request. Please try again.