Collection.php 10.7 KB
Newer Older
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
<?php

/**
 * EasyRdf
 *
 * LICENSE
 *
 * Copyright (c) 2013 Nicholas J Humfrey.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author 'Nicholas J Humfrey" may be used to endorse or
 *    promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * @package    EasyRdf
 * @copyright  Copyright (c) 2013 Nicholas J Humfrey
 * @license    http://www.opensource.org/licenses/bsd-license.php
 */

/**
 * Sub-class of EasyRdf_Resource that represents an RDF collection (rdf:List)
 *
 * This class can be used to iterate through a collection of items.
 *
 * Note that items are numbered from 1 (not 0) for consistency with RDF Containers.
 *
 * @package    EasyRdf
 * @link       http://www.w3.org/TR/xmlschema-2/#date
 * @copyright  Copyright (c) 2013 Nicholas J Humfrey
 * @license    http://www.opensource.org/licenses/bsd-license.php
 */
class EasyRdf_Collection extends EasyRdf_Resource implements ArrayAccess, Countable, SeekableIterator
{
    private $position;
    private $current;

    /** Create a new collection - do not use this directly
     *
     * @ignore
     */
    public function __construct($uri, $graph)
    {
        $this->position = 1;
        $this->current = null;
        parent::__construct($uri, $graph);
    }

    /** Seek to a specific position in the container
     *
     * The first item is postion 1
     *
     * @param  integer  $position     The position in the container to seek to
     * @throws OutOfBoundsException
     */
    public function seek($position)
    {
        if (is_int($position) and $position > 0) {
            list($node, $actual) = $this->getCollectionNode($position);
            if ($actual === $position) {
                $this->position = $actual;
                $this->current = $node;
            } else {
                throw new OutOfBoundsException(
                    "Unable to seek to position $position in the collection"
                );
            }
        } else {
            throw new InvalidArgumentException(
                "Collection position must be a positive integer"
            );
        }
    }

    /** Rewind the iterator back to the start of the collection
     *
     */
    public function rewind()
    {
        $this->position = 1;
        $this->current = null;
    }

    /** Return the current item in the collection
     *
     * @return mixed The current item
     */
    public function current()
    {
        if ($this->position === 1) {
            return $this->get('rdf:first');
        } elseif ($this->current) {
            return $this->current->get('rdf:first');
        }
    }

    /** Return the key / current position in the collection
     *
     * Note: the first item is number 1
     *
     * @return int The current position
     */
    public function key()
    {
        return $this->position;
    }

    /** Move forward to next item in the collection
     *
     */
    public function next()
    {
        if ($this->position === 1) {
            $this->current = $this->get('rdf:rest');
        } elseif ($this->current) {
            $this->current = $this->current->get('rdf:rest');
        }
        $this->position++;
    }

    /** Checks if current position is valid
     *
     * @return bool True if the current position is valid
     */
    public function valid()
    {
        if ($this->position === 1 and $this->hasProperty('rdf:first')) {
            return true;
        } elseif ($this->current !== null and $this->current->hasProperty('rdf:first')) {
            return true;
        } else {
            return false;
        }
    }

    /** Get a node for a particular offset into the collection
     *
     * This function may not return the item you requested, if
     * it does not exist. Please check the $postion parameter
     * returned.
     *
     * If the offset is null, then the last node in the
     * collection (before rdf:nil) will be returned.
     *
     * @param  integer $offset          The offset into the collection (or null)
     * @return array   $node, $postion  The node object and postion of the node
     */
    public function getCollectionNode($offset)
    {
        $position = 1;
        $node = $this;
        $nil = $this->graph->resource('rdf:nil');
        while (($rest = $node->get('rdf:rest')) and $rest !== $nil and (is_null($offset) or ($position < $offset))) {
            $node = $rest;
            $position++;
        }
        return array($node, $position);
    }

    /** Counts the number of items in the collection
     *
     * Note that this is an slow method - it is more efficient to use
     * the iterator interface, if you can.
     *
     * @return integer The number of items in the collection
     */
    public function count()
    {
        // Find the end of the collection
        list($node, $position) = $this->getCollectionNode(null);
        if (!$node->hasProperty('rdf:first')) {
            return 0;
        } else {
            return $position;
        }
    }

