├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── doc ├── 00-Installing.md └── 01-Usage.md ├── phpunit.xml.dist └── src ├── lib └── Herrera │ └── Util │ └── ObjectStorage.php └── tests ├── Herrera └── Util │ ├── Test │ ├── ExampleInterface.php │ ├── ExampleObject.php │ └── ExampleStore.php │ └── Tests │ └── ObjectStorageTest.php └── bootstrap.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea/ 2 | /bin/ 3 | /coverage/ 4 | /src/vendors/ 5 | 6 | /composer.lock 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | - 5.5 7 | 8 | before_script: 9 | - composer self-update 10 | - composer install --dev --no-interaction --prefer-source 11 | 12 | script: bin/phpunit 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Kevin Herrera 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 7 | of the Software, and to permit persons to whom the Software is furnished to do 8 | so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Object Storage 2 | ============== 3 | 4 | [![Build Status]](http://travis-ci.org/herrera-io/php-object-storage) 5 | 6 | A simple object storage class that will only allow "supported" objects. 7 | 8 | ```php 9 | class MyObjectStorage extends Herrera\Util\ObjectStorage 10 | { 11 | public function isSupported($object) 12 | { 13 | return ($object instanceof PDO); 14 | } 15 | } 16 | 17 | $store = new MyObjectStorage(); 18 | $pdo = new PDO('dsn...'); 19 | $time = new DateTime(); 20 | 21 | $store->attach($pdo); 22 | $store->attach($time); // throws "UnexpectedValueException" 23 | ``` 24 | 25 | Documentation 26 | ------------- 27 | 28 | - [Installing][] 29 | - [Usage][] 30 | 31 | [Build Status]: https://secure.travis-ci.org/herrera-io/php-object-storage.png?branch=master 32 | [Installing]: doc/00-Installing.md 33 | [Usage]: doc/01-Usage.md 34 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "herrera-io/object-storage", 3 | "description": "An object store for specific objects.", 4 | "keywords": ["object", "store", "storage"], 5 | "homepage": "http://github.com/herrera-io/php-object-storage", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Kevin Herrera", 10 | "email": "kevin@herrera.io", 11 | "homepage": "http://kevin.herrera.io" 12 | } 13 | ], 14 | "support": { 15 | "issues": "https://github.com/herrera-io/php-object-storage/issues" 16 | }, 17 | "require": { 18 | "php": ">=5.3.3" 19 | }, 20 | "require-dev": { 21 | "herrera-io/phpunit-test-case": "1.*", 22 | "phpunit/phpunit": "3.7.*" 23 | }, 24 | "autoload": { 25 | "psr-0": { 26 | "Herrera\\Util": "src/lib" 27 | } 28 | }, 29 | "config": { 30 | "bin-dir": "bin", 31 | "vendor-dir": "src/vendors" 32 | }, 33 | "extra": { 34 | "branch-alias": { 35 | "dev-master": "1.0-dev" 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /doc/00-Installing.md: -------------------------------------------------------------------------------- 1 | Installing 2 | ========== 3 | 4 | Composer 5 | -------- 6 | 7 | The easiest way to install Object Storage is by using [Composer][]: 8 | 9 | $ composer require herrera-io/object-storage=~1.0 10 | 11 | You may then load it by requiring the Composer autoloader: 12 | 13 | ```php 14 | require 'vendor/autoload.php'; 15 | ``` 16 | 17 | PSR-0 18 | ----- 19 | 20 | You may use any class loader that supports [PSR-0][]. 21 | 22 | ```php 23 | $loader = new SplClassLoader(); 24 | $loader->add('Herrera\\Util', 'src/lib'); 25 | ``` 26 | 27 | [Composer]: http://getcomposer.org/ 28 | [PSR-0]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md 29 | -------------------------------------------------------------------------------- /doc/01-Usage.md: -------------------------------------------------------------------------------- 1 | Usage 2 | ===== 3 | 4 | To use the `ObjectStorage` class, you must extend it with your own class. 5 | 6 | ```php 7 | use Herrera\Util\ObjectStorage; 8 | 9 | /** 10 | * This is your class. 11 | */ 12 | class MyObjectStorage extends ObjectStorage 13 | { 14 | /** 15 | * @override 16 | */ 17 | public function isSupported($object) 18 | { 19 | return ($object instanceof DateTime); 20 | } 21 | } 22 | ``` 23 | 24 | In the example above, `MyObjectStorage` class will only permit `DateTime` to 25 | be added. Any attempt to add a different type of object will result in an 26 | exception (`UnexpectedValueException`) being thrown with the following message: 27 | `The object, SomeClass, is not supported.` 28 | 29 | How you implement the `isSupported()` method is entirely up to you. The method 30 | only needs to return `true` if the object it is given is supported, or `false` 31 | if it is not. 32 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | src/lib/ 15 | 16 | 17 | 18 | 19 | src/tests/ 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/lib/Herrera/Util/ObjectStorage.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | abstract class ObjectStorage extends SplObjectStorage 20 | { 21 | /** 22 | * @override 23 | */ 24 | public function addAll($storage) 25 | { 26 | if (!($storage instanceof SplObjectStorage)) { 27 | throw new InvalidArgumentException( 28 | 'The $storage argument must be an instance of SplObjectStorage.' 29 | ); 30 | } 31 | 32 | foreach ($storage as $object) { 33 | if ($this->isSupported($object)) { 34 | $this[$object] = $storage[$object]; 35 | } 36 | } 37 | } 38 | 39 | /** 40 | * @override 41 | * 42 | * @throws UnexpectedValueException If the object is not supported. 43 | */ 44 | public function attach($object, $data = null) 45 | { 46 | if (!$this->isSupported($object)) { 47 | throw new UnexpectedValueException( 48 | sprintf( 49 | 'The object, %s, is not supported.', 50 | get_class($object) 51 | ) 52 | ); 53 | } 54 | 55 | parent::attach($object, $data); 56 | } 57 | 58 | /** 59 | * Checks if an object is supported. 60 | * 61 | * @param object $object An object. 62 | * 63 | * @return boolean TRUE if it is supported, FALSE if not. 64 | */ 65 | abstract public function isSupported($object); 66 | 67 | /** 68 | * @override 69 | * 70 | * @throws UnexpectedValueException If the object is not supported. 71 | */ 72 | public function offsetSet($object, $data = null) 73 | { 74 | if (!$this->isSupported($object)) { 75 | throw new UnexpectedValueException( 76 | sprintf( 77 | 'The object, %s, is not supported.', 78 | get_class($object) 79 | ) 80 | ); 81 | } 82 | 83 | parent::offsetSet($object, $data); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/tests/Herrera/Util/Test/ExampleInterface.php: -------------------------------------------------------------------------------- 1 | attach($two); 23 | $store->attach($one); 24 | 25 | $this->store->addAll($store); 26 | 27 | $this->assertTrue($this->store->contains($one)); 28 | $this->assertFalse($this->store->contains($two)); 29 | } 30 | 31 | public function testAddAllInvalidArgument() 32 | { 33 | $this->setExpectedException( 34 | 'InvalidArgumentException', 35 | 'The $storage argument must be an instance of SplObjectStorage.' 36 | ); 37 | 38 | $this->store->addAll(123); 39 | } 40 | 41 | public function testAttach() 42 | { 43 | $one = new ExampleObject(); 44 | 45 | $this->store->attach($one); 46 | 47 | $this->assertTrue($this->store->contains($one)); 48 | } 49 | 50 | public function testAttachUnsupported() 51 | { 52 | $one = new stdClass(); 53 | 54 | $this->setExpectedException( 55 | 'UnexpectedValueException', 56 | 'The object, stdClass, is not supported.' 57 | ); 58 | 59 | $this->store->attach($one); 60 | } 61 | 62 | public function testOffsetSet() 63 | { 64 | $one = new ExampleObject(); 65 | 66 | $this->store[$one] = 123; 67 | 68 | $this->assertTrue($this->store->contains($one)); 69 | $this->assertSame(123, $this->store[$one]); 70 | } 71 | 72 | public function testOffsetSetUnsupported() 73 | { 74 | $one = new stdClass(); 75 | 76 | $this->setExpectedException( 77 | 'UnexpectedValueException', 78 | 'The object, stdClass, is not supported.' 79 | ); 80 | 81 | $this->store[$one] = 123; 82 | } 83 | 84 | protected function setUp() 85 | { 86 | $this->store = new ExampleStore(); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | add(null, __DIR__); 5 | --------------------------------------------------------------------------------