Skip to content

Commit 213a70d

Browse files
authored
Added function for getting properties (#10)
* Added function for getting properties * cs * Updated changelog * USe PHP 5.3 syntax * Use master build status * PHP 5.3 code changes
1 parent 59018cf commit 213a70d

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# Change Log
22

33

4-
## 1.0.1 - 2016-08-x?
4+
## 1.1.0 - 2017-04-13
5+
6+
* Added `NSA::getProperties`
7+
8+
## 1.0.1 - 2016-08-x9
59

610
* Using stable version of webmozart/assert
711

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Latest Version](https://img.shields.io/github/release/Nyholm/NSA.svg?style=flat-square)](https://github.com/Nyholm/NSA/releases)
44
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
5-
[![Build Status](https://img.shields.io/travis/Nyholm/NSA.svg?style=flat-square)](https://travis-ci.org/Nyholm/NSA)
5+
[![Build Status](https://img.shields.io/travis/Nyholm/NSA/master.svg?style=flat-square)](https://travis-ci.org/Nyholm/NSA)
66
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/Nyholm/NSA.svg?style=flat-square)](https://scrutinizer-ci.com/g/Nyholm/NSA)
77
[![Quality Score](https://img.shields.io/scrutinizer/g/Nyholm/NSA.svg?style=flat-square)](https://scrutinizer-ci.com/g/Nyholm/NSA)
88
[![Total Downloads](https://img.shields.io/packagist/dt/nyholm/nsa.svg?style=flat-square)](https://packagist.org/packages/nyholm/nsa)

src/NSA.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,37 @@ protected static function getAccessibleReflectionProperty($objectOrClass, $prope
156156

157157
return $property;
158158
}
159+
160+
/**
161+
* Get all property names on a class or object
162+
*
163+
* @param object|string $objectOrClass
164+
*
165+
* @return array of strings
166+
*
167+
* @throws \InvalidArgumentException
168+
*/
169+
public static function getProperties($objectOrClass)
170+
{
171+
$class = $objectOrClass;
172+
if (!is_string($objectOrClass)) {
173+
Assert::object($objectOrClass, 'Can not get a property of a non object. Variable of type "%s" was given.');
174+
Assert::notInstanceOf($objectOrClass, '\stdClass', 'Can not get a property of \stdClass.');
175+
$class = get_class($objectOrClass);
176+
}
177+
178+
$refl = new \ReflectionClass($class);
179+
$properties = $refl->getProperties();
180+
181+
// check parents
182+
while (false !== $parent = get_parent_class($class)) {
183+
$parentRefl = new \ReflectionClass($parent);
184+
$properties = array_merge($properties, $parentRefl->getProperties());
185+
$class = $parent;
186+
}
187+
188+
return array_map(function($reflectionProperty) {
189+
return $reflectionProperty->name;
190+
}, $properties);
191+
}
159192
}

tests/Unit/GetPropertiesTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Nyholm\NSA\tests\Unit;
4+
5+
use Nyholm\NSA;
6+
use Nyholm\NSA\Tests\Fixture\Dog;
7+
8+
class GetPropertiesTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @expectedException \InvalidArgumentException
12+
*/
13+
public function testIntegerProperty()
14+
{
15+
NSA::getProperties(1);
16+
}
17+
18+
/**
19+
* @expectedException \InvalidArgumentException
20+
*/
21+
public function testStdClassProperty()
22+
{
23+
$o = new \stdClass();
24+
$o->foo = 'bar';
25+
26+
NSA::getProperties($o);
27+
}
28+
29+
public function propertyDataProvider()
30+
{
31+
$dog = new Dog();
32+
return array(
33+
array('Nyholm\NSA\Tests\Fixture\Dog', 'name'),
34+
array('Nyholm\NSA\Tests\Fixture\Dog', 'owner'),
35+
array('Nyholm\NSA\Tests\Fixture\Dog', 'owner'),
36+
array('Nyholm\NSA\Tests\Fixture\Dog', 'color'),
37+
array('Nyholm\NSA\Tests\Fixture\Dog', 'latinName'),
38+
array('Nyholm\NSA\Tests\Fixture\Dog', 'age'),
39+
array('Nyholm\NSA\Tests\Fixture\Dog', 'birthday'),
40+
array('Nyholm\NSA\Tests\Fixture\Dog', 'count'),
41+
array($dog, 'name'),
42+
array($dog, 'owner'),
43+
array($dog, 'owner'),
44+
array($dog, 'color'),
45+
array($dog, 'latinName'),
46+
array($dog, 'age'),
47+
array($dog, 'birthday'),
48+
array($dog, 'count'),
49+
);
50+
}
51+
52+
/**
53+
* @dataProvider propertyDataProvider
54+
*/
55+
public function testListProperties($classOrObject, $propertyName)
56+
{
57+
$result = NSA::getProperties($classOrObject);
58+
59+
$message = sprintf('Count not find property "%s" in array [%s]', $propertyName, implode(', ', $result));
60+
$this->assertTrue(in_array($propertyName, $result), $message);
61+
}
62+
}

0 commit comments

Comments
 (0)