    /** Append an item to the end of the collection
     *
     * @param  mixed $value      The value to append
     * @return integer           The number of values appended (1 or 0)
     */
    public function append($value)
    {
        // Find the end of the collection
        list($node, $position) = $this->getCollectionNode(null);
        $rest = $node->get('rdf:rest');

        if ($node === $this and is_null($rest)) {
            $node->set('rdf:first', $value);
            $node->addResource('rdf:rest', 'rdf:nil');
        } else {
            $new = $this->graph->newBnode();
            $node->set('rdf:rest', $new);
            $new->add('rdf:first', $value);
            $new->addResource('rdf:rest', 'rdf:nil');
        }

        return 1;
    }

    /** Array Access: check if a position exists in collection using array syntax
     *
     * Example: isset($list[2])
     */
    public function offsetExists($offset)
    {
        if (is_int($offset) and $offset > 0) {
            list($node, $position) = $this->getCollectionNode($offset);
            return ($node and $position === $offset and $node->hasProperty('rdf:first'));
        } else {
            throw new InvalidArgumentException(
                "Collection offset must be a positive integer"
            );
        }
    }

    /** Array Access: get an item at a specified position in collection using array syntax
     *
     * Example: $item = $list[2];
     */
    public function offsetGet($offset)
    {
        if (is_int($offset) and $offset > 0) {
            list($node, $position) = $this->getCollectionNode($offset);
            if ($node and $position === $offset) {
                return $node->get('rdf:first');
            }
        } else {
            throw new InvalidArgumentException(
                "Collection offset must be a positive integer"
            );
        }
    }

    /**
     * Array Access: set an item at a positon in collection using array syntax
     *
     * Example: $list[2] = $item;
     */
    public function offsetSet($offset, $value)
    {
        if (is_null($offset)) {
            // No offset - append to end of collection
            $this->append($value);
        } elseif (is_int($offset) and $offset > 0) {
            list($node, $position) = $this->getCollectionNode($offset);

            // Create nodes, if they are missing
            while ($position < $offset) {
                $new = $this->graph->newBnode();
                $node->set('rdf:rest', $new);
                $new->addResource('rdf:rest', 'rdf:nil');
                $node = $new;
                $position++;
            }

            // Terminate the list
            if (!$node->hasProperty('rdf:rest')) {
                $node->addResource('rdf:rest', 'rdf:nil');
            }

            return $node->set('rdf:first', $value);
        } else {
            throw new InvalidArgumentException(
                "Collection offset must be a positive integer"
            );
        }
    }

    /**
     * Array Access: delete an item at a specific postion using array syntax
     *
     * Example: unset($seq[2]);
     */
    public function offsetUnset($offset)
    {
        if (is_int($offset) and $offset > 0) {
            list($node, $position) = $this->getCollectionNode($offset);
        } else {
            throw new InvalidArgumentException(
                "Collection offset must be a positive integer"
            );
        }

        // Does the item exist?
        if ($node and $position === $offset) {
            $nil = $this->graph->resource('rdf:nil');
            if ($position === 1) {
                $rest = $node->get('rdf:rest');
                if ($rest and $rest !== $nil) {
                    // Move second value, so we can keep the head of list
                    $node->set('rdf:first', $rest->get('rdf:first'));
                    $node->set('rdf:rest', $rest->get('rdf:rest'));
                    $rest->delete('rdf:first');
                    $rest->delete('rdf:rest');
                } else {
                    // Just remove the value
                    $node->delete('rdf:first');
                    $node->delete('rdf:rest');
                }
            } else {
                // Remove the value and re-link the list
                $node->delete('rdf:first');
                $rest = $node->get('rdf:rest');
                $previous = $node->get('^rdf:rest');
                if (is_null($rest)) {
                    $rest = $nil;
                }
                if ($previous) {
                    $previous->set('rdf:rest', $rest);
                }
            }
        }
    }
}