Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Modified the logic for passing an invalid key to the Decorator and

functionality for automatically building decorators for associations.
  • Loading branch information...
commit b25989411d3112f7b89236138dc7fbefed20c62a 1 parent 4628819
Joey Trapp authored February 19, 2012
6  Controller/Component/DecoratorComponent.php
@@ -3,11 +3,9 @@
3 3
  * DecoratorComponent 
4 4
  * 
5 5
  * @uses Component
6  
- * @package 
  6
+ * @package Controller.Component
7 7
  * @version $id$
8  
- * @copyright 1997-2005 The PHP Group
9  
- * @author Tobias Schlitt <toby@php.net> 
10  
- * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
  8
+ * @author Joey Trapp <jtrapp07@gmail.com> 
11 9
  */
12 10
 
13 11
 require_once(App::pluginPath("Decorator") . "View" . DS . "Decorator" . DS . "DecoratorFactory.php");
3  Test/Case/Controller/Component/DecoratorComponentTest.php
@@ -60,7 +60,8 @@ public function testBuildReturnsListOfTheSameSizeFromInvalidName() {
60 60
 		$d = $this->Decorator->build("NoExist", $data);
61 61
 		$this->assertEquals(2, count($d));
62 62
 		$this->assertEquals("Decorator", get_class($d[0]));
63  
-		$this->assertEquals($this->data["DecoratorComponentTest"], $d[1]->raw("DecoratorComponentTest"));
  63
+		$this->assertEmpty($d[0]->model);
  64
+		$this->assertEquals(5, $d[0]->DecoratorComponentTest->id());
64 65
 	}
65 66
 
66 67
 	public function testBuildReturnsListOfTheSameSizeValidName() {
37  Test/Case/View/Decorator/DecoratorTest.php
@@ -40,9 +40,42 @@ public function testDecoratorCanImportDataForItsModelName() {
40 40
 		$this->assertEquals($this->data["DecoratorTest"], $d->model);
41 41
 	}
42 42
 
43  
-	public function testDecoratorWillAssignAllDataIfNoMatch() {
  43
+	public function testDecoratorWillNotAssignAllDataIfNoMatch() {
44 44
 		$d = new DecoratorTestCustomDecorator($this->data);
45  
-		$this->assertEquals($this->data, $d->model);
  45
+		$this->assertEmpty($d->model);
  46
+	}
  47
+
  48
+	public function testDecoratorCanTakeASecondArgToDefineTheParseName() {
  49
+		$d = new DecoratorTestCustomDecorator($this->data, "DecoratorTest");
  50
+		$this->assertEquals("DecoratorTestCustomDecorator", get_class($d));
  51
+		$this->assertEquals(5, $d->id());
  52
+	}
  53
+
  54
+	public function testDecoratorWillAutoCreateSiblingObjects() {
  55
+		$data = array(
  56
+			"TestOne" => array("id" => 1, "content" => "Sample content"),
  57
+			"TestTwo" => array("id" => 2, "content" => "Sample content")
  58
+		);
  59
+		$d = new Decorator($data, "TestOne");
  60
+		$this->assertEquals(1, $d->id());
  61
+		$this->assertEquals(2, $d->TestTwo->id());
  62
+		$d = new Decorator($data, "TestTwo");
  63
+		$this->assertEquals(2, $d->id());
  64
+		$this->assertEquals(1, $d->TestOne->id());
  65
+	}
  66
+
  67
+	public function testDecoratorWillAutoCreateNestedObjects() {
  68
+		$data = array(
  69
+			"TestOne" => array("id" => 1, "content" => "Sample content"),
  70
+			"TestTwo" => array(
  71
+				array("id" => 2, "content" => "Different content"),
  72
+				array("id" => 3, "content" => "Something content")
  73
+			)
  74
+		);
  75
+		$d = new Decorator($data, "TestOne");
  76
+		$this->assertEquals(1, $d->id());
  77
+		$this->assertEquals(2, count($d->TestTwo));
  78
+		$this->assertEquals("Something content", $d->TestTwo[1]->content());
46 79
 	}
47 80
 
48 81
 	public function testDecoratorWillReturnDataFromUndefinedMethods() {
73  View/Decorator/Decorator.php
@@ -2,14 +2,13 @@
2 2
 /**
3 3
  * Decorator 
4 4
  * 
5  
- * @uses Object
6  
- * @package 
  5
+ * @package View.Decorator
7 6
  * @version $id$
8  
- * @copyright 1997-2005 The PHP Group
9  
- * @author Tobias Schlitt <toby@php.net> 
10  
- * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
  7
+ * @author Joey Trapp <jtrapp07@gmail.com> 
11 8
  */
12 9
 
  10
+require_once(App::pluginPath("Decorator") . "View" . DS . "Decorator" . DS . "DecoratorFactory.php");
  11
+
13 12
 class Decorator {
14 13
 
15 14
 	/**
@@ -33,6 +32,7 @@ class Decorator {
33 32
 	 * then calls the parseData method to parse the passed in array of data.
34 33
 	 * 
35 34
 	 * @param array $data 
  35
+	 * @param mixed $parse
36 36
 	 * @access public
37 37
 	 * @return void
38 38
 	 */
@@ -40,11 +40,7 @@ public function __construct($data = array(), $parse = true) {
40 40
 		if (!$this->name) {
41 41
 			$this->name = preg_replace("/Decorator$/", "", get_class($this));
42 42
 		}
43  
-		if ($parse) {
44  
-			$this->model = $this->parseData($data);
45  
-		} else {
46  
-			$this->model = $data;
47  
-		}
  43
+		$this->model = $this->parseData($data, $parse);
48 44
 	}
49 45
 
50 46
 	/**
@@ -53,13 +49,29 @@ public function __construct($data = array(), $parse = true) {
53 49
 	 * parsing logic. 
54 50
 	 * 
55 51
 	 * @param array $data 
  52
+	 * @param mixed $parse
56 53
 	 * @access public
57 54
 	 * @return array
58 55
 	 */
59  
-	public function parseData($data = array()) {
  56
+	public function parseData($data = array(), $parse = true) {
60 57
 		$ret = array();
61  
-		if (array_key_exists($this->name, $data)) {
62  
-			$ret = $data[$this->name];
  58
+		$name = $this->name;
  59
+		if (is_string($parse)) {
  60
+			$name = $parse;
  61
+		}
  62
+		if ($parse) {
  63
+			if (array_key_exists($name, $data)) {
  64
+				$ret = $data[$name];
  65
+				unset($data[$name]);
  66
+				// Builds sibling associations
  67
+				$this->_buildAssociations($data);
  68
+				// Builds nested associations
  69
+				$this->_buildAssociations($ret);
  70
+			} else {
  71
+				// Builds nested associations and returns array of non model key/values
  72
+				$data = $this->_buildAssociations($data);
  73
+				$ret = $data;
  74
+			}
63 75
 		} else {
64 76
 			$ret = $data;
65 77
 		}
@@ -67,6 +79,41 @@ public function parseData($data = array()) {
67 79
 	}
68 80
 
69 81
 	/**
  82
+	 * Takes an array of data loops over them. If any of the keys match a model
  83
+	 * name regex, then the value for that key is inspected. If the value is an
  84
+	 * array and they keys are numeric, then the DecoratorFactory::build() method
  85
+	 * is called. If the value is array and the keys are not numeric then the
  86
+	 * DecoratorFactory::create() method is called. The results of build() or
  87
+	 * create() are assigned to properties of the model name on this decorator.
  88
+	 * All keys that are used to create decorators are unset from the data and
  89
+	 * the data array is returned.
  90
+	 * 
  91
+	 * @param mixed $data 
  92
+	 * @access protected
  93
+	 * @return array
  94
+	 */
  95
+	protected function _buildAssociations($data) {
  96
+		$keys = array_keys($data);
  97
+		for ($i = 0; $i < count($keys); $i++) {
  98
+			$key = $keys[$i];
  99
+			$value = $data[$key];
  100
+			if (
  101
+				preg_match("/^[A-Z]{1}[a-zA-Z]+/", $key) &&
  102
+				is_array($value) &&
  103
+				!empty($value)
  104
+			) {
  105
+				if (preg_match("/[0-9]+/", key($value))) {
  106
+					$this->{$key} = DecoratorFactory::build($key, $value, $key);
  107
+				} else {
  108
+					$this->{$key} = DecoratorFactory::create($key, $value, $key);
  109
+				}
  110
+				unset($data[$key]);
  111
+			}
  112
+		}
  113
+		return $data;
  114
+	}
  115
+
  116
+	/**
70 117
 	 * By default this is available to return the raw value from the model
71 118
 	 * array. The logic is in the _raw protected method so that the raw
72 119
 	 * method can be overwritten if you have a key called raw. 
8  View/Decorator/DecoratorFactory.php
@@ -2,11 +2,9 @@
2 2
 /**
3 3
  * DecoratorFactory 
4 4
  * 
5  
- * @package 
  5
+ * @package View.Decorator
6 6
  * @version $id$
7  
- * @copyright 1997-2005 The PHP Group
8  
- * @author Tobias Schlitt <toby@php.net> 
9  
- * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
  7
+ * @author Joey Trapp <jtrapp07@gmail.com> 
10 8
  */
11 9
 
12 10
 require_once(App::pluginPath("Decorator") . "View" . DS . "Decorator" . DS . "Decorator.php");
@@ -18,6 +16,7 @@ class DecoratorFactory {
18 16
 	 * 
19 17
 	 * @param mixed $name 
20 18
 	 * @param array $data 
  19
+	 * @param mixed $parse
21 20
 	 * @static
22 21
 	 * @access public
23 22
 	 * @return void
@@ -46,6 +45,7 @@ public static function create($name, $data = array(), $parse = true) {
46 45
 	 * 
47 46
 	 * @param mixed $name 
48 47
 	 * @param array $data 
  48
+	 * @param mixed $parse
49 49
 	 * @static
50 50
 	 * @access public
51 51
 	 * @return void
6  View/Helper/DecoratorHelper.php
@@ -3,11 +3,9 @@
3 3
  * DecoratorHelper 
4 4
  * 
5 5
  * @uses AppHelper
6  
- * @package 
  6
+ * @package View.Helper
7 7
  * @version $id$
8  
- * @copyright 1997-2005 The PHP Group
9  
- * @author Tobias Schlitt <toby@php.net> 
10  
- * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
  8
+ * @author Joey Trapp <jtrapp07@gmail.com> 
11 9
  */
12 10
 
13 11
 require_once(App::pluginPath("Decorator") . "View" . DS . "Decorator" . DS . "DecoratorFactory.php");

0 notes on commit b259894

Please sign in to comment.
Something went wrong with that request. Please try again.