├── .gitignore ├── .travis.yml ├── README.md ├── composer.json ├── src └── CreateOpen │ └── Traits │ ├── ClassExtras.php │ ├── Decorator.php │ ├── Hook.php │ ├── Logging.php │ ├── Options.php │ ├── Singleton.php │ └── ToString.php └── tests └── CreateOpen └── Traits └── ToStringTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | /vendor/ 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.4 5 | - 5.5 6 | - hhvm 7 | 8 | before_script: 9 | - curl -s http://getcomposer.org/installer | php 10 | - php composer.phar install --prefer-source --no-interaction --dev 11 | 12 | script: ./vendor/phpunit/phpunit/phpunit.php tests/ 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This is a collection of handy PHP traits. These are all stand alone components, without any dependencies, 2 | that can be dropped into any project. Requires >= PHP 5.4.0 as it uses traits. Collaborators / pull requests welcome. 3 | 4 | [![Build Status](https://travis-ci.org/dave1010/php-traits.png?branch=master)](https://travis-ci.org/dave1010/php-traits) 5 | 6 | ## Traits 7 | 8 | * **ClassExtras** - add utility methods to a class 9 | * **Decorator** - decorator design pattern: add behaviour to an object dynamically 10 | * **Logging** - log messages to 1 or more logs 11 | * **Options** - get and set an object's options 12 | * **Singleton** - design anti-pattern 13 | * **ToString** - safely convert an object to a string 14 | 15 | ## Installation 16 | 17 | Use [packagist](http://packagist.org/packages/createopen/traits) - put this in your `composer.json` 18 | 19 | { 20 | "require": { 21 | "createopen/traits": "dev-master", 22 | } 23 | } 24 | 25 | ## Usage 26 | 27 | require 'vendor/autoload.php'; 28 | 29 | class Foo { 30 | use CreateOpen\Traits\Decorator; 31 | } 32 | 33 | 34 | ## Traits to make 35 | 36 | * Event 37 | * Commandline 38 | * Publicize (use the magic `__call()` to make all methodds pseudo-public, for debugging) 39 | * Benchmarking 40 | * ArrayAccessible / Iterator / Countable / Traversable / Filter 41 | * ORM / DataMapper / ActiveRecord 42 | * Cache 43 | * Template 44 | * Foldl 45 | * Monads: Maybe, Collection 46 | 47 | ## License (MIT) 48 | 49 | Copyright (C) 2012 Dave Hulbert 50 | 51 | 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: 52 | 53 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 54 | 55 | 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. 56 | 57 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "createopen/traits", 3 | "type": "library", 4 | "homepage": "https://github.com/dave1010/php-traits", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Dave Hulbert", 9 | "email": "dave1010@gmail.com", 10 | "homepage": "http://createopen.com", 11 | "role": "Developer" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.4.7" 16 | }, 17 | "require-dev": { 18 | "phpunit/phpunit": "3.8.*" 19 | }, 20 | "autoload": { 21 | "psr-0": { 22 | "CreateOpen": "src" 23 | } 24 | }, 25 | "minimum-stability": "dev" 26 | } 27 | -------------------------------------------------------------------------------- /src/CreateOpen/Traits/ClassExtras.php: -------------------------------------------------------------------------------- 1 | setObject($object); 18 | } 19 | 20 | public function setObject($object) 21 | { 22 | $this->object = $object; 23 | } 24 | 25 | public function __call($method, $args) 26 | { 27 | return call_user_func_array(array($this->object, $method), $args); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/CreateOpen/Traits/Hook.php: -------------------------------------------------------------------------------- 1 | 1, 18 | ); 19 | 20 | public function log($message, $level = 1) 21 | { 22 | foreach ($this->log_methods as $method => $method_level) { 23 | if ($level >= $method_level) { 24 | $this->$method($message); 25 | } 26 | } 27 | } 28 | 29 | protected function errorLog($message) 30 | { 31 | error_log($message); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/CreateOpen/Traits/Options.php: -------------------------------------------------------------------------------- 1 | optionsContainer = $container ? $container : new \stdClass; 11 | } 12 | 13 | protected function getOptionsContainer() 14 | { 15 | return $this->optionsContainer; 16 | } 17 | 18 | public function getOption($name, $default = null) 19 | { 20 | $c = $this->getOptionsContainer(); 21 | 22 | return isset($c->$name) ? $c->$name : $default; 23 | } 24 | 25 | public function updateOption($name, $newValue) 26 | { 27 | $c = $this->getOptionsContainer(); 28 | 29 | return $c->$name = $newValue; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/CreateOpen/Traits/Singleton.php: -------------------------------------------------------------------------------- 1 | getMessage()); 22 | 23 | return ''; 24 | } 25 | 26 | public function __toString() 27 | { 28 | // cannot throw exceptions in toString 29 | try { 30 | $r = (string) $this->toString(); 31 | 32 | return $r; 33 | } catch (\Exception $e) { 34 | return (string) $this->handleException($e); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/CreateOpen/Traits/ToStringTest.php: -------------------------------------------------------------------------------- 1 | getObjectForTrait(__NAMESPACE__.'\ToString'); 19 | } 20 | 21 | /** 22 | * @return mixed 23 | */ 24 | private function getStub() 25 | { 26 | $stub = $this->getMockForTrait(__NAMESPACE__ . '\ToString'); 27 | return $stub; 28 | } 29 | 30 | public function testStringIsUntouched() 31 | { 32 | $expected = 'foo'; 33 | $stub = $this->getStub(); 34 | 35 | $stub->expects($this->any()) 36 | ->method('toString') 37 | ->will($this->returnValue('foo')); 38 | 39 | $actual = (string) $stub; 40 | 41 | $this->assertEquals($expected, $actual); 42 | } 43 | 44 | public function testIntIsCastedToString() 45 | { 46 | $expected = '7'; 47 | $stub = $this->getStub(); 48 | 49 | $stub->expects($this->any()) 50 | ->method('toString') 51 | ->will($this->returnValue(7)); 52 | 53 | $actual = (string) $stub; 54 | 55 | $this->assertEquals($expected, $actual); 56 | } 57 | 58 | public function testThrowingExceptionIsNotFatal() 59 | { 60 | $expected = ''; 61 | $exception = new Exception; 62 | $stub = $this->getStub(); 63 | 64 | $stub->expects($this->any()) 65 | ->method('toString') 66 | ->will($this->throwException($exception)); 67 | 68 | $actual = (string) $stub; 69 | 70 | $this->assertEquals($expected, $actual); 71 | } 72 | } --------------------------------------------------------------------------------