2207956599574fccaf52af7945e8f23c7b0d5598
[yaffs-website] / vendor / easyrdf / easyrdf / lib / EasyRdf / Container.php
1 <?php
2
3 /**
4  * EasyRdf
5  *
6  * LICENSE
7  *
8  * Copyright (c) 2013 Nicholas J Humfrey.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  *    this list of conditions and the following disclaimer in the documentation
16  *    and/or other materials provided with the distribution.
17  * 3. The name of the author 'Nicholas J Humfrey" may be used to endorse or
18  *    promote products derived from this software without specific prior
19  *    written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * @package    EasyRdf
34  * @copyright  Copyright (c) 2013 Nicholas J Humfrey
35  * @license    http://www.opensource.org/licenses/bsd-license.php
36  */
37
38 /**
39  * Sub-class of EasyRdf_Resource that represents an RDF container
40  * (rdf:Alt, rdf:Bag and rdf:Seq)
41  *
42  * This class can be used to iterate through a list of items.
43  *
44  * @package    EasyRdf
45  * @link       http://www.w3.org/TR/xmlschema-2/#date
46  * @copyright  Copyright (c) 2013 Nicholas J Humfrey
47  * @license    http://www.opensource.org/licenses/bsd-license.php
48  */
49 class EasyRdf_Container extends EasyRdf_Resource implements ArrayAccess, Countable, SeekableIterator
50 {
51     private $position;
52
53     /** Create a new container - do not use this directly
54      *
55      * @ignore
56      */
57     public function __construct($uri, $graph)
58     {
59         $this->position = 1;
60         parent::__construct($uri, $graph);
61     }
62
63     /** Seek to a specific position in the container
64      *
65      * The first item is postion 1
66      *
67      * @param  integer  $position     The position in the container to seek to
68      * @throws OutOfBoundsException
69      */
70     public function seek($position)
71     {
72         if (is_int($position) and $position > 0) {
73             if ($this->hasProperty('rdf:_'.$position)) {
74                 $this->position = $position;
75             } else {
76                 throw new OutOfBoundsException(
77                     "Unable to seek to position $position in the container"
78                 );
79             }
80         } else {
81             throw new InvalidArgumentException(
82                 "Container position must be a positive integer"
83             );
84         }
85     }
86
87     /** Rewind the iterator back to the start of the container (item 1)
88      *
89      */
90     public function rewind()
91     {
92         $this->position = 1;
93     }
94
95     /** Return the current item in the container
96      *
97      * @return mixed The current item
98      */
99     public function current()
100     {
101         return $this->get('rdf:_'.$this->position);
102     }
103
104     /** Return the key / current position in the container
105      *
106      * @return int The current position
107      */
108     public function key()
109     {
110         return $this->position;
111     }
112
113     /** Move forward to next item in the container
114      *
115      */
116     public function next()
117     {
118         $this->position++;
119     }
120
121     /** Checks if current position is valid
122      *
123      * @return bool True if the current position is valid
124      */
125     public function valid()
126     {
127         return $this->hasProperty('rdf:_'.$this->position);
128     }
129
130     /** Counts the number of items in the container
131      *
132      * Note that this is an slow method - it is more efficient to use
133      * the iterator interface, if you can.
134      *
135      * @return integer The number of items in the container
136      */
137     public function count()
138     {
139         $pos = 1;
140         while ($this->hasProperty('rdf:_'.$pos)) {
141             $pos++;
142         }
143         return $pos - 1;
144     }
145
146     /** Append an item to the end of the container
147      *
148      * @param  mixed $value      The value to append
149      * @return integer           The number of values appended (1 or 0)
150      */
151     public function append($value)
152     {
153         // Find the end of the list
154         $pos = 1;
155         while ($this->hasProperty('rdf:_'.$pos)) {
156             $pos++;
157         }
158
159         // Add the item
160         return $this->add('rdf:_'.$pos, $value);
161     }
162
163     /** Array Access: check if a position exists in container using array syntax
164      *
165      * Example: isset($seq[2])
166      */
167     public function offsetExists($offset)
168     {
169         if (is_int($offset) and $offset > 0) {
170             return $this->hasProperty('rdf:_'.$offset);
171         } else {
172             throw new InvalidArgumentException(
173                 "Container position must be a positive integer"
174             );
175         }
176     }
177
178     /** Array Access: get an item at a specified position in container using array syntax
179      *
180      * Example: $item = $seq[2];
181      */
182     public function offsetGet($offset)
183     {
184         if (is_int($offset) and $offset > 0) {
185             return $this->get('rdf:_'.$offset);
186         } else {
187             throw new InvalidArgumentException(
188                 "Container position must be a positive integer"
189             );
190         }
191     }
192
193     /**
194      * Array Access: set an item at a positon in container using array syntax
195      *
196      * Example: $seq[2] = $item;
197      *
198      * Warning: creating gaps in the sequence will result in unexpected behavior
199      */
200     public function offsetSet($offset, $value)
201     {
202         if (is_int($offset) and $offset > 0) {
203             return $this->set('rdf:_'.$offset, $value);
204         } elseif (is_null($offset)) {
205             return $this->append($value);
206         } else {
207             throw new InvalidArgumentException(
208                 "Container position must be a positive integer"
209             );
210         }
211     }
212
213     /**
214      * Array Access: delete an item at a specific postion using array syntax
215      *
216      * Example: unset($seq[2]);
217      *
218      * Warning: creating gaps in the sequence will result in unexpected behavior
219      */
220     public function offsetUnset($offset)
221     {
222         if (is_int($offset) and $offset > 0) {
223             return $this->delete('rdf:_'.$offset);
224         } else {
225             throw new InvalidArgumentException(
226                 "Container position must be a positive integer"
227             );
228         }
229     }
230 }