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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
<?php
/*
* The MIT License
*
* @author Brian Dayhoff <mopsyd@me.com>
* @copyright (c) 2017, Brian Dayhoff <mopsyd@me.com> all rights reserved.
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace oroboros\core\traits\libraries\container;
/**
* <Collection Trait>
* Represents a collection of other assets.
* @see http://www.php-fig.org/psr/psr-11/
* @satisfies \oroboros\core\interfaces\contract\libraries\container\CollectionContract
* @author Brian Dayhoff <mopsyd@me.com>
*/
trait CollectionTrait
{
use \oroboros\core\traits\libraries\container\ContainerTrait;
/**
* Whether or not the container is initialized
* @var bool
*/
private $_is_initialized = false;
/**
* The current iterator pointer index
* @var int
*/
private $_collection_pointer = 0;
/**
* The possible iterator pointer indices
* @var array
*/
private $_collection_pointer_keys = array();
/**
* -------------------------------------------------------------------------
* Contract Methods
*
* These methods satisfy the public api defined in the collection contract
*
* @satisfies \oroboros\core\interfaces\contract\libraries\container\CollectionContract
*
* -------------------------------------------------------------------------
*/
/**
* <Collection Constructor>
* Provides a means of setting multiple values on object instantiation.
* @param array $params
*/
public function __construct( array $params = null )
{
$this->_init( $params );
}
/**
* <Isset Magic Method>
* Provides compatibility with the PHP native isset() function.
* @param scalar $name
* @return bool
*/
public function __isset( $name )
{
return $this->_check( $name );
}
/**
* <Unset Magic Method>
* Provides compatibility with the PHP native unset() function.
* @param scalar $name
* @return void
*/
public function __unset( $name )
{
$this->_unsetRegistryIndex( $name );
}
/**
* <Set Magic Method>
* Provides compatibility with the PHP native setter language construct.
* @param scalar $name
* @param mixed $value
* @return void
*/
public function __set( $name, $value )
{
$set = $this->_set( $name, $value );
$values = $this->_fetchRegistry();
if ( isset( $this->_collection_pointer_keys[$this->_collection_pointer] ) )
{
//This approach is necessary because the prior
//array_search method was spamming notices when the value was an object.
$key = null;
foreach ( $values as
$k =>
$v )
{
if ( $k === $this->_collection_pointer_keys[$this->_collection_pointer] )
{
$key = $k;
break;
}
}
$this->_collection_pointer_keys = array_keys( $values );
$this->_collection_pointer = array_search( $key,
$this->_collection_pointer_keys );
} else
{
$this->_collection_pointer_keys = array_keys( $values );
}
}
/**
* <Get Magic Method>
* Provides compatibility with the PHP native getter language construct.
* @param scalar $name
* @return mixed
*/
public function __get( $name )
{
return $this->_get( $name );
}
/**
* <Collection Clear>
* Resets the collection to an empty state.
* @return void
*/
public function clear()
{
$this->_resetRegistry();
}
/**
* <Collection Copy>
* Returns a new instance of the same object this trait is instantiated on.
* @return \oroboros\core\traits\libraries\container\CollectionTrait
*/
public function copy()
{
$class = get_class( $this );
$object = new $class();
foreach ( $this->toArray() as
$key =>
$value )
{
$object->$key = $value;
}
return $object;
}
/**
* <Collection Count>
* Returns a count of the values in the collection.
* The $mode parameter does nothing, due to a native PHP bug that
* prevents recursive counting from working on instances of \Countable,
* but it must be provided to honor the interface.
* @param int $mode Currently does nothing due to a native PHP bug
* @return int
*/
public function count( $mode = COUNT_NORMAL )
{
if ( $mode == COUNT_NORMAL )
{
return $this->_countRegistryKeys();
}
return count( $this->_fetchRegistry(), COUNT_RECURSIVE );
}
/**
* <Iterator Current>
* Gets the current iterable index, provided to honor
* the PHP native \Iterable interface.
* @return mixed
*/
public function current()
{
return $this->_get( $this->_collection_pointer_keys[$this->_collection_pointer] );
}
/**
* <Empty Check>
* Returns a boolean determination as to whether the collection is empty.
* @return bool
*/
public function isEmpty()
{
return $this->_countRegistryKeys() === 0;
}
/**
* <Iterator Key>
* Provided for compatibility with the PHP native \Iterator interface.
* Returns the current key assigned to the pointer.
* @return scalar
*/
public function key()
{
return $this->_collection_pointer_keys[$this->_collection_pointer];
}
/**
* <Json Serializable>
* Provided for compatibilibty with the PHP native \JsonSerializable interface.
* @return array
*/
public function jsonSerialize()
{
return $this->toArray();
}
/**
* <Iterator Next>
* Provided for compatibility with the PHP native \Iterator interface.
* Increments the pointer, returns false if it has reached
* the end of iterable values.
* @return void|bool
*/
public function next()
{
if ( $this->_collection_pointer >= count( $this->_collection_pointer_keys ) )
{
return false;
}
$this->_collection_pointer++;
}
/**
* <Array Access Offset Check>
* Provided for compatibility with the PHP native \ArrayAccess interface.
* Returns a boolean determination as to whether the specified offset exists.
* @param scalar $offset
* @return bool
*/
public function offsetExists( $offset )
{
return $this->_check( $offset );
}
/**
* <Array Access Offset Getter>
* Provided for compatibility with the PHP native \ArrayAccess interface.
* Returns the value for the specified offset.
* @param scalar $offset
* @return mixed
*/
public function offsetGet( $offset )
{
return $this->_get( $offset );
}
/**
* <Array Access Offset Setter>
* Provided for compatibility with the PHP native \ArrayAccess interface.
* Sets the specified value to the specified offset.
* @param scalar $offset
* @param mixed $value
*/
public function offsetSet( $offset, $value )
{
$this->_set( $offset, $value );
}
/**
* <Array Access Unsetter>
* Provided for compatibility with the PHP native \ArrayAccess interface.
* Unsets the specified offset.
* @param scalar $offset
* @return void
*/
public function offsetUnset( $offset )
{
$this->_unsetRegistryIndex( $offset );
}
/**
* <Iterator Rewind>
* Provided for compatibility with the PHP native \Iterator interface.
* Rewinds the iterator. Called natively when loops begin.
* @return void
*/
public function rewind()
{
$this->_collection_pointer = 0;
}
/**
* <Collection Array Method>
* Returns an array of the values in the collection.
* @return array
*/
public function toArray()
{
return $this->_fetchRegistry();
}
/**
* <Iterator Valid>
* Provided for compatibility with the PHP native \Iterator interface.
* Returns a boolean determination as to whether a specified
* pointer key exists.
* @return bool
*/
public function valid()
{
if ( array_key_exists( $this->_collection_pointer,
$this->_collection_pointer_keys ) )
{
return $this->_check( $this->_collection_pointer_keys[$this->_collection_pointer] );
}
return false;
}
/**
* -------------------------------------------------------------------------
* Extension Methods (protected)
*
* These methods may be extended by inheriting constructs as needed.
* They represent the interal api.
* -------------------------------------------------------------------------
*/
/**
* <Collection Initialization Method>
* Provides an alternative initialization method if another trait
* needs to provide a different constructor method.
* @param array $params
*/
protected function _initializeCollection( array $params = null )
{
$this->_init( $params );
}
/**
* -------------------------------------------------------------------------
* Logic Methods (private)
*
* These methods are not externally exposed.
* They represent the actual work.
* -------------------------------------------------------------------------
*/
/**
* <Private Initialization Method>
* This is the real initialization method.
* It insures that core initialization logic is not overridden,
* and backreference always points to this trait on execution.
* @param type $params
* @param type $flags
*/
private function _init( array $params = null )
{
$this->_initializeRegistry();
if ( is_array( $params ) )
{
foreach ( $params as
$key =>
$value )
{
$this->__set( $key, $value );
}
}
$this->_is_initialized = true;
}
/**
* <Private Auto Initialization Method>
* This method will initialize the trait only
* if it was not already initialized.
* @return void
*/
private function _autoInit()
{
if ( !$this->_is_initialized )
{
$this->_init();
}
}
}