├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── README.md ├── bootstrap.php ├── build.default.properties ├── build.xml ├── composer.json ├── phpunit.xml ├── src └── AppserverIo │ └── Lang │ ├── Boolean.php │ ├── ClassCastException.php │ ├── Flt.php │ ├── IllegalStateException.php │ ├── Integer.php │ ├── NotImplementedException.php │ ├── NullPointerException.php │ ├── Number.php │ ├── NumberFormatException.php │ ├── Objct.php │ ├── Reflection │ ├── AnnotationInterface.php │ ├── ClassInterface.php │ ├── MethodInterface.php │ ├── ParameterInterface.php │ ├── PropertyInterface.php │ ├── ReflectionAnnotation.php │ ├── ReflectionClass.php │ ├── ReflectionException.php │ ├── ReflectionMethod.php │ ├── ReflectionObject.php │ ├── ReflectionParameter.php │ └── ReflectionProperty.php │ ├── Strng.php │ └── StrngIndexOutOfBoundsException.php └── tests └── AppserverIo └── Lang ├── BooleanTest.php ├── FltTest.php ├── IntegerTest.php ├── ObjectTest.php ├── Reflection ├── MockAnnotation.php ├── ReflectionAnnotationTest.php ├── ReflectionClassTest.php ├── ReflectionMethodTest.php ├── ReflectionParameterTest.php └── ReflectionPropertyTest.php └── StrngTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /.settings 3 | /.buildpath 4 | /.project 5 | /.DS_Store 6 | /target 7 | /instance-src 8 | /vendor 9 | /build.properties 10 | /composer.lock 11 | /composer.phar 12 | /*.iml 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 7.2 5 | - 5.6 6 | 7 | matrix: 8 | allow_failures: 9 | - php: 7.2 10 | 11 | before_install: 12 | - if [[ "$TRAVIS_PHP_VERSION" < "7.0" ]]; then pecl install xdebug-2.5.5; fi 13 | - if [[ "$TRAVIS_PHP_VERSION" = "7.0" ]] || [[ "$TRAVIS_PHP_VERSION" > "7.0" ]]; then pecl install xdebug; fi 14 | - phpenv rehash 15 | - wget https://scrutinizer-ci.com/ocular.phar 16 | 17 | before_script: 18 | - composer selfupdate 19 | 20 | script: 21 | - ant composer-init 22 | - ant build 23 | - php ocular.phar code-coverage:upload --format=php-clover $TRAVIS_BUILD_DIR/target/reports/unit/clover.xml 24 | 25 | notifications: 26 | email: info@appserver.io 27 | hipchat: 95d47a72c5372d4a0fef20048c3200@Appserver 28 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Version 5.0.0 2 | 3 | ## Bugfixes 4 | 5 | * None 6 | 7 | ## Features 8 | 9 | * Renamed "Object" class to "Objct" to avoid typing issues 10 | 11 | # Version 4.0.0 12 | 13 | ## Bugfixes 14 | 15 | * None 16 | 17 | ## Features 18 | 19 | * Refactoring to work with PHP 7 20 | 21 | # Version 3.0.0 22 | 23 | ## Bugfixes 24 | 25 | * None 26 | 27 | ## Features 28 | 29 | * Add getType() method to ParameterInterface + implement ReflectionParameter::getType() method 30 | 31 | # Version 2.0.0 32 | 33 | ## Bugfixes 34 | 35 | * None 36 | 37 | ## Features 38 | 39 | * Add implementsInterface(), isAbstract() and isInterface() methods to ClassInterface 40 | * Implement ReflectionClass::isAbstract() and ReflectionClass::isInterface() methods 41 | 42 | # Version 1.0.1 43 | 44 | ## Bugfixes 45 | 46 | * None 47 | 48 | ## Features 49 | 50 | * Add private static ReflectionAnnotation::fromStdClass() method to initialize a ReflectionAnnotation from a \stdClass instance 51 | 52 | 53 | # Version 1.0.0 54 | 55 | ## Bugfixes 56 | 57 | * None 58 | 59 | ## Features 60 | 61 | * Updated to new stable dependencies 62 | 63 | # Version 0.1.18 64 | 65 | ## Bugfixes 66 | 67 | * Declarative property default values may break thread safety 68 | 69 | ## Features 70 | 71 | * None 72 | 73 | # Version 0.1.17 74 | 75 | ## Bugfixes 76 | 77 | * None 78 | 79 | ## Features 80 | 81 | * Applied new coding conventions 82 | 83 | # Version 0.1.16 84 | 85 | ## Bugfixes 86 | 87 | * None 88 | 89 | ## Features 90 | 91 | * Added a NotImplementedException class 92 | 93 | # Version 0.1.15 94 | 95 | ## Bugfixes 96 | 97 | * None 98 | 99 | ## Features 100 | 101 | * Implement ReflectionClass::addAnnotationAlias(), ReflectionMethod::addAnnotationAlias() and ReflectionProperty::addAnnotationAlias() 102 | 103 | # Version 0.1.14 104 | 105 | ## Bugfixes 106 | 107 | * None 108 | 109 | ## Features 110 | 111 | * Implement ReflectionMethod::getParameters() method 112 | 113 | # Version 0.1.13 114 | 115 | ## Bugfixes 116 | 117 | * None 118 | 119 | ## Features 120 | 121 | * Add ParameterInterface interface and ReflectionParameter class 122 | 123 | # Version 0.1.12 124 | 125 | ## Bugfixes 126 | 127 | * None 128 | 129 | ## Features 130 | 131 | * Do not overwrite annotation values when passing them to constructor, instead copy each key value pair 132 | 133 | # Version 0.1.11 134 | 135 | ## Bugfixes 136 | 137 | * Use isset() to check for initialized instances instead of == null comparison 138 | 139 | ## Features 140 | 141 | * None 142 | 143 | # Version 0.1.10 144 | 145 | ## Bugfixes 146 | 147 | * Performance optimizations 148 | 149 | ## Features 150 | 151 | * None 152 | 153 | # Version 0.1.9 154 | 155 | ## Bugfixes 156 | 157 | * Bugfix ReflectionMethod::invoke() method to correctly pass args to method invocation 158 | 159 | ## Features 160 | 161 | * None 162 | 163 | # Version 0.1.8 164 | 165 | ## Bugfixes 166 | 167 | * Activate ReflectionClass::getMethods() $filter parameter with -1 to ignore filter by default 168 | 169 | ## Features 170 | 171 | * Add ReflectionProperty functionality 172 | 173 | # Version 0.1.7 174 | 175 | ## Bugfixes 176 | 177 | * Bugfix to return real class name when invoking ReflectionClass:getName() instead of the one passed to the constructor 178 | 179 | ## Features 180 | 181 | * Add method ReflectionClass::getShortName() 182 | 183 | # Version 0.1.6 184 | 185 | ## Bugfixes 186 | 187 | * None 188 | 189 | ## Features 190 | 191 | * Add ReflectionObject implementation 192 | * Add ReflectionClass::newInstance() and ReflectionClass::newInstanceArgs() methods 193 | * Rename method ReflectionClass::getClassName() to ReflectionClass::getName() 194 | 195 | # Version 0.1.5 196 | 197 | ## Bugfixes 198 | 199 | * None 200 | 201 | ## Features 202 | 203 | * Add generic userland implementations for reflection annotation, class and method 204 | * Add dependency to herrera-io/annotations to parse and initialized Doctrine style annotations 205 | 206 | # Version 0.1.4 207 | 208 | ## Bugfixes 209 | 210 | * None 211 | 212 | ## Features 213 | 214 | * Add userland interfaces for reflection annotation, class and method 215 | 216 | # Version 0.1.3 217 | 218 | ## Bugfixes 219 | 220 | * None 221 | 222 | ## Features 223 | 224 | * String, Integer, Float and Boolean class now implements \Serializable interface 225 | 226 | # Version 0.1.2 227 | 228 | ## Bugfixes 229 | 230 | * None 231 | 232 | ## Features 233 | 234 | * Add new IllegalStateException 235 | 236 | # Version 0.1.1 237 | 238 | ## Bugfixes 239 | 240 | * None 241 | 242 | ## Features 243 | 244 | * Refactoring ANT PHPUnit execution process 245 | * Composer integration by optimizing folder structure (move bootstrap.php + phpunit.xml.dist => phpunit.xml) 246 | * Switch to new appserver-io/build build- and deployment environment 247 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP language extension 2 | 3 | [![Latest Stable Version](https://img.shields.io/packagist/v/appserver-io/lang.svg?style=flat-square)](https://packagist.org/packages/appserver-io/lang) 4 | [![Total Downloads](https://img.shields.io/packagist/dt/appserver-io/lang.svg?style=flat-square)](https://packagist.org/packages/appserver-io/lang) 5 | [![License](https://img.shields.io/packagist/l/appserver-io/lang.svg?style=flat-square)](https://packagist.org/packages/appserver-io/lang) 6 | [![Build Status](https://img.shields.io/travis/appserver-io/lang/master.svg?style=flat-square)](http://travis-ci.org/appserver-io/lang) 7 | [![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/appserver-io/lang/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/appserver-io/lang/?branch=master) 8 | [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/appserver-io/lang/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/appserver-io/lang/?branch=master) 9 | 10 | ## Introduction 11 | 12 | This package provides implementation of basic PHP datatypes. 13 | 14 | ## Issues 15 | 16 | In order to bundle our efforts we would like to collect all issues regarding this package in [the main project repository's issue tracker](https://github.com/appserver-io/appserver/issues). 17 | Please reference the originating repository as the first element of the issue title e.g.: 18 | `[appserver-io/] A issue I am having` 19 | 20 | ## Usage 21 | 22 | This package provides classes representing an object oriented implementation for some basic datatypes. 23 | 24 | As there has been (and still are) many discussions about PHP and type safety i decided to implement a small, really 25 | basic library that will offer the most basic datatype needed in nearly every project. To be honest, I really like 26 | almost all of those nice possibilities languages like Java provides, but as PHP is not Java, you always have to find a 27 | neat way to implement similar things in a way that makes sense in a PHP environment. 28 | 29 | The intention for implementing those classes was the possibility to make your critical functions and methods Type-Safe, 30 | by using them for type hinting on one hand and the possibility to have a quick and easy to use kind of data 31 | validation mechanism on the other hand. 32 | 33 | As you may know, using type hints will probably slow down your code a bit, so take care when you use them and 34 | always have an eye on possible performance impacts by running performance tests regularly. 35 | 36 | The data type implementations this library will provide, are 37 | 38 | * Objct 39 | * Boolean 40 | * Integer 41 | * Flt 42 | * Strng 43 | 44 | The following examples wil give you a short introduction about the functionality each class will provide and 45 | how you can use it in your code. Please be aware, that the examples are not intended to make any sense, they 46 | should only give you an idea what is possible. 47 | 48 | ### Objct (Object before version 5.0) 49 | 50 | The abstract class `Objct` implements a low level presentation of a class. All other classes of this library use it 51 | as superclass. 52 | 53 | ### Boolean 54 | 55 | Using a `Boolean` instance to compare against another one or try to instantiate it with a not boolean value. 56 | 57 | ```php 58 | // initialize a new Integer instance 59 | $bool = new Boolean(true); 60 | $bool->equals(new Boolean(false)); // false 61 | 62 | // throws a ClassCastException 63 | $invalid = new Boolean('aValue'); 64 | ``` 65 | 66 | ### Integer 67 | 68 | Here are some examples how you can use the `Integer` class 69 | 70 | ```php 71 | // initialize a new Integer instance 72 | $int = new Integer(17); 73 | 74 | // get the float value of the Integer 75 | echo $int->floatValue() . PHP_EOL; // 17.0 76 | echo $int->stringValue() . PHP_EOL; // '17' 77 | 78 | // check that the two Integer instances are equal 79 | $int->equals(Integer::valueOf(new String('17'))); // true 80 | ``` 81 | 82 | ### Flt (Float before version 3.0) 83 | 84 | The example for using a `Flt` shows you how to add to instances 85 | and print the float value 86 | 87 | ```php 88 | // initialize a new Float instance 89 | $float = new Flt(10.005); 90 | $float->add(new Flt(10.105)); 91 | 92 | // check the value 93 | echo $float->floatValue() . PHP_EOL; // 20.11 94 | ``` 95 | 96 | ### Strng (String before version 3.0) 97 | 98 | To show you how you can use the `Strng` class we'll simple concatenate 99 | to instances 100 | 101 | ```php 102 | // initialize a new String instance 103 | $string = new Strng('value to'); 104 | 105 | // check that String was successfully concatenated 106 | echo $string->concat(new Strng(' search')) . PHP_EOL; // 'value to search' 107 | ``` 108 | 109 | Yeah, this are really simple examples, and as i told you before in most cases 110 | i'll use that classes for simple things like type hints and so on. 111 | 112 | If you like to use that stuff also, feel free to implement your own types and 113 | send me a pull request :) 114 | 115 | ## External Links 116 | 117 | * Documentation at [appserver.io](http://docs.appserver.io) 118 | -------------------------------------------------------------------------------- /bootstrap.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | $loader = require 'vendor/autoload.php'; 22 | $loader->add('AppserverIo\\Lang', 'src'); 23 | -------------------------------------------------------------------------------- /build.default.properties: -------------------------------------------------------------------------------- 1 | #-------------------------------------------------------------------------------- 2 | # appserver-io/lang Build Default Properties 3 | # 4 | # @copyright Copyright (c) 2010 - TechDivision GmbH 5 | # @license http://opensource.org/licenses/osl-3.0.php 6 | # Open Software License (OSL 3.0) 7 | # @author TechDivision GmbH - Core Team 8 | #-------------------------------------------------------------------------------- 9 | 10 | # ---- Module Release Settings -------------------------------------------------- 11 | release.version = 5.0.0 12 | -------------------------------------------------------------------------------- /build.xml: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "appserver-io/lang", 3 | "description": "Lang package implementation providing basic PHP datatypes.", 4 | "homepage": "https://github.com/appserver-io/lang", 5 | "license": "OSL-3.0", 6 | "require": { 7 | "php": ">=5.4.0", 8 | "herrera-io/annotations": "1.0.*" 9 | }, 10 | "require-dev": { 11 | "appserver-io/build": "~2.0" 12 | }, 13 | "autoload": { 14 | "psr-0": { 15 | "AppserverIo\\Lang": [ 16 | "src/", 17 | "tests/" 18 | ] 19 | } 20 | }, 21 | "authors": [ 22 | { 23 | "name": "Tim Wagner", 24 | "email": "tw@appserver.io", 25 | "homepage": "https://github.com/wagnert", 26 | "role": "Developer" 27 | }, 28 | { 29 | "name": "Bernhard Wick", 30 | "email": "bw@appserver.io", 31 | "homepage": "https://github.com/wick-ed", 32 | "role": "Developer" 33 | } 34 | ], 35 | "support": { 36 | "email": "t.wagner@techdivision.com", 37 | "issues": "https://github.com/appserver-io/lang/issues", 38 | "source": "https://github.com/appserver-io/lang" 39 | }, 40 | "keywords": [ 41 | "basic datatypes objects boolean string float integer" 42 | ] 43 | } 44 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | tests 5 | 6 | 7 | 8 | 9 | src 10 | 11 | src 12 | src 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/Boolean.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang; 22 | 23 | /** 24 | * This class implements functionality to handle 25 | * a boolean value as object. 26 | * 27 | * @author Tim Wagner 28 | * @copyright 2015 TechDivision GmbH 29 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 30 | * @link https://github.com/appserver-io/lang 31 | * @link http://www.appserver.io 32 | */ 33 | class Boolean extends Objct 34 | { 35 | 36 | /** 37 | * The value of the Boolean. 38 | * 39 | * @var boolean 40 | */ 41 | protected $value; 42 | 43 | /** 44 | * The accepted values for a Boolean object. 45 | * 46 | * @var array 47 | */ 48 | protected $booleans; 49 | 50 | /** 51 | * The boolean representation of 52 | * the requested value. 53 | * 54 | * @var array 55 | */ 56 | protected $values; 57 | 58 | /** 59 | * Constructs a newly allocated Boolean object that 60 | * represents the primitive boolean argument. 61 | * 62 | * @param boolean $value The value to be represented by the Boolean. 63 | * 64 | * @throws \AppserverIo\Lang\ClassCastException The passed value is not a valid boolean representation 65 | */ 66 | public function __construct($value) 67 | { 68 | // initialize property default values here, as declarative default values may break thread safety, 69 | // when utilizing static and non-static access on class methods within same thread context! 70 | $this->value = false; 71 | $this->booleans = array( 72 | true, 73 | false, 74 | 1, 75 | 0, 76 | "1", 77 | "0", 78 | "true", 79 | "false", 80 | "on", 81 | "off", 82 | "yes", 83 | "no", 84 | "y", 85 | "n" 86 | ); 87 | $this->values = array( 88 | true => true, 89 | false => false, 90 | 1 => true, 91 | 0 => false, 92 | "1" => true, 93 | "0" => false, 94 | "true" => true, 95 | "false" => false, 96 | "on" => true, 97 | "off" => false, 98 | "yes" => true, 99 | "no" => false, 100 | "y" => true, 101 | "n" => false 102 | ); 103 | 104 | 105 | if (in_array($value, $this->booleans, true)) { 106 | $this->value = $this->values[$value]; 107 | } else { 108 | throw new ClassCastException('The passed value ' . $value . ' is not a valid Boolean'); 109 | } 110 | } 111 | 112 | /** 113 | * This method returns the class name as 114 | * a string. 115 | * 116 | * @return string 117 | */ 118 | public static function __getClass() 119 | { 120 | return __CLASS__; 121 | } 122 | 123 | /** 124 | * Returns the value of this Boolean object as a 125 | * boolean primitive. 126 | * 127 | * @return boolean Holds the value as a boolean primitive 128 | */ 129 | public function booleanValue() 130 | { 131 | return $this->value; 132 | } 133 | 134 | /** 135 | * Returns a Boolean with a value represented by the specified String. 136 | * 137 | * If the passed string has the primitive value TRUE or 1 then the 138 | * returned object is initialized with the primitive value TRUE else 139 | * with FALSE. 140 | * 141 | * @param \AppserverIo\Lang\Strng $string Holds the String object to get the Boolean representation for 142 | * 143 | * @return \AppserverIo\Lang\Boolean The Boolean object representing the specified String. 144 | */ 145 | public static function valueOf(Strng $string) 146 | { 147 | // if the passed value is "true" or "1" then return a new Boolean 148 | // object initialized with true 149 | if ($string->equals(new Strng("1")) || 150 | $string->equals(new Strng("true")) || 151 | $string->equals(new Strng("yes")) || 152 | $string->equals(new Strng("on")) || 153 | $string->equals(new Strng("y"))) { 154 | return new Boolean(true); 155 | } 156 | // else return a new Boolean object initialized with false 157 | return new Boolean(false); 158 | } 159 | 160 | /** 161 | * This method checks if the passed object is equal 162 | * to itself. 163 | * 164 | * @param \AppserverIo\Lang\Objct $obj The object to check 165 | * 166 | * @return boolean Returns TRUE if the passed object is equal 167 | * @see \AppserverIo\Lang\Objct::equals() 168 | */ 169 | public function equals(Objct $obj) 170 | { 171 | return $this->booleanValue() == $obj->booleanValue(); 172 | } 173 | 174 | /** 175 | * This object as String returned. 176 | * 177 | * @return \AppserverIo\Lang\Strng The value as String. 178 | */ 179 | public function toString() 180 | { 181 | return new Strng($this->__toString()); 182 | } 183 | 184 | /** 185 | * This method returns the class as 186 | * a string representation. 187 | * 188 | * @return string The objects string representation 189 | * @see \AppserverIo\Lang\Objct::__toString() 190 | */ 191 | public function __toString() 192 | { 193 | // if TRUE, return string 'true' 194 | if ($this->value) { 195 | return 'true'; 196 | } 197 | // else return string 'false' 198 | return 'false'; 199 | } 200 | 201 | /** 202 | * This method has to be called to serialize the Boolean. 203 | * 204 | * @return string Returns a serialized version of the Boolean 205 | * @see \Serializable::serialize() 206 | */ 207 | public function serialize() 208 | { 209 | return serialize($this->value); 210 | } 211 | 212 | /** 213 | * This method unserializes the passed string and initializes the Boolean 214 | * itself with the data. 215 | * 216 | * @param string $data Holds the data of the instance as serialized string 217 | * 218 | * @return void 219 | * @see \Serializable::unserialize($data) 220 | */ 221 | public function unserialize($data) 222 | { 223 | $this->value = unserialize($data); 224 | } 225 | } 226 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/ClassCastException.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang; 22 | 23 | /** 24 | * Exception thrown when an attempt is made to access an element that does not 25 | * exist. 26 | * This exception is thrown by the Enumeration classes if the 27 | * nextElement, method goes beyond the end of the list of elements that are 28 | * being accessed. 29 | * 30 | * @author Tim Wagner 31 | * @copyright 2015 TechDivision GmbH 32 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 33 | * @link https://github.com/appserver-io/lang 34 | * @link http://www.appserver.io 35 | */ 36 | class ClassCastException extends \Exception 37 | { 38 | } 39 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/Flt.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang; 22 | 23 | /** 24 | * This class implements functionality to handle 25 | * a float value as object. 26 | * 27 | * @author Tim Wagner 28 | * @copyright 2015 TechDivision GmbH 29 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 30 | * @link https://github.com/appserver-io/lang 31 | * @link http://www.appserver.io 32 | */ 33 | class Flt extends Number implements \Serializable 34 | { 35 | 36 | /** 37 | * The value of the Float. 38 | * 39 | * @var float 40 | */ 41 | protected $value; 42 | 43 | /** 44 | * Constructs a newly allocated Float object that 45 | * represents the primitive float argument. 46 | * 47 | * @param float $value The value to be represented by the Float 48 | */ 49 | public function __construct($value) 50 | { 51 | // initialize property default values here, as declarative default values may break thread safety, 52 | // when utilizing static and non-static access on class methods within same thread context! 53 | $this->value = null; 54 | 55 | if (!is_float($value)) { 56 | NumberFormatException::forInputString($value); 57 | } 58 | $this->value = $value; 59 | } 60 | 61 | /** 62 | * This method returns the class name as 63 | * a string. 64 | * 65 | * @return string 66 | */ 67 | public static function __getClass() 68 | { 69 | return __CLASS__; 70 | } 71 | 72 | /** 73 | * Returns a Float object holding the 74 | * float value represented by the argument string 75 | * s. 76 | *

77 | * If s is null, then a 78 | * NullPointerException is thrown. 79 | *

80 | * Leading and trailing whitespace characters in s 81 | * are ignored. The rest of s should constitute a 82 | * Float as described by the lexical syntax rules: 83 | *

84 | *
85 | *
Float: 86 | *
Signopt NaN 87 | *
Signopt Infinity 88 | *
Signopt FloatingPointLiteral 89 | *
90 | *
91 | * where Sign and FloatingPointLiteral are as 92 | * defined in §3.10.2 94 | * of the Java 95 | * Language Specification. If s does not have the 96 | * form of a Float, then a 97 | * NumberFormatException is thrown. Otherwise, 98 | * s is regarded as representing an exact decimal 99 | * value in the usual "computerized scientific notation"; this 100 | * exact decimal value is then conceptually converted to an 101 | * "infinitely precise" binary value that is then rounded to type 102 | * float by the usual round-to-nearest rule of IEEE 103 | * 754 floating-point arithmetic, which includes preserving the 104 | * sign of a zero value. Finally, a Float object 105 | * representing this float value is returned. 106 | *

107 | * To interpret localized string representations of a 108 | * floating-point value, use subclasses of {@link 109 | * java.text.NumberFormat}. 110 | * 111 | *

Note that trailing format specifiers, specifiers that 112 | * determine the type of a floating-point literal 113 | * (1.0f is a float value; 114 | * 1.0d is a double value), do 115 | * not influence the results of this method. In other 116 | * words, the numerical value of the input string is converted 117 | * directly to the target floating-point type. In general, the 118 | * two-step sequence of conversions, string to double 119 | * followed by double to float, is 120 | * not equivalent to converting a string directly to 121 | * float. For example, if first converted to an 122 | * intermediate double and then to 123 | * float, the string
124 | * "1.00000017881393421514957253748434595763683319091796875001d" 125 | *
results in the float value 126 | * 1.0000002f; if the string is converted directly to 127 | * float, 1.0000001f results. 128 | * 129 | * @param \AppserverIo\Lang\Strng $string The string to be parsed 130 | * 131 | * @return \AppserverIo\Lang\Flt A Float object holding the value represented by the Strng argument 132 | * @exception \AppserverIo\Lang\NumberFormatException If the string does not contain a parsable number 133 | */ 134 | public static function valueOf(Strng $string) 135 | { 136 | if (! preg_match("/([0-9\.-]+)/", $string->stringValue())) { 137 | NumberFormatException::forInputString($string->stringValue()); 138 | } 139 | if (! is_numeric($string->stringValue())) { 140 | NumberFormatException::forInputString($string->stringValue()); 141 | } 142 | return new Flt((float) $string->stringValue()); 143 | } 144 | 145 | /** 146 | * Returns a new float initialized to the value 147 | * represented by the specified Strng, as performed 148 | * by the valueOf method of class Float. 149 | * 150 | * @param \AppserverIo\Lang\Strng $string She string to be parsed 151 | * 152 | * @return float The float value represented by the string argument 153 | * @exception \AppserverIo\Lang\NumberFormatException If the string does not contain a parsable float. 154 | * @see \AppserverIo\Lang\Flt::valueOf($string) 155 | */ 156 | public static function parseFloat(Strng $string) 157 | { 158 | return Flt::valueOf($string)->floatValue(); 159 | } 160 | 161 | /** 162 | * Returns the value of the specified number as a float. 163 | * This may involve rounding. 164 | * 165 | * @return float The numeric value represented by this object after conversion to type float 166 | * @see \AppserverIo\Lang\Number::floatValue() 167 | */ 168 | public function floatValue() 169 | { 170 | return $this->value; 171 | } 172 | 173 | /** 174 | * Returns the value of the specified number as an int. 175 | * This may involve rounding or truncation. 176 | * 177 | * @return integer The numeric value represented by this object after conversion to type int 178 | * @see \AppserverIo\Lang\Number::intValue() 179 | */ 180 | public function intValue() 181 | { 182 | return (int) $this->value; 183 | } 184 | 185 | /** 186 | * Returns the value of the specified number as a double. 187 | * This may involve rounding. 188 | * 189 | * @return double The numeric value represented by this object after conversion to type double 190 | * @see \AppserverIo\Lang\Number::doubleValue() 191 | */ 192 | public function doubleValue() 193 | { 194 | return (double) $this->value; 195 | } 196 | 197 | /** 198 | * This method has to be called to serialize the Float. 199 | * 200 | * @return string Returns a serialized version of the Float 201 | * @see \Serializable::serialize() 202 | */ 203 | public function serialize() 204 | { 205 | return serialize($this->value); 206 | } 207 | 208 | /** 209 | * This method unserializes the passed string and initializes the Float 210 | * itself with the data. 211 | * 212 | * @param string $data Holds the data of the instance as serialized string 213 | * 214 | * @return void 215 | * @see \Serializable::unserialize($data) 216 | */ 217 | public function unserialize($data) 218 | { 219 | $this->value = unserialize($data); 220 | } 221 | 222 | /** 223 | * This object as String returned. 224 | * 225 | * @return \AppserverIo\Lang\Strng The value as String. 226 | */ 227 | public function toString() 228 | { 229 | return new Strng($this->value); 230 | } 231 | 232 | /** 233 | * This method returns the class as 234 | * a string representation. 235 | * 236 | * @return string The objects string representation 237 | * @see \AppserverIo\Lang\Objct::__toString() 238 | */ 239 | public function __toString() 240 | { 241 | $string = new Strng($this->value); 242 | return $string->stringValue(); 243 | } 244 | 245 | /** 246 | * Returns true if the passed value is equal. 247 | * 248 | * @param \AppserverIo\Lang\Objct $val The value to check 249 | * 250 | * @return boolean 251 | */ 252 | public function equals(Objct $val) 253 | { 254 | if ($val instanceof Flt) { 255 | return $this->floatValue() == $val->floatValue(); 256 | } 257 | return false; 258 | } 259 | 260 | /** 261 | * Adds the value of the passed Float. 262 | * 263 | * @param \AppserverIo\Lang\Flt $toAdd The Float to add 264 | * 265 | * @return \AppserverIo\Lang\Flt The instance 266 | */ 267 | public function add(Flt $toAdd) 268 | { 269 | $this->value += $toAdd->floatValue(); 270 | return $this; 271 | } 272 | 273 | /** 274 | * Subtracts the value of the passed Float. 275 | * 276 | * @param \AppserverIo\Lang\Flt $toSubtract The Float to subtract 277 | * 278 | * @return \AppserverIo\Lang\Flt The instance 279 | */ 280 | public function subtract(Flt $toSubtract) 281 | { 282 | $this->value -= $toSubtract->floatValue(); 283 | return $this; 284 | } 285 | 286 | /** 287 | * Multiplies the Float with the passed one. 288 | * 289 | * @param \AppserverIo\Lang\Flt $toMultiply The Float to multiply 290 | * 291 | * @return \AppserverIo\Lang\Flt The instance 292 | */ 293 | public function multiply(Flt $toMultiply) 294 | { 295 | $this->value *= $toMultiply->intValue(); 296 | return $this; 297 | } 298 | 299 | /** 300 | * Divides the Float by the passed one. 301 | * 302 | * @param \AppserverIo\Lang\Flt $dividyBy The Float to dividy by 303 | * 304 | * @return \AppserverIo\Lang\Flt The instance 305 | */ 306 | public function divide(Flt $dividyBy) 307 | { 308 | $this->value = $this->value / $dividyBy->floatValue(); 309 | return $this; 310 | } 311 | } 312 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/IllegalStateException.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang; 22 | 23 | /** 24 | * Is thrown if the instance is in a state that does not allow access to this method. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | class IllegalStateException extends \Exception 33 | { 34 | } 35 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/Integer.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang; 22 | 23 | /** 24 | * This class implements functionality to handle 25 | * a integer value as object. 26 | * 27 | * @author Tim Wagner 28 | * @copyright 2015 TechDivision GmbH 29 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 30 | * @link https://github.com/appserver-io/lang 31 | * @link http://www.appserver.io 32 | */ 33 | class Integer extends Number implements \Serializable 34 | { 35 | 36 | /** 37 | * The value of the Integer. 38 | * 39 | * @var integer 40 | */ 41 | protected $value; 42 | 43 | /** 44 | * Constructs a newly allocated Integer object that 45 | * represents the primitive integer argument. 46 | * 47 | * @param integer $value The value to be represented by the Integer. 48 | * 49 | * @throws \AppserverIo\Lang\NumberFormatException Is thrown if the passed value is not an integer 50 | */ 51 | public function __construct($value) 52 | { 53 | // initialize property default values here, as declarative default values may break thread safety, 54 | // when utilizing static and non-static access on class methods within same thread context! 55 | $this->value = null; 56 | 57 | if (!is_int($value)) { 58 | NumberFormatException::forInputString($value); 59 | } 60 | $this->value = $value; 61 | } 62 | 63 | /** 64 | * This method returns the class name as 65 | * a string. 66 | * 67 | * @return string 68 | */ 69 | public static function __getClass() 70 | { 71 | return __CLASS__; 72 | } 73 | 74 | /** 75 | * Returns a Integer object holding the 76 | * float value represented by the argument string 77 | * s. 78 | *

79 | * If s is null, then a 80 | * NullPointerException is thrown. 81 | *

82 | * Leading and trailing whitespace characters in s 83 | * are ignored. The rest of s should constitute a 84 | * Integer as described by the lexical syntax rules: 85 | *

86 | *
87 | *
Integer: 88 | *
Signopt NaN 89 | *
Signopt Infinity 90 | *
Signopt FloatingPointLiteral 91 | *
92 | *
93 | * where Sign and FloatingPointLiteral are as 94 | * defined in §3.10.2 96 | * of the Java 97 | * Language Specification. If s does not have the 98 | * form of a Integer, then a 99 | * NumberFormatException is thrown. Otherwise, 100 | * s is regarded as representing an exact decimal 101 | * value in the usual "computerized scientific notation"; this 102 | * exact decimal value is then conceptually converted to an 103 | * "infinitely precise" binary value that is then rounded to type 104 | * float by the usual round-to-nearest rule of IEEE 105 | * 754 floating-point arithmetic, which includes preserving the 106 | * sign of a zero value. Finally, a Float object 107 | * representing this float value is returned. 108 | *

109 | * To interpret localized string representations of a 110 | * floating-point value, use subclasses of {@link 111 | * java.text.NumberFormat}. 112 | * 113 | *

Note that trailing format specifiers, specifiers that 114 | * determine the type of a floating-point literal 115 | * (1.0f is a float value; 116 | * 1.0d is a double value), do 117 | * not influence the results of this method. In other 118 | * words, the numerical value of the input string is converted 119 | * directly to the target floating-point type. In general, the 120 | * two-step sequence of conversions, string to double 121 | * followed by double to float, is 122 | * not equivalent to converting a string directly to 123 | * float. For example, if first converted to an 124 | * intermediate double and then to 125 | * float, the string
126 | * "1.00000017881393421514957253748434595763683319091796875001d" 127 | *
results in the float value 128 | * 1.0000002f; if the string is converted directly to 129 | * float, 1.0000001f results. 130 | * 131 | * @param \AppserverIo\Lang\Strng $string The string to be parsed 132 | * 133 | * @return \AppserverIo\Lang\Integer A Integer object holding the value represented by the Strng argument 134 | * @exception \AppserverIo\Lang\NumberFormatException If the string does not contain a parsable number 135 | */ 136 | public static function valueOf(Strng $string) 137 | { 138 | if (! preg_match("/([0-9-]+)/", $string->stringValue())) { 139 | NumberFormatException::forInputString($string->stringValue()); 140 | } 141 | if (! is_numeric($string->stringValue())) { 142 | NumberFormatException::forInputString($string->stringValue()); 143 | } 144 | return new Integer((integer) $string->stringValue()); 145 | } 146 | 147 | /** 148 | * Returns a new Integer initialized to the value 149 | * represented by the specified Strng, as performed 150 | * by the valueOf method of class Integer. 151 | * 152 | * @param Strng $string The string to be parsed. 153 | * 154 | * @return \AppserverIo\Lang\Integer The Integer value represented by the string argument. 155 | * @exception \AppserverIo\Lang\NumberFormatException If the string does not contain a parsable Integer. 156 | * @see \AppserverIo\Lang\Integer::valueOf($string) 157 | */ 158 | public static function parseInteger(Strng $string) 159 | { 160 | return Integer::valueOf($string)->intValue(); 161 | } 162 | 163 | /** 164 | * Returns the value of the specified number as a float. 165 | * This may involve rounding. 166 | * 167 | * @return float The numeric value represented by this object after conversion to type float 168 | * @see \AppserverIo\Lang\Number::floatValue() 169 | */ 170 | public function floatValue() 171 | { 172 | return (float) $this->value; 173 | } 174 | 175 | /** 176 | * Returns the value of the specified number as an int. 177 | * This may involve rounding or truncation. 178 | * 179 | * @return integer The numeric value represented by this object after conversion to type int 180 | * @see \AppserverIo\Lang\Number::intValue() 181 | */ 182 | public function intValue() 183 | { 184 | return $this->value; 185 | } 186 | 187 | /** 188 | * Returns the value of the specified number as a double. 189 | * This may involve rounding. 190 | * 191 | * @return double The numeric value represented by this object after conversion to type double 192 | * @see \AppserverIo\Lang\Number::doubleValue() 193 | */ 194 | public function doubleValue() 195 | { 196 | return (double) $this->value; 197 | } 198 | 199 | /** 200 | * This method has to be called to serialize the Integer. 201 | * 202 | * @return string Returns a serialized version of the Integer 203 | * @see \Serializable::serialize() 204 | */ 205 | public function serialize() 206 | { 207 | return serialize($this->value); 208 | } 209 | 210 | /** 211 | * This method unserializes the passed string and initializes the Integer 212 | * itself with the data. 213 | * 214 | * @param string $data Holds the data of the instance as serialized string 215 | * 216 | * @return void 217 | * @see \Serializable::unserialize($data) 218 | */ 219 | public function unserialize($data) 220 | { 221 | $this->value = unserialize($data); 222 | } 223 | 224 | /** 225 | * This object as String returned. 226 | * 227 | * @return \AppserverIo\Lang\Strng The value as String. 228 | */ 229 | public function toString() 230 | { 231 | return new Strng($this->value); 232 | } 233 | 234 | /** 235 | * This method returns the class as 236 | * a string representation. 237 | * 238 | * @return string The objects string representation 239 | * @see \AppserverIo\Lang\Objct::__toString() 240 | */ 241 | public function __toString() 242 | { 243 | $string = new Strng($this->value); 244 | return $string->stringValue(); 245 | } 246 | 247 | /** 248 | * Returns true if the passed value is equal. 249 | * 250 | * @param \AppserverIo\Lang\Objct $val The value to check 251 | * 252 | * @return boolean 253 | */ 254 | public function equals(Objct $val) 255 | { 256 | if ($val instanceof Integer) { 257 | return $this->intValue() == $val->intValue(); 258 | } 259 | return false; 260 | } 261 | 262 | /** 263 | * Adds the value of the passed Integer. 264 | * 265 | * @param \AppserverIo\Lang\Integer $toAdd The Integer to add 266 | * 267 | * @return \AppserverIo\Lang\Integer The instance 268 | */ 269 | public function add(Integer $toAdd) 270 | { 271 | $this->value += $toAdd->intValue(); 272 | return $this; 273 | } 274 | 275 | /** 276 | * Subtracts the value of the passed Integer. 277 | * 278 | * @param \AppserverIo\Lang\Integer $toSubtract The integer to subtract 279 | * 280 | * @return \AppserverIo\Lang\Integer The instance 281 | */ 282 | public function subtract(Integer $toSubtract) 283 | { 284 | $this->value -= $toSubtract->intValue(); 285 | return $this; 286 | } 287 | 288 | /** 289 | * Multiplies the Integer with the passed one. 290 | * 291 | * @param \AppserverIo\Lang\Integer $toMultiply The Integer to multiply 292 | * 293 | * @return \AppserverIo\Lang\Integer The instance 294 | */ 295 | public function multiply(Integer $toMultiply) 296 | { 297 | $this->value *= $toMultiply->intValue(); 298 | return $this; 299 | } 300 | 301 | /** 302 | * Divides the Integer by the passed one. 303 | * As this is an Integer 304 | * the result will ALWAYS be casted to an Integer agains, what 305 | * means that everything after the decimal point will be cutted 306 | * of! 307 | * 308 | * @param \AppserverIo\Lang\Integer $dividyBy The Integer to dividy by 309 | * 310 | * @return \AppserverIo\Lang\Integer The instance 311 | */ 312 | public function divide(Integer $dividyBy) 313 | { 314 | $this->value = intval(($this->value / $dividyBy->intValue())); 315 | return $this; 316 | } 317 | 318 | /** 319 | * The methods returns the remainder of division of objects value 320 | * by the passed divisor. 321 | * 322 | * @param \AppserverIo\Lang\Integer $divisor The divisor for the modulo operation 323 | * 324 | * @return \AppserverIo\Lang\Integer The remainder of the modulo operation 325 | */ 326 | public function modulo(Integer $divisor) 327 | { 328 | return new Integer($this->value % $divisor->intValue()); 329 | } 330 | 331 | /** 332 | * Returns TRUE if the object's value is greater than the passed one. 333 | * 334 | * @param \AppserverIo\Lang\Integer $val The value to test the object's value against 335 | * 336 | * @return boolean TRUE if the object's value is greater 337 | */ 338 | public function greaterThan(Integer $val) 339 | { 340 | return $this->value > $val->intValue(); 341 | } 342 | 343 | /** 344 | * Returns TRUE if the object's value is greater or equal than 345 | * the passed one. 346 | * 347 | * @param \AppserverIo\Lang\Integer $val The value to test the object's value against 348 | * 349 | * @return boolean TRUE if the object's value is greater or equal 350 | */ 351 | public function greaterThanOrEqual(Integer $val) 352 | { 353 | return $this->value >= $val->intValue(); 354 | } 355 | 356 | /** 357 | * Returns TRUE if the objects value is lower than the passed one. 358 | * 359 | * @param \AppserverIo\Lang\Integer $val The value to test the object's value against 360 | * 361 | * @return boolean TRUE if the objects value is lower 362 | */ 363 | public function lowerThan(Integer $val) 364 | { 365 | return $this->value < $val->intValue(); 366 | } 367 | 368 | /** 369 | * Returns TRUE if the objects value is lower or equal than 370 | * the passed one. 371 | * 372 | * @param \AppserverIo\Lang\Integer $val The value to test the objects value against 373 | * 374 | * @return boolean TRUE if the objects value is lower or equal 375 | */ 376 | public function lowerThanOrEqual(Integer $val) 377 | { 378 | return $this->value <= $val->intValue(); 379 | } 380 | } 381 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/NotImplementedException.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang; 22 | 23 | /** 24 | * Thrown to indicate that the called method has not been implemented yet and can therefore not be used. 25 | * 26 | * @author Bernhard Wick 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | class NotImplementedException extends \BadMethodCallException 33 | { 34 | } 35 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/NullPointerException.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang; 22 | 23 | /** 24 | * Thrown when an application attempts to use null in a 25 | * case where an object is required. 26 | * These include: 27 | *

    28 | *
  • Calling the instance method of a null object. 29 | *
  • Accessing or modifying the field of a null object. 30 | *
  • Taking the length of null as if it were an array. 31 | *
  • Accessing or modifying the slots of null as if it 32 | * were an array. 33 | *
  • Throwing null as if it were a Throwable 34 | * value. 35 | *
36 | *

37 | * Applications should throw instances of this class to indicate 38 | * other illegal uses of the null object. 39 | * 40 | * @author Tim Wagner 41 | * @copyright 2015 TechDivision GmbH 42 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 43 | * @link https://github.com/appserver-io/lang 44 | * @link http://www.appserver.io 45 | */ 46 | class NullPointerException extends \Exception 47 | { 48 | } 49 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/Number.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang; 22 | 23 | /** 24 | * This class is the abstract class of all numbers. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | abstract class Number extends Objct 33 | { 34 | 35 | /** 36 | * Returns the value of the specified number as an int. 37 | * This may involve rounding or truncation. 38 | * 39 | * @return integer The numeric value represented by this object after conversion to type int 40 | */ 41 | abstract public function intValue(); 42 | 43 | /** 44 | * Returns the value of the specified number as a float. 45 | * This may involve rounding. 46 | * 47 | * @return float The numeric value represented by this object after conversion to type float 48 | */ 49 | abstract public function floatValue(); 50 | 51 | /** 52 | * Returns the value of the specified number as a double. 53 | * This may involve rounding. 54 | * 55 | * @return double The numeric value represented by this object after conversion to type double 56 | */ 57 | abstract public function doubleValue(); 58 | } 59 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/NumberFormatException.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang; 22 | 23 | /** 24 | * Thrown to indicate that the application has attempted to convert 25 | * a string to one of the numeric types, but that the string does not 26 | * have the appropriate format. 27 | * 28 | * @author Tim Wagner 29 | * @copyright 2015 TechDivision GmbH 30 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 31 | * @link https://github.com/appserver-io/lang 32 | * @link http://www.appserver.io 33 | */ 34 | class NumberFormatException extends \Exception 35 | { 36 | 37 | /** 38 | * Factory method for making a NumberFormatException 39 | * given the specified input which caused the error. 40 | * 41 | * @param string $message The input causing the error 42 | * 43 | * @return void 44 | * @throws \AppserverIo\Lang\NumberFormatException The number format exception itself 45 | */ 46 | public static function forInputString($message) 47 | { 48 | throw new NumberFormatException('For input string: "' . $message . '"'); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/Objct.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang; 22 | 23 | /** 24 | * This class is the abstract class of all other classes. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | abstract class Objct 33 | { 34 | 35 | /** 36 | * This method returns the class as 37 | * a string representation. 38 | * 39 | * @return string The objects string representation 40 | */ 41 | public function __toString() 42 | { 43 | return get_class($this) . "@" . sha1(serialize($this)); 44 | } 45 | 46 | /** 47 | * This method returns the class name as 48 | * a string. 49 | * 50 | * @return string 51 | */ 52 | public static function __getClass() 53 | { 54 | return __CLASS__; 55 | } 56 | 57 | /** 58 | * This method checks if the passed object is equal 59 | * to itself. 60 | * 61 | * @param \AppserverIo\Lang\Objct $obj The object to check 62 | * 63 | * @return boolean Returns TRUE if the passed object is equal 64 | */ 65 | public function equals(Objct $obj) 66 | { 67 | if ($obj === $this) { 68 | return true; 69 | } 70 | return false; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/Reflection/AnnotationInterface.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang\Reflection; 22 | 23 | /** 24 | * A reflection annotation interface. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | interface AnnotationInterface 33 | { 34 | 35 | /** 36 | * Returns the annotation name. 37 | * 38 | * @return string The annotation name 39 | */ 40 | public function getAnnotationName(); 41 | 42 | /** 43 | * Returns the annotation values. 44 | * 45 | * @return array The annotation values 46 | */ 47 | public function getValues(); 48 | 49 | /** 50 | * Queries whether this annotation instance has a value with the passed key or not. 51 | * 52 | * @param string $key The key we want to query 53 | * 54 | * @return boolean TRUE if the value is available, else FALSE 55 | */ 56 | public function hasValue($key); 57 | 58 | /** 59 | * Returns the value for the passed key, if available. 60 | * 61 | * @param string $key The key of the value to return 62 | * 63 | * @return mixed|null The requested value 64 | */ 65 | public function getValue($key); 66 | 67 | /** 68 | * Sets the value with the passed key, existing values 69 | * are overwritten. 70 | * 71 | * @param string $key The key of the value 72 | * @param string $value The value to set 73 | * 74 | * @return void 75 | */ 76 | public function setValue($key, $value); 77 | 78 | /** 79 | * Returns a PHP reflection class representation of this instance. 80 | * 81 | * @return \ReflectionClass The PHP reflection class instance 82 | * @see \AppserverIo\Lang\Reflection\ClassInterface::toPhpReflectionClass() 83 | */ 84 | public function toPhpReflectionClass(); 85 | } 86 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/Reflection/ClassInterface.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang\Reflection; 22 | 23 | /** 24 | * A reflection class interface. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | interface ClassInterface 33 | { 34 | 35 | /** 36 | * Returns the class name. 37 | * 38 | * @return string The class name 39 | */ 40 | public function getName(); 41 | 42 | /** 43 | * Returns the short class name (without namespace). 44 | * 45 | * @return string The short class name 46 | */ 47 | public function getShortName(); 48 | 49 | /** 50 | * Returns an array with annotation names we want to ignore when loaded. 51 | * 52 | * @return array The annotation names we want to ignore 53 | */ 54 | public function getAnnotationsToIgnore(); 55 | 56 | /** 57 | * Returns an array with annotation aliases used when create annotation instances. 58 | * 59 | * @return array The annotation aliases used when create annotation instances 60 | */ 61 | public function getAnnotationAliases(); 62 | 63 | /** 64 | * Returns the class annotations. 65 | * 66 | * @return array The class annotations 67 | */ 68 | public function getAnnotations(); 69 | 70 | /** 71 | * Queries whether the reflection class has an annotation with the passed name or not. 72 | * 73 | * @param string $annotationName The annotation we want to query 74 | * 75 | * @return boolean TRUE if the reflection class has the annotation, else FALSE 76 | */ 77 | public function hasAnnotation($annotationName); 78 | 79 | /** 80 | * Returns the annotation instance with the passed name. 81 | * 82 | * @param string $annotationName The name of the requested annotation instance 83 | * 84 | * @return \AppserverIo\Lang\Reflection\AnnotationInterface|null The requested annotation instance 85 | */ 86 | public function getAnnotation($annotationName); 87 | 88 | /** 89 | * Returns the class methods. 90 | * 91 | * @param integer $filter Filter the results to include only methods with certain attributes 92 | * 93 | * @return array The class methods 94 | * @link http://php.net/manual/en/reflectionclass.getmethods.php 95 | */ 96 | public function getMethods($filter = 0); 97 | 98 | /** 99 | * Queries whether the reflection class has an method with the passed name or not. 100 | * 101 | * @param string $name The method we want to query 102 | * 103 | * @return boolean TRUE if the reflection class has the method, else FALSE 104 | */ 105 | public function hasMethod($name); 106 | 107 | /** 108 | * Returns the requested reflection method. 109 | * 110 | * @param string $name The name of the reflection method to return 111 | * 112 | * @return \AppserverIo\Lang\Reflection\ReflectionMethod The requested reflection method 113 | * @throws \AppserverIo\Lang\Reflection\ReflectionException Is thrown if the requested method is not available 114 | * @link http://php.net/manual/en/reflectionclass.getmethod.php 115 | */ 116 | public function getMethod($name); 117 | 118 | /** 119 | * Returns the class properties. 120 | * 121 | * @param integer $filter Filter the results to include only properties with certain attributes 122 | * 123 | * @return array The class properties 124 | * @link http://php.net/manual/en/reflectionclass.getproperties.php 125 | */ 126 | public function getProperties($filter = 0); 127 | 128 | /** 129 | * Queries whether the reflection class has an property with the passed name or not. 130 | * 131 | * @param string $name The property we want to query 132 | * 133 | * @return boolean TRUE if the reflection class has the property, else FALSE 134 | */ 135 | public function hasProperty($name); 136 | 137 | /** 138 | * Returns the requested reflection property. 139 | * 140 | * @param string $name The name of the reflection property to return 141 | * 142 | * @return \AppserverIo\Lang\Reflection\ReflectionProperty The requested reflection property 143 | * @throws \AppserverIo\Lang\Reflection\ReflectionException Is thrown if the requested property is not available 144 | * @link http://php.net/manual/en/reflectionclass.getproperty.php 145 | */ 146 | public function getProperty($name); 147 | 148 | /** 149 | * Returns a new annotation instance. 150 | * 151 | * You can pass a random number of arguments to this function. These 152 | * arguments will be passed to the constructor of the new instance. 153 | * 154 | * @return object A new annotation instance initialized with the passed arguments 155 | * @link http://php.net/manual/en/reflectionclass.newinstance.php 156 | */ 157 | public function newInstance(); 158 | 159 | /** 160 | * Returns a new annotation instance. 161 | * 162 | * @param array $args The arguments that will be passed to the instance constructor 163 | * 164 | * @return object A new annotation instance initialized with the passed arguments 165 | * @link http://php.net/manual/en/reflectionclass.newinstanceargs.php 166 | */ 167 | public function newInstanceArgs(array $args = array()); 168 | 169 | /** 170 | * Returns a PHP reflection class representation of this instance. 171 | * 172 | * @return \ReflectionClass The PHP reflection class instance 173 | */ 174 | public function toPhpReflectionClass(); 175 | 176 | /** 177 | * Registers the annotation alias for the passed class name. 178 | * 179 | * @param string $annotationName The alias 180 | * @param string $annotationClassName The resolving class name 181 | * 182 | * @return void 183 | */ 184 | public function addAnnotationAlias($annotationName, $annotationClassName); 185 | 186 | /** 187 | * Checks whether it implements the passed interface or not. 188 | * 189 | * @param string $interface The interface name 190 | * 191 | * @return boolean Returns TRUE on success or FALSE on failure 192 | * @link php.net/manual/en/reflectionclass.implementsinterface.php 193 | */ 194 | public function implementsInterface($interface); 195 | 196 | /** 197 | * Checks whether the class is an interface. 198 | * 199 | * @return boolean Returns TRUE on success or FALSE on failure 200 | * @link php.net/manual/en/reflectionclass.isinterface.php 201 | */ 202 | public function isInterface(); 203 | 204 | /** 205 | * Checks if the class is abstract. 206 | * 207 | * @return boolean Rturns TRUE on success or FALSE on failure 208 | * @link php.net/manual/en/reflectionclass.isabstract.php 209 | */ 210 | public function isAbstract(); 211 | } 212 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/Reflection/MethodInterface.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang\Reflection; 22 | 23 | /** 24 | * A reflection method interface. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | interface MethodInterface 33 | { 34 | 35 | /** 36 | * Returns the class name to invoke the method on. 37 | * 38 | * @return string The class name 39 | */ 40 | public function getClassName(); 41 | 42 | /** 43 | * Returns the method name to invoke on the class. 44 | * 45 | * @return string The method name 46 | */ 47 | public function getMethodName(); 48 | 49 | /** 50 | * Returns the method parameters. 51 | * 52 | * @return array The method parameters 53 | */ 54 | public function getParameters(); 55 | 56 | /** 57 | * Returns an array with annotation names we want to ignore when loaded. 58 | * 59 | * @return array The annotation names we want to ignore 60 | */ 61 | public function getAnnotationsToIgnore(); 62 | 63 | /** 64 | * Returns an array with annotation aliases used when create annotation instances. 65 | * 66 | * @return array The annotation aliases used when create annotation instances 67 | */ 68 | public function getAnnotationAliases(); 69 | 70 | /** 71 | * Returns the method annotations. 72 | * 73 | * @return array The method annotations 74 | */ 75 | public function getAnnotations(); 76 | 77 | /** 78 | * Queries whether the reflection method has an annotation with the passed name or not. 79 | * 80 | * @param string $annotationName The annotation we want to query 81 | * 82 | * @return boolean TRUE if the reflection method has the annotation, else FALSE 83 | */ 84 | public function hasAnnotation($annotationName); 85 | 86 | /** 87 | * Returns the annotation instance with the passed name. 88 | * 89 | * @param string $annotationName The name of the requested annotation instance 90 | * 91 | * @return \AppserverIo\Lang\Reflection\AnnotationInterface|null The requested annotation instance 92 | */ 93 | public function getAnnotation($annotationName); 94 | 95 | /** 96 | * Returns a PHP reflection method representation of this instance. 97 | * 98 | * @return \ReflectionMethod The PHP reflection method instance 99 | */ 100 | public function toPhpReflectionMethod(); 101 | 102 | /** 103 | * Registers the annotation alias for the passed class name. 104 | * 105 | * @param string $annotationName The alias 106 | * @param string $annotationClassName The resolving class name 107 | * 108 | * @return void 109 | */ 110 | public function addAnnotationAlias($annotationName, $annotationClassName); 111 | } 112 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/Reflection/ParameterInterface.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang\Reflection; 22 | 23 | /** 24 | * A reflection parameter interface. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | interface ParameterInterface 33 | { 34 | 35 | /** 36 | * Returns name of the class the parameter belongs to. 37 | * 38 | * @return string The class name 39 | */ 40 | public function getClassName(); 41 | 42 | /** 43 | * Returns name of the method the parameter belongs to. 44 | * 45 | * @return string The method name 46 | */ 47 | public function getMethodName(); 48 | 49 | /** 50 | * Returns the parameter name. 51 | * 52 | * @return string The parameter name 53 | */ 54 | public function getParameterName(); 55 | 56 | /** 57 | * Returns the parameters position. 58 | * 59 | * @return string The parameters position 60 | */ 61 | public function getPosition(); 62 | 63 | /** 64 | * Returns the parameters class name. 65 | * 66 | * @return string The parameters class name 67 | */ 68 | public function getType(); 69 | 70 | /** 71 | * Returns a PHP reflection parameter representation of this instance. 72 | * 73 | * @return \ReflectionParameter The PHP reflection parameter instance 74 | */ 75 | public function toPhpReflectionParameter(); 76 | } 77 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/Reflection/PropertyInterface.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang\Reflection; 22 | 23 | /** 24 | * A reflection property interface. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | interface PropertyInterface 33 | { 34 | 35 | /** 36 | * Returns the class name to invoke the method on. 37 | * 38 | * @return string The class name 39 | */ 40 | public function getClassName(); 41 | 42 | /** 43 | * Returns the property name. 44 | * 45 | * @return string The property name 46 | */ 47 | public function getPropertyName(); 48 | 49 | /** 50 | * Returns an array with annotation names we want to ignore when loaded. 51 | * 52 | * @return array The annotation names we want to ignore 53 | */ 54 | public function getAnnotationsToIgnore(); 55 | 56 | /** 57 | * Returns an array with annotation aliases used when create annotation instances. 58 | * 59 | * @return array The annotation aliases used when create annotation instances 60 | */ 61 | public function getAnnotationAliases(); 62 | 63 | /** 64 | * Returns the method annotations. 65 | * 66 | * @return array The method annotations 67 | */ 68 | public function getAnnotations(); 69 | 70 | /** 71 | * Queries whether the reflection method has an annotation with the passed name or not. 72 | * 73 | * @param string $annotationName The annotation we want to query 74 | * 75 | * @return boolean TRUE if the reflection method has the annotation, else FALSE 76 | */ 77 | public function hasAnnotation($annotationName); 78 | 79 | /** 80 | * Returns the annotation instance with the passed name. 81 | * 82 | * @param string $annotationName The name of the requested annotation instance 83 | * 84 | * @return \AppserverIo\Lang\Reflection\AnnotationInterface|null The requested annotation instance 85 | */ 86 | public function getAnnotation($annotationName); 87 | 88 | /** 89 | * Returns a PHP reflection property representation of this instance. 90 | * 91 | * @return \ReflectionProperty The PHP reflection property instance 92 | */ 93 | public function toPhpReflectionProperty(); 94 | 95 | /** 96 | * Registers the annotation alias for the passed class name. 97 | * 98 | * @param string $annotationName The alias 99 | * @param string $annotationClassName The resolving class name 100 | * 101 | * @return void 102 | */ 103 | public function addAnnotationAlias($annotationName, $annotationClassName); 104 | } 105 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/Reflection/ReflectionAnnotation.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang\Reflection; 22 | 23 | use AppserverIo\Lang\Objct; 24 | use Herrera\Annotations\Tokens; 25 | use Herrera\Annotations\Tokenizer; 26 | use Herrera\Annotations\Convert\ToArray; 27 | 28 | /** 29 | * A generic and serializable annotation implementation. 30 | * 31 | * @author Tim Wagner 32 | * @copyright 2015 TechDivision GmbH 33 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 34 | * @link https://github.com/appserver-io/lang 35 | * @link http://www.appserver.io 36 | */ 37 | class ReflectionAnnotation extends Objct implements AnnotationInterface, \Serializable 38 | { 39 | 40 | /** 41 | * The annotation name. 42 | * 43 | * @var string 44 | */ 45 | protected $annotationName; 46 | 47 | /** 48 | * The array with the annotation values. 49 | * 50 | * @var array 51 | */ 52 | protected $values; 53 | 54 | /** 55 | * The constructor the initializes the instance with the 56 | * data passed with the token. 57 | * 58 | * @param string $annotationName The annotation name 59 | * @param array $values The annotation values 60 | */ 61 | public function __construct($annotationName, array $values = array()) 62 | { 63 | // initialize property default values here, as declarative default values may break thread safety, 64 | // when utilizing static and non-static access on class methods within same thread context! 65 | $this->annotationName = ''; 66 | $this->values = array(); 67 | 68 | // set the annotation name 69 | $this->annotationName = $annotationName; 70 | 71 | // ATTENTION: We need to copy the values, because if not, it would not be 72 | // possible to pre-initialize them in an annotation implements 73 | // constructor! 74 | foreach ($values as $key => $value) { 75 | $this->values[$key] = $value; 76 | } 77 | } 78 | 79 | /** 80 | * This method returns the class name as 81 | * a string. 82 | * 83 | * @return string 84 | */ 85 | public static function __getClass() 86 | { 87 | return __CLASS__; 88 | } 89 | 90 | /** 91 | * Returns the annation name. 92 | * 93 | * @return string The annotation name 94 | */ 95 | public function getAnnotationName() 96 | { 97 | return $this->annotationName; 98 | } 99 | 100 | /** 101 | * Returns the annotation values. 102 | * 103 | * @return array The annotation values 104 | */ 105 | public function getValues() 106 | { 107 | return $this->values; 108 | } 109 | 110 | /** 111 | * Queries whether this annotation instance has a value with the passed key or not. 112 | * 113 | * @param string $key The key we want to query 114 | * 115 | * @return boolean TRUE if the value is available, else FALSE 116 | */ 117 | public function hasValue($key) 118 | { 119 | return isset($this->values[$key]); 120 | } 121 | 122 | /** 123 | * Returns the value for the passed key, if available. 124 | * 125 | * @param string|null $key The key of the value to return 126 | * 127 | * @return mixed|null The requested value 128 | */ 129 | public function getValue($key) 130 | { 131 | if ($this->hasValue($key)) { 132 | return $this->values[$key]; 133 | } 134 | } 135 | 136 | /** 137 | * Sets the value with the passed key, existing values 138 | * are overwritten. 139 | * 140 | * @param string $key The key of the value 141 | * @param string $value The value to set 142 | * 143 | * @return void 144 | */ 145 | public function setValue($key, $value) 146 | { 147 | $this->values[$key] = $value; 148 | } 149 | 150 | /** 151 | * String representation of object. 152 | * 153 | * @return string the string representation of the object or null 154 | * @link http://php.net/manual/en/serializable.serialize.php 155 | */ 156 | public function serialize() 157 | { 158 | return serialize(get_object_vars($this)); 159 | } 160 | 161 | /** 162 | * Constructs the object 163 | * 164 | * @param string $data The string representation of the object 165 | * 166 | * @return void 167 | * @link http://php.net/manual/en/serializable.unserialize.php 168 | */ 169 | public function unserialize($data) 170 | { 171 | foreach (unserialize($data) as $propertyName => $propertyValue) { 172 | $this->$propertyName = $propertyValue; 173 | } 174 | } 175 | 176 | /** 177 | * Returns a PHP reflection class representation of this instance. 178 | * 179 | * @return \ReflectionClass The PHP reflection class instance 180 | * @see \AppserverIo\Lang\Reflection\ClassInterface::toPhpReflectionClass() 181 | */ 182 | public function toPhpReflectionClass() 183 | { 184 | return new \ReflectionClass($this->getAnnotationName()); 185 | } 186 | 187 | /** 188 | * Returns a new annotation instance. 189 | * 190 | * You can pass a random number of arguments to this function. These 191 | * arguments will be passed to the constructor of the new instance. 192 | * 193 | * @return object A new annotation instance initialized with the passed arguments 194 | * @link http://de2.php.net/func_get_args 195 | */ 196 | public function newInstance() 197 | { 198 | return $this->newInstanceArgs(func_get_args()); 199 | } 200 | 201 | /** 202 | * Returns a new annotation instance. 203 | * 204 | * @param array $args The arguments that will be passed to the instance constructor 205 | * 206 | * @return object A new annotation instance initialized with the passed arguments 207 | */ 208 | public function newInstanceArgs(array $args = array()) 209 | { 210 | // create a reflection instance of the found annotation name 211 | $reflectionClass = $this->toPhpReflectionClass(); 212 | 213 | // create a new instance passing the found arguements to the constructor 214 | return $reflectionClass->newInstanceArgs($args); 215 | } 216 | 217 | /** 218 | * Initializes and returns an array with annotation instances from the 219 | * passed doc comment. 220 | * 221 | * @param string $docComment The doc comment to initialize the annotations from 222 | * @param array $ignore Array with annotations we want to ignore 223 | * @param array $aliases Array with aliases to create annotation instances with 224 | * 225 | * @return array The array with the ReflectionAnnotation instances loaded from the passed doc comment 226 | */ 227 | public static function fromDocComment($docComment, array $ignore = array(), array $aliases = array()) 228 | { 229 | 230 | // initialize the array for the annotations 231 | $annotations = array(); 232 | 233 | // initialize the annotation tokenizer 234 | $tokenizer = new Tokenizer(); 235 | $tokenizer->ignore($ignore); 236 | 237 | // parse the doc block 238 | $parsed = $tokenizer->parse($docComment, $aliases); 239 | 240 | // convert tokens and return one 241 | $tokens = new Tokens($parsed); 242 | $toArray = new ToArray(); 243 | 244 | // register annotations with the real annotation name (not the alias) 245 | foreach ($toArray->convert($tokens) as $token) { 246 | // check if we've an annotation that matched an alias 247 | if (array_key_exists($token->name, $flipped = array_flip($aliases))) { 248 | $annotationName = $flipped[$token->name]; 249 | } else { 250 | $annotationName = $token->name; 251 | } 252 | 253 | // register the annotation with the real annotation name (not the alias) 254 | $annotations[$annotationName] = ReflectionAnnotation::fromStdClass($token, $aliases); 255 | } 256 | 257 | // return the list with the annotation instances 258 | return $annotations; 259 | } 260 | 261 | /** 262 | * Initializes and returns a ReflectionAnnotation instance from the passed token. 263 | * 264 | * @param \stdClass $token The token to initialize and return the ReflectionAnnotation instance from 265 | * @param array $aliases The class aliases to use 266 | * 267 | * @return \AppserverIo\Lang\Reflection\ReflectionAnnotation The initialized ReflectionAnnotation instance 268 | */ 269 | private static function fromStdClass(\stdClass $token, array $aliases = array()) 270 | { 271 | 272 | // iterate over the tokens values to process them recursively 273 | foreach ($token->values as $name => $value) { 274 | // query whether we've an array 275 | if (is_array($value)) { 276 | // iterate over all values of the array an process them recursively 277 | foreach ($value as $key => $val) { 278 | // query whether we've a nested annotation 279 | if (is_object($val)) { 280 | $token->values[$name][$key] = ReflectionAnnotation::fromStdClass($val, $aliases); 281 | } 282 | } 283 | } 284 | 285 | // query whether we've a nested annotation 286 | if (is_object($value)) { 287 | $token->values[$name] = ReflectionAnnotation::fromStdClass($value, $aliases); 288 | } 289 | } 290 | 291 | // initialize and return the reflection annotation 292 | return new ReflectionAnnotation($token->name, $token->values); 293 | } 294 | 295 | /** 296 | * Initializes and returns an array with annotation instances from the doc comment 297 | * found in the passed reflection property instance. 298 | * 299 | * @param \AppserverIo\Lang\Reflection\PropertyInterface $reflectionProperty The reflection property to load the doc comment from 300 | * 301 | * @return array The array with the ReflectionAnnotation instances loaded from the passed reflection property 302 | * @see \AppserverIo\Lang\Reflection\ReflectionAnnotation::fromDocComment() 303 | */ 304 | public static function fromReflectionProperty(PropertyInterface $reflectionProperty) 305 | { 306 | 307 | // load the reflection method data we need to initialize the annotations 308 | $aliases = $reflectionProperty->getAnnotationAliases(); 309 | $ignore = $reflectionProperty->getAnnotationsToIgnore(); 310 | $docComment = $reflectionProperty->toPhpReflectionProperty()->getDocComment(); 311 | 312 | // load and return the annotations found in the doc comment 313 | return ReflectionAnnotation::fromDocComment($docComment, $ignore, $aliases); 314 | } 315 | 316 | /** 317 | * Initializes and returns an array with annotation instances from the doc comment 318 | * found in the passed reflection method instance. 319 | * 320 | * @param \AppserverIo\Lang\Reflection\MethodInterface $reflectionMethod The reflection method to load the doc comment from 321 | * 322 | * @return array The array with the ReflectionAnnotation instances loaded from the passed reflection method 323 | * @see \AppserverIo\Lang\Reflection\ReflectionAnnotation::fromDocComment() 324 | */ 325 | public static function fromReflectionMethod(MethodInterface $reflectionMethod) 326 | { 327 | 328 | // load the reflection method data we need to initialize the annotations 329 | $aliases = $reflectionMethod->getAnnotationAliases(); 330 | $ignore = $reflectionMethod->getAnnotationsToIgnore(); 331 | $docComment = $reflectionMethod->toPhpReflectionMethod()->getDocComment(); 332 | 333 | // load and return the annotations found in the doc comment 334 | return ReflectionAnnotation::fromDocComment($docComment, $ignore, $aliases); 335 | } 336 | 337 | /** 338 | * Initializes and returns an array with annotation instances from the doc comment 339 | * found in the passed reflection class instance. 340 | * 341 | * @param \AppserverIo\Lang\Reflection\ClassInterface $reflectionClass The reflection class to load the doc comment from 342 | * 343 | * @return array The array with the ReflectionAnnotation instances loaded from the passed reflection class 344 | * @see \AppserverIo\Lang\Reflection\ReflectionAnnotation::fromDocComment() 345 | */ 346 | public static function fromReflectionClass(ClassInterface $reflectionClass) 347 | { 348 | 349 | // load the reflection method data we need to initialize the annotations 350 | $aliases = $reflectionClass->getAnnotationAliases(); 351 | $ignore = $reflectionClass->getAnnotationsToIgnore(); 352 | $docComment = $reflectionClass->toPhpReflectionClass()->getDocComment(); 353 | 354 | // load and return the annotations found in the doc comment 355 | return ReflectionAnnotation::fromDocComment($docComment, $ignore, $aliases); 356 | } 357 | } 358 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/Reflection/ReflectionException.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang\Reflection; 22 | 23 | /** 24 | * A generic exceptions that is thrown if an unexpected error occurs during reflection handling. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | class ReflectionException extends \Exception 33 | { 34 | } 35 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/Reflection/ReflectionMethod.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang\Reflection; 22 | 23 | use AppserverIo\Lang\Objct; 24 | 25 | /** 26 | * A wrapper instance for a reflection method. 27 | * 28 | * @author Tim Wagner 29 | * @copyright 2015 TechDivision GmbH 30 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 31 | * @link https://github.com/appserver-io/lang 32 | * @link http://www.appserver.io 33 | */ 34 | class ReflectionMethod extends Objct implements MethodInterface, \Serializable 35 | { 36 | 37 | /** 38 | * Default filter for loading reflection methods from a reflection class. 39 | * 40 | * @var integer 41 | */ 42 | const ALL_MODIFIERS = -1; 43 | 44 | /** 45 | * The class name to invoke the method on. 46 | * 47 | * @var string 48 | */ 49 | protected $className; 50 | 51 | /** 52 | * The method name to invoke on the class. 53 | * 54 | * @var string 55 | */ 56 | protected $methodName; 57 | 58 | /** 59 | * The method parameters. 60 | * 61 | * @var string 62 | */ 63 | protected $parameters; 64 | 65 | /** 66 | * The method annotations. 67 | * 68 | * @var array 69 | */ 70 | protected $annotations; 71 | 72 | /** 73 | * Array with annotations names we want to ignore when loaded. 74 | * 75 | * @var array 76 | */ 77 | protected $annotationsToIgnore; 78 | 79 | /** 80 | * Array with annotation aliases used when create annotation instances. 81 | * 82 | * @var array 83 | */ 84 | protected $annotationAliases; 85 | 86 | /** 87 | * Initializes the timeout method with the passed data. 88 | * 89 | * @param string $className The class name to invoke the method on 90 | * @param string $methodName The method name to invoke on the class 91 | * @param array $annotationsToIgnore An array with annotations names we want to ignore when loaded 92 | * @param array $annotationAliases An array with annotation aliases used when create annotation instances 93 | */ 94 | public function __construct($className, $methodName, array $annotationsToIgnore = array(), array $annotationAliases = array()) 95 | { 96 | // initialize property default values here, as declarative default values may break thread safety, 97 | // when utilizing static and non-static access on class methods within same thread context! 98 | $this->className = ''; 99 | $this->methodName = ''; 100 | $this->parameters = null; 101 | $this->annotations = null; 102 | $this->annotationsToIgnore = array(); 103 | $this->annotationAliases = array(); 104 | 105 | $this->className = $className; 106 | $this->methodName = $methodName; 107 | $this->annotationsToIgnore = $annotationsToIgnore; 108 | $this->annotationAliases = $annotationAliases; 109 | } 110 | 111 | /** 112 | * This method returns the class name as 113 | * a string. 114 | * 115 | * @return string 116 | */ 117 | public static function __getClass() 118 | { 119 | return __CLASS__; 120 | } 121 | 122 | /** 123 | * Returns the class name to invoke the method on. 124 | * 125 | * @return string The class name 126 | * @see \AppserverIo\Lang\Reflection\MethodInterface::getClassName() 127 | */ 128 | public function getClassName() 129 | { 130 | return $this->className; 131 | } 132 | 133 | /** 134 | * Returns the method name to invoke on the class. 135 | * 136 | * @return string The method name 137 | * @see \AppserverIo\Lang\Reflection\MethodInterface::getMethodName() 138 | */ 139 | public function getMethodName() 140 | { 141 | return $this->methodName; 142 | } 143 | 144 | /** 145 | * Returns an array with annotation names we want to ignore when loaded. 146 | * 147 | * @return array The annotation names we want to ignore 148 | * @see \AppserverIo\Lang\Reflection\MethodInterface::getAnnotationsToIgnore() 149 | */ 150 | public function getAnnotationsToIgnore() 151 | { 152 | return $this->annotationsToIgnore; 153 | } 154 | 155 | /** 156 | * Returns an array with annotation aliases used when create annotation instances. 157 | * 158 | * @return array The annotation aliases used when create annotation instances 159 | * @see \AppserverIo\Lang\Reflection\MethodInterface::getAnnotationAliases() 160 | */ 161 | public function getAnnotationAliases() 162 | { 163 | return $this->annotationAliases; 164 | } 165 | 166 | /** 167 | * Registers the annotation alias for the passed class name. 168 | * 169 | * @param string $annotationName The alias 170 | * @param string $annotationClassName The resolving class name 171 | * 172 | * @return void 173 | * @see \AppserverIo\Lang\Reflection\MethodInterface::addAnnotationAlias() 174 | */ 175 | public function addAnnotationAlias($annotationName, $annotationClassName) 176 | { 177 | $this->annotationAliases[$annotationName] = $annotationClassName; 178 | } 179 | 180 | /** 181 | * Returns the method parameters. 182 | * 183 | * @return array The method parameters 184 | * @see \AppserverIo\Lang\Reflection\MethodInterface::getParameters() 185 | */ 186 | public function getParameters() 187 | { 188 | 189 | // check if the parameters has been loaded 190 | if (isset($this->parameters) === false) { 191 | $this->parameters = ReflectionParameter::fromReflectionMethod($this); 192 | } 193 | 194 | // return the parameters 195 | return $this->parameters; 196 | } 197 | 198 | /** 199 | * Returns the method annotations. 200 | * 201 | * @return array The method annotations 202 | * @see \AppserverIo\Lang\Reflection\MethodInterface::getAnnotations() 203 | */ 204 | public function getAnnotations() 205 | { 206 | 207 | // check if the annotations has been loaded 208 | if (isset($this->annotations) === false) { 209 | $this->annotations = ReflectionAnnotation::fromReflectionMethod($this); 210 | } 211 | 212 | // return the annotations 213 | return $this->annotations; 214 | } 215 | 216 | /** 217 | * Queries whether the reflection method has an annotation with the passed name or not. 218 | * 219 | * @param string $annotationName The annotation we want to query 220 | * 221 | * @return boolean TRUE if the reflection method has the annotation, else FALSE 222 | * @see \AppserverIo\Lang\Reflection\MethodInterface::hasAnnotation() 223 | */ 224 | public function hasAnnotation($annotationName) 225 | { 226 | $annotations = $this->getAnnotations(); 227 | return isset($annotations[$annotationName]); 228 | } 229 | 230 | /** 231 | * Returns the annotation instance with the passed name. 232 | * 233 | * @param string $annotationName The name of the requested annotation instance 234 | * 235 | * @return \AppserverIo\Lang\Reflection\AnnotationInterface|null The requested annotation instance 236 | * @throws \AppserverIo\Lang\Reflection\ReflectionException Is thrown if the requested annotation is not available 237 | * @see \AppserverIo\Lang\Reflection\MethodInterface::hasAnnotation() 238 | */ 239 | public function getAnnotation($annotationName) 240 | { 241 | 242 | // first check if the method is available 243 | $annotations = $this->getAnnotations(); 244 | if (isset($annotations[$annotationName])) { 245 | // if yes, return it 246 | return $annotations[$annotationName]; 247 | } 248 | 249 | // if not, throw an exception 250 | throw new ReflectionException(sprintf('The requested reflection annotation %s is not available', $annotationName)); 251 | } 252 | 253 | /** 254 | * Serializes the timeout method and returns a string representation. 255 | * 256 | * @return string The serialized string representation of the instance 257 | * @see \Serializable::serialize() 258 | */ 259 | public function serialize() 260 | { 261 | return serialize(get_object_vars($this)); 262 | } 263 | 264 | /** 265 | * Restores the instance with the serialized data of the passed string. 266 | * 267 | * @param string $data The serialized method representation 268 | * 269 | * @return void 270 | * @see \Serializable::unserialize() 271 | */ 272 | public function unserialize($data) 273 | { 274 | foreach (unserialize($data) as $propertyName => $propertyValue) { 275 | $this->$propertyName = $propertyValue; 276 | } 277 | } 278 | 279 | /** 280 | * Invokes a reflected method. You cann pass a random number of additional 281 | * parameters that'll be passed to the method as parameters. 282 | * 283 | * @param object $object The object to invoke the method on 284 | * 285 | * @return mixed Returns the method result 286 | * @see \AppserverIo\Lang\Reflection\ReflectionMethod::invokeArgs() 287 | * @link http://php.net/manual/en/reflectionmethod.invoke.php 288 | */ 289 | public function invoke($object) 290 | { 291 | $args = func_get_args(); // load the arguments, drop the first one (because this is always the instance itself) 292 | return $this->invokeArgs($object, array_splice($args, 1, 1)); 293 | } 294 | 295 | /** 296 | * Invokes the reflected method and pass its arguments as array. 297 | * 298 | * @param object $object The object to invoke the method on 299 | * @param array $args The parameters to be passed to the function, as an array 300 | * 301 | * @return mixed Returns the method result 302 | * @link http://php.net/manual/en/reflectionmethod.invokeargs.php 303 | */ 304 | public function invokeArgs($object, array $args = array()) 305 | { 306 | return $this->toPhpReflectionMethod()->invokeArgs($object, $args); 307 | } 308 | 309 | /** 310 | * Returns a PHP reflection method representation of this instance. 311 | * 312 | * @return \ReflectionMethod The PHP reflection method instance 313 | * @see \AppserverIo\Lang\Reflection\MethodInterface::toPhpReflectionMethod() 314 | */ 315 | public function toPhpReflectionMethod() 316 | { 317 | return new \ReflectionMethod($this->getClassName(), $this->getMethodName()); 318 | } 319 | 320 | /** 321 | * Returns an array of reflection method instances from the passed reflection class. 322 | * 323 | * @param \AppserverIo\Lang\Reflection\ReflectionClass $reflectionClass The reflection class to return the methods for 324 | * @param integer $filter The filter used for loading the methods 325 | * @param array $annotationsToIgnore An array with annotations names we want to ignore when loaded 326 | * @param array $annotationAliases An array with annotation aliases used when create annotation instances 327 | * 328 | * @return array An array with ReflectionMethod instances 329 | */ 330 | public static function fromReflectionClass(ReflectionClass $reflectionClass, $filter = -1, array $annotationsToIgnore = array(), array $annotationAliases = array()) 331 | { 332 | 333 | // initialize the array for the reflection methods 334 | $reflectionMethods = array(); 335 | 336 | // load the reflection methods and initialize the array with the reflection methods 337 | $phpReflectionClass = $reflectionClass->toPhpReflectionClass(); 338 | foreach ($phpReflectionClass->getMethods($filter) as $phpReflectionMethod) { 339 | $reflectionMethods[$phpReflectionMethod->getName()] = ReflectionMethod::fromPhpReflectionMethod($phpReflectionMethod, $annotationsToIgnore, $annotationAliases); 340 | } 341 | 342 | // return the array with the initialized reflection methods 343 | return $reflectionMethods; 344 | } 345 | 346 | /** 347 | * Creates a new reflection method instance from the passed PHP reflection method. 348 | * 349 | * @param \ReflectionMethod $reflectionMethod The reflection method to load the data from 350 | * @param array $annotationsToIgnore An array with annotations names we want to ignore when loaded 351 | * @param array $annotationAliases An array with annotation aliases used when create annotation instances 352 | * 353 | * @return \AppserverIo\Lang\Reflection\ReflectionMethod The instance 354 | */ 355 | public static function fromPhpReflectionMethod(\ReflectionMethod $reflectionMethod, array $annotationsToIgnore = array(), array $annotationAliases = array()) 356 | { 357 | 358 | // load class and method name from the reflection class 359 | $className = $reflectionMethod->getDeclaringClass()->getName(); 360 | $methodName = $reflectionMethod->getName(); 361 | 362 | // initialize and return the timeout method instance 363 | return new ReflectionMethod($className, $methodName, $annotationsToIgnore, $annotationAliases); 364 | } 365 | } 366 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/Reflection/ReflectionObject.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang\Reflection; 22 | 23 | /** 24 | * A wrapper instance for a reflection object. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | class ReflectionObject extends ReflectionClass 33 | { 34 | 35 | /** 36 | * Initializes the timed object with the passed data. 37 | * 38 | * @param object $object The object to create the reflection object for 39 | * @param array $annotationsToIgnore An array with annotations names we want to ignore when loaded 40 | * @param array $annotationAliases An array with annotation aliases used when create annotation instances 41 | */ 42 | public function __construct($object, array $annotationsToIgnore = array(), array $annotationAliases = array()) 43 | { 44 | parent::__construct(get_class($object), $annotationsToIgnore, $annotationAliases); 45 | } 46 | 47 | /** 48 | * This method returns the class name as 49 | * a string. 50 | * 51 | * @return string 52 | */ 53 | public static function __getClass() 54 | { 55 | return __CLASS__; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/Reflection/ReflectionParameter.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang\Reflection; 22 | 23 | use AppserverIo\Lang\Objct; 24 | 25 | /** 26 | * A wrapper instance for a reflection parameter. 27 | * 28 | * @author Tim Wagner 29 | * @copyright 2015 TechDivision GmbH 30 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 31 | * @link https://github.com/appserver-io/lang 32 | * @link http://www.appserver.io 33 | */ 34 | class ReflectionParameter extends Objct implements ParameterInterface, \Serializable 35 | { 36 | 37 | /** 38 | * The name of the class the parameter belongs to. 39 | * 40 | * @var string 41 | */ 42 | protected $className; 43 | 44 | /** 45 | * The name of the method the parameter belongs to. 46 | * 47 | * @var string 48 | */ 49 | protected $methodName; 50 | 51 | /** 52 | * The parameter name. 53 | * 54 | * @var string 55 | */ 56 | protected $parameterName; 57 | 58 | /** 59 | * Initializes the reflection parameter with the passed data. 60 | * 61 | * @param string $className The name of the class the parameter belongs to 62 | * @param string $methodName The name of the method the parameter belongs to 63 | * @param string $parameterName The parameter name 64 | */ 65 | public function __construct($className, $methodName, $parameterName) 66 | { 67 | // initialize property default values here, as declarative default values may break thread safety, 68 | // when utilizing static and non-static access on class methods within same thread context! 69 | $this->className = ''; 70 | $this->methodName = ''; 71 | $this->parameterName = ''; 72 | 73 | $this->className = $className; 74 | $this->methodName = $methodName; 75 | $this->parameterName = $parameterName; 76 | } 77 | 78 | /** 79 | * This method returns the class name as 80 | * a string. 81 | * 82 | * @return string 83 | */ 84 | public static function __getClass() 85 | { 86 | return __CLASS__; 87 | } 88 | 89 | /** 90 | * Returns name of the class the parameter belongs to. 91 | * 92 | * @return string The class name 93 | * @see \AppserverIo\Lang\Reflection\ParameterInterface::getClassName() 94 | */ 95 | public function getClassName() 96 | { 97 | return $this->className; 98 | } 99 | 100 | /** 101 | * Returns name of the method the parameter belongs to. 102 | * 103 | * @return string The method name 104 | * @see \AppserverIo\Lang\Reflection\ParameterInterface::getMethodName() 105 | */ 106 | public function getMethodName() 107 | { 108 | return $this->methodName; 109 | } 110 | 111 | /** 112 | * Returns the parameter position. 113 | * 114 | * @return string The parameter position 115 | * @see \AppserverIo\Lang\Reflection\ParameterInterface::getParameterName() 116 | */ 117 | public function getParameterName() 118 | { 119 | return $this->parameterName; 120 | } 121 | 122 | /** 123 | * Returns the parameters position. 124 | * 125 | * @return string The parameters position 126 | * @see \AppserverIo\Lang\Reflection\ParameterInterface::getPosition() 127 | */ 128 | public function getPosition() 129 | { 130 | return $this->toPhpReflectionParameter()->getPosition(); 131 | } 132 | 133 | /** 134 | * Returns the parameters class name. 135 | * 136 | * @return string The parameters class name 137 | * @see \AppserverIo\Lang\Reflection\ParameterInterface::getType() 138 | */ 139 | public function getType() 140 | { 141 | return $this->toPhpReflectionParameter()->getClass()->getName(); 142 | } 143 | 144 | /** 145 | * Serializes the timeout method and returns a string representation. 146 | * 147 | * @return string The serialized string representation of the instance 148 | * @see \Serializable::serialize() 149 | */ 150 | public function serialize() 151 | { 152 | return serialize(get_object_vars($this)); 153 | } 154 | 155 | /** 156 | * Restores the instance with the serialized data of the passed string. 157 | * 158 | * @param string $data The serialized property representation 159 | * 160 | * @return void 161 | * @see \Serializable::unserialize() 162 | */ 163 | public function unserialize($data) 164 | { 165 | foreach (unserialize($data) as $propertyName => $propertyValue) { 166 | $this->$propertyName = $propertyValue; 167 | } 168 | } 169 | 170 | /** 171 | * Returns a PHP reflection parameter representation of this instance. 172 | * 173 | * @return \ReflectionProperty The PHP reflection parameter instance 174 | * @see \AppserverIo\Lang\Reflection\ParameterInterface::toPhpReflectionParameter() 175 | */ 176 | public function toPhpReflectionParameter() 177 | { 178 | return new \ReflectionParameter(array($this->getClassName(), $this->getMethodName()), $this->getParameterName()); 179 | } 180 | 181 | /** 182 | * Returns an array of reflection parameter instances from the passed reflection method. 183 | * 184 | * @param \AppserverIo\Lang\Reflection\ReflectionMethod $reflectionMethod The reflection method to return the parameters for 185 | * 186 | * @return array An array with ReflectionParameter instances 187 | */ 188 | public static function fromReflectionMethod(ReflectionMethod $reflectionMethod) 189 | { 190 | 191 | // initialize the array for the reflection parameters 192 | $reflectionParameters = array(); 193 | 194 | // load the reflection parameters and initialize the array with the reflection parameters 195 | $phpReflectionMethod = $reflectionMethod->toPhpReflectionMethod(); 196 | foreach ($phpReflectionMethod->getParameters() as $phpReflectionParameter) { 197 | $reflectionParameters[$phpReflectionParameter->getName()] = ReflectionParameter::fromPhpReflectionParameter($phpReflectionParameter); 198 | } 199 | 200 | // return the array with the initialized reflection parameters 201 | return $reflectionParameters; 202 | } 203 | 204 | /** 205 | * Creates a new reflection parameter instance from the passed PHP reflection parameter. 206 | * 207 | * @param \ReflectionParameter $reflectionParameter The reflection parameter to load the data from 208 | * 209 | * @return \AppserverIo\Lang\Reflection\ReflectionParameter The instance 210 | */ 211 | public static function fromPhpReflectionParameter(\ReflectionParameter $reflectionParameter) 212 | { 213 | 214 | // load class, method and parameter name from the reflection parameter 215 | $className = $reflectionParameter->getDeclaringClass()->getName(); 216 | $methodName = $reflectionParameter->getDeclaringFunction()->getName(); 217 | $parameterName = $reflectionParameter->getName(); 218 | 219 | // initialize and return the parameter instance 220 | return new ReflectionParameter($className, $methodName, $parameterName); 221 | } 222 | } 223 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/Reflection/ReflectionProperty.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang\Reflection; 22 | 23 | use AppserverIo\Lang\Objct; 24 | 25 | /** 26 | * A wrapper instance for a reflection property. 27 | * 28 | * @author Tim Wagner 29 | * @copyright 2015 TechDivision GmbH 30 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 31 | * @link https://github.com/appserver-io/lang 32 | * @link http://www.appserver.io 33 | */ 34 | class ReflectionProperty extends Objct implements PropertyInterface, \Serializable 35 | { 36 | 37 | /** 38 | * Default filter for loading reflection properties from a reflection class. 39 | * 40 | * @var integer 41 | */ 42 | const ALL_MODIFIERS = -1; 43 | 44 | /** 45 | * The properties class name. 46 | * 47 | * @var string 48 | */ 49 | protected $className; 50 | 51 | /** 52 | * The property name. 53 | * 54 | * @var string 55 | */ 56 | protected $propertyName; 57 | 58 | /** 59 | * The method annotations. 60 | * 61 | * @var array 62 | */ 63 | protected $annotations; 64 | 65 | /** 66 | * Array with annotations names we want to ignore when loaded. 67 | * 68 | * @var array 69 | */ 70 | protected $annotationsToIgnore; 71 | 72 | /** 73 | * Array with annotation aliases used when create annotation instances. 74 | * 75 | * @var array 76 | */ 77 | protected $annotationAliases; 78 | 79 | /** 80 | * Initializes the reflection property with the passed data. 81 | * 82 | * @param string $className The properties class name 83 | * @param string $propertyName The property name 84 | * @param array $annotationsToIgnore An array with annotations names we want to ignore when loaded 85 | * @param array $annotationAliases An array with annotation aliases used when create annotation instances 86 | */ 87 | public function __construct($className, $propertyName, array $annotationsToIgnore = array(), array $annotationAliases = array()) 88 | { 89 | // initialize property default values here, as declarative default values may break thread safety, 90 | // when utilizing static and non-static access on class methods within same thread context! 91 | $this->className = ''; 92 | $this->propertyName = ''; 93 | $this->annotations = null; 94 | $this->annotationsToIgnore = array(); 95 | $this->annotationAliases = array(); 96 | 97 | $this->className = $className; 98 | $this->propertyName = $propertyName; 99 | $this->annotationsToIgnore = $annotationsToIgnore; 100 | $this->annotationAliases = $annotationAliases; 101 | } 102 | 103 | /** 104 | * This method returns the class name as 105 | * a string. 106 | * 107 | * @return string 108 | */ 109 | public static function __getClass() 110 | { 111 | return __CLASS__; 112 | } 113 | 114 | /** 115 | * Returns the properties class name. 116 | * 117 | * @return string The class name 118 | * @see \AppserverIo\Lang\Reflection\PropertyInterface::getClassName() 119 | */ 120 | public function getClassName() 121 | { 122 | return $this->className; 123 | } 124 | 125 | /** 126 | * Returns the property name. 127 | * 128 | * @return string The property name 129 | * @see \AppserverIo\Lang\Reflection\PropertyInterface::getPropertyName() 130 | */ 131 | public function getPropertyName() 132 | { 133 | return $this->propertyName; 134 | } 135 | 136 | /** 137 | * Returns an array with annotation names we want to ignore when loaded. 138 | * 139 | * @return array The annotation names we want to ignore 140 | * @see \AppserverIo\Lang\Reflection\PropertyInterface::getAnnotationsToIgnore() 141 | */ 142 | public function getAnnotationsToIgnore() 143 | { 144 | return $this->annotationsToIgnore; 145 | } 146 | 147 | /** 148 | * Returns an array with annotation aliases used when create annotation instances. 149 | * 150 | * @return array The annotation aliases used when create annotation instances 151 | * @see \AppserverIo\Lang\Reflection\PropertyInterface::getAnnotationAliases() 152 | */ 153 | public function getAnnotationAliases() 154 | { 155 | return $this->annotationAliases; 156 | } 157 | 158 | /** 159 | * Registers the annotation alias for the passed class name. 160 | * 161 | * @param string $annotationName The alias 162 | * @param string $annotationClassName The resolving class name 163 | * 164 | * @return void 165 | * @see \AppserverIo\Lang\Reflection\PropertyInterface::addAnnotationAlias() 166 | */ 167 | public function addAnnotationAlias($annotationName, $annotationClassName) 168 | { 169 | $this->annotationAliases[$annotationName] = $annotationClassName; 170 | } 171 | 172 | /** 173 | * Returns the method annotations. 174 | * 175 | * @return array The method annotations 176 | * @see \AppserverIo\Lang\Reflection\PropertyInterface::getAnnotations() 177 | */ 178 | public function getAnnotations() 179 | { 180 | 181 | // check if the annotations has been loaded 182 | if (isset($this->annotations) === false) { 183 | $this->annotations = ReflectionAnnotation::fromReflectionProperty($this); 184 | } 185 | 186 | // return the annotations 187 | return $this->annotations; 188 | } 189 | 190 | /** 191 | * Queries whether the reflection method has an annotation with the passed name or not. 192 | * 193 | * @param string $annotationName The annotation we want to query 194 | * 195 | * @return boolean TRUE if the reflection method has the annotation, else FALSE 196 | * @see \AppserverIo\Lang\Reflection\PropertyInterface::hasAnnotation() 197 | */ 198 | public function hasAnnotation($annotationName) 199 | { 200 | $annotations = $this->getAnnotations(); 201 | return isset($annotations[$annotationName]); 202 | } 203 | 204 | /** 205 | * Returns the annotation instance with the passed name. 206 | * 207 | * @param string $annotationName The name of the requested annotation instance 208 | * 209 | * @return \AppserverIo\Lang\Reflection\AnnotationInterface|null The requested annotation instance 210 | * @throws \AppserverIo\Lang\Reflection\ReflectionException Is thrown if the requested annotation is not available 211 | * @see \AppserverIo\Lang\Reflection\PropertyInterface::hasAnnotation() 212 | */ 213 | public function getAnnotation($annotationName) 214 | { 215 | 216 | // first check if the method is available 217 | $annotations = $this->getAnnotations(); 218 | if (isset($annotations[$annotationName])) { 219 | // if yes, return it 220 | return $annotations[$annotationName]; 221 | } 222 | 223 | // if not, throw an exception 224 | throw new ReflectionException(sprintf('The requested reflection annotation %s is not available', $annotationName)); 225 | } 226 | 227 | /** 228 | * Serializes the timeout method and returns a string representation. 229 | * 230 | * @return string The serialized string representation of the instance 231 | * @see \Serializable::serialize() 232 | */ 233 | public function serialize() 234 | { 235 | return serialize(get_object_vars($this)); 236 | } 237 | 238 | /** 239 | * Restores the instance with the serialized data of the passed string. 240 | * 241 | * @param string $data The serialized property representation 242 | * 243 | * @return void 244 | * @see \Serializable::unserialize() 245 | */ 246 | public function unserialize($data) 247 | { 248 | foreach (unserialize($data) as $propertyName => $propertyValue) { 249 | $this->$propertyName = $propertyValue; 250 | } 251 | } 252 | 253 | /** 254 | * Returns a PHP reflection property representation of this instance. 255 | * 256 | * @return \ReflectionProperty The PHP reflection property instance 257 | * @see \AppserverIo\Lang\Reflection\PropertyInterface::toPhpReflectionProperty() 258 | */ 259 | public function toPhpReflectionProperty() 260 | { 261 | return new \ReflectionProperty($this->getClassName(), $this->getPropertyName()); 262 | } 263 | 264 | /** 265 | * Returns an array of reflection property instances from the passed reflection class. 266 | * 267 | * @param \AppserverIo\Lang\Reflection\ReflectionClass $reflectionClass The reflection class to return the properties for 268 | * @param integer $filter The filter used for loading the properties 269 | * @param array $annotationsToIgnore An array with annotations names we want to ignore when loaded 270 | * @param array $annotationAliases An array with annotation aliases used when create annotation instances 271 | * 272 | * @return array An array with ReflectionProperty instances 273 | */ 274 | public static function fromReflectionClass(ReflectionClass $reflectionClass, $filter = 0, array $annotationsToIgnore = array(), array $annotationAliases = array()) 275 | { 276 | 277 | // initialize the array for the reflection properties 278 | $reflectionProperties = array(); 279 | 280 | // load the reflection properties and initialize the array with the reflection properties 281 | $phpReflectionClass = $reflectionClass->toPhpReflectionClass(); 282 | foreach ($phpReflectionClass->getProperties($filter) as $phpReflectionProperty) { 283 | $reflectionProperties[$phpReflectionProperty->getName()] = ReflectionProperty::fromPhpReflectionProperty($phpReflectionProperty, $annotationsToIgnore, $annotationAliases); 284 | } 285 | 286 | // return the array with the initialized reflection properties 287 | return $reflectionProperties; 288 | } 289 | 290 | /** 291 | * Creates a new reflection property instance from the passed PHP reflection property. 292 | * 293 | * @param \ReflectionProperty $reflectionProperty The reflection property to load the data from 294 | * @param array $annotationsToIgnore An array with annotations names we want to ignore when loaded 295 | * @param array $annotationAliases An array with annotation aliases used when create annotation instances 296 | * 297 | * @return \AppserverIo\Lang\Reflection\ReflectionProperty The instance 298 | */ 299 | public static function fromPhpReflectionProperty(\ReflectionProperty $reflectionProperty, array $annotationsToIgnore = array(), array $annotationAliases = array()) 300 | { 301 | 302 | // load class and method name from the reflection class 303 | $className = $reflectionProperty->getDeclaringClass()->getName(); 304 | $propertyName = $reflectionProperty->getName(); 305 | 306 | // initialize and return the timeout method instance 307 | return new ReflectionProperty($className, $propertyName, $annotationsToIgnore, $annotationAliases); 308 | } 309 | } 310 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/Strng.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang; 22 | 23 | /** 24 | * This class implements functionality to handle 25 | * a string value as object. 26 | * 27 | * @author Tim Wagner 28 | * @copyright 2015 TechDivision GmbH 29 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 30 | * @link https://github.com/appserver-io/lang 31 | * @link http://www.appserver.io 32 | */ 33 | class Strng extends Objct implements \Serializable 34 | { 35 | 36 | /** 37 | * Holds the chars the String contains of. 38 | * 39 | * @var array 40 | */ 41 | protected $value; 42 | 43 | /** 44 | * The length of the String. 45 | * 46 | * @var integer 47 | */ 48 | protected $length; 49 | 50 | /** 51 | * The cached hash of the String itself. 52 | * 53 | * @var integer 54 | */ 55 | protected $hash; 56 | 57 | /** 58 | * Initializes a newly created String object so that it 59 | * represents the same sequence of characters as the argument; in other 60 | * words, the newly created string is a copy of the argument string. 61 | * Unless 62 | * an explicit copy of value is needed, use of this 63 | * constructor is unnecessary since Strings are immutable. 64 | * 65 | * @param mixed $value Holds the value to initialize the String instance with 66 | */ 67 | public function __construct($value = null) 68 | { 69 | // initialize property default values here, as declarative default values may break thread safety, 70 | // when utilizing static and non-static access on class methods within same thread context! 71 | $this->value = ''; 72 | $this->length = 0; 73 | $this->hash = 0; 74 | 75 | $this->init($value); 76 | } 77 | 78 | /** 79 | * Initializes the string and returns the 80 | * instance. 81 | * 82 | * @param mixed $value The value to initialize the instance with 83 | * 84 | * @return \AppserverIo\Lang\Strng The initialized instance 85 | */ 86 | protected function init($value) 87 | { 88 | // check if a value was passed 89 | if (! is_null($value)) { 90 | // if yes, cast and set it 91 | $this->value = (string) $value; 92 | $this->length = strlen($this->value); 93 | } 94 | // return the instance 95 | return $this; 96 | } 97 | 98 | /** 99 | * Initializes a new String instance with the passed value 100 | * and returns it. 101 | * 102 | * @param mixed $value The value to initialize the String with 103 | * 104 | * @return \AppserverIo\Lang\Strng The initialized String instance 105 | */ 106 | public static function valueOf($value) 107 | { 108 | return new Strng($value); 109 | } 110 | 111 | /** 112 | * This method returns the class name as 113 | * a string. 114 | * 115 | * @return string 116 | */ 117 | public static function __getClass() 118 | { 119 | return __CLASS__; 120 | } 121 | 122 | /** 123 | * A copy of this object is returned. 124 | * 125 | * @return \AppserverIo\Lang\Strng A copy of the String itself. 126 | */ 127 | public function toString() 128 | { 129 | return new Strng($this->stringValue()); 130 | } 131 | 132 | /** 133 | * This returns the string value of 134 | * the String itself. 135 | * 136 | * @return string Returns the string value of itself 137 | */ 138 | public function __toString() 139 | { 140 | return $this->stringValue(); 141 | } 142 | 143 | /** 144 | * Returns a new String, containing the concatenated value 145 | * of the this string with the passed one. 146 | * 147 | * @param \AppserverIo\Lang\Strng $string The String to concatenate 148 | * 149 | * @return \AppserverIo\Lang\Strng The concatenated String 150 | */ 151 | public function concat(Strng $string) 152 | { 153 | return new Strng($this->stringValue() . $string->stringValue()); 154 | } 155 | 156 | /** 157 | * Returns the length of this string. 158 | * The length is equal to the number of 16-bit 159 | * Unicode characters in the string. 160 | * 161 | * @return integer The length of the sequence of characters represented by this object. 162 | */ 163 | public function length() 164 | { 165 | return $this->length; 166 | } 167 | 168 | /** 169 | * Returns the value as string. 170 | * 171 | * @return string The string value represented by this object 172 | */ 173 | public function stringValue() 174 | { 175 | return $this->value; 176 | } 177 | 178 | /** 179 | * Returns a new string resulting from replacing all occurrences of 180 | * oldChar in this string with newChar. 181 | *

182 | * If the character oldChar does not occur in the 183 | * character sequence represented by this String object, 184 | * then a reference to this String object is returned. 185 | * Otherwise, a new String object is created that 186 | * represents a character sequence identical to the character sequence 187 | * represented by this String object, except that every 188 | * occurrence of oldChar is replaced by an occurrence 189 | * of newChar. 190 | *

191 | * Examples: 192 | *

193 |      * "mesquite in your cellar".replace('e', 'o')
194 |      * returns "mosquito in your collar"
195 |      * "the war of baronets".replace('r', 'y')
196 |      * returns "the way of bayonets"
197 |      * "sparring with a purple porpoise".replace('p', 't')
198 |      * returns "starring with a turtle tortoise"
199 |      * "JonL".replace('q', 'x') returns "JonL" (no change)
200 |      * 
201 | * 202 | * @param string $oldChar The old character 203 | * @param string $newChar The new character 204 | * 205 | * @return \AppserverIo\Lang\Strng A string derived from this string by replacing every occurrence of oldChar with newChar 206 | */ 207 | public function replace($oldChar, $newChar) 208 | { 209 | return new Strng(str_replace($oldChar, $newChar, $this->stringValue())); 210 | } 211 | 212 | /** 213 | * Returns true if the passed value is equal. 214 | * 215 | * @param \AppserverIo\Lang\Objct $val The value to check 216 | * 217 | * @return boolean 218 | */ 219 | public function equals(Objct $val) 220 | { 221 | return $this->stringValue() == $val->stringValue(); 222 | } 223 | 224 | /** 225 | * Returns a new String that is a substring of this String. 226 | * The 227 | * substring begins at the specified beginIndex and 228 | * extends to the character at index endIndex - 1. 229 | * Thus the length of the substring is endIndex-beginIndex. 230 | *

231 | * Examples: 232 | *

233 |      * $hamburger = new String("hamburger");
234 |      * $hamburger->substring(4, 8) returns "urge"
235 |      * 
236 | * 237 | * @param integer $beginIndex The beginning index, inclusive 238 | * @param integer $endIndex The ending index, exclusive 239 | * 240 | * @return \AppserverIo\Lang\Strng The specified substring 241 | * @exception \AppserverIo\Lang\StrngIndexOutOfBoundsException if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex. 242 | */ 243 | public function substring($beginIndex, $endIndex) 244 | { 245 | if ($beginIndex < 0) { 246 | StrngIndexOutOfBoundsException::forIndex($beginIndex); 247 | } 248 | if ($endIndex > $this->length()) { 249 | StrngIndexOutOfBoundsException::forIndex($endIndex); 250 | } 251 | if ($beginIndex > $endIndex) { 252 | StrngIndexOutOfBoundsException::forIndex($endIndex - $beginIndex); 253 | } 254 | if (($beginIndex == 0) && ($endIndex == $this->length())) { 255 | return $this; 256 | } 257 | $value = substr($this->stringValue(), $beginIndex, $endIndex); 258 | return new Strng($value); 259 | } 260 | 261 | /** 262 | * Returns a hash code for this string. 263 | * The hash code for a 264 | * String object is computed as 265 | *
266 |      * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
267 |      * 
268 | * using int arithmetic, where s[i] is the 269 | * ith character of the string, n is the length of 270 | * the string, and ^ indicates exponentiation. 271 | * (The hash value of the empty string is zero.) 272 | * 273 | * @return string A hash code value for this object. 274 | */ 275 | public function hashCode() 276 | { 277 | $h = $this->hash; 278 | if ($h == 0) { 279 | $off = 0; 280 | $len = $this->length(); 281 | for ($i = 0; $i < $len; $i ++) { 282 | $h = 31 * $h + $off ++; 283 | } 284 | $this->hash = $h; 285 | } 286 | return $h; 287 | } 288 | 289 | /** 290 | * Tells whether or not this string matches the 291 | * given regular expression. 292 | * 293 | * @param string $regex The regular expression to which this string is to be matched 294 | * 295 | * @return boolean TRUE if, and only if, this string matches the given regular expression 296 | */ 297 | public function matches($regex) 298 | { 299 | $isExisting = false; 300 | if (ereg($regex, $this->stringValue()) != false) { 301 | $isExisting = true; 302 | } 303 | return $isExisting; 304 | } 305 | 306 | /** 307 | * This method has to be called to serialize the String. 308 | * 309 | * @return string Returns a serialized version of the String 310 | * @see \Serializable::serialize() 311 | */ 312 | public function serialize() 313 | { 314 | return serialize(get_object_vars($this)); 315 | } 316 | 317 | /** 318 | * This method unserializes the passed string and initializes the String 319 | * itself with the data. 320 | * 321 | * @param string $data Holds the data of the instance as serialized string 322 | * 323 | * @return void 324 | * @see \Serializable::unserialize($data) 325 | */ 326 | public function unserialize($data) 327 | { 328 | foreach (unserialize($data) as $propertyName => $propertyValue) { 329 | $this->$propertyName = $propertyValue; 330 | } 331 | } 332 | 333 | /** 334 | * Splits this string around matches of the given regular expression. 335 | * 336 | * The array returned by this method contains each substring of this 337 | * string that is terminated by another substring that matches the 338 | * given expression or is terminated by the end of the string. The 339 | * substrings in the array are in the order in which they occur in 340 | * this string. If the expression does not match any part of the 341 | * input then the resulting array has just one element, namely this 342 | * string. 343 | * 344 | * The limit parameter controls the number of times the pattern is 345 | * applied and therefore affects the length of the resulting array. 346 | * If the limit n is greater than zero then the pattern will be applied 347 | * at most n - 1 times, the array's length will be no greater than n, 348 | * and the array's last entry will contain all input beyond the last 349 | * matched delimiter. If n is non-positive then the pattern will be 350 | * applied as many times as possible and the array can have any length. 351 | * If n is zero then the pattern will be applied as many times as 352 | * possible, the array can have any length, and trailing empty strings 353 | * will be discarded. 354 | * 355 | * The string "boo:and:foo", for example, yields the following results 356 | * with these parameters: 357 | * 358 | * Regex Limit Result 359 | * : 2 { "boo", "and:foo" } 360 | * : 5 { "boo", "and", "foo" } 361 | * : -2 { "boo", "and", "foo" } 362 | * o 5 { "b", "", ":and:f", "", "" } 363 | * o -2 { "b", "", ":and:f", "", "" } 364 | * o 0 { "b", "", ":and:f" } 365 | * 366 | * An invocation of this method of the form str.split(regex, n) yields the 367 | * same result as the expression. 368 | * 369 | * @param string $regex The delimiting regular expression 370 | * @param integer $limit The result threshold, as described above 371 | * 372 | * @return array The array of strings computed by splitting this string around matches of the given regular expression 373 | */ 374 | public function split($regex, $limit = -1) 375 | { 376 | // split the internal value into it's parts 377 | return preg_split($regex, $this->stringValue(), $limit, PREG_SPLIT_NO_EMPTY); 378 | } 379 | 380 | /** 381 | * Returns a copy of the string, with leading and trailing whitespace 382 | * omitted. 383 | *

384 | * If this String object represents an empty character 385 | * sequence, or the first and last characters of character sequence 386 | * represented by this String object both have codes 387 | * greater than '\u0020' (the space character), then a 388 | * reference to this String object is returned. 389 | *

390 | * Otherwise, if there is no character with a code greater than 391 | * '\u0020' in the string, then a new 392 | * String object representing an empty string is created 393 | * and returned. 394 | *

395 | * Otherwise, let k be the index of the first character in the 396 | * string whose code is greater than '\u0020', and let 397 | * m be the index of the last character in the string whose code 398 | * is greater than '\u0020'. A new String 399 | * object is created, representing the substring of this string that 400 | * begins with the character at index k and ends with the 401 | * character at index m-that is, the result of 402 | * this.substring(km+1). 403 | *

404 | * This method may be used to trim 405 | * {@link Character#isSpace(char) whitespace} from the beginning and end 406 | * of a string; in fact, it trims all ASCII control characters as well. 407 | * 408 | * @return \AppserverIo\Lang\Strng A reference of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space. 409 | */ 410 | public function trim() 411 | { 412 | return $this->init(trim($this->stringValue())); 413 | } 414 | 415 | /** 416 | * md5 encryptes the string and returns the 417 | * instance. 418 | * 419 | * @return \AppserverIo\Lang\Strng The instance md5 encrypted 420 | */ 421 | public function md5() 422 | { 423 | return $this->init(md5($this->stringValue())); 424 | } 425 | 426 | /** 427 | * Converts the string value to upper case 428 | * and returns the instance. 429 | * 430 | * @return \AppserverIo\Lang\Strng The instance 431 | */ 432 | public function toUpperCase() 433 | { 434 | return $this->init(strtoupper($this->stringValue())); 435 | } 436 | 437 | /** 438 | * Converts the string value to lower case 439 | * and returns the instance. 440 | * 441 | * @return \AppserverIo\Lang\Strng The instance 442 | */ 443 | public function toLowerCase() 444 | { 445 | return $this->init(strtolower($this->stringValue())); 446 | } 447 | } 448 | -------------------------------------------------------------------------------- /src/AppserverIo/Lang/StrngIndexOutOfBoundsException.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang; 22 | 23 | /** 24 | * Thrown to indicate that the application has attempted to convert 25 | * a string to one of the numeric types, but that the string does not 26 | * have the appropriate format. 27 | * 28 | * @author Tim Wagner 29 | * @copyright 2015 TechDivision GmbH 30 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 31 | * @link https://github.com/appserver-io/lang 32 | * @link http://www.appserver.io 33 | */ 34 | class StrngIndexOutOfBoundsException extends \Exception 35 | { 36 | 37 | /** 38 | * Constructs a new StrngIndexOutOfBoundsException 39 | * class with an argument indicating the illegal index. 40 | * 41 | * @param integer $index The illegal index 42 | * 43 | * @return void 44 | * @throws StrngIndexOutOfBoundsException 45 | */ 46 | public static function forIndex($index) 47 | { 48 | throw new StrngIndexOutOfBoundsException('String index out of range: ' . $index); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /tests/AppserverIo/Lang/BooleanTest.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang; 22 | 23 | /** 24 | * This is the test for the Boolean class. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | class BooleanTest extends \PHPUnit_Framework_TestCase 33 | { 34 | 35 | /** 36 | * Checks the serialize/unserialize methods implemented 37 | * from the \Serializable interface. 38 | * 39 | * @return void 40 | */ 41 | public function testSerializeAndUnserialize() 42 | { 43 | // initialize a Boolean instance and clone it 44 | $booleanOne = new Boolean(true); 45 | $clonedOne = clone $booleanOne; 46 | // serialize/unserialize the Boolean value 47 | $booleanOne->unserialize($booleanOne->serialize()); 48 | // check that the two Booleans are equal 49 | $this->assertEquals($clonedOne, $booleanOne); 50 | } 51 | 52 | /** 53 | * This test checks the resolved class name. 54 | * 55 | * @return void 56 | */ 57 | public function testGetClass() 58 | { 59 | // check for the correct class name 60 | $this->assertEquals('AppserverIo\Lang\Boolean', Boolean::__getClass()); 61 | } 62 | 63 | /** 64 | * This test checks the Boolean's __constructor() method 65 | * with a valid boolean value TRUE. 66 | * 67 | * @return void 68 | */ 69 | public function testToStringWithBooleanValueTrue() 70 | { 71 | // initialize a new Boolean instance 72 | $boolean = new Boolean(true); 73 | // check that the two booleans are equal 74 | $this->assertEquals('true', $boolean->__toString()); 75 | } 76 | 77 | /** 78 | * This test checks the Boolean's __constructor() method 79 | * with a valid string value 'true'. 80 | * 81 | * @return void 82 | */ 83 | public function testToStringWithStringValueTrue() 84 | { 85 | // initialize a new Boolean instance 86 | $boolean = new Boolean('true'); 87 | // check that the two booleans are equal 88 | $this->assertEquals('true', $boolean->__toString()); 89 | } 90 | 91 | /** 92 | * This test checks the Boolean's __constructor() method 93 | * with a valid string value 'on'. 94 | * 95 | * @return void 96 | */ 97 | public function testToStringWithStringValueOn() 98 | { 99 | // initialize a new Boolean instance 100 | $boolean = new Boolean('on'); 101 | // check that the two booleans are equal 102 | $this->assertEquals('true', $boolean->__toString()); 103 | } 104 | 105 | /** 106 | * This test checks the Boolean's __constructor() method 107 | * with a valid string value 'yes'. 108 | * 109 | * @return void 110 | */ 111 | public function testToStringWithStringValueYes() 112 | { 113 | // initialize a new Boolean instance 114 | $boolean = new Boolean('yes'); 115 | // check that the two booleans are equal 116 | $this->assertEquals('true', $boolean->__toString()); 117 | } 118 | 119 | /** 120 | * This test checks the Boolean's __constructor() method 121 | * with a valid string value 'y'. 122 | * 123 | * @return void 124 | */ 125 | public function testToStringWithStringValueY() 126 | { 127 | // initialize a new Boolean instance 128 | $boolean = new Boolean('y'); 129 | // check that the two booleans are equal 130 | $this->assertEquals('true', $boolean->__toString()); 131 | } 132 | 133 | /** 134 | * This test checks the Boolean's __constructor() method 135 | * with a valid boolean value FALSE. 136 | * 137 | * @return void 138 | */ 139 | public function testToStringWithBooleanValueFalse() 140 | { 141 | // initialize a new Boolean instance 142 | $boolean = new Boolean(false); 143 | // check that the two boolean are equal 144 | $this->assertEquals('false', $boolean->__toString()); 145 | } 146 | 147 | /** 148 | * This test checks the Boolean's __constructor() method 149 | * with a valid string value 'false'. 150 | * 151 | * @return void 152 | */ 153 | public function testToStringWithStringValueFalse() 154 | { 155 | // initialize a new Boolean instance 156 | $boolean = new Boolean('false'); 157 | // check that the two boolean are equal 158 | $this->assertEquals('false', $boolean->__toString()); 159 | } 160 | 161 | /** 162 | * This test checks the Boolean's __constructor() method 163 | * with a valid string value 'off'. 164 | * 165 | * @return void 166 | */ 167 | public function testToStringWithStringValueOff() 168 | { 169 | // initialize a new Boolean instance 170 | $boolean = new Boolean('off'); 171 | // check that the two boolean are equal 172 | $this->assertEquals('false', $boolean->__toString()); 173 | } 174 | 175 | /** 176 | * This test checks the Boolean's __constructor() method 177 | * with a valid string value 'no'. 178 | * 179 | * @return void 180 | */ 181 | public function testToStringWithStringValueNo() 182 | { 183 | // initialize a new Boolean instance 184 | $boolean = new Boolean('no'); 185 | // check that the two boolean are equal 186 | $this->assertEquals('false', $boolean->__toString()); 187 | } 188 | 189 | /** 190 | * This test checks the Boolean's __constructor() method 191 | * with a valid string value 'n'. 192 | * 193 | * @return void 194 | */ 195 | public function testToStringWithStringValueN() 196 | { 197 | // initialize a new Boolean instance 198 | $boolean = new Boolean('n'); 199 | // check that the two boolean are equal 200 | $this->assertEquals('false', $boolean->__toString()); 201 | } 202 | 203 | /** 204 | * This test checks the Boolean's toString() method 205 | * returns a valid String instance. 206 | * 207 | * @return void 208 | */ 209 | public function testMethodToStringWithStringValueN() 210 | { 211 | // initialize a new Boolean instance 212 | $boolean = new Boolean(true); 213 | // check that the two boolean are equal 214 | $this->assertEquals(new Strng('true'), $boolean->toString()); 215 | } 216 | 217 | /** 218 | * This test checks the Boolean's __constructor() method 219 | * by expecting an exception. 220 | * 221 | * @return void 222 | */ 223 | public function testConstructorWithClassCastException() 224 | { 225 | // set the expected exception 226 | $this->setExpectedException('\AppserverIo\Lang\ClassCastException'); 227 | // try to initialize a new Boolean instance 228 | $boolean = new Boolean('xxx'); 229 | } 230 | } 231 | -------------------------------------------------------------------------------- /tests/AppserverIo/Lang/FltTest.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang; 22 | 23 | /** 24 | * This is the test for the Flt class. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | class FltTest extends \PHPUnit_Framework_TestCase 33 | { 34 | 35 | /** 36 | * Checks the serialize/unserialize methods implemented 37 | * from the \Serializable interface. 38 | * 39 | * @return void 40 | */ 41 | public function testSerializeAndUnserialize() 42 | { 43 | // initialize a Flt instance and clone it 44 | $floatOne = new Flt(0.1); 45 | $clonedOne = clone $floatOne; 46 | // serialize/unserialize the Flt instance 47 | $floatOne->unserialize($floatOne->serialize()); 48 | // check that the two Flt instances are equal 49 | $this->assertEquals($clonedOne, $floatOne); 50 | } 51 | 52 | /** 53 | * This test checks the resolved class name. 54 | * 55 | * @return void 56 | */ 57 | public function testGetClass() 58 | { 59 | // check for the correct class name 60 | $this->assertEquals('AppserverIo\Lang\Flt', Flt::__getClass()); 61 | } 62 | 63 | /** 64 | * This test checks the Flt's equal method. 65 | * 66 | * @return void 67 | */ 68 | public function testEquals() 69 | { 70 | // initialize a new Flt instance 71 | $float = new Flt(1.01); 72 | // check that the two Flt's are equal 73 | $this->assertTrue($float->equals(new Flt(1.01))); 74 | } 75 | 76 | /** 77 | * This test checks the Flt's floatValue() method. 78 | * 79 | * @return void 80 | */ 81 | public function testFloatValue() 82 | { 83 | // initialize a new Flt instance 84 | $float = new Flt(1.0005); 85 | // check that float value of the Flt instance 86 | $this->assertEquals(1.0005, $float->floatValue()); 87 | } 88 | 89 | /** 90 | * This test checks the Flt's intValue() method. 91 | * 92 | * @return void 93 | */ 94 | public function testIntValue() 95 | { 96 | // initialize a new Flt instance 97 | $float = new Flt(17.1); 98 | // check that float value of the Float instance 99 | $this->assertEquals(17, $float->intValue()); 100 | } 101 | 102 | /** 103 | * This test checks the Float's doubleValue() method. 104 | * 105 | * @return void 106 | */ 107 | public function testDoubleValue() 108 | { 109 | // initialize a new Float instance 110 | $float = new Flt(17.05); 111 | // check that double value of the Float instance 112 | $this->assertEquals(17.05, $float->doubleValue()); 113 | } 114 | 115 | /** 116 | * This test checks the Float's valueOf() method. 117 | * 118 | * @return void 119 | */ 120 | public function testValueOf() 121 | { 122 | // initialize a new Flt instance 123 | $float = Flt::valueOf( 124 | new Strng('17.6') 125 | ); 126 | // check that the two Float instances are equal 127 | $this->assertTrue($float->equals(new Flt(17.6))); 128 | } 129 | 130 | /** 131 | * This test checks the Float's valueOf() method. 132 | * 133 | * @return void 134 | */ 135 | public function testValueOfWithNumberFormatException() 136 | { 137 | // set the expected exception 138 | $this->setExpectedException('\AppserverIo\Lang\NumberFormatException'); 139 | // initialize a new Float instance 140 | $int = Flt::valueOf( 141 | new Strng('!17') 142 | ); 143 | } 144 | 145 | /** 146 | * This test checks the Flt's parseFloat() method. 147 | * 148 | * @return void 149 | */ 150 | public function testParseFloat() 151 | { 152 | // initialize a new Flt instance 153 | $float = Flt::parseFloat( 154 | new Strng('17') 155 | ); 156 | // check that the two floats are equal 157 | $this->assertEquals(17, $float); 158 | } 159 | 160 | /** 161 | * This test checks the Flt's parseFloat() method. 162 | * 163 | * @return void 164 | */ 165 | public function testParseFloatWithNumberFormaException() 166 | { 167 | // set the expected exception 168 | $this->setExpectedException('\AppserverIo\Lang\NumberFormatException'); 169 | // initialize a new Float instance 170 | $float = Flt::parseFloat(new Strng('!17')); 171 | } 172 | 173 | /** 174 | * This test checks the Flt's add() method. 175 | * 176 | * @return void 177 | */ 178 | public function testAdd() 179 | { 180 | // initialize a new Flt instance 181 | $float = new Flt(10.005); 182 | $float->add(new Flt(10.105)); 183 | // check the value 184 | $this->assertEquals(20.11, $float->floatValue()); 185 | } 186 | 187 | /** 188 | * This test checks the Flt's subtract() method. 189 | * 190 | * @return void 191 | */ 192 | public function testSubtract() 193 | { 194 | // initialize a new Flt instance 195 | $float = new Flt(10.6); 196 | $float->subtract(new Flt(32.6)); 197 | // check the value 198 | $this->assertEquals(-22, $float->intValue()); 199 | } 200 | 201 | /** 202 | * This test checks the Flt's multiply() method. 203 | * 204 | * @return void 205 | */ 206 | public function testMultiply() 207 | { 208 | // initialize a new Flt instance 209 | $int = new Flt(10.00); 210 | $int->multiply(new Flt(10.00)); 211 | // check the value 212 | $this->assertEquals(100.00, $int->floatValue()); 213 | } 214 | 215 | /** 216 | * This test checks the Flt's divide() method. 217 | * 218 | * @return void 219 | */ 220 | public function testDivide() 221 | { 222 | // initialize a new Float instance 223 | $int = new Flt(10.00); 224 | $int->divide(new Flt(10.00)); 225 | // check the value 226 | $this->assertEquals(1.00, $int->floatValue()); 227 | } 228 | 229 | /** 230 | * This test checks the Flt's divide() method 231 | * with an odd result. 232 | * 233 | * @return void 234 | */ 235 | public function testDivideToOddNumber() 236 | { 237 | // initialize a new Float instance 238 | $float = new Flt(11.00); 239 | $float->divide(new Flt(3.00)); 240 | // check the value 241 | $this->assertEquals(11.00 / 3.00, $float->floatValue()); 242 | } 243 | } 244 | -------------------------------------------------------------------------------- /tests/AppserverIo/Lang/IntegerTest.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang; 22 | 23 | /** 24 | * This is the test for the Integer class. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | class IntegerTest extends \PHPUnit_Framework_TestCase 33 | { 34 | 35 | /** 36 | * Checks the serialize/unserialize methods implemented 37 | * from the \Serializable interface. 38 | * 39 | * @return void 40 | */ 41 | public function testSerializeAndUnserialize() 42 | { 43 | // initialize a Integer instance and clone it 44 | $integerOne = new Integer(17); 45 | $clonedOne = clone $integerOne; 46 | // serialize/unserialize the Integer instance 47 | $integerOne->unserialize($integerOne->serialize()); 48 | // check that the two Integer instances are equal 49 | $this->assertEquals($clonedOne, $integerOne); 50 | } 51 | 52 | /** 53 | * This test checks the resolved class name. 54 | * 55 | * @return void 56 | */ 57 | public function testGetClass() 58 | { 59 | // check for the correct class name 60 | $this->assertEquals('AppserverIo\Lang\Integer', Integer::__getClass()); 61 | } 62 | 63 | /** 64 | * This test checks the Integer's equal method. 65 | * 66 | * @return void 67 | */ 68 | public function testEquals() 69 | { 70 | // initialize a new Integer instance 71 | $int = new Integer(1); 72 | // check that the two Integers are equal 73 | $this->assertTrue($int->equals(new Integer(1))); 74 | } 75 | 76 | /** 77 | * This test checks the Integer's floatValue() method. 78 | * 79 | * @return void 80 | */ 81 | public function testFloatValue() 82 | { 83 | // initialize a new Integer instance 84 | $int = new Integer(17); 85 | // check that float value of the Integer instance 86 | $this->assertEquals(17.0, $int->floatValue()); 87 | } 88 | 89 | /** 90 | * This test checks the Integer's intValue() method. 91 | * 92 | * @return void 93 | */ 94 | public function testIntValue() 95 | { 96 | // initialize a new Integer instance 97 | $int = new Integer(17); 98 | // check that integer value of the Integer instance 99 | $this->assertEquals(17, $int->intValue()); 100 | } 101 | 102 | /** 103 | * This test checks the Integer's doubleValue() method. 104 | * 105 | * @return void 106 | */ 107 | public function testDoubleValue() 108 | { 109 | // initialize a new Integer instance 110 | $int = new Integer(17); 111 | // check that double value of the Integer instance 112 | $this->assertEquals(17.0, $int->doubleValue()); 113 | } 114 | 115 | /** 116 | * This test checks the Integer's valueOf() method. 117 | * 118 | * @return void 119 | */ 120 | public function testValueOf() 121 | { 122 | // initialize a new Integer instance 123 | $int = Integer::valueOf(new Strng('17')); 124 | // check that the two Integer instances are equal 125 | $this->assertTrue($int->equals(new Integer(17))); 126 | } 127 | 128 | /** 129 | * This test checks the Integer's valueOf() method. 130 | * 131 | * @return void 132 | */ 133 | public function testValueOfWithNumberFormatException() 134 | { 135 | // set the expected exception 136 | $this->setExpectedException('\AppserverIo\Lang\NumberFormatException'); 137 | // initialize a new Integer instance 138 | $int = Integer::valueOf(new Strng('!17')); 139 | } 140 | 141 | /** 142 | * This test checks the Integer's parseInteger() method. 143 | * 144 | * @return void 145 | */ 146 | public function testParseInteger() 147 | { 148 | // initialize a new Integer instance 149 | $int = Integer::parseInteger(new Strng('17')); 150 | // check that the two integers are equal 151 | $this->assertEquals(17, $int); 152 | } 153 | 154 | /** 155 | * This test checks the Integer's parseInteger() method. 156 | * 157 | * @return void 158 | */ 159 | public function testParseIntegerWithNumberFormaException() 160 | { 161 | // set the expected exception 162 | $this->setExpectedException('\AppserverIo\Lang\NumberFormatException'); 163 | // initialize a new Integer instance 164 | $int = Integer::parseInteger(new Strng('!17')); 165 | } 166 | 167 | /** 168 | * This test checks the Integer's add() method. 169 | * 170 | * @return void 171 | */ 172 | public function testAdd() 173 | { 174 | // initialize a new Integer instance 175 | $int = new Integer(10); 176 | $int->add(new Integer(22)); 177 | // check the value 178 | $this->assertEquals(32, $int->intValue()); 179 | } 180 | 181 | /** 182 | * This test checks the Integer's subtract() method. 183 | * 184 | * @return void 185 | */ 186 | public function testSubtract() 187 | { 188 | // initialize a new Integer instance 189 | $int = new Integer(10); 190 | $int->subtract(new Integer(32)); 191 | // check the value 192 | $this->assertEquals(-22, $int->intValue()); 193 | } 194 | 195 | /** 196 | * This test checks the Integer's multiply() method. 197 | * 198 | * @return void 199 | */ 200 | public function testMultiply() 201 | { 202 | // initialize a new Integer instance 203 | $int = new Integer(10); 204 | $int->multiply(new Integer(10)); 205 | // check the value 206 | $this->assertEquals(100, $int->intValue()); 207 | } 208 | 209 | /** 210 | * This test checks the Integer's divide() method. 211 | * 212 | * @return void 213 | */ 214 | public function testDivide() 215 | { 216 | // initialize a new Integer instance 217 | $int = new Integer(10); 218 | $int->divide(new Integer(10)); 219 | // check the value 220 | $this->assertEquals(1, $int->intValue()); 221 | } 222 | 223 | /** 224 | * This test checks the Integer's divide() method 225 | * with an odd result. 226 | * 227 | * @return void 228 | */ 229 | public function testDivideToOddNumber() 230 | { 231 | // initialize a new Integer instance 232 | $int = new Integer(11); 233 | $int->divide(new Integer(3)); 234 | // check the value 235 | $this->assertEquals(3, $int->intValue()); 236 | } 237 | 238 | /** 239 | * This test checks the Integer's modulo() method. 240 | * 241 | * @return void 242 | */ 243 | public function testModulo() 244 | { 245 | // initialize a new Integer instance 246 | $int = new Integer(11); 247 | $remeinder = $int->modulo(new Integer(3)); 248 | // check the value 249 | $this->assertEquals(2, $remeinder->intValue()); 250 | } 251 | 252 | /** 253 | * This test checks the Integer's greaterThan() method. 254 | * 255 | * @return void 256 | */ 257 | public function testGreaterThan() 258 | { 259 | $int = new Integer(2); 260 | $this->assertFalse($int->greaterThan(new Integer(3))); 261 | $this->assertTrue($int->greaterThan(new Integer(1))); 262 | } 263 | 264 | /** 265 | * This test checks the Integer's greaterThanOrEqual() method. 266 | * 267 | * @return void 268 | */ 269 | public function testGreaterThanOrEqual() 270 | { 271 | $int = new Integer(2); 272 | $this->assertFalse($int->greaterThanOrEqual(new Integer(3))); 273 | $this->assertTrue($int->greaterThanOrEqual(new Integer(2))); 274 | $this->assertTrue($int->greaterThanOrEqual(new Integer(1))); 275 | } 276 | 277 | /** 278 | * This test checks the Integer's lowerThan() method. 279 | * 280 | * @return void 281 | */ 282 | public function testLowerThan() 283 | { 284 | $int = new Integer(2); 285 | $this->assertTrue($int->lowerThan(new Integer(3))); 286 | $this->assertFalse($int->lowerThan(new Integer(1))); 287 | } 288 | 289 | /** 290 | * This test checks the Integer's lowerThanOrEqual() method. 291 | * 292 | * @return void 293 | */ 294 | public function testLowerThanOrEqual() 295 | { 296 | $int = new Integer(2); 297 | $this->assertTrue($int->lowerThanOrEqual(new Integer(3))); 298 | $this->assertTrue($int->lowerThanOrEqual(new Integer(2))); 299 | $this->assertFalse($int->lowerThanOrEqual(new Integer(1))); 300 | } 301 | } 302 | -------------------------------------------------------------------------------- /tests/AppserverIo/Lang/ObjectTest.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang; 22 | 23 | /** 24 | * This is the test for the Object class. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | class ObjectTest extends \PHPUnit_Framework_TestCase 33 | { 34 | 35 | /** 36 | * This test multiplies the Integer with an 37 | * integer value. 38 | * 39 | * @return void 40 | */ 41 | public function testGetClass() 42 | { 43 | $this->assertEquals('AppserverIo\Lang\Objct', Objct::__getClass()); 44 | } 45 | 46 | /** 47 | * This method checks, that the equal method 48 | * does return FALSE for not equal objects. 49 | * 50 | * @return void 51 | */ 52 | public function testEqualFails() 53 | { 54 | $object1 = $this->getMockForAbstractClass('\AppserverIo\Lang\Objct'); 55 | $object2 = $this->getMockForAbstractClass('\AppserverIo\Lang\Objct'); 56 | $this->assertFalse($object1->equals($object2)); 57 | } 58 | 59 | /** 60 | * This method checks that the equal method 61 | * equals itself. 62 | * 63 | * @return void 64 | */ 65 | public function testEqualSuccess() 66 | { 67 | $object1 = $this->getMockForAbstractClass('\AppserverIo\Lang\Objct'); 68 | $this->assertTrue($object1->equals($object1)); 69 | } 70 | 71 | /** 72 | * This method tests the __toString() and the toString() 73 | * methods. 74 | * 75 | * @return void 76 | */ 77 | public function testToString() 78 | { 79 | $object = $this->getMockForAbstractClass('\AppserverIo\Lang\Objct'); 80 | $this->assertEquals(get_class($object) . '@' . sha1(serialize($object)), $object->__toString()); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /tests/AppserverIo/Lang/Reflection/MockAnnotation.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang\Reflection; 22 | 23 | use AppserverIo\Lang\Objct; 24 | 25 | /** 26 | * A mock annotation implementation. 27 | * 28 | * @author Tim Wagner 29 | * @copyright 2015 TechDivision GmbH 30 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 31 | * @link https://github.com/appserver-io/lang 32 | * @link http://www.appserver.io 33 | */ 34 | class MockAnnotation extends Objct 35 | { 36 | 37 | /** 38 | * Initializes the mock instance with dummy args. 39 | * 40 | * @param array $values The args to pass to the instance 41 | */ 42 | public function __construct(array $values = array()) 43 | { 44 | $this->values = $values; 45 | } 46 | 47 | /** 48 | * Returns the requested value if available. 49 | * 50 | * @param string $key The key of the value to return 51 | * 52 | * @return mixed The requested value 53 | */ 54 | public function getValue($key) 55 | { 56 | if (isset($this->values[$key])) { 57 | return $this->values[$key]; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /tests/AppserverIo/Lang/Reflection/ReflectionAnnotationTest.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang\Reflection; 22 | 23 | /** 24 | * This is the test for the ReflectionAnnotation class. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | * 32 | * @Test(name=ReflectionAnnotationTest) 33 | * @Outer({ 34 | * @Inner(name="Test"), 35 | * @Inner(name="AnotherTest") 36 | * }) 37 | */ 38 | class ReflectionAnnotationTest extends \PHPUnit_Framework_TestCase 39 | { 40 | 41 | /** 42 | * A random name of a reflection annotation. 43 | * 44 | * @var string 45 | */ 46 | const ANNOTATION_NAME = 'annotationName'; 47 | 48 | /** 49 | * Initializes the instance before we run each test. 50 | * @return void 51 | * @see PHPUnit_Framework_TestCase::setUp() 52 | */ 53 | protected function setUp() 54 | { 55 | $this->annotationInstance = new ReflectionAnnotation(ReflectionAnnotationTest::ANNOTATION_NAME); 56 | } 57 | 58 | /** 59 | * Checks the serialize/unserialize methods implemented 60 | * from the \Serializable interface. 61 | * 62 | * @return void 63 | */ 64 | public function testSerializeAndUnserialize() 65 | { 66 | // initialize a ReflectionAnnotation instance and clone it 67 | $clonedOne = clone $this->annotationInstance; 68 | // serialize/unserialize the ReflectionAnnotation 69 | $this->annotationInstance->unserialize($this->annotationInstance->serialize()); 70 | // check that the two ReflectionAnnotation instances are equal 71 | $this->assertEquals($clonedOne, $this->annotationInstance); 72 | } 73 | 74 | /** 75 | * This test checks the resolved class name. 76 | * 77 | * @return void 78 | */ 79 | public function testGetClass() 80 | { 81 | // check for the correct class name 82 | $this->assertEquals('AppserverIo\Lang\Reflection\ReflectionAnnotation', ReflectionAnnotation::__getClass()); 83 | } 84 | 85 | /** 86 | * This test checks if the correct annotation name will be returned. 87 | * 88 | * @return void 89 | */ 90 | public function testGetAnnotationName() 91 | { 92 | $this->assertSame(ReflectionAnnotationTest::ANNOTATION_NAME, $this->annotationInstance->getAnnotationName()); 93 | } 94 | 95 | /** 96 | * This test checks if the annotations passed from a PHP reflection class will 97 | * be initialized correctly. 98 | * 99 | * @return void 100 | */ 101 | public function testFromReflectionClass() 102 | { 103 | 104 | // load the annotations of this class 105 | $annotations = ReflectionAnnotation::fromReflectionClass(new ReflectionClass($this)); 106 | 107 | // check if we can find the default annotations 108 | $this->assertTrue(isset($annotations['author'])); 109 | $this->assertTrue(isset($annotations['copyright'])); 110 | $this->assertTrue(isset($annotations['link'])); 111 | $this->assertTrue(isset($annotations['license'])); 112 | 113 | // try to load the @Test annotation with the specified name 114 | $this->assertSame($annotations['Test']->getValue('name'), 'ReflectionAnnotationTest'); 115 | } 116 | 117 | /** 118 | * This test checks if the annotations passed from this instance will 119 | * be initialized correctly. 120 | * 121 | * @return void 122 | * @FirstTest 123 | */ 124 | public function testFromReflectionMethod() 125 | { 126 | 127 | // load the reflection method 128 | $reflectionClass = new ReflectionClass($this); 129 | $reflectionMethod = $reflectionClass->getMethod('testFromReflectionMethod'); 130 | 131 | // check if we can find the default annotations 132 | $this->assertTrue($reflectionMethod->hasAnnotation('return')); 133 | $this->assertTrue($reflectionMethod->hasAnnotation('FirstTest')); 134 | } 135 | 136 | /** 137 | * This test checks if the annotations will be ignored correctly. 138 | * 139 | * @return void 140 | */ 141 | public function testFromReflectionMethodIgnoreReturnAnnotation() 142 | { 143 | 144 | // initialize the annotations to ignore and the aliases 145 | $ignore = array('return'); 146 | 147 | // load the reflection method 148 | $reflectionClass = new ReflectionClass($this, $ignore); 149 | $reflectionMethod = $reflectionClass->getMethod('testFromReflectionMethod'); 150 | 151 | // check if we can find the default annotations 152 | $this->assertFalse($reflectionMethod->hasAnnotation('return')); 153 | } 154 | 155 | /** 156 | * This test checks if the annotations newInstance() method works as expected. 157 | * 158 | * @return void 159 | * @MockAnnotation(name=Test, description="Another Test", value={ "key" : "a value" }) 160 | */ 161 | public function testNewInstance() 162 | { 163 | 164 | // initialize the annotations to ignore and the aliases 165 | $ignore = array(); 166 | $aliases = array('MockAnnotation' => 'AppserverIo\Lang\Reflection\MockAnnotation'); 167 | 168 | // load the reflection method 169 | $reflectionClass = new ReflectionClass($this, $ignore, $aliases); 170 | $reflectionMethod = $reflectionClass->getMethod('testNewInstance'); 171 | 172 | // create a new instance of the found alias 173 | $mockAnnotation = $reflectionMethod->getAnnotation('MockAnnotation'); 174 | $instance = $mockAnnotation->newInstance($mockAnnotation->getValues()); 175 | 176 | // check the values passed to the alias instance 177 | $this->assertSame($instance->getValue('name'), 'Test'); 178 | $this->assertSame($instance->getValue('description'), 'Another Test'); 179 | $this->assertSame($instance->getValue('value'), array('key' => 'a value')); 180 | } 181 | 182 | /** 183 | * This test checks if the annotations newInstanceArgs() method works as expected. 184 | * 185 | * @return void 186 | * @MockAnnotation(name=Test, description="Another Test", value={ "key" : "a value" }) 187 | */ 188 | public function testNewInstanceArgs() 189 | { 190 | 191 | // initialize the annotations to ignore and the aliases 192 | $ignore = array(); 193 | $aliases = array('MockAnnotation' => 'AppserverIo\Lang\Reflection\MockAnnotation'); 194 | 195 | // load the reflection method 196 | $reflectionClass = new ReflectionClass($this, $ignore, $aliases); 197 | $reflectionMethod = $reflectionClass->getMethod('testNewInstanceArgs'); 198 | 199 | // create a new instance of the found alias 200 | $mockAnnotation = $reflectionMethod->getAnnotation('MockAnnotation'); 201 | $instance = $mockAnnotation->newInstanceArgs(array($mockAnnotation->getValues())); 202 | 203 | // check the values passed to the annotation instance 204 | $this->assertSame($instance->getValue('name'), 'Test'); 205 | $this->assertSame($instance->getValue('description'), 'Another Test'); 206 | $this->assertSame($instance->getValue('value'), array('key' => 'a value')); 207 | } 208 | 209 | /** 210 | * This test checks if the annotations newInstanceArgs() method works as expected. 211 | * 212 | * @return void 213 | * @MockAnnotation 214 | */ 215 | public function testNewInstanceArgsWithoutArgs() 216 | { 217 | 218 | // initialize the annotations to ignore and the aliases 219 | $ignore = array(); 220 | $aliases = array('MockAnnotation' => 'AppserverIo\Lang\Reflection\MockAnnotation'); 221 | 222 | // load the reflection method 223 | $reflectionClass = new ReflectionClass($this, $ignore, $aliases); 224 | $reflectionMethod = $reflectionClass->getMethod('testNewInstanceArgsWithoutArgs'); 225 | 226 | // create a new instance of the found alias 227 | $mockAnnotation = $reflectionMethod->getAnnotation('MockAnnotation'); 228 | $instance = $mockAnnotation->newInstanceArgs(array($mockAnnotation->getValues())); 229 | 230 | // check that no values have been passed to the annotation instance 231 | $this->assertNull($instance->getValue('name')); 232 | } 233 | 234 | /** 235 | * Test if the set/get methods works as exptected. 236 | * 237 | * @return void 238 | */ 239 | public function testSetGetValue() 240 | { 241 | $this->annotationInstance->setValue($key = 'test', $value = 'a test value'); 242 | $this->assertSame($this->annotationInstance->getValue($key), $value); 243 | } 244 | 245 | /** 246 | * Test if the requesting a value with an unknown key returns NULL. 247 | * 248 | * @return void 249 | */ 250 | public function testGetWithInvalidKey() 251 | { 252 | $this->assertNull($this->annotationInstance->getValue('unknownKey')); 253 | } 254 | 255 | /** 256 | * Test if nested annotations are successfully initialized. 257 | * 258 | * @return void 259 | */ 260 | public function testNestedAnnotationsFromReflectionClass() 261 | { 262 | 263 | // load the annotations of this class 264 | $reflectionClass = new ReflectionClass($this); 265 | 266 | // load the outer annotation 267 | $outer = $reflectionClass->getAnnotation('Outer'); 268 | 269 | // make sure that we've two inner annotations 270 | $this->assertCount(2, $outer->getValue(0)); 271 | 272 | // query whether the annotation has been instanciated successfully 273 | foreach ($outer->getValue(0) as $inner) { 274 | $this->assertInstanceOf('AppserverIo\Lang\Reflection\AnnotationInterface', $inner); 275 | } 276 | } 277 | } 278 | -------------------------------------------------------------------------------- /tests/AppserverIo/Lang/Reflection/ReflectionClassTest.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | namespace AppserverIo\Lang\Reflection; 21 | 22 | /** 23 | * This is the test for the ReflectionClass class. 24 | * 25 | * @author Tim Wagner 26 | * @copyright 2015 TechDivision GmbH 27 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 28 | * @link https://github.com/appserver-io/lang 29 | * @link http://www.appserver.io 30 | * 31 | * @MockAnnotation(name=MockAnnotation, description="some description", value="a value") 32 | */ 33 | class ReflectionClassTest extends \PHPUnit_Framework_TestCase 34 | { 35 | 36 | /** 37 | * The reflection class intance we want to test. 38 | * 39 | * @var \AppserverIo\Lang\Reflection\ReflectionClass 40 | */ 41 | protected $reflectionClass; 42 | 43 | /** 44 | * Initializes the instance before we run each test. 45 | * 46 | * @return void 47 | * @see PHPUnit_Framework_TestCase::setUp() 48 | */ 49 | protected function setUp() 50 | { 51 | $this->reflectionClass = new ReflectionClass(__CLASS__); 52 | } 53 | 54 | /** 55 | * Checks the serialize/unserialize methods implemented 56 | * from the \Serializable interface. 57 | * 58 | * @return void 59 | */ 60 | public function testSerializeAndUnserialize() 61 | { 62 | // initialize a ReflectionClass instance and clone it 63 | $classOne = new ReflectionClass(__CLASS__); 64 | $clonedOne = clone $classOne; 65 | // serialize/unserialize the ReflectionClass 66 | $classOne->unserialize($classOne->serialize()); 67 | // check that the two ReflectionClass instances are equal 68 | $this->assertEquals($clonedOne, $classOne); 69 | } 70 | 71 | /** 72 | * This test checks the resolved class name. 73 | * 74 | * @return void 75 | */ 76 | public function testGetClass() 77 | { 78 | // check for the correct class name 79 | $this->assertEquals('AppserverIo\Lang\Reflection\ReflectionClass', ReflectionClass::__getClass()); 80 | } 81 | 82 | /** 83 | * Test if the returned class name equals the one passed to the constructor. 84 | * 85 | * @return void 86 | */ 87 | public function testGetClassName() 88 | { 89 | $this->assertSame(__CLASS__, $this->reflectionClass->getName()); 90 | } 91 | 92 | /** 93 | * Test if this method is available in the reflection method list. 94 | * 95 | * @return void 96 | */ 97 | public function testHasMethodWithExistingMethod() 98 | { 99 | $this->assertTrue($this->reflectionClass->hasMethod('testGetMethodWithExistingMethod')); 100 | } 101 | 102 | /** 103 | * Test if this method is available in the reflection method list. 104 | * 105 | * @return void 106 | */ 107 | public function testGetMethodWithExistingMethod() 108 | { 109 | $reflectionMethod = $this->reflectionClass->getMethod($methodName = 'testGetMethodWithExistingMethod'); 110 | $this->assertSame($reflectionMethod->getClassName(), __CLASS__); 111 | $this->assertSame($reflectionMethod->getMethodName(), $methodName); 112 | } 113 | 114 | /** 115 | * Test if this method is available in the reflection method list. 116 | * 117 | * @return void @expectedException AppserverIo\Lang\Reflection\ReflectionException 118 | */ 119 | public function testGetMethodWithException() 120 | { 121 | $this->reflectionClass->getMethod('someUnknownMethod'); 122 | } 123 | 124 | /** 125 | * Test if the class annotation is available. 126 | * 127 | * @return void 128 | */ 129 | public function testHasAnnotationWithExistingAnnotation() 130 | { 131 | $this->assertTrue($this->reflectionClass->hasAnnotation('MockAnnotation')); 132 | } 133 | 134 | /** 135 | * Test if the class annotation is available and has the correct values set. 136 | * 137 | * @return void 138 | */ 139 | public function testGetAnnotationWithExistingAnnotation() 140 | { 141 | $annotation = $this->reflectionClass->getAnnotation('MockAnnotation'); 142 | $this->assertSame($annotation->getValue('name'), 'MockAnnotation'); 143 | $this->assertSame($annotation->getValue('description'), 'some description'); 144 | $this->assertSame($annotation->getValue('value'), 'a value'); 145 | } 146 | 147 | /** 148 | * Test if an execption is thrown if a requested annotation is not available. 149 | * 150 | * @return void 151 | * @expectedException AppserverIo\Lang\Reflection\ReflectionException 152 | */ 153 | public function testGetAnnotationWithException() 154 | { 155 | $this->reflectionClass->getAnnotation('UnknownAnnotation'); 156 | } 157 | 158 | /** 159 | * Checks if the initialization of a reflection class works as exepected. 160 | * 161 | * @return void 162 | */ 163 | public function testFromPhpReflectionClass() 164 | { 165 | $reflectionClass = ReflectionClass::fromPhpReflectionClass(new \ReflectionClass($this)); 166 | $this->assertSame(__CLASS__, $reflectionClass->getName()); 167 | } 168 | 169 | /** 170 | * Checks if the hasMethod() method works as expected. 171 | * 172 | * @return void 173 | */ 174 | public function testHasMethod() 175 | { 176 | $this->assertTrue($this->reflectionClass->hasMethod('testHasMethod')); 177 | } 178 | 179 | /** 180 | * Checks if the hasProperty() method works as expected. 181 | * 182 | * @return void 183 | */ 184 | public function testHasProperty() 185 | { 186 | $this->assertTrue($this->reflectionClass->hasProperty('reflectionClass')); 187 | } 188 | 189 | /** 190 | * Checks if the addAnnotationAlias() method works as expected. 191 | * 192 | * @return void 193 | */ 194 | public function testAddAnnotationAlias() 195 | { 196 | 197 | // add the annotation alias 198 | $this->reflectionClass->addAnnotationAlias($name = 'test', $className = __CLASS__); 199 | 200 | // load the annotation aliases 201 | $annotationAliases = $this->reflectionClass->getAnnotationAliases(); 202 | 203 | // check the annotation alias 204 | $this->assertCount(1, $annotationAliases); 205 | $this->assertTrue(array_key_exists($name, $annotationAliases)); 206 | $this->assertContains($className, $annotationAliases); 207 | } 208 | 209 | /** 210 | * Checks if the implementsInterface() method works as expected. 211 | * 212 | * @return void 213 | */ 214 | public function testImplementsInterfaceWithNotImplementedInterface() 215 | { 216 | $this->assertFalse($this->reflectionClass->implementsInterface('\Serializable')); 217 | } 218 | 219 | /** 220 | * Checks if the isInterface() method works as expected. 221 | * 222 | * @return void 223 | */ 224 | public function testIsInterface() 225 | { 226 | $this->assertFalse($this->reflectionClass->isInterface()); 227 | } 228 | 229 | /** 230 | * Checks if the isAbstract() method works as expected. 231 | * 232 | * @return void 233 | */ 234 | public function testIsAbstract() 235 | { 236 | $this->assertFalse($this->reflectionClass->isAbstract()); 237 | } 238 | } 239 | -------------------------------------------------------------------------------- /tests/AppserverIo/Lang/Reflection/ReflectionMethodTest.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang\Reflection; 22 | 23 | /** 24 | * This is the test for the ReflectionMethod class. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | class ReflectionMethodTest extends \PHPUnit_Framework_TestCase 33 | { 34 | 35 | /** 36 | * A random name for a method. 37 | * 38 | * @var string 39 | */ 40 | const METHOD_NAME = 'testGetAnnotation'; 41 | 42 | const METHOD_WITH_PARAMETERS = 'methodWithTwoParameters'; 43 | 44 | /** 45 | * Initializes the instance before we run each test. 46 | * 47 | * @return void 48 | * @see PHPUnit_Framework_TestCase::setUp() 49 | */ 50 | protected function setUp() 51 | { 52 | $this->reflectionMethod = new ReflectionMethod(__CLASS__, ReflectionMethodTest::METHOD_NAME); 53 | } 54 | 55 | /** 56 | * Checks the serialize/unserialize methods implemented 57 | * from the \Serializable interface. 58 | * 59 | * @return void 60 | */ 61 | public function testSerializeAndUnserialize() 62 | { 63 | // initialize a ReflectionMethod instance and clone it 64 | $methodOne = new ReflectionMethod(__CLASS__, ReflectionMethodTest::METHOD_NAME); 65 | $clonedOne = clone $methodOne; 66 | // serialize/unserialize the ReflectionMethod 67 | $methodOne->unserialize($methodOne->serialize()); 68 | // check that the two ReflectionMethod instances are equal 69 | $this->assertEquals($clonedOne, $methodOne); 70 | } 71 | 72 | /** 73 | * This test checks the resolved class name. 74 | * 75 | * @return void 76 | */ 77 | public function testGetClass() 78 | { 79 | // check for the correct class name 80 | $this->assertEquals('AppserverIo\Lang\Reflection\ReflectionMethod', ReflectionMethod::__getClass()); 81 | } 82 | 83 | /** 84 | * Test if the class annotation is available and has the correct values set. 85 | * 86 | * @return void 87 | * @MockAnnotation(name=MockAnnotation, description="some description", value="a value") 88 | */ 89 | public function testGetAnnotation() 90 | { 91 | $annotation = $this->reflectionMethod->getAnnotation('MockAnnotation'); 92 | $this->assertSame($annotation->getValue('name'), 'MockAnnotation'); 93 | $this->assertSame($annotation->getValue('description'), 'some description'); 94 | $this->assertSame($annotation->getValue('value'), 'a value'); 95 | } 96 | 97 | /** 98 | * Test if an execption is thrown if a requested annotation is not available. 99 | * 100 | * @return void 101 | * @expectedException AppserverIo\Lang\Reflection\ReflectionException 102 | */ 103 | public function testGetAnnotationWithException() 104 | { 105 | $this->reflectionMethod->getAnnotation('UnknownAnnotation'); 106 | } 107 | 108 | /** 109 | * Test if the invoke() method passes the args correctly to the method. 110 | * 111 | * @return void 112 | */ 113 | public function testInvokeWithArgs() 114 | { 115 | 116 | // initialize the array with the values 117 | $values = array($key = 'test' => $value = 'aValue'); 118 | 119 | // create a new MockAnnotation instance 120 | $reflectionClass = new ReflectionClass('AppserverIo\Lang\Reflection\MockAnnotation'); 121 | $instance = $reflectionClass->newInstanceArgs(array($values)); 122 | 123 | // create the reflection method and invoke it with arguments 124 | $reflectionMethod = $reflectionClass->getMethod('getValue'); 125 | $this->assertSame($value, $reflectionMethod->invoke($instance, $key)); 126 | } 127 | 128 | /** 129 | * Tests if the method parameters returns the correct number of parameters. 130 | * 131 | * @return void 132 | */ 133 | public function testGetParametersCount() 134 | { 135 | $reflectionMethod = new ReflectionMethod(__CLASS__, ReflectionMethodTest::METHOD_WITH_PARAMETERS); 136 | $this->assertCount(2, $reflectionMethod->getParameters()); 137 | } 138 | 139 | /** 140 | * Tests if the method parameters returns the correct number of parameters. 141 | * 142 | * @return void 143 | */ 144 | public function testGetParametersName() 145 | { 146 | 147 | // load the reflection method 148 | $reflectionMethod = new ReflectionMethod(__CLASS__, ReflectionMethodTest::METHOD_WITH_PARAMETERS); 149 | 150 | // initialize the counter 151 | $counter = 0; 152 | 153 | // iterate over the parameters and check the parameter names 154 | foreach ($reflectionMethod->getParameters() as $reflectionParameter) { 155 | $this->assertSame('test' . $counter++, $reflectionParameter->getParameterName()); 156 | } 157 | } 158 | 159 | /** 160 | * Checks if the addAnnotationAlias() method works as expected. 161 | * 162 | * @return void 163 | */ 164 | public function testAddAnnotationAlias() 165 | { 166 | 167 | // add the annotation alias 168 | $this->reflectionMethod->addAnnotationAlias($name = 'test', $className = __CLASS__); 169 | 170 | // load the annotation aliases 171 | $annotationAliases = $this->reflectionMethod->getAnnotationAliases(); 172 | 173 | // check the annotation alias 174 | $this->assertCount(1, $annotationAliases); 175 | $this->assertTrue(array_key_exists($name, $annotationAliases)); 176 | $this->assertContains($className, $annotationAliases); 177 | } 178 | 179 | /** 180 | * A method with one parameter, used for testing purposes. 181 | * 182 | * @param string $test0 First test parameter 183 | * @param string $test1 Second test parameter 184 | * 185 | * @return void 186 | */ 187 | public function methodWithTwoParameters($test0, $test1) 188 | { 189 | // we do nothing here 190 | } 191 | } 192 | -------------------------------------------------------------------------------- /tests/AppserverIo/Lang/Reflection/ReflectionParameterTest.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang\Reflection; 22 | 23 | /** 24 | * This is the test for the ReflectionParameter class. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | class ReflectionParameterTest extends \PHPUnit_Framework_TestCase 33 | { 34 | 35 | /** 36 | * Checks the serialize/unserialize methods implemented 37 | * from the \Serializable interface. 38 | * 39 | * @return void 40 | */ 41 | public function testSerializeAndUnserialize() 42 | { 43 | // initialize a ReflectionParameter instance and clone it 44 | $parameterOne = new ReflectionParameter(__CLASS__, 'methodWithOneParameter', 'test'); 45 | $clonedOne = clone $parameterOne; 46 | // serialize/unserialize the ReflectionParameter 47 | $parameterOne->unserialize($parameterOne->serialize()); 48 | // check that the two ReflectionParameter instances are equal 49 | $this->assertEquals($clonedOne, $parameterOne); 50 | } 51 | 52 | /** 53 | * Checks if reflection for a a method with one parameter works. 54 | * 55 | * @return void 56 | */ 57 | public function testMethodWithOneParameter() 58 | { 59 | $reflectionParameter = new ReflectionParameter(__CLASS__, 'methodWithOneParameter', 'test'); 60 | $this->assertEquals(0, $reflectionParameter->getPosition()); 61 | } 62 | 63 | /** 64 | * Checks if reflection for a a method with two parameters works. 65 | * 66 | * @return void 67 | */ 68 | public function testMethodWithTwoParameters() 69 | { 70 | $reflectionParameter = new ReflectionParameter(__CLASS__, 'methodWithTwoParameters', 'test1'); 71 | $this->assertEquals(1, $reflectionParameter->getPosition()); 72 | } 73 | 74 | /** 75 | * A method with one parameter, used for testing purposes. 76 | * 77 | * @param string $test A test parameter 78 | * 79 | * @return void 80 | */ 81 | public function methodWithOneParameter($test) 82 | { 83 | // we do nothing here 84 | } 85 | 86 | /** 87 | * A method with one parameter, used for testing purposes. 88 | * 89 | * @param string $test0 First test parameter 90 | * @param string $test1 Second test parameter 91 | * 92 | * @return void 93 | */ 94 | public function methodWithTwoParameters($test0, $test1) 95 | { 96 | // we do nothing here 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /tests/AppserverIo/Lang/Reflection/ReflectionPropertyTest.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang\Reflection; 22 | 23 | /** 24 | * This is the test for the ReflectionMethod class. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | class ReflectionPropertyTest extends \PHPUnit_Framework_TestCase 33 | { 34 | 35 | /** 36 | * A random name for a method. 37 | * 38 | * @var string 39 | */ 40 | const PROPERTY_NAME = 'reflectionProperty'; 41 | 42 | /** 43 | * The reflection property instance we want to test. 44 | * 45 | * @var \AppserverIo\Lang\Reflection\ReflectionProperty 46 | * @MockAnnotation(name=MockAnnotation, description="some description", value="a value") 47 | */ 48 | protected $reflectionProperty; 49 | 50 | /** 51 | * Initializes the instance before we run each test. 52 | * 53 | * @return void 54 | * @see PHPUnit_Framework_TestCase::setUp() 55 | */ 56 | protected function setUp() 57 | { 58 | $this->reflectionProperty = new ReflectionProperty(__CLASS__, ReflectionPropertyTest::PROPERTY_NAME); 59 | } 60 | 61 | /** 62 | * Checks the serialize/unserialize methods implemented 63 | * from the \Serializable interface. 64 | * 65 | * @return void 66 | */ 67 | public function testSerializeAndUnserialize() 68 | { 69 | // initialize a ReflectionProperty instance and clone it 70 | $propertyOne = new ReflectionProperty(__CLASS__, ReflectionPropertyTest::PROPERTY_NAME); 71 | $clonedOne = clone $propertyOne; 72 | // serialize/unserialize the ReflectionProperty 73 | $propertyOne->unserialize($propertyOne->serialize()); 74 | // check that the two ReflectionProperty instances are equal 75 | $this->assertEquals($clonedOne, $propertyOne); 76 | } 77 | 78 | /** 79 | * This test checks the resolved class name. 80 | * 81 | * @return void 82 | */ 83 | public function testGetClass() 84 | { 85 | // check for the correct class name 86 | $this->assertEquals('AppserverIo\Lang\Reflection\ReflectionProperty', ReflectionProperty::__getClass()); 87 | } 88 | 89 | /** 90 | * Test if the class annotation is available and has the correct values set. 91 | * 92 | * @return void 93 | * @MockAnnotation(name=MockAnnotation, description="some description", value="a value") 94 | */ 95 | public function testGetAnnotation() 96 | { 97 | $annotation = $this->reflectionProperty->getAnnotation('MockAnnotation'); 98 | $this->assertSame($annotation->getValue('name'), 'MockAnnotation'); 99 | $this->assertSame($annotation->getValue('description'), 'some description'); 100 | $this->assertSame($annotation->getValue('value'), 'a value'); 101 | } 102 | 103 | /** 104 | * Test if an execption is thrown if a requested annotation is not available. 105 | * 106 | * @return void 107 | * @expectedException AppserverIo\Lang\Reflection\ReflectionException 108 | */ 109 | public function testGetAnnotationWithException() 110 | { 111 | $this->reflectionProperty->getAnnotation('UnknownAnnotation'); 112 | } 113 | 114 | /** 115 | * Checks if the addAnnotationAlias() method works as expected. 116 | * 117 | * @return void 118 | */ 119 | public function testAddAnnotationAlias() 120 | { 121 | 122 | // add the annotation alias 123 | $this->reflectionProperty->addAnnotationAlias($name = 'test', $className = __CLASS__); 124 | 125 | // load the annotation aliases 126 | $annotationAliases = $this->reflectionProperty->getAnnotationAliases(); 127 | 128 | // check the annotation alias 129 | $this->assertCount(1, $annotationAliases); 130 | $this->assertTrue(array_key_exists($name, $annotationAliases)); 131 | $this->assertContains($className, $annotationAliases); 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /tests/AppserverIo/Lang/StrngTest.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright 2015 TechDivision GmbH 16 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 17 | * @link https://github.com/appserver-io/lang 18 | * @link http://www.appserver.io 19 | */ 20 | 21 | namespace AppserverIo\Lang; 22 | 23 | /** 24 | * This is the test for the Strng class. 25 | * 26 | * @author Tim Wagner 27 | * @copyright 2015 TechDivision GmbH 28 | * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 29 | * @link https://github.com/appserver-io/lang 30 | * @link http://www.appserver.io 31 | */ 32 | class StrngTest extends \PHPUnit_Framework_TestCase 33 | { 34 | 35 | /** 36 | * Checks the serialize/unserialize methods implemented 37 | * from the \Serializable interface. 38 | * 39 | * @return void 40 | */ 41 | public function testSerializeAndUnserialize() 42 | { 43 | // initialize a Strng instance and clone it 44 | $stringOne = new Strng('aStringValue'); 45 | $clonedOne = clone $stringOne; 46 | // serialize/unserialize the Strng instance 47 | $stringOne->unserialize($stringOne->serialize()); 48 | // check that the two Strng instances are equal 49 | $this->assertEquals($clonedOne, $stringOne); 50 | } 51 | 52 | /** 53 | * This test checks the resolved class name. 54 | * 55 | * @return void 56 | */ 57 | public function testGetClass() 58 | { 59 | $this->assertEquals('AppserverIo\Lang\Strng', Strng::__getClass()); 60 | } 61 | 62 | /** 63 | * This test checks the Strng's equal method. 64 | * 65 | * @return void 66 | */ 67 | public function testEquals() 68 | { 69 | // initialize a new Strng instance 70 | $string = new Strng('Mustermann'); 71 | // check that the two Strng's are equal 72 | $this->assertTrue($string->equals(new Strng('Mustermann'))); 73 | } 74 | 75 | /** 76 | * This test checks the Strng's stringValue() method. 77 | * 78 | * @return void 79 | */ 80 | public function testStringValue() 81 | { 82 | // initialize a new Strng instance 83 | $string = new Strng($test = 'Mustermann'); 84 | // check that string value of the Strng instance 85 | $this->assertEquals($test, $string->stringValue()); 86 | } 87 | 88 | /** 89 | * This test checks the Strng's trim() method. 90 | * 91 | * @return void 92 | */ 93 | public function testTrim() 94 | { 95 | // initialize a new Strng instance 96 | $string = new Strng(' Mustermann '); 97 | // check that Strng was trimmed successfully 98 | $this->assertTrue($string->trim()->equals(new Strng('Mustermann'))); 99 | } 100 | 101 | /** 102 | * This test checks the Strng's md5() method. 103 | * 104 | * @return void 105 | */ 106 | public function testMd5() 107 | { 108 | // initialize a new Strng instance 109 | $string = new Strng('Mustermann'); 110 | // check that Strng's md5 summs equals 111 | $this->assertTrue($string->md5()->equals(new Strng(md5('Mustermann')))); 112 | } 113 | 114 | /** 115 | * This test checks the Strng's toUpperCase() method. 116 | * 117 | * @return void 118 | */ 119 | public function testToUpperCase() 120 | { 121 | // initialize a new Strng instance 122 | $string = new Strng('mustermann1'); 123 | // check that Strng was successfully convered to upper case 124 | $this->assertTrue($string->toUpperCase()->equals(new Strng('MUSTERMANN1'))); 125 | } 126 | 127 | /** 128 | * This test checks the Strng's toLowerCase() method. 129 | * 130 | * @return void 131 | */ 132 | public function testToLowerCase() 133 | { 134 | // initialize a new Strng instance 135 | $string = new Strng('MUSTERMANN1'); 136 | // check that Strng was successfully convered to upper case 137 | $this->assertTrue($string->toLowerCase()->equals(new Strng('mustermann1'))); 138 | } 139 | 140 | /** 141 | * This test checks the Strng's concat method. 142 | * 143 | * @return void 144 | */ 145 | public function testConcat() 146 | { 147 | // initialize a new Strng instance 148 | $string = new Strng('value to'); 149 | // check that Strng was successfully concatenated 150 | $this->assertTrue($string->concat(new Strng(' search'))->equals(new Strng('value to search'))); 151 | } 152 | 153 | /** 154 | * This test checks the Strng's valueOf method. 155 | * 156 | * @return void 157 | */ 158 | public function testValueOf() 159 | { 160 | // initialize a new Strng instance 161 | $string = Strng::valueOf($value = 'value of'); 162 | // check that Strng was successfully concatenated 163 | $this->assertTrue($string->equals(new Strng($string))); 164 | } 165 | } 166 | --------------------------------------------------------------------------------