├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src ├── ClassFunctions.php └── functions.php └── tests └── ClassFunctionsTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | vendor/ 3 | bin/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Bernhard Schussek 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ClassFunctions 2 | 3 | PHP Utilities to get different variations of class names 4 | 5 | ```php 6 | use Verraes\ClassFunctions; 7 | 8 | // Fully qualified class name of an object, without a leading backslash 9 | ClassFunctions\fqcn($object); 10 | 11 | // Canonical class name of an object, of the form "My.Namespace.MyClass" 12 | ClassFunctions\canonical($object); 13 | 14 | // Underscored and lowercased class name of an object, of the form "my.namespace.my_class" 15 | ClassFunctions\underscore($object); 16 | 17 | // The class name of an object, without the namespace 18 | ClassFunctions\short($object); 19 | ``` 20 | 21 | The above methods also accept strings, and `ClassFunctions\fqcn()` accepts a canonical 22 | class name as returned from `ClassFunctions\canonical()`. 23 | 24 | Free bonus feature: 25 | 26 | ```php 27 | // Returns an associative array of 'CONSTANT_NAME' => 'value' 28 | ClassFunctions\constants('Verraes\ClassFunctions\Tests\MyClass') 29 | ``` 30 | 31 | ## Installation 32 | 33 | You can install ClassFunctions with Composer: 34 | 35 | ``` 36 | composer require mathiasverraes/classfunctions 37 | ``` 38 | 39 | Run `composer install` or `composer update` and you're ready to start. 40 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mathiasverraes/classfunctions", 3 | "description": "Functions to manipulate class names", 4 | "homepage": "http://verraes.net", 5 | "license": "MIT", 6 | "type": "library", 7 | "keywords": [ 8 | "classes", 9 | "utilities" 10 | ], 11 | "authors": [ 12 | { 13 | "name": "Mathias Verraes", 14 | "email": "mathias@verraes.net", 15 | "role": "Developer", 16 | "homepage": "http://verraes.net" 17 | } 18 | ], 19 | "support": { 20 | "issues": "https://github.com/mathiasverraes/classfunctions/issues" 21 | }, 22 | 23 | "extra": { 24 | "branch-alias": { 25 | "dev-master": "1.1-dev" 26 | } 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "Verraes\\ClassFunctions\\": "src/" 31 | }, 32 | "files": ["src/functions.php"] 33 | }, 34 | "autoload-dev": { 35 | "psr-4": { 36 | "Verraes\\ClassFunctions\\Tests\\": "tests/" 37 | } 38 | }, 39 | 40 | "require": { 41 | "php": ">=5.4" 42 | }, 43 | "require-dev": { 44 | "phpunit/phpunit": "4.*" 45 | }, 46 | 47 | "config": { 48 | "bin-dir": "bin" 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | tests 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/ClassFunctions.php: -------------------------------------------------------------------------------- 1 | contents for a given class 54 | * @param string $className 55 | * @return string[] 56 | */ 57 | public static function constants($className) 58 | { 59 | return constants($className); 60 | } 61 | } 62 | 63 | -------------------------------------------------------------------------------- /src/functions.php: -------------------------------------------------------------------------------- 1 | contents for a given class 55 | * @param string $className 56 | * @return string[] 57 | */ 58 | function constants($className) 59 | { 60 | return (new \ReflectionClass($className))->getConstants(); 61 | } 62 | -------------------------------------------------------------------------------- /tests/ClassFunctionsTest.php: -------------------------------------------------------------------------------- 1 | object = new MyClass; 15 | $this->string = 'Verraes\ClassFunctions\Tests\MyClass'; 16 | } 17 | 18 | /** 19 | * @test 20 | */ 21 | public function fqcn() 22 | { 23 | $this->assertEquals('Verraes\ClassFunctions\Tests\MyClass', ClassFunctions::fqcn($this->object)); 24 | $this->assertEquals('Verraes\ClassFunctions\Tests\MyClass', ClassFunctions::fqcn($this->string)); 25 | $this->assertEquals('Verraes\ClassFunctions\Tests\MyClass', ClassFunctions::fqcn('Verraes.ClassFunctions.Tests.MyClass')); 26 | } 27 | 28 | /** 29 | * @test 30 | */ 31 | public function canonical() 32 | { 33 | $this->assertEquals('Verraes.ClassFunctions.Tests.MyClass', ClassFunctions::canonical($this->object)); 34 | $this->assertEquals('Verraes.ClassFunctions.Tests.MyClass', ClassFunctions::canonical($this->string)); 35 | } 36 | 37 | /** 38 | * @test 39 | */ 40 | public function underscore() 41 | { 42 | $this->assertEquals('verraes.class_functions.tests.my_class', ClassFunctions::underscore($this->object)); 43 | $this->assertEquals('verraes.class_functions.tests.my_class', ClassFunctions::underscore($this->string)); 44 | } 45 | 46 | /** 47 | * @test 48 | */ 49 | public function short() 50 | { 51 | $this->assertEquals('MyClass', ClassFunctions::short($this->object)); 52 | $this->assertEquals('MyClass', ClassFunctions::short($this->string)); 53 | } 54 | 55 | /** 56 | * @test 57 | */ 58 | public function constants() 59 | { 60 | $this->assertEquals( 61 | ['MY_CONST1' => 'a', 'MY_CONST2' => 'b'], 62 | ClassFunctions::constants('Verraes\ClassFunctions\Tests\MyClass') 63 | ); 64 | } 65 | } 66 | 67 | class MyClass 68 | { 69 | const MY_CONST1 = 'a'; 70 | const MY_CONST2 = 'b'; 71 | } --------------------------------------------------------------------------------