ImageToTextTask::fromBase64()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Anticaptcha\Task;
4
5
use InvalidArgumentException;
6
7
/**
8
 * @see https://anti-captcha.com/apidoc/task-types/ImageToTextTask
9
 */
10
class ImageToTextTask extends AbstractTask
11
{
12
    /*
13
     * File body encoded in base64. Make sure to send it without line breaks.
14
     */
15
    public string $body;
16
17
    /*
18
     * false - no requirements
19
     * true - worker must enter an answer with at least one "space".
20
     */
21
    public bool $phrase = false;
22
23
    /*
24
     * false - no requirements
25
     * true - worker will see a special mark telling that answer must be entered with case sensitivity.
26
     */
27
    public bool $case = false;
28
29
    /*
30
     * 0 - no requirements
31
     * 1 - only number are allowed
32
     * 2 - any letters are allowed except numbers
33
     */
34
    public int $numeric = 0;
35
36
    /*
37
     * false - no requirements
38
     * true - worker will see a special mark telling that answer must be calculated;
39
     */
40
    public bool $math = false;
41
42
    /*
43
     * 0 - no requirements
44
     * >1 - defines minimum length of the answer
45
     */
46
    public int $minLength = 0;
47
48
    /*
49
     * 0 - no requirements
50
     * >1 - defines maximum length of the answer
51
     */
52
    public int $maxLength = 0;
53
54
    /**
55
     * From image task builder.
56
     *
57
     * @param string $path      # Path po the image
58
     * @param array $options    # Recognition options
59
     *
60
     * @return static
61
     *
62
     * @throws InvalidArgumentException
63
     */
64
    public static function fromImage(string $path, array $options = [])
65
    {
66
        if (!is_readable($path)) {
67
            throw new InvalidArgumentException(
68
                sprintf('Image "%s" is not found.', $path)
69
            );
70
        }
71
72
        $options['body'] = base64_encode(file_get_contents($path));
73
74
        return new static($options);
75
    }
76
    /**
77
     * From Base64 task builder.
78
     *
79
     * @param string $base64Content      # Base64 string
80
     * @param array $options      # Recognition options
81
     * @return static
82
     */
83
    public static function fromBase64(string $base64Content, array $options = []): self
84
    {
85
        $options['body'] = $base64Content;
86
87
        return new static($options);
88
    }
89
}
90