property-accessor.php

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

commit 25f90c6900134bd36e917cb89d9225fce6f346ef
parent 72522e2cfd910e5227ad33af773d4e100eb6f879
Author: finwo <finwo@pm.me>
Date:   Fri,  6 May 2016 15:31:03 +0200

Added safe getter

Diffstat:
Msrc/PropertyAccessor.php | 19+++++++++++++++++++
1 file changed, 19 insertions(+), 0 deletions(-)

diff --git a/src/PropertyAccessor.php b/src/PropertyAccessor.php @@ -15,6 +15,25 @@ class PropertyAccessor * @param $subject * @param string $path * @param string $pathSplit + * @param null $default + * @param array $options + * + * @return array|mixed|null + * @throws \Exception + */ + public function getSafe(&$subject, $path = '', $pathSplit = '|', $default = null, $options = array()) + { + $result = $this->get($subject, $path, $pathSplit); + if (in_array($result, $options) || is_null($result)) { + $result = &$default; + } + return $result; + } + + /** + * @param $subject + * @param string $path + * @param string $pathSplit * * @return array|mixed|null * @throws \Exception