Changeset 1785

Show
Ignore:
Timestamp:
08/08/08 15:14:36 (3 months ago)
Author:
prema
Message:

Add property handling and fix some bugs

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • labs/incubator/src/main/php/net/stubbles/lang/mop/stubMetaClass.php

    r1783 r1785  
    5555 
    5656    /** 
    57      * returns true of the implementing MetaClass has a property of the given name 
     57     * returns true if the implementing MetaClass has a property of the given name 
    5858     * 
    5959     * @param   string   $propertyName 
     
    143143 
    144144    /** 
    145      * invokes a propetry on the given receiver 
     145     * invokes a property on the given receiver 
    146146     * 
    147      * @param   stubMetaObjectProtocol  $receiver      the object which the method invoked on 
    148      * @param   string                  $propertyName  the name of the method 
    149      * @param   mixed                   $value         the arguments to the method 
    150      * @param   boolean                 $isGetter      whether the missing property event was the result of a getter 
    151      * @param   boolean                 $isSetter      whether the missing property event was the result of a setter 
    152      * @return  mixed                                  the return value of the called property 
    153      * @throws  stubIllegalAccessException             on unsupported property 
     147     * @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 
    154154     */ 
    155     public function invokeMissingProperty(stubMetaObjectProtocol $receiver, $propertyName, $value, $isGetter = false, $isSetter = false) 
     155    public function invokeMissingProperty(stubMetaObjectProtocol $receiver, $property, $value, $isSetter = true, $isGetter = false) 
    156156    { 
    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); 
    166162        } 
    167163 
    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()); 
    174165    } 
    175166} 
  • labs/incubator/src/main/php/net/stubbles/lang/mop/stubMetaObject.php

    r1784 r1785  
    2020class stubMetaObject extends stubBaseObject implements stubMetaObjectProtocol 
    2121{ 
     22    /** 
     23     * hold the meta object properties 
     24     * 
     25     * @var  array <string,mixed> 
     26     */ 
     27    private $properties = array(); 
     28 
    2229    /** 
    2330     * sets the meta class instance of the meta object 
     
    5360     * sets the given property to the given value 
    5461     * 
    55      * @param   string $propertyName  property name 
    56      * @param   mixed  $value         property value 
    57      * @return  stubMetaObject        implement fluent interface 
     62     * @param   string $property  property name 
     63     * @param   mixed  $value     property value 
     64     * @return  stubMetaObject    implement fluent interface 
    5865     */ 
    59     public function setProperty($propertyName, $value = null) 
     66    public function setProperty($property, $value = null) 
    6067    { 
    61         if (property_exists($propertyName)) { 
    62             $this->{$propertyName} = $value; 
    63         } else { 
    64             $this->invokeMissingProperty($this, $propertyName, $value); 
    65         } 
     68        $this->properties[$property] = $value; 
    6669 
    6770        return $this; 
     
    7174     * retrieves a property value 
    7275     * 
    73      * @param   string $propertyName  property name 
    74      * @param   mixed  $value         property value 
     76     * @param   string $property  property name 
    7577     * @return  mixed  property value if exists or null 
    7678     */ 
    77     public function getProperty($propertyName
     79    public function getProperty($property
    7880    { 
    79         if (property_exists($propertyName)) { 
    80             return $this->{$propertyName}
     81        if ($this->hasProperty($property)) { 
     82            return $this->properties[$property]
    8183        } 
    8284 
    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); 
    8497    } 
    8598 
     
    98111 
    99112    /** 
    100      * delegate the static method call to registered meta class 
     113     * delegate protected or unknown field/property access to registered meta class 
    101114     * 
    102      * @param   string   $method name of the method 
    103      * @param   array    $arguments given method arguments 
     115     * @param   string   $property name of the property 
     116     * @param   array    $value    given property valeu 
    104117     * @return  mixed 
    105118     * @throws  stubMethodNotSupportedException 
    106      * 
    107      * @todo wait for php version >= 5.3 to implement __callStatic 
    108119     */ 
    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        } 
    113125 
    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); 
    135127    } 
    136128 
    137129    /** 
    138      * delegate protected or unknown field/property access to registered meta class 
     130     * delegate protected, private or unknown field/property access to registered meta class 
    139131     * 
    140      * @param   string   $fieldOrPropertyName name of the method 
    141      * @return  voi
     132     * @param   string   $property name of the method 
     133     * @return  mixe
    142134     * @throws  stubMethodNotSupportedException 
    143      * 
    144      * @todo getter/setter have higher prio. if thoose exists use it! 
    145135     */ 
    146     public function __get($fieldOrPropertyName
     136    public function __get($property
    147137    { 
    148         return $this->getMetaClass()->invokeMissingProperty( 
    149             $this, 
    150             $fieldOrPropertyName, 
    151             null, 
    152             $this->getMetaClass()->hasProperty($propertyName) ? false : true 
    153         ); 
     138        return $this->getProperty($property); 
    154139    } 
    155140} 
  • labs/incubator/src/test/php/net/stubbles/lang/mop/stubMetaClassTestCase.php

    r1783 r1785  
    2828 
    2929    /** 
     30     * dummy for stubMetaClass 
     31     * 
     32     * @var  stubMetaClassTestCaseDummy 
     33     */ 
     34    protected $stubMetaClassDummy; 
     35 
     36    /** 
    3037     * set up test environment 
    3138     */ 
    3239    public function setUp() 
    3340    { 
    34         $this->stubMetaClass = new stubMetaClass(); 
     41        $this->stubMetaClass        = new stubMetaClass; 
     42        $this->stubMetaClassDummy   = new stubMetaClassTestCaseDummy; 
    3543    } 
    3644 
     
    4250    public function hasExpectedMethods() 
    4351    { 
    44         $this->markTestIncomplete(); 
    4552        $this->assertEquals( 
    4653            $this->stubMetaClass->getMethods(), 
    4754            array( 
    4855                'getProperties', 
     56                'hasProperty', 
    4957                'getMethods', 
    5058                'hasMethod', 
    5159                'invokeMethod', 
    5260                'invokeMissingMethod', 
     61                'invokeMissingProperty', 
    5362                'getClass', 
    5463                'getPackage', 
     
    6473 
    6574    /** 
    66      * test have none properties 
     75     * test even can retrieve all property names as array 
    6776     * 
    6877     * @test 
    6978     */ 
    70     public function hasNoVisibileProperties() 
     79    public function canGetAllPropertiesByName() 
    7180    { 
    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')); 
    7696    } 
    7797 
     
    88108        ); 
    89109    } 
     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 */ 
     142class 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;} 
    90149} 
    91150?> 
  • labs/incubator/src/test/php/net/stubbles/lang/mop/stubMetaObjectTestCase.php

    r1771 r1785  
    6868        $this->assertEquals($mock, $this->stubMetaObject->getMetaClass()); 
    6969    } 
     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    } 
    70135} 
    71136?>