Skip to content

Commit 389a78c

Browse files
committed
first commit
0 parents  commit 389a78c

File tree

8 files changed

+287
-0
lines changed

8 files changed

+287
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.buildpath
2+
.project
3+
.settings
4+
composer.lock
5+
vendor

composer.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "php-mod/object",
3+
"description": "Object class for PHP",
4+
"keywords": ["object"],
5+
"homepage": "https://github.com/php-mod/object",
6+
"type": "library",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Hassan Amouhzi",
11+
"email": "[email protected]",
12+
"homepage": "http://hassan.amouhzi.com"
13+
}
14+
],
15+
"require": {
16+
"php": ">=5.3.0"
17+
},
18+
"require-dev": {
19+
"phpunit/phpunit": "3.7.28"
20+
},
21+
"autoload": {
22+
"psr-0": {
23+
"PHP\\Lang": "src/"
24+
}
25+
}
26+
}

phpunit.xml.dist

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
processIsolation="false"
9+
stopOnFailure="false"
10+
syntaxCheck="true"
11+
verbose="false"
12+
bootstrap="vendor/autoload.php"
13+
>
14+
<php>
15+
<ini name="display_errors" value="on"/>
16+
</php>
17+
18+
<testsuites>
19+
<testsuite name="PHP Object Class Tests Suite">
20+
<directory>tests</directory>
21+
</testsuite>
22+
</testsuites>
23+
24+
</phpunit>

src/PHP/Lang/ArrayObject.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace PHP\Lang;
4+
5+
class ArrayObject extends \ArrayObject implements ObjectInterface {
6+
7+
static public function alias($alias, $autoload = TRUE) {
8+
return class_alias ( get_called_class() , $alias , $autoload);
9+
}
10+
11+
static public function getClassName() {
12+
return get_called_class();
13+
}
14+
15+
static public function getClassMethods() {
16+
return get_class_methods(get_called_class());
17+
}
18+
19+
static public function getClassVars() {
20+
return get_class_vars(get_called_class());
21+
}
22+
23+
public function getVars() {
24+
return get_object_vars($this);
25+
}
26+
27+
static public function getParentClass() {
28+
return get_parent_class(get_called_class());
29+
}
30+
31+
public function offsetGet($index) {
32+
if($this->offsetExists($index)) {
33+
return parent::offsetGet($index);
34+
} else {
35+
return array_column($this->getArrayCopy(), $index);
36+
}
37+
}
38+
}

src/PHP/Lang/Object.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace PHP\Lang;
4+
5+
class Object extends \stdClass {
6+
7+
static public function alias($alias, $autoload = TRUE) {
8+
return class_alias ( get_called_class() , $alias , $autoload);
9+
}
10+
11+
static public function getClassName() {
12+
return get_called_class();
13+
}
14+
15+
static public function getClassMethods() {
16+
return get_class_methods(get_called_class());
17+
}
18+
19+
static public function getClassVars() {
20+
return get_class_vars(get_called_class());
21+
}
22+
23+
public function getVars() {
24+
return get_object_vars($this);
25+
}
26+
27+
static public function getParentClass() {
28+
return get_parent_class(get_called_class());
29+
}
30+
31+
}

src/PHP/Lang/ObjectInterface.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace PHP\Lang;
4+
5+
interface ObjectInterface {
6+
static public function alias($alias, $autoload = TRUE);
7+
8+
static public function getClassName();
9+
10+
static public function getClassMethods();
11+
12+
static public function getClassVars();
13+
14+
public function getVars();
15+
16+
static public function getParentClass();
17+
}

tests/PHP/Lang/ArrayObjectTest.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace PHP\Lang;
4+
5+
class ArrayObjectTest extends \PHPUnit_Framework_TestCase {
6+
7+
function setUp() {
8+
}
9+
10+
public function test() {
11+
12+
$records = array(
13+
array(
14+
'id' => 2135,
15+
'first_name' => 'John',
16+
'last_name' => 'Doe',
17+
),
18+
array(
19+
'id' => 3245,
20+
'first_name' => 'Sally',
21+
'last_name' => 'Smith',
22+
),
23+
array(
24+
'id' => 5342,
25+
'first_name' => 'Jane',
26+
'last_name' => 'Jones',
27+
),
28+
array(
29+
'id' => 5623,
30+
'last_name' => 'Doe',
31+
)
32+
);
33+
34+
$array = new ArrayObject($records);
35+
36+
$first_names = $array['first_name'];
37+
$this->assertEquals(array
38+
(
39+
'John',
40+
'Sally',
41+
'Jane',
42+
//'Peter'
43+
), $first_names);
44+
45+
}
46+
47+
}

tests/PHP/Lang/ObjectTest.php

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace PHP\Lang;
4+
5+
class Foo extends Object {
6+
7+
}
8+
9+
class ObjectTest extends \PHPUnit_Framework_TestCase {
10+
11+
function setUp() {
12+
}
13+
14+
public function testAlias() {
15+
16+
Object::alias('Foo');
17+
\Foo::alias('TestNS\Bar');
18+
19+
$a = new Object;
20+
$b = new \Foo;
21+
$c = new \TestNS\Bar;
22+
23+
$this->assertTrue($a == $b);
24+
$this->assertFalse($a === $b);
25+
$this->assertTrue($a instanceof $b);
26+
27+
$this->assertTrue($a == $c);
28+
$this->assertFalse($a === $c);
29+
$this->assertTrue($a instanceof $c);
30+
31+
$this->assertTrue($a instanceof Object);
32+
$this->assertTrue($a instanceof \Foo);
33+
$this->assertTrue($a instanceof \TestNS\Bar);
34+
35+
$this->assertTrue($b instanceof Object);
36+
$this->assertTrue($b instanceof \Foo);
37+
$this->assertTrue($b instanceof \TestNS\Bar);
38+
39+
$this->assertTrue($c instanceof Object);
40+
$this->assertTrue($c instanceof \Foo);
41+
$this->assertTrue($c instanceof \TestNS\Bar);
42+
43+
}
44+
45+
public function testGetClassName() {
46+
47+
$this->assertEquals('PHP\Lang\Object', Object::getClassName());
48+
$this->assertEquals('PHP\Lang\Foo', Foo::getClassName());
49+
50+
}
51+
52+
public function testGetClassMethods() {
53+
54+
$class_methods = Foo::getClassMethods();
55+
56+
$this->assertEquals(array(
57+
'alias',
58+
'getClassName',
59+
'getClassMethods',
60+
'getClassVars',
61+
'getVars',
62+
'getParentClass'
63+
), $class_methods);
64+
65+
}
66+
67+
public function testGetClassVars() {
68+
69+
$class_vars = Foo::getClassVars();
70+
71+
$this->assertEquals(array(), $class_vars);
72+
73+
}
74+
75+
public function testGetClass() {
76+
77+
$a = new Foo();
78+
$className = $a->getClassName();
79+
80+
$this->assertEquals('PHP\Lang\Foo', $className);
81+
82+
}
83+
84+
public function testGetVars() {
85+
86+
$a = new Foo();
87+
88+
$this->assertEquals(array(), $a->getVars());
89+
90+
}
91+
92+
public function testGetParentClass() {
93+
94+
$this->assertEquals('stdClass', Object::getParentClass());
95+
$this->assertEquals('PHP\Lang\Object', Foo::getParentClass());
96+
97+
}
98+
99+
}

0 commit comments

Comments
 (0)