Skip to content

Commit d05b52a

Browse files
committed
Add some methods to get array attribute sibling key and value
1 parent 619a2b3 commit d05b52a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/Attribute.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,36 @@ public function getKeyIndexes()
106106
return $this->keyIndexes;
107107
}
108108

109+
public function getValue($key = null)
110+
{
111+
if ($key && $this->isArrayAttribute()) {
112+
$key = $this->resolveSiblingKey($key);
113+
}
114+
115+
if (!$key) {
116+
$key = $this->getKey();
117+
}
118+
119+
return $this->validation->getValue($key);
120+
}
121+
122+
public function isArrayAttribute()
123+
{
124+
return count($this->getKeyIndexes()) > 0;
125+
}
126+
127+
public function resolveSiblingKey($key)
128+
{
129+
$indexes = $this->getKeyIndexes();
130+
$keys = explode("*", $key);
131+
$countAsterisks = count($keys) - 1;
132+
if (count($indexes) < $countAsterisks) {
133+
$indexes = array_merge($indexes, array_fill(0, $countAsterisks - count($indexes), "*"));
134+
}
135+
$args = array_merge([str_replace("*", "%s", $key)], $indexes);
136+
return call_user_func_array('sprintf', $args);
137+
}
138+
109139
public function getHumanizedKey()
110140
{
111141
$primaryAttribute = $this->getPrimaryAttribute();

0 commit comments

Comments
 (0)