DataSift_Source::listSources PHP Method

listSources() public static method

Gets a page of Sources where each page contains up to $perPage items.
public static listSources ( DataSift_User $user, integer $page = 1, integer $perPage = 25, string | false $sourceType = false ) : array
$user DataSift_User The user making the request.
$page integer The page number to fetch.
$perPage integer The number of items per page.
$sourceType string | false The type of source to filter by; false for no filter
return array An array containing the number of sources returned in 'count' and a list of DataSift_Sources in 'sources'
    public static function listSources(DataSift_User $user, $page = 1, $perPage = 25, $sourceType = false)
    {
        try {
            $params = array('page' => $page, 'per_page' => $perPage);
            if ($sourceType !== false) {
                $params['source_type'] = $sourceType;
            }
            $res = $user->post('source/get', $params);
            $retval = array('count' => $res['count'], 'sources' => array());
            foreach ($res['sources'] as $source) {
                $retval['sources'][] = new self($user, $source);
            }
            return $retval;
        } catch (DataSift_Exception_APIError $e) {
            switch ($e->getCode()) {
                case 400:
                    // Missing or invalid parameters
                    throw new DataSift_Exception_InvalidData($e->getMessage());
                default:
                    throw new DataSift_Exception_APIError('Unexpected APIError code: ' . $e->getCode() . ' [' . $e->getMessage() . ']');
            }
        }
    }

Usage Example

Example #1
0
 public function testSourceList()
 {
     $response = array('response_code' => 200, 'data' => array('count' => 1, 'sources' => array(array('id' => '78b3601ef667466d95f19570dcb74699', 'name' => 'My Updated PHP managed source', 'created_at' => 1435869526, 'status' => 'active', 'auth' => array(array('identity_id' => '7b1be3a398e646bbb3c7a5cb9717ba45', 'expires_at' => 1495869526, 'parameters' => array('value' => '363056350669209|09af1ce9c5d8d23147ec4eeb9a33aac2'))), 'resources' => array(array('resource_id' => '30bc448896de44b88604ac223cb7f26f', 'status' => 'valid', 'parameters' => array('url' => 'http://www.facebook.com/theguardian', 'title' => 'The Guardian', 'id' => 10513336322))), 'parameters' => array('comments' => true, 'likes' => true, 'page_likes' => true, 'posts_by_others' => true)))), 'rate_limit' => 200, 'rate_limit_remaining' => 150);
     DataSift_MockApiClient::setResponse($response);
     $sources = DataSift_Source::listSources($this->user);
     $source1 = $sources['sources'][0];
     $this->assertEquals($source1->getId(), '78b3601ef667466d95f19570dcb74699', 'Source ID differs to test data');
     $this->assertEquals($sources['count'], 1, 'Count differs to test data');
 }