Auth_Yadis_XRDS::services PHP Method

services() public method

Optionally, an array of filter callbacks may be given to limit the list of returned service objects. Furthermore, the default mode is to return all service objects which match ANY of the specified filters, but $filter_mode may be SERVICES_YADIS_MATCH_ALL if you want to be sure that the returned services match all the given filters. See {@link Auth_Yadis_Yadis} for detailed usage information on filter functions.
public services ( mixed $filters = null, integer $filter_mode = SERVICES_YADIS_MATCH_ANY ) : mixed
$filters mixed An array of callbacks to filter the returned services, or null if all services are to be returned.
$filter_mode integer SERVICES_YADIS_MATCH_ALL or SERVICES_YADIS_MATCH_ANY, depending on whether the returned services should match ALL or ANY of the specified filters, respectively.
return mixed $services An array of {@link Auth_Yadis_Service} objects if $filter_mode is a valid mode; null if $filter_mode is an invalid mode (i.e., not SERVICES_YADIS_MATCH_ANY or SERVICES_YADIS_MATCH_ALL).
    function services($filters = null, $filter_mode = SERVICES_YADIS_MATCH_ANY)
    {
        $pri_keys = array_keys($this->serviceList);
        sort($pri_keys, SORT_NUMERIC);
        // If no filters are specified, return the entire service
        // list, ordered by priority.
        if (!$filters || !is_array($filters)) {
            $result = array();
            foreach ($pri_keys as $pri) {
                $result = array_merge($result, $this->serviceList[$pri]);
            }
            return $result;
        }
        // If a bad filter mode is specified, return null.
        if (!in_array($filter_mode, array(SERVICES_YADIS_MATCH_ANY, SERVICES_YADIS_MATCH_ALL))) {
            return null;
        }
        // Otherwise, use the callbacks in the filter list to
        // determine which services are returned.
        $filtered = array();
        foreach ($pri_keys as $priority_value) {
            $service_obj_list = $this->serviceList[$priority_value];
            foreach ($service_obj_list as $service) {
                $matches = 0;
                foreach ($filters as $filter) {
                    if (call_user_func_array($filter, array($service))) {
                        $matches++;
                        if ($filter_mode == SERVICES_YADIS_MATCH_ANY) {
                            $pri = $service->getPriority();
                            if ($pri === null) {
                                $pri = SERVICES_YADIS_MAX_PRIORITY;
                            }
                            if (!array_key_exists($pri, $filtered)) {
                                $filtered[$pri] = array();
                            }
                            $filtered[$pri][] = $service;
                            break;
                        }
                    }
                }
                if ($filter_mode == SERVICES_YADIS_MATCH_ALL && $matches == count($filters)) {
                    $pri = $service->getPriority();
                    if ($pri === null) {
                        $pri = SERVICES_YADIS_MAX_PRIORITY;
                    }
                    if (!array_key_exists($pri, $filtered)) {
                        $filtered[$pri] = array();
                    }
                    $filtered[$pri][] = $service;
                }
            }
        }
        $pri_keys = array_keys($filtered);
        sort($pri_keys, SORT_NUMERIC);
        $result = array();
        foreach ($pri_keys as $pri) {
            $result = array_merge($result, $filtered[$pri]);
        }
        return $result;
    }