Changeset 1785
- Timestamp:
- 08/08/08 15:14:36 (3 months ago)
- Files:
-
- labs/incubator/src/main/php/net/stubbles/lang/mop/stubMetaClass.php (modified) (2 diffs)
- labs/incubator/src/main/php/net/stubbles/lang/mop/stubMetaObject.php (modified) (4 diffs)
- labs/incubator/src/test/php/net/stubbles/lang/mop/stubMetaClassTestCase.php (modified) (4 diffs)
- labs/incubator/src/test/php/net/stubbles/lang/mop/stubMetaObjectTestCase.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
labs/incubator/src/main/php/net/stubbles/lang/mop/stubMetaClass.php
r1783 r1785 55 55 56 56 /** 57 * returns true of the implementing MetaClass has a property of the given name57 * returns true if the implementing MetaClass has a property of the given name 58 58 * 59 59 * @param string $propertyName … … 143 143 144 144 /** 145 * invokes a prope try on the given receiver145 * invokes a property on the given receiver 146 146 * 147 * @param stubMetaObjectProtocol $receiver the object which the method invoked on148 * @param string $property Name the name of the method149 * @param mixed $value the arguments to the method150 * @param boolean $is Getter whether the missing property event was the result of a getter151 * @param boolean $is Setter whether the missing property event was the result of a setter152 * @return mixed the return value of the called property153 * @throws stubIllegalAccessException on unsupported property147 * @param stubMetaObjectProtocol $receiver the object which the method invoked on 148 * @param string $property the name of the property 149 * @param mixed $value the value of the property 150 * @param boolean $isSetter whether the missing property event was the result of a setter 151 * @param boolean $isGetter whether the missing property event was the result of a getter 152 * @return mixed the return value of the called property 153 * @throws stubIllegalAccessException on unsupported property 154 154 */ 155 public function invokeMissingProperty(stubMetaObjectProtocol $receiver, $property Name, $value, $isGetter = false, $isSetter = false)155 public function invokeMissingProperty(stubMetaObjectProtocol $receiver, $property, $value, $isSetter = true, $isGetter = false) 156 156 { 157 if ($isSetter === true || $isGetter === true) { 158 $method = ($isGetter ? 'get' : 'set').ucfirst($propertyName); 159 160 if (is_callable(array($receiver, $method)) === true) { 161 return ($receiver->{$method}($value)); 162 } 163 164 } else if ($this->hasProperty($propertyName)) { 165 return ($receiver->{$propertyName} = $value); 157 if (($hasProperty = $this->hasProperty($property)) && $isGetter) { 158 $receiver->setProperty($property, $this->{$property}); 159 return($this->{$property}); 160 } else if ($hasProperty && $isSetter) { 161 return $receiver->setProperty($property, $value); 166 162 } 167 163 168 throw new stubIllegalAccessException( 169 sprintf('Invoke unsupported property: %s on: %s' 170 , $propertyName 171 , $receiver->getClassName() 172 ) 173 ); 164 throw new stubIllegalAccessException('Invoke unsupported property: '.$property.' on: '.$receiver->getClassName()); 174 165 } 175 166 } labs/incubator/src/main/php/net/stubbles/lang/mop/stubMetaObject.php
r1784 r1785 20 20 class stubMetaObject extends stubBaseObject implements stubMetaObjectProtocol 21 21 { 22 /** 23 * hold the meta object properties 24 * 25 * @var array <string,mixed> 26 */ 27 private $properties = array(); 28 22 29 /** 23 30 * sets the meta class instance of the meta object … … 53 60 * sets the given property to the given value 54 61 * 55 * @param string $property Nameproperty name56 * @param mixed $value property value57 * @return stubMetaObject implement fluent interface62 * @param string $property property name 63 * @param mixed $value property value 64 * @return stubMetaObject implement fluent interface 58 65 */ 59 public function setProperty($property Name, $value = null)66 public function setProperty($property, $value = null) 60 67 { 61 if (property_exists($propertyName)) { 62 $this->{$propertyName} = $value; 63 } else { 64 $this->invokeMissingProperty($this, $propertyName, $value); 65 } 68 $this->properties[$property] = $value; 66 69 67 70 return $this; … … 71 74 * retrieves a property value 72 75 * 73 * @param string $propertyName property name 74 * @param mixed $value property value 76 * @param string $property property name 75 77 * @return mixed property value if exists or null 76 78 */ 77 public function getProperty($property Name)79 public function getProperty($property) 78 80 { 79 if ( property_exists($propertyName)) {80 return $this-> {$propertyName};81 if ($this->hasProperty($property)) { 82 return $this->properties[$property]; 81 83 } 82 84 83 return $this->invokeMissingProperty($this, $propertyName, null); 85 return $this->getMetaClass()->invokeMissingProperty($this, $property, null, false, true); 86 } 87 88 /** 89 * returns true if has a property of the given name 90 * 91 * @param string $propertyName 92 * @return boolean 93 */ 94 public function hasProperty($propertyName) 95 { 96 return array_key_exists($propertyName, $this->properties); 84 97 } 85 98 … … 98 111 99 112 /** 100 * delegate the static method callto registered meta class113 * delegate protected or unknown field/property access to registered meta class 101 114 * 102 * @param string $ method name of the method103 * @param array $ arguments given method arguments115 * @param string $property name of the property 116 * @param array $value given property valeu 104 117 * @return mixed 105 118 * @throws stubMethodNotSupportedException 106 *107 * @todo wait for php version >= 5.3 to implement __callStatic108 119 */ 109 // public static function __callStatic($method, array $arguments) 110 // { 111 // return self::getMetaClass()->invokeMethod($this, $method, $arguments); 112 // } 120 public function __set($property, $value) 121 { 122 if ($this->hasProperty($property) === true) { 123 return $this->setProperty($property, $value); 124 } 113 125 114 /** 115 * delegate protected or unknown field/property access to registered meta class 116 * 117 * @param string $fieldOrPropertyName name of the method 118 * @param array $arguments given method arguments 119 * @return void 120 * @throws stubMethodNotSupportedException 121 */ 122 public function __set($fieldOrPropertyName, $value) 123 { 124 if (in_array($fieldOrPropertyName, get_object_vars($this))) { 125 $this->{$fieldOrPropertyName} = $value; 126 } else { 127 $this->getMetaClass()->invokeMissingProperty( 128 $this, 129 $fieldOrPropertyName, 130 $value, 131 false, 132 true 133 ); 134 } 126 return $this->getMetaClass()->invokeMissingProperty($this, $property, $value); 135 127 } 136 128 137 129 /** 138 * delegate protected or unknown field/property access to registered meta class130 * delegate protected, private or unknown field/property access to registered meta class 139 131 * 140 * @param string $ fieldOrPropertyNamename of the method141 * @return void132 * @param string $property name of the method 133 * @return mixed 142 134 * @throws stubMethodNotSupportedException 143 *144 * @todo getter/setter have higher prio. if thoose exists use it!145 135 */ 146 public function __get($ fieldOrPropertyName)136 public function __get($property) 147 137 { 148 return $this->getMetaClass()->invokeMissingProperty( 149 $this, 150 $fieldOrPropertyName, 151 null, 152 $this->getMetaClass()->hasProperty($propertyName) ? false : true 153 ); 138 return $this->getProperty($property); 154 139 } 155 140 } labs/incubator/src/test/php/net/stubbles/lang/mop/stubMetaClassTestCase.php
r1783 r1785 28 28 29 29 /** 30 * dummy for stubMetaClass 31 * 32 * @var stubMetaClassTestCaseDummy 33 */ 34 protected $stubMetaClassDummy; 35 36 /** 30 37 * set up test environment 31 38 */ 32 39 public function setUp() 33 40 { 34 $this->stubMetaClass = new stubMetaClass(); 41 $this->stubMetaClass = new stubMetaClass; 42 $this->stubMetaClassDummy = new stubMetaClassTestCaseDummy; 35 43 } 36 44 … … 42 50 public function hasExpectedMethods() 43 51 { 44 $this->markTestIncomplete();45 52 $this->assertEquals( 46 53 $this->stubMetaClass->getMethods(), 47 54 array( 48 55 'getProperties', 56 'hasProperty', 49 57 'getMethods', 50 58 'hasMethod', 51 59 'invokeMethod', 52 60 'invokeMissingMethod', 61 'invokeMissingProperty', 53 62 'getClass', 54 63 'getPackage', … … 64 73 65 74 /** 66 * test have none properties75 * test even can retrieve all property names as array 67 76 * 68 77 * @test 69 78 */ 70 public function hasNoVisibileProperties()79 public function canGetAllPropertiesByName() 71 80 { 72 $this->assertEquals( 73 $this->stubMetaClass->getProperties(), 74 array() 75 ); 81 $this->assertEquals($this->stubMetaClass->getProperties(), array()); 82 $this->assertEquals($this->stubMetaClassDummy->getProperties(),array('foo','bar')); 83 } 84 85 /** 86 * can check for a property name 87 * 88 * @test 89 */ 90 public function canCheckByNameIfPropertyExists() 91 { 92 $this->assertFalse($this->stubMetaClass->hasProperty('foo')); 93 $this->assertTrue($this->stubMetaClassDummy->hasProperty('foo')); 94 $this->assertTrue($this->stubMetaClassDummy->hasProperty('bar')); 95 $this->assertFalse($this->stubMetaClassDummy->hasProperty('value')); 76 96 } 77 97 … … 88 108 ); 89 109 } 110 111 /** 112 * test delegate setProperty to meta object 113 * 114 * @test 115 */ 116 public function canDelegateSetPropertyToMetaObjectIfMetaClassPropertyExists() 117 { 118 $metaObject = new stubMetaObject; 119 $this->stubMetaClassDummy->invokeMissingProperty($metaObject, 'foo', 'bar'); 120 121 $this->assertEquals('bar', $metaObject->foo); 122 } 123 124 /** 125 * test delegate getProperty on meta object 126 * 127 * @test 128 */ 129 public function canDelegateGetPropertyToMetaObjectIfMetaClassPropertyExists() 130 { 131 $metaObject = new stubMetaObject; 132 $this->stubMetaClassDummy->invokeMissingProperty($metaObject, 'bar', null, false, true); 133 $this->assertEquals(88, $metaObject->bar); 134 } 135 } 136 /** 137 * Tests dummy for net::stubbles::lang::mop::stubMetaClass. 138 * 139 * @package stubbles 140 * @subpackage lang_mop_test 141 */ 142 class stubMetaClassTestCaseDummy extends stubMetaClass 143 { 144 public $foo = null; 145 public $bar = 88; 146 protected $value; 147 public function setValue($v){$this->value = $v;} 148 public function getValue(){return $this->value;} 90 149 } 91 150 ?> labs/incubator/src/test/php/net/stubbles/lang/mop/stubMetaObjectTestCase.php
r1771 r1785 68 68 $this->assertEquals($mock, $this->stubMetaObject->getMetaClass()); 69 69 } 70 71 /** 72 * test if each instance of the same meta object has the same meta class by default 73 * 74 * @test 75 */ 76 public function firstMetaObjectRegistrationDecideWhichMetaClassIsUsed() 77 { 78 $metaObject = new stubMetaObject; 79 $this->assertSame($this->stubMetaObject->getMetaClass(), $metaObject->getMetaClass()); 80 } 81 82 /** 83 * test if property not declared by meta object or meta class raise a exception 84 * 85 * @test 86 * @expectedException stubIllegalAccessException 87 */ 88 public function throwsExceptionIfGetPropertyAndUnknownByObjectAndMetaClass() 89 { 90 $this->stubMetaObject->setMetaClass(new stubMetaClass); 91 $this->stubMetaObject->foo; 92 93 94 } 95 96 /** 97 * test if property not declared by meta object or meta class raise a exception 98 * 99 * @test 100 * @expectedException stubIllegalAccessException 101 */ 102 public function throwsExceptionIfSetPropertyAndUnknownByObjectAndMetaClass() 103 { 104 $this->stubMetaObject->setMetaClass(new stubMetaClass); 105 $this->stubMetaObject->foo = 'bar'; 106 } 107 108 /** 109 * test can set and get property if is declared on meta object 110 * 111 * @test 112 */ 113 public function getAndSetPropertyIfDeclaredOnMetaObject() 114 { 115 $this->assertEquals($this->stubMetaObject, $this->stubMetaObject->setProperty('foo', 'bar')); 116 $this->assertEquals('bar', $this->stubMetaObject->getProperty('foo')); 117 $this->assertEquals('bar', $this->stubMetaObject->foo); 118 $this->assertEquals('foo', ($this->stubMetaObject->foo = 'foo')); 119 $this->assertEquals('foo', $this->stubMetaObject->foo); 120 } 121 122 /** 123 * test if getProperty method set a property value if not exists 124 * 125 * @test 126 */ 127 public function getProperty() 128 { 129 $this->assertEquals($this->stubMetaObject, $this->stubMetaObject->setProperty('foo', 'bar')); 130 $this->assertEquals('bar', $this->stubMetaObject->getProperty('foo')); 131 $this->assertEquals('bar', $this->stubMetaObject->foo); 132 $this->assertEquals('foo', ($this->stubMetaObject->foo = 'foo')); 133 $this->assertEquals('foo', $this->stubMetaObject->foo); 134 } 70 135 } 71 136 ?>
