AnonSec Team
Server IP : 10.2.73.233  /  Your IP : 216.73.216.223
Web Server : Apache/2.4.59 (Debian)
System : Linux polon 4.19.0-27-amd64 #1 SMP Debian 4.19.316-1 (2024-06-25) x86_64
User : www-data ( 33)
PHP Version : 5.6.40-64+0~20230107.71+debian10~1.gbp673146
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,
MySQL : ON  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /home/ifk/web/assets/../prado4.3.2/Util/../Util/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/ifk/web/assets/../prado4.3.2/Util/../Util/TDataFieldAccessor.php
<?php
/**
 * TDataFieldAccessor class file
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link https://github.com/pradosoft/prado
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
 */

namespace Prado\Util;

use Exception;
use Prado\Exceptions\TInvalidDataValueException;

/**
 * TDataFieldAccessor class
 *
 * TDataFieldAccessor is a utility class that provides access to a field of some data.
 * The accessor attempts to obtain the field value in the following order:
 * - If the data is an array, then the field is treated as an array index
 *   and the corresponding element value is returned;
 * - If the data is a TMap or TList object, then the field is treated as a key
 *   into the map or list, and the corresponding value is returned.
 * - If the data is an object, the field is treated as a property or sub-property
 *   defined with getter methods. For example, if the object has a method called
 *   getMyValue(), then field 'MyValue' will retrieve the result of this method call.
 *   If getMyValue() returns an object which contains a method getMySubValue(),
 *   then field 'MyValue.MySubValue' will return that method call result.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 3.0
 */
class TDataFieldAccessor
{
	/**
	 * Evaluates the data value at the specified field.
	 * - If the data is an array, then the field is treated as an array index
	 *   and the corresponding element value is returned; the field name can also include
	 *   dots to access subarrays. For example a field named 'MyField.MySubField' will
	 *   first try to access $data['MyField.MySubField'], then try $data['MyField']['MySubField'].
	 * - If the data is a TMap or TList object, then the field is treated as a key
	 *   into the map or list, and the corresponding value is returned.
	 * - If the data is an object, the field is treated as a property or sub-property
	 *   defined with getter methods. For example, if the object has a method called
	 *   getMyValue(), then field 'MyValue' will retrieve the result of this method call.
	 *   If getMyValue() returns an object which contains a method getMySubValue(),
	 *   then field 'MyValue.MySubValue' will return that method call result.
	 * @param mixed $data data containing the field value, can be an array, TMap, TList or object.
	 * @param mixed $field field value
	 * @throws TInvalidDataValueException if field or data is invalid
	 * @return mixed value at the specified field
	 */
	public static function getDataFieldValue($data, $field)
	{
		try {
			if (is_array($data) || ($data instanceof \ArrayAccess)) {
				if (isset($data[$field])) {
					return $data[$field];
				}

				$tmp = $data;
				foreach (explode(".", $field) as $f) {
					$tmp = $tmp[$f];
				}
				return $tmp;
			} elseif (is_object($data)) {
				if (strpos($field, '.') === false) {  // simple field
					if (method_exists($data, 'get' . $field)) {
						return call_user_func([$data, 'get' . $field]);
					} else {
						return $data->{$field};
					}
				} else { // field in the format of xxx.yyy.zzz
					$object = $data;
					foreach (explode('.', $field) as $f) {
						$object = TDataFieldAccessor::getDataFieldValue($object, $f);
					}
					return $object;
				}
			}
		} catch (Exception $e) {
			throw new TInvalidDataValueException('datafieldaccessor_datafield_invalid', $field, $e->getMessage());
		}
		throw new TInvalidDataValueException('datafieldaccessor_data_invalid', $field);
	}
}

AnonSec - 2021