| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | /* RDFInt.php - RDF Interfaces for PHP
|
|---|
| 4 | * Copyright 2011 netlabs.org
|
|---|
| 5 | * Author: Christian Langanke, Adrian Gschwend
|
|---|
| 6 | *
|
|---|
| 7 | * Licensed under the Apache License, Version 2.0 (the "License");
|
|---|
| 8 | * you may not use this file except in compliance with the License.
|
|---|
| 9 | * You may obtain a copy of the License at
|
|---|
| 10 | *
|
|---|
| 11 | * http://www.apache.org/licenses/LICENSE-2.0
|
|---|
| 12 | *
|
|---|
| 13 | * Unless required by applicable law or agreed to in writing, software
|
|---|
| 14 | * distributed under the License is distributed on an "AS IS" BASIS,
|
|---|
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|---|
| 16 | * See the License for the specific language governing permissions and
|
|---|
| 17 | * limitations under the License.
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 | namespace rdfa;
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * \class Projection
|
|---|
| 24 | * \brief This class implements a projection as described in the RDF API and RDFa API specs of W3C.
|
|---|
| 25 | * \details A projection relates to a specific subject of RDF data parsed by an instance of
|
|---|
| 26 | * the rdfa::Data class and is to be created by the projection API methods this class only.
|
|---|
| 27 | * \note Non-standard methods are marked as library specific extensions
|
|---|
| 28 | * and the methodname is prepended by a single underscore character.
|
|---|
| 29 | * For further information on differences to the W3C specifications,
|
|---|
| 30 | * see the section
|
|---|
| 31 | * \htmllink{page_w3cspecs.html,W3C Specifications Overview and Library Compatibility}
|
|---|
| 32 | * \author Christian Langanke
|
|---|
| 33 | * \author Adrian Gschwend
|
|---|
| 34 | * \date 2011
|
|---|
| 35 | */
|
|---|
| 36 | class Projection {
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * Version of the class
|
|---|
| 40 | */
|
|---|
| 41 | const version = '1.0.0';
|
|---|
| 42 | /**
|
|---|
| 43 | * Name of the fDebug context
|
|---|
| 44 | */
|
|---|
| 45 | const debugcontext = 'RDFA_PROJECTION';
|
|---|
| 46 |
|
|---|
| 47 | private $debugger;
|
|---|
| 48 | private $rdfaData;
|
|---|
| 49 | private $subject;
|
|---|
| 50 |
|
|---|
| 51 | // ---------------------------------------
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * Creates a Projection instance from a Data instance, reflecting a given subject in the triple data.
|
|---|
| 55 | *
|
|---|
| 56 | * \param rdfaData instance of rdfa::Data holding the graph of the subject
|
|---|
| 57 | * \param subject Subject of the projection
|
|---|
| 58 | * \param template Associative array( URI/CURIE => membername) as a template to be applied to the projection object
|
|---|
| 59 | *
|
|---|
| 60 | * \exception Exception The parameter rdfaData is not of object rdfa::Data
|
|---|
| 61 | *
|
|---|
| 62 | * \note A projection is to be created by the following methods only:
|
|---|
| 63 | * - rdfa::Data::getProjection()
|
|---|
| 64 | * - rdfa::Data::getProjections()
|
|---|
| 65 | * - rdfa::Data::query()
|
|---|
| 66 | */
|
|---|
| 67 | public function __construct( $rdfaData, $subject, $template) {
|
|---|
| 68 |
|
|---|
| 69 | // setup debugger
|
|---|
| 70 | $this->debugger = \fDebug::getInstance();
|
|---|
| 71 |
|
|---|
| 72 | // check for required class
|
|---|
| 73 | if ((!is_object( $rdfaData)) ||
|
|---|
| 74 | ('\\' . get_class( $rdfaData) != '\\rdfa\\Data'))
|
|---|
| 75 | throw new \Exception( 'Invalid Parameter: $rdfdata is not object of class \rdfa\Data.');
|
|---|
| 76 |
|
|---|
| 77 | // initialize
|
|---|
| 78 | $this->rdfaData = $rdfaData;
|
|---|
| 79 | $this->subject = $subject;
|
|---|
| 80 |
|
|---|
| 81 | $debugmessage = "Creating projection for subject: $subject";
|
|---|
| 82 |
|
|---|
| 83 | // create public members from template
|
|---|
| 84 | if (($template != Null) && (is_array( $template))) {
|
|---|
| 85 | $debugmessage .= "\nApplying template:\n";
|
|---|
| 86 | foreach ( $template as $key => $uri) {
|
|---|
| 87 | $value = $this->get( $uri);
|
|---|
| 88 | if ($value != false) {
|
|---|
| 89 | $this->$key = $value;
|
|---|
| 90 | $debugmessage .= "$key=$value\n";
|
|---|
| 91 | } else {
|
|---|
| 92 | $debugmessage .= "### not found: $key\n";
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | $this->debugger->sendMessage( $debugmessage,
|
|---|
| 98 | self::debugcontext);
|
|---|
| 99 |
|
|---|
| 100 | } // public function __construct
|
|---|
| 101 |
|
|---|
| 102 | // --------------------------------------------------------
|
|---|
| 103 |
|
|---|
| 104 | /**
|
|---|
| 105 | * Gets properties of the subject of the projection.
|
|---|
| 106 | *
|
|---|
| 107 | * \retval array List of properties
|
|---|
| 108 | */
|
|---|
| 109 | public function getProperties() {
|
|---|
| 110 | return $this->rdfaData->getProperties( $this->subject);
|
|---|
| 111 | } // public function getProperties
|
|---|
| 112 |
|
|---|
| 113 | // --------------------------------------------------------
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | * Gets an associative list of unique properties of the projection
|
|---|
| 117 | * with their values.
|
|---|
| 118 | *
|
|---|
| 119 | * \retval array Associative list of unqique properties and their values
|
|---|
| 120 | *
|
|---|
| 121 | * \note
|
|---|
| 122 | * - All non-unique properties are \c NOT returned !
|
|---|
| 123 | * - This method is a library specific extension to the RDF API and RDFa API
|
|---|
| 124 | */
|
|---|
| 125 | public function _getUniqueProperties() {
|
|---|
| 126 | return $this->rdfaData->_getUniqueProperties( $this->subject);
|
|---|
| 127 | } // public function _getUniqueProperties
|
|---|
| 128 |
|
|---|
| 129 | // --------------------------------------------------------
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * Gets the subject of of the subject of the projection.
|
|---|
| 133 | *
|
|---|
| 134 | * \retval string Subject of the property
|
|---|
| 135 | */
|
|---|
| 136 | public function getSubject() {
|
|---|
| 137 | return $this->subject;
|
|---|
| 138 | } // public function getSubject
|
|---|
| 139 |
|
|---|
| 140 | // --------------------------------------------------------
|
|---|
| 141 |
|
|---|
| 142 | /**
|
|---|
| 143 | * Gets the first available value of a given property.
|
|---|
| 144 | *
|
|---|
| 145 | * \param property Property to retrieve the first value of#
|
|---|
| 146 | * \retval string First value of the property
|
|---|
| 147 | */
|
|---|
| 148 |
|
|---|
| 149 | public function get( $property) {
|
|---|
| 150 | return $this->rdfaData->_getFirstValue( $this->subject, $property);
|
|---|
| 151 | } // public function get
|
|---|
| 152 |
|
|---|
| 153 | // --------------------------------------------------------
|
|---|
| 154 |
|
|---|
| 155 | /**
|
|---|
| 156 | * Gets a list of all available values of a given property.
|
|---|
| 157 | *
|
|---|
| 158 | * \param property Property to retrieve all values of
|
|---|
| 159 | */
|
|---|
| 160 |
|
|---|
| 161 | public function getAll( $property) {
|
|---|
| 162 | return $this->rdfaData->getValues( $this->subject, $property);
|
|---|
| 163 | } // public function getAll
|
|---|
| 164 |
|
|---|
| 165 | // --------------------------------------------------------
|
|---|
| 166 |
|
|---|
| 167 | /**
|
|---|
| 168 | * Sets a property value.
|
|---|
| 169 | *
|
|---|
| 170 | * \param property Property to be added
|
|---|
| 171 | * \param value Value to be set for the property
|
|---|
| 172 | * \param type The type of the value, either 'uri', 'literal' or 'bnode'
|
|---|
| 173 | * (case-insensitive). Specifying the first character is sufficient.
|
|---|
| 174 | * If not specified, strings starting with 'http://' are taken as a URI,
|
|---|
| 175 | * strings starting with '_:' as a blank node, otherwise as a literal
|
|---|
| 176 | * \retval rdfa::Projection Success
|
|---|
| 177 | * \retval boolean false: invalid value type specified
|
|---|
| 178 | *
|
|---|
| 179 | * \see rdfa::Data::_setValue
|
|---|
| 180 | *
|
|---|
| 181 | * \note
|
|---|
| 182 | * - This method is a library specific extension to the RDFa API
|
|---|
| 183 | * - The parameter type is a library specific extension to the RDF API
|
|---|
| 184 | */
|
|---|
| 185 |
|
|---|
| 186 | public function set( $property, $value, $type = NULL) {
|
|---|
| 187 |
|
|---|
| 188 | $ret = $this->rdfaData->_setValue($this->subject, $property, $value, $type );
|
|---|
| 189 |
|
|---|
| 190 | if($ret)
|
|---|
| 191 | return $this;
|
|---|
| 192 | else
|
|---|
| 193 | return false;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | } // class Projection
|
|---|
| 197 |
|
|---|
| 198 | ?>
|
|---|