Completed
Push — develop ( bc21b0...0bdfd4 )
by Adam
24:26 queued 09:29
created

src/VisualRecognition/Api/Classify.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace IBM\Watson\VisualRecognition\Api;
4
5
use IBM\Watson\Common\Api\AbstractApi;
6
use IBM\Watson\VisualRecognition\Model\ClassifiedImages;
7
8
class Classify extends AbstractApi
9
{
10
    /**
11
     * Classify image
12
     *
13
     * @param string|resource $image
14
     * @param array           $params
15
     *
16
     * @return \IBM\Watson\VisualRecognition\Model\ClassifiedImages
17
     *
18
     * @throws \Http\Client\Exception
19
     * @throws \Exception
20
     */
21
    public function classify($image, $params = [])
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
22
    {
23
        if (filter_var($image, FILTER_VALIDATE_URL)) {
24
            $response = $this->classifyUrl($image, $params);
25
        }
26
27
        if (file_exists($image)) {
28
            $image = fopen($image, 'r');
29
        }
30
31
        if (is_resource($image)) {
32
            $params[] = [
33
                'name' => 'images_file',
34
                'content' => $image
35
            ];
36
37
            $response = $this->postRaw('/v3/classify', $params);
38
        }
39
40
        if ($response->getStatusCode() !== 200) {
41
            $this->handleErrors($response);
42
        }
43
44
        return $this->hydrator->hydrate($response, ClassifiedImages::class);
45
    }
46
47
    /**
48
     * Classify image by url
49
     *
50
     * @param string $url
51
     * @param array  $params
52
     *
53
     * @return \Psr\Http\Message\ResponseInterface
54
     *
55
     * @throws \Http\Client\Exception
56
     * @throws \Exception
57
     */
58
    public function classifyUrl($url, $params = [])
59
    {
60
        $params['url'] = $url;
61
62
        $response = $this->get('/v3/classify', $params);
63
64
        if ($response->getStatusCode() !== 200) {
65
            $this->handleErrors($response);
66
        }
67
68
        return $response;
69
    }
70
}
71