Skip to content
This repository

Improve hydrator code and add has support #2268

Merged
merged 2 commits into from over 1 year ago

3 participants

Blanchon Vincent Don't Add Me To Your Organization a.k.a The Travis Bot Matthew Weier O'Phinney
Blanchon Vincent

Improve hydrator code and add has support

Blanchon Vincent improve code & add has support
improve code & add has support
2ae10ae
Blanchon Vincent add unit tests
add unit tests for has support
37ace2c
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request fails (merged 37ace2c into 89eaf41).

Matthew Weier O'Phinney weierophinney referenced this pull request from a commit August 30, 2012
Matthew Weier O'Phinney [#2268] CS fixes
- Trailing whitespace
564a461
Matthew Weier O'Phinney weierophinney referenced this pull request from a commit August 30, 2012
Matthew Weier O'Phinney [#2268] Updated README
- Noted change to ClassMethods hydrator
84f977f
Matthew Weier O'Phinney weierophinney merged commit 37ace2c into from August 30, 2012
Matthew Weier O'Phinney weierophinney closed this August 30, 2012
Matthew Weier O'Phinney
Owner

Merged to 2.0.1 and master

Deleted user Unknown referenced this pull request from a commit August 30, 2012
Matthew Weier O'Phinney [#2268] CS fixes
- Trailing whitespace
ba4ac78
Deleted user Unknown referenced this pull request from a commit August 30, 2012
Matthew Weier O'Phinney [#2268] Updated README
- Noted change to ClassMethods hydrator
68b3f4a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Aug 30, 2012
Blanchon Vincent improve code & add has support
improve code & add has support
2ae10ae
Blanchon Vincent add unit tests
add unit tests for has support
37ace2c
This page is out of date. Refresh to see the latest.
29  library/Zend/Stdlib/Hydrator/ClassMethods.php
@@ -58,31 +58,28 @@ public function extract($object)
58 58
         };
59 59
         $attributes = array();
60 60
         $methods = get_class_methods($object);
  61
+
61 62
         foreach ($methods as $method) {
62  
-            if (preg_match('/^get[A-Z]\w*/', $method)) {
  63
+            if (!preg_match('/^(get|has|is)[A-Z]\w*/', $method)) {
  64
+                continue;
  65
+            }
  66
+            if (preg_match('/^get/', $method)) {
63 67
                 // setter verification
64 68
                 $setter = preg_replace('/^get/', 'set', $method);
65  
-                if (!in_array($setter, $methods)) {
66  
-                    continue;
67  
-                }
68 69
                 $attribute = substr($method, 3);
69 70
                 $attribute = lcfirst($attribute);
70  
-                if ($this->underscoreSeparatedKeys) {
71  
-                    $attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute);
72  
-                }
73  
-                $attributes[$attribute] = $this->extractValue($attribute, $object->$method());
74  
-            } elseif (preg_match('/^is[A-Z]\w*/', $method)) {
  71
+            } else {
75 72
                 // setter verification
76 73
                 $setter = 'set' . ucfirst($method);
77  
-                if (!in_array($setter, $methods)) {
78  
-                    continue;
79  
-                }
80 74
                 $attribute = $method;
81  
-                if ($this->underscoreSeparatedKeys) {
82  
-                    $attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute);
83  
-                }
84  
-                $attributes[$attribute] = $this->extractValue($attribute, $object->$method());
85 75
             }
  76
+            if (!in_array($setter, $methods)) {
  77
+                continue;
  78
+            }
  79
+            if ($this->underscoreSeparatedKeys) {
  80
+                $attribute = preg_replace_callback('/([A-Z])/', $transform, $attribute);
  81
+            }
  82
+            $attributes[$attribute] = $this->extractValue($attribute, $object->$method());
86 83
         }
87 84
 
88 85
         return $attributes;
22  tests/ZendTest/Stdlib/HydratorTest.php
@@ -60,10 +60,14 @@ public function testInitiateValues()
60 60
         $this->assertEquals($this->classMethodsCamelCase->getFooBarBaz(), '2');
61 61
         $this->assertEquals($this->classMethodsCamelCase->getIsFoo(), true);
62 62
         $this->assertEquals($this->classMethodsCamelCase->isBar(), true);
  63
+        $this->assertEquals($this->classMethodsCamelCase->getHasFoo(), true);
  64
+        $this->assertEquals($this->classMethodsCamelCase->hasBar(), true);
63 65
         $this->assertEquals($this->classMethodsUnderscore->getFooBar(), '1');
64 66
         $this->assertEquals($this->classMethodsUnderscore->getFooBarBaz(), '2');
65 67
         $this->assertEquals($this->classMethodsUnderscore->getIsFoo(), true);
66 68
         $this->assertEquals($this->classMethodsUnderscore->isBar(), true);
  69
+        $this->assertEquals($this->classMethodsUnderscore->getHasFoo(), true);
  70
+        $this->assertEquals($this->classMethodsUnderscore->hasBar(), true);
67 71
     }
68 72
 
69 73
     public function testHydratorReflection()
@@ -95,12 +99,18 @@ public function testHydratorClassMethodsCamelCase()
95 99
         $this->assertEquals($datas['isFoo'], true);
96 100
         $this->assertTrue(isset($datas['isBar']));
97 101
         $this->assertEquals($datas['isBar'], true);
  102
+        $this->assertTrue(isset($datas['hasFoo']));
  103
+        $this->assertEquals($datas['hasFoo'], true);
  104
+        $this->assertTrue(isset($datas['hasBar']));
  105
+        $this->assertEquals($datas['hasBar'], true);
98 106
         $test = $hydrator->hydrate(
99 107
             array(
100 108
                 'fooBar' => 'foo',
101 109
                 'fooBarBaz' => 'bar',
102 110
                 'isFoo' => false,
103 111
                 'isBar' => false,
  112
+                'hasFoo' => false,
  113
+                'hasBar' => false,
104 114
             ),
105 115
             $this->classMethodsCamelCase
106 116
         );
@@ -109,6 +119,8 @@ public function testHydratorClassMethodsCamelCase()
109 119
         $this->assertEquals($test->getFooBarBaz(), 'bar');
110 120
         $this->assertEquals($test->getIsFoo(), false);
111 121
         $this->assertEquals($test->isBar(), false);
  122
+        $this->assertEquals($test->getHasFoo(), false);
  123
+        $this->assertEquals($test->hasBar(), false);
112 124
     }
113 125
 
114 126
     public function testHydratorClassMethodsCamelCaseWithSetterMissing()
@@ -139,12 +151,20 @@ public function testHydratorClassMethodsUnderscore()
139 151
         $this->assertTrue(isset($datas['is_bar']));
140 152
         $this->assertFalse(isset($datas['isBar']));
141 153
         $this->assertEquals($datas['is_bar'], true);
  154
+        $this->assertTrue(isset($datas['has_foo']));
  155
+        $this->assertFalse(isset($datas['hasFoo']));
  156
+        $this->assertEquals($datas['has_foo'], true);
  157
+        $this->assertTrue(isset($datas['has_bar']));
  158
+        $this->assertFalse(isset($datas['hasBar']));
  159
+        $this->assertEquals($datas['has_bar'], true);
142 160
         $test = $hydrator->hydrate(
143 161
             array(
144 162
                 'foo_bar' => 'foo',
145 163
                 'foo_bar_baz' => 'bar',
146 164
                 'is_foo' => false,
147 165
                 'is_bar' => false,
  166
+                'has_foo' => false,
  167
+                'has_bar' => false,
148 168
             ),
149 169
             $this->classMethodsUnderscore
150 170
         );
@@ -153,6 +173,8 @@ public function testHydratorClassMethodsUnderscore()
153 173
         $this->assertEquals($test->getFooBarBaz(), 'bar');
154 174
         $this->assertEquals($test->getIsFoo(), false);
155 175
         $this->assertEquals($test->isBar(), false);
  176
+        $this->assertEquals($test->getHasFoo(), false);
  177
+        $this->assertEquals($test->hasBar(), false);
156 178
     }
157 179
 
158 180
     public function testHydratorClassMethodsIgnoresInvalidValues()
26  tests/ZendTest/Stdlib/TestAsset/ClassMethodsCamelCase.php
@@ -20,6 +20,10 @@ class ClassMethodsCamelCase
20 20
 
21 21
     protected $isBar = true;
22 22
 
  23
+    protected $hasFoo = true;
  24
+
  25
+    protected $hasBar = true;
  26
+
23 27
     public function getFooBar()
24 28
     {
25 29
         return $this->fooBar;
@@ -63,4 +67,26 @@ public function setIsBar($value)
63 67
         $this->isBar = $value;
64 68
         return $this;
65 69
     }
  70
+    
  71
+    public function getHasFoo()
  72
+    {
  73
+        return $this->hasFoo;
  74
+    }
  75
+
  76
+    public function setHasFoo($value)
  77
+    {
  78
+        $this->hasFoo = $value;
  79
+        return $this;
  80
+    }
  81
+
  82
+    public function hasBar()
  83
+    {
  84
+        return $this->hasBar;
  85
+    }
  86
+
  87
+    public function setHasBar($value)
  88
+    {
  89
+        $this->hasBar = $value;
  90
+        return $this;
  91
+    }
66 92
 }
26  tests/ZendTest/Stdlib/TestAsset/ClassMethodsUnderscore.php
@@ -20,6 +20,10 @@ class ClassMethodsUnderscore
20 20
 
21 21
     protected $is_bar = true;
22 22
 
  23
+    protected $has_foo = true;
  24
+
  25
+    protected $has_bar = true;
  26
+
23 27
     public function getFooBar()
24 28
     {
25 29
         return $this->foo_bar;
@@ -63,4 +67,26 @@ public function setIsBar($value)
63 67
         $this->is_bar = $value;
64 68
         return $this;
65 69
     }
  70
+    
  71
+    public function getHasFoo()
  72
+    {
  73
+        return $this->has_foo;
  74
+    }
  75
+
  76
+    public function setHasFoo($value)
  77
+    {
  78
+        $this->has_foo = $value;
  79
+        return $this;
  80
+    }
  81
+
  82
+    public function hasBar()
  83
+    {
  84
+        return $this->has_bar;
  85
+    }
  86
+
  87
+    public function setHasBar($value)
  88
+    {
  89
+        $this->has_bar = $value;
  90
+        return $this;
  91
+    }
66 92
 }
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.