property-accessor.php

Simple property accessor library
git clone git://git.finwo.net/lib/property-accessor.php
Log | Files | Refs

commit 935ee2724ceb97e145c2b6aec3a7699cbf43a0de
parent a92b6441a6f0fe290c41feaaa8615430285ee26a
Author: finwo <finwo@pm.me>
Date:   Wed, 13 Apr 2016 01:41:27 +0200

Added set methods for objects

Diffstat:
Msrc/PropertyAccessor.php | 49++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 46 insertions(+), 3 deletions(-)

diff --git a/src/PropertyAccessor.php b/src/PropertyAccessor.php @@ -53,17 +53,17 @@ class PropertyAccessor )); } - // try generic getter + // try magic getter if (method_exists($subject, 'get')) { return $subject->get($path); } - // try generic hidden getter + // try magic hidden getter if (method_exists($subject, '__get')) { return $subject->__get($path); } - // try fetching directly + // try getting directly $rc = new \ReflectionObject($subject); foreach ($rc->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { if ($property->getName() == $path) { @@ -107,6 +107,49 @@ class PropertyAccessor )); } + $camelized = $this->camelize($path); + + // try the default setter + if (method_exists($subject, $method = sprintf("set%s", $camelized))) { + call_user_func(array( + $subject, + $method, + $value + )); + return $this; + } + + // try less-common setter + if (method_exists($subject, $method = lcfirst($camelized))) { + call_user_func(array( + $subject, + $method, + $value + )); + return $this; + } + + // try magic getter + if (method_exists($subject, 'set')) { + $subject->set($path, $value); + return $this; + } + + // try magic hidden setter + if (method_exists($subject, '__set')) { + $subject->__set($path, $value); + return $this; + } + + // try getting directly + $rc = new \ReflectionObject($subject); + foreach ($rc->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { + if ($property->getName() == $path) { + $subject->{$path} = $value; + return $this; + } + } + // all methods failed, throw exception if ($this->getDebug()) { return '#ERROR';