PodsField_Pick::simple_value PHP Method

simple_value() public method

Convert a simple value to the correct value
public simple_value ( string $name, string | array $value = null, array $options = null, array $pod = null, integer $id = null, boolean $raw = false ) : mixed
$name string The name of the field
$value string | array The value of the field
$options array Field options
$pod array Pod data
$id integer Item ID
$raw boolean Whether to return the raw list of keys (true) or convert to key=>value (false)
return mixed Corrected value
    public function simple_value($name, $value = null, $options = null, $pod = null, $id = null, $raw = false)
    {
        if (in_array(pods_var(self::$type . '_object', $options), self::simple_objects())) {
            if (isset($options['options'])) {
                $options = array_merge($options, $options['options']);
                unset($options['options']);
            }
            if (!is_array($value) && 0 < strlen($value)) {
                $simple = @json_decode($value, true);
                if (is_array($simple)) {
                    $value = $simple;
                }
            }
            $data = pods_var_raw('data', $options, null, null, true);
            $object_params = array('name' => $name, 'value' => $value, 'options' => $options, 'pod' => $pod, 'id' => $id, 'context' => 'simple_value');
            if (null === $data) {
                $data = $this->get_object_data($object_params);
            }
            $data = (array) $data;
            $key = 0;
            if (is_array($value)) {
                if (!empty($data)) {
                    $val = array();
                    foreach ($value as $k => $v) {
                        if (isset($data[$v])) {
                            if (false === $raw) {
                                $k = $v;
                                $v = $data[$v];
                            }
                            $val[$k] = $v;
                        }
                    }
                    $value = $val;
                }
            } elseif (isset($data[$value]) && false === $raw) {
                $key = $value;
                $value = $data[$value];
            }
            $single_multi = pods_var(self::$type . '_format_type', $options, 'single');
            if ('multi' == $single_multi) {
                $limit = (int) pods_var(self::$type . '_limit', $options, 0);
            } else {
                $limit = 1;
            }
            if (is_array($value) && 0 < $limit) {
                if (1 == $limit) {
                    $value = current($value);
                } else {
                    $value = array_slice($value, 0, $limit, true);
                }
            } elseif (!is_array($value) && null !== $value && 0 < strlen($value)) {
                if (1 != $limit || true === $raw && 'multi' == $single_multi) {
                    $value = array($key => $value);
                }
            }
        }
        return $value;
    }