|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace smtech\StMarksSearch\Canvas; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use smtech\CanvasPest\CanvasPest; |
|
7
|
|
|
use smtech\StMarksSearch\AbstractSearchDomain; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Parent class for all Canvas search domains |
|
11
|
|
|
* |
|
12
|
|
|
* @author Seth Battis <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class AbstractCanvasSearchDomain extends AbstractSearchDomain |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* API access object |
|
18
|
|
|
* @var CanvasPest |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $api; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Canvas ID or SIS ID of the Canvas object |
|
24
|
|
|
* @var string|integer |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $id; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Construct a CanvasSearchDomain from `$params`, requires `id` param, will |
|
30
|
|
|
* extract `url` param from `$api` if necessary. |
|
31
|
|
|
* |
|
32
|
|
|
* @inheritdoc |
|
33
|
|
|
* |
|
34
|
|
|
* @param CanvasPest $api |
|
35
|
|
|
* @param mixed[] $params |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(CanvasPest $api, $params) |
|
38
|
|
|
{ |
|
39
|
|
|
/* API access may be required to process $params */ |
|
40
|
|
|
$this->setApi($api); |
|
41
|
|
|
|
|
42
|
|
|
if (empty($params['url'])) { |
|
43
|
|
|
$params['url'] = preg_replace('%^(.*)/api/v\d+$%', '$1', $this->getApi()->base_url); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
parent::__construct($params); |
|
47
|
|
|
|
|
48
|
|
|
assert(isset($params['id']), new Exception('`id` parameter required')); |
|
49
|
|
|
$this->setId($params['id']); |
|
50
|
|
|
|
|
51
|
|
|
$this->localizeUrl(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Update the `$api` field |
|
56
|
|
|
* |
|
57
|
|
|
* @used-by AbstractCanvasSearchDomain::__construct() |
|
58
|
|
|
* @param CanvasPest $api |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function setApi(CanvasPest $api) |
|
61
|
|
|
{ |
|
62
|
|
|
assert($api !== null, new Exception('Initialized CanvasPest object required')); |
|
63
|
|
|
$this->api = $api; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get the Canvas API field |
|
68
|
|
|
* |
|
69
|
|
|
* @return CanvasPest |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getApi() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->api; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Update the ID of the Canvas object |
|
78
|
|
|
* |
|
79
|
|
|
* @used-by AbstractCanvasSearchDomain::__construct() |
|
80
|
|
|
* @param string|integer $id Canvas ID or SIS ID formatted as `sis_*_id:*` |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function setId($id) |
|
83
|
|
|
{ |
|
84
|
|
|
assert( |
|
85
|
|
|
is_numeric($id) || |
|
86
|
|
|
preg_match('/^sis_[a-z]+_id:\S+$/i', $id), |
|
87
|
|
|
new Exception('ID must be a Canvas ID or SIS ID, received:' . PHP_EOL . print_r($id, true)) |
|
88
|
|
|
); |
|
89
|
|
|
$this->id = $id; |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get the Canvas object ID |
|
94
|
|
|
* |
|
95
|
|
|
* @return string|integer |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getId() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->id; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Localize the object URL field within the Canvas instance |
|
104
|
|
|
|
|
105
|
|
|
* @return void |
|
106
|
|
|
*/ |
|
107
|
|
|
abstract protected function localizeUrl(); |
|
108
|
|
|
} |
|
109
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.