/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
* This source code was highlighted with Source Code Highlighter .
- / **
- * Collection class
- * Basic universal type, based on which collections will be created.
- *
- * @author [x26] VOLAND
- * /
- abstract class Collection implements IteratorAggregate, ArrayAccess, Countable {
- / **
- * The type of items stored in this collection.
- * @var string
- * /
- private $ __ type;
- / **
- * Object storage
- * @var array
- * /
- private $ __ collection = array ();
- // ------------------------------------------------ --------------------
- / **
- * Constructor.
- * Sets the type of elements that will be stored in this collection.
- *
- * @param string $ type Element type
- * @return void
- * /
- public function __construct ($ type) {
- $ this -> __ type = $ type;
- }
- // ------------------------------------------------ --------------------
- / **
- * Checks the type of object.
- * Prevents the addition to the collection of objects of someone else’s type.
- *
- * @param object $ object Object to check
- * @return void
- * @throws Exception
- * /
- private function __check_type (& $ object ) {
- if (get_class ($ object )! = $ this -> __ type) {
- throw new Exception ( 'Object of type' ' . get_class ($ object )
- . '`cannot be added to the collection of objects of type' ' . $ this -> __ type. '`' );
- }
- }
- // ------------------------------------------------ --------------------
- / **
- * Adds the objects passed in the arguments to the collection.
- *
- * @param object (s) Objects
- * @return mixed collection
- * /
- public function add ()
- {
- $ args = func_get_args ();
- foreach ($ args as $ object ) {
- $ this -> __ check_type ($ object );
- $ this -> __ collection [] = $ object ;
- }
- return $ this ;
- }
- // ------------------------------------------------ --------------------
- / **
- * Removes from the collection the objects passed in the arguments.
- *
- * @param object (s) Objects
- * @return mixed collection
- * /
- public function remove ()
- {
- $ args = func_get_args ();
- foreach ($ args as $ object ) {
- unset ($ this -> __ collection [array_search ($ object , $ this -> __ collection)]);
- }
- return $ this ;
- }
- // ------------------------------------------------ --------------------
- / **
- * Clears the collection.
- *
- * @return mixed collection
- * /
- public function clear () {
- $ this -> __ collection = array ();
- return $ this ;
- }
- // ------------------------------------------------ --------------------
- / **
- * Determines if the collection is empty.
- *
- * @return bool
- * /
- public function isEmpty () {
- return empty ($ this -> __ collection);
- }
- // ------------------------------------------------ --------------------
- / **
- * IteratorAggregate interface implementation
- * /
- / **
- * Returns an iterator object.
- *
- * @return CollectionIterator
- * /
- public function getIterator () {
- return new CollectionIterator ($ this -> __ collection);
- }
- // ------------------------------------------------ --------------------
- / **
- * Implementation of the ArrayAccess interface.
- * /
- / **
- * Sets an element of collection at the offset
- *
- * @param ineter $ offset Offset
- * @param mixed $ offset Object
- * @return void
- * /
- public function offsetSet ($ offset, $ object ) {
- $ this -> __ check_type ($ object );
- if ($ offset === NULL) {
- $ offset = max (array_keys ($ this -> __ collection)) + 1;
- }
- $ this -> __ collection [$ offset] = $ object ;
- }
- // ------------------------------------------------ --------------------
- / **
- * Find out if an item exists with a given key.
- *
- * @param integer $ offset Key
- * @return bool
- * /
- public function offsetExists ($ offset) {
- return isset ($ this -> __ collection [$ offset]);
- }
- // ------------------------------------------------ --------------------
- / **
- * Removes the item referenced by the $ offset key.
- *
- * @param integer $ offset Key
- * @return void
- * /
- public function offsetUnset ($ offset) {
- unset ($ this -> __ collection [$ offset]);
- }
- // ------------------------------------------------ --------------------
- / **
- * Returns an item by key.
- *
- * @param integer $ offset Key
- * @return mixed
- * /
- public function offsetGet ($ offset) {
- if (isset ($ this -> __ collection [$ offset]) === FALSE) {
- return NULL;
- }
- return $ this -> __ collection [$ offset];
- }
- // ------------------------------------------------ --------------------
- / **
- * Implementing the Countable Interface
- * /
- / **
- * Returns the number of items in the collection.
- *
- * @return integer
- * /
- public function count () {
- return sizeof ($ this -> __ collection);
- }
- }
* This source code was highlighted with Source Code Highlighter .
- <? php
- class BookStore {
- function addBooks (BookCollection $ books) {
- // implementation
- }
- function addMagazines (MagazineCollection $ magazines) {
- // implementation
- }
- function addGoods (Collection $ goods) {
- // If collection type is not important,
- // you can specify the base type of collection
- }
- }
- class Book {
- var $ id;
- function Book ($ id) {
- $ this -> id = $ id;
- }
- }
- class Magazine {
- var $ id;
- function Magazine ($ id) {
- $ this -> id = $ id;
- }
- }
- // Create a collection
- $ books = CollectionFactory :: create ( 'Book' );
- echo get_class ($ books); // BookCollection
- // Add objects to the collection:
- $ books-> add ( new Book (1), new Book (2));
- $ books-> add ( new Book (3)) -> add ( new Book (2));
- $ books [] = new Book (5);
- echo count ($ books); // five
- ...
- foreach ($ books as $ book) {
- echo $ book-> id;
- } // 12345
- ...
- $ books-> add ( new Magazine (1)); // Error (wrong type)
- ...
- $ magazines = CollectionFactory :: create ( 'Magazine' );
- $ magazines-> add ( new Magazine (1));
- ...
- $ bookStore = new BookStore ();
- $ bookStore-> addBooks ($ books); // Everything is good
- $ bookStore-> addBooks ($ magazines); // Error (wrong type)
- $ bookStore-> addMagazines ($ magazines); // Everything is good
- $ bookStore-> addGoods ($ books); // Everything is good
- $ bookStore-> addGoods ($ magazines); // Everything is good
- ?>
Source: https://habr.com/ru/post/64840/
All Articles