Skip to content

Commit eb5fbba

Browse files
committed
Merge branch 'release-1.0'
2 parents fea6c27 + 70dada9 commit eb5fbba

24 files changed

+2031
-5
lines changed

CHANGELOG.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
CHANGELOG
2+
=========
3+
4+
## 1.0 (2015.03.07)
5+
6+
* 8d81dea Exorg\ namespace added, autoloading changed to PSR-4
7+
* b2127d8 Merge pull request #8 from katheroine/issue-2-decapsulator
8+
* d3ad956 ObjectDecapsulator code style fixed (#2)
9+
* 10f1fee Merge pull request #7 from katheroine/issue-2-decapsulator
10+
* 8f7df0d ObjectDecapsulator tests moved to separated directory and namespace (#2)
11+
* 9dec4b0 ObjectDacapsulator magic __construct disabled (#2)
12+
* 305cf16 Tests refactoring. ObjectDecapsulator bugs fixes. (#2)
13+
* c691130 Merge pull request #6 from katheroine/issue-2-decapsulator
14+
* 51bd7d1 ObjectDecapsulator magic __call method implemented (#2)
15+
* 068511c ObjectDecapsulator callMethod method implemented (#2)
16+
* ff680b9 ObjectDecapsulator methodExists method implemented (#2)
17+
* 5f83b91 ObjectDecapsulator magic __get method implemented (#2)
18+
* 15f7307 ObjectDecapsulator getProperty method implemented (#2)
19+
* 4df3f77 ObjectDecapsulator magic __set method implemented (#2)
20+
* 782f7f8 ObjectDecapsulator setProperty method implemented (#2)
21+
* 0895a13 ObjectDecapsulator propertyExists method implemented (#2)
22+
* a0b1330 ObjectDecapsulator buildForObject method implemented (#2)
23+
* 445a8c2 ObjectDecapsulator objectIsValid method implemented (#2)
24+
* 7bbfa79 ObjectDecapsulator createInstanceFromObject method implemented (#2)
25+
* 0535d73 ObjectDecapsulator setUpWithObject method implemented (#2)
26+
* 9c79d60 ObjectDecapsulator setObject method implemented (#2)
27+
* a184695 ObjectDecapsulator setUpReflection method implemented (#2)
28+
* fea6c27 Merge pull request #5 from katheroine/issue-1-composer-config
29+
* 766d1f6 Add PHP_CodeSniffer to the composer configuration (#1)
30+
* 584c283 Merge pull request #4 from katheroine/issue-1-composer-config
31+
* 431ba22 Fix composer config and add PHPUnit config (#1)
32+
* ffdd6c2 Merge pull request #3 from katheroine/issue-1-composer-config
33+
* 0dee43f Fix composer configuration and git ignored files (#1)
34+
* fb0ede8 Add composer configuration and update git ignored files (#1)
35+
* 79d95f8 Create README.md
36+
* 1a4ff5f Create LICENSE.md
37+
38+

composer.json

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
{
22
"name": "exorg/decapsulator",
3-
"description": "Decapsulator",
3+
"description": "Decapsulating objects wrapper.",
44
"type": "library",
55
"keywords": [
66
"exorg",
77
"decapsulator"
88
],
99
"license": "MIT",
10+
"authors" : [
11+
{
12+
"name": "Katarzyna Krasińska",
13+
"email": "[email protected]",
14+
"role": "Developer"
15+
}
16+
],
1017
"require": {
1118
"php": ">=5.5.9"
1219
},
@@ -15,13 +22,13 @@
1522
"squizlabs/php_codesniffer": "*"
1623
},
1724
"autoload": {
18-
"psr-0": {
19-
"Decapsulator\\": "src/"
25+
"psr-4": {
26+
"Exorg\\Decapsulator\\": "src/"
2027
}
2128
},
2229
"autoload-dev": {
23-
"psr-0": {
24-
"Decapsulator\\": "tests/"
30+
"psr-4": {
31+
"Exorg\\Decapsulator\\": "tests/"
2532
}
2633
}
2734
}

src/ObjectDecapsulator.php

+276
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Decapsulator package.
5+
*
6+
* (c) Katarzyna Krasińska <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ExOrg\Decapsulator;
13+
14+
/**
15+
* ObjectDecapsulator.
16+
* Decapsulator for object.
17+
* Provider direct access to non-public properties and methods
18+
* of the class of the wrapped object.
19+
*
20+
* @package Decapsulator
21+
* @author Katarzyna Krasińska <[email protected]>
22+
* @copyright Copyright (c) 2015 Katarzyna Krasińska
23+
* @license http://framework.zend.com/license/new-bsd New BSD License
24+
* @link http://github.com/exorg/decapsulator
25+
*/
26+
class ObjectDecapsulator
27+
{
28+
/**
29+
* Valid decapsulated object type.
30+
*
31+
* @var unknown
32+
*/
33+
const VALID_OBJECT_TYPE = 'object';
34+
35+
/**
36+
* Decapsulated object.
37+
*
38+
* @var mixed
39+
*/
40+
private $object;
41+
42+
/**
43+
* Reflection for decapsulated object.
44+
*
45+
* @var \ReflectionClass
46+
*/
47+
private $reflection;
48+
49+
/**
50+
* Constructor not available for public usage.
51+
*/
52+
private function __construct()
53+
{
54+
// Default action.
55+
}
56+
57+
/**
58+
* Build ObjectDecapsulator instance for given decapsulated object.
59+
*
60+
* @param mixed $object
61+
* @throws \InvalidArgumentException
62+
* @return \Decapsulator\ObjectDecapsulator
63+
*/
64+
public static function buildForObject($object)
65+
{
66+
if (self::objectIsValid($object)) {
67+
$decapsulator = self::createInstanceFromObject($object);
68+
69+
return $decapsulator;
70+
} else {
71+
$message = 'Argument is not an object.';
72+
$exception = new \InvalidArgumentException($message);
73+
74+
throw $exception;
75+
}
76+
}
77+
78+
/**
79+
* Magically set object choosen property value.
80+
* Called when property is set directly by the property name.
81+
*
82+
* @param string $name
83+
* @param mixed $value
84+
*/
85+
public function __set($name, $value)
86+
{
87+
$propertyExists = $this->propertyExists($name);
88+
89+
if ($propertyExists) {
90+
$this->setProperty($name, $value);
91+
} else {
92+
$message = 'Property does not exist.';
93+
$exception = new \InvalidArgumentException($message);
94+
95+
throw $exception;
96+
}
97+
}
98+
99+
/**
100+
* Magically get object choosen property value.
101+
* Called when property is get directly by the property name.
102+
*
103+
* @param string $name
104+
* @return mixed
105+
*/
106+
public function __get($name)
107+
{
108+
$propertyExists = $this->propertyExists($name);
109+
110+
if ($propertyExists) {
111+
$value = $this->getProperty($name);
112+
113+
return $value;
114+
} else {
115+
$message = 'Property does not exist.';
116+
$exception = new \InvalidArgumentException($message);
117+
118+
throw $exception;
119+
}
120+
}
121+
122+
/**
123+
* Magically call object choosen method.
124+
* Called when method is called directly by the method name.
125+
*
126+
* @param string $name
127+
* @param array[mixed] $arguments
128+
* @return mixed
129+
*/
130+
public function __call($name, $arguments)
131+
{
132+
$methodExists = $this->methodExists($name);
133+
134+
if ($methodExists) {
135+
$result = $this->callMethod($name, $arguments);
136+
137+
return $result;
138+
} else {
139+
$message = 'Method does not exist.';
140+
$exception = new \InvalidArgumentException($message);
141+
142+
throw $exception;
143+
}
144+
}
145+
146+
/**
147+
* Check object is valid instance of the class.
148+
*
149+
* @param mixed $object
150+
* @return bool
151+
*/
152+
private static function objectIsValid($object)
153+
{
154+
$objectType = gettype($object);
155+
$objectIsValid = ($objectType === self::VALID_OBJECT_TYPE);
156+
157+
return $objectIsValid;
158+
}
159+
160+
/**
161+
* Create ObjectDecapsulator instance wrapped given object.
162+
*
163+
* @param mixed $object
164+
* @return \Decapsulator\ObjectDecapsulator
165+
*/
166+
private static function createInstanceFromObject($object)
167+
{
168+
$instance = new self();
169+
$instance->setUpWithObject($object);
170+
171+
return $instance;
172+
}
173+
174+
/**
175+
* Set-up instance properties with object data.
176+
*
177+
* @param mixed $object
178+
*/
179+
private function setUpWithObject($object)
180+
{
181+
$this->setObject($object);
182+
$this->setUpReflection();
183+
}
184+
185+
/**
186+
* Set decapsulated object.
187+
*
188+
* @param mixed $object
189+
*/
190+
private function setObject($object)
191+
{
192+
$this->object = $object;
193+
}
194+
195+
/**
196+
* Set-up reflection for decapsulated object.
197+
*/
198+
private function setUpReflection()
199+
{
200+
$className = get_class($this->object);
201+
$this->reflection = new \ReflectionClass($className);
202+
}
203+
204+
/**
205+
* Check object property with given name exists.
206+
*
207+
* @param string $name
208+
* @throws \UnexpectedValueException
209+
* @return boolean
210+
*/
211+
private function propertyExists($name)
212+
{
213+
$className = get_class($this->object);
214+
$propertyExists = property_exists($className, $name);
215+
216+
return $propertyExists;
217+
}
218+
219+
/**
220+
* Check object method with given name exists.
221+
*
222+
* @param string $name
223+
* @throws \UnexpectedValueException
224+
* @return boolean
225+
*/
226+
private function methodExists($name)
227+
{
228+
$methodExists = method_exists($this->object, $name);
229+
230+
return $methodExists;
231+
}
232+
233+
/**
234+
* Set value of the given object property.
235+
*
236+
* @param string $name
237+
* @param mixed $value
238+
*/
239+
private function setProperty($name, $value)
240+
{
241+
$property = $this->reflection->getProperty($name);
242+
$property->setAccessible(true);
243+
$property->setValue($this->object, $value);
244+
}
245+
246+
/**
247+
* Get value of the given object property.
248+
*
249+
* @param string $name
250+
* @return mixed
251+
*/
252+
private function getProperty($name)
253+
{
254+
$property = $this->reflection->getProperty($name);
255+
$property->setAccessible(true);
256+
$value = $property->getValue($this->object);
257+
258+
return $value;
259+
}
260+
261+
/**
262+
* Call given object method.
263+
*
264+
* @param string $name
265+
* @param array[mixed] $arguments
266+
* @return mixed
267+
*/
268+
private function callMethod($name, $arguments = array())
269+
{
270+
$method = $this->reflection->getMethod($name);
271+
$method->setAccessible(true);
272+
$result = $method->invokeArgs($this->object, $arguments);
273+
274+
return $result;
275+
}
276+
}

0 commit comments

Comments
 (0)