Skip to content

Commit 9ced62a

Browse files
committed
Merge pull request #1 from atkrad/master
Add append method
2 parents 1fe58b7 + a64606d commit 9ced62a

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/templates/objectSetJson.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function set<?php echo ucfirst($columnName) ?>(array $data = array())
1919
* @param string $path path to change or append
2020
* @param string $data data to set
2121
*
22-
* @return The current object (for fluent API support)
22+
* @return Current object (for fluent API support)
2323
*/
2424
public function set<?php echo ucfirst($columnName) ?>Path($path, $data)
2525
{
@@ -43,3 +43,34 @@ public function set<?php echo ucfirst($columnName) ?>Path($path, $data)
4343
$this->set<?php echo ucfirst($columnName) ?>($this-><?php echo $columnName ?>AsArray);
4444
return $this;
4545
}
46+
47+
/**
48+
* Append partial of a json
49+
*
50+
* @param string $path path to change or append
51+
* @param string $data data to set
52+
*
53+
* @return Current object (for fluent API support)
54+
*/
55+
public function append<?php echo ucfirst($columnName) ?>Path($path, $data)
56+
{
57+
if (!$this-><?php echo $columnName ?>AsArray || !is_array($this-><?php echo $columnName ?>AsArray)) {
58+
$this->initJsonFields();
59+
}
60+
61+
$pathArray = explode('.', $path);
62+
$current = &$this-><?php echo $columnName ?>AsArray;
63+
while ($p = array_shift($pathArray)) {
64+
if (is_array($current) && array_key_exists($p, $current)) {
65+
$current = &$current[$p];
66+
} elseif (!is_array($current)) {
67+
throw new PropelException("Can not set $path in this json");
68+
} else {
69+
$current[$p] = array();
70+
$current = &$current[$p];
71+
}
72+
}
73+
$current[] = $data;
74+
$this->set<?php echo ucfirst($columnName) ?>($this-><?php echo $columnName ?>AsArray);
75+
return $this;
76+
}

0 commit comments

Comments
 (0)