1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Pinterest PHP library. |
5
|
|
|
* |
6
|
|
|
* (c) Hans Ott <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE.md. |
10
|
|
|
* |
11
|
|
|
* Source: https://github.com/hansott/pinterest-php |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Pinterest; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This class represents an image. |
20
|
|
|
* |
21
|
|
|
* @author Toon Daelman <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class Image |
24
|
|
|
{ |
25
|
|
|
const TYPE_URL = 'url'; |
26
|
|
|
const TYPE_BASE64 = 'base64'; |
27
|
|
|
const TYPE_FILE = 'file'; |
28
|
|
|
|
29
|
|
|
private $type; |
30
|
|
|
private $data; |
31
|
|
|
|
32
|
8 |
|
private function __construct($type, $data) |
33
|
|
|
{ |
34
|
8 |
|
$allowedTypes = array(self::TYPE_URL, self::TYPE_BASE64, self::TYPE_FILE); |
35
|
8 |
|
if (!in_array($type, $allowedTypes, true)) { |
36
|
|
|
throw new InvalidArgumentException('Type '.$type.' is not allowed.'); |
37
|
|
|
} |
38
|
|
|
|
39
|
8 |
|
$this->type = $type; |
40
|
8 |
|
$this->data = $data; |
41
|
8 |
|
} |
42
|
|
|
|
43
|
4 |
|
public static function url($url) |
44
|
|
|
{ |
45
|
4 |
|
return new static(static::TYPE_URL, $url); |
46
|
|
|
} |
47
|
|
|
|
48
|
4 |
|
public static function base64($base64) |
49
|
|
|
{ |
50
|
4 |
|
return new static(static::TYPE_BASE64, $base64); |
51
|
|
|
} |
52
|
|
|
|
53
|
4 |
|
public static function file($file) |
54
|
|
|
{ |
55
|
4 |
|
return new static(static::TYPE_FILE, $file); |
56
|
|
|
} |
57
|
|
|
|
58
|
14 |
|
public function isUrl() |
59
|
|
|
{ |
60
|
14 |
|
return $this->type === static::TYPE_URL; |
61
|
|
|
} |
62
|
|
|
|
63
|
10 |
|
public function isBase64() |
64
|
|
|
{ |
65
|
10 |
|
return $this->type === static::TYPE_BASE64; |
66
|
|
|
} |
67
|
|
|
|
68
|
14 |
|
public function isFile() |
69
|
|
|
{ |
70
|
14 |
|
return $this->type === static::TYPE_FILE; |
71
|
|
|
} |
72
|
|
|
|
73
|
14 |
|
public function getData() |
74
|
|
|
{ |
75
|
14 |
|
if ($this->isFile()) { |
76
|
4 |
|
return $this->data; |
77
|
|
|
} |
78
|
|
|
|
79
|
10 |
|
return $this->data; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|