Skip to content

Commit 3655942

Browse files
committed
refactor: use PHP 8 attributes to configure array_converter
1 parent 56ff7ab commit 3655942

37 files changed

+689
-292
lines changed

classes/api/attempt_file.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717
namespace qtype_questionpy\api;
1818

19-
use qtype_questionpy\array_converter\array_converter;
20-
use qtype_questionpy\array_converter\converter_config;
21-
22-
defined('MOODLE_INTERNAL') || die;
19+
use qtype_questionpy\array_converter\attributes\array_key;
2320

2421
/**
2522
* A file used in an attempt at a QuestionPy question.
@@ -34,6 +31,7 @@ class attempt_file {
3431
public string $name;
3532

3633
/** @var string|null */
34+
#[array_key("mime_type")]
3735
public ?string $mimetype = null;
3836

3937
/** @var string $data */
@@ -52,7 +50,3 @@ public function __construct(string $name, string $data, ?string $mimetype = null
5250
$this->mimetype = $mimetype;
5351
}
5452
}
55-
56-
array_converter::configure(attempt_file::class, function (converter_config $config) {
57-
$config->rename("mimetype", "mime_type");
58-
});

classes/api/attempt_scored.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717
namespace qtype_questionpy\api;
1818

19-
use qtype_questionpy\array_converter\array_converter;
20-
use qtype_questionpy\array_converter\converter_config;
21-
22-
defined('MOODLE_INTERNAL') || die;
19+
use qtype_questionpy\array_converter\attributes\array_key;
2320

2421
/**
2522
* A scored attempt at a QuestionPy question.
@@ -31,9 +28,11 @@
3128
*/
3229
class attempt_scored extends attempt {
3330
/** @var string|null */
31+
#[array_key("scoring_state")]
3432
public ?string $scoringstate;
3533

3634
/** @var scoring_code */
35+
#[array_key("scoring_code")]
3736
public scoring_code $scoringcode;
3837

3938
/** @var float|null */
@@ -54,8 +53,3 @@ public function __construct(int $variant, attempt_ui $ui, scoring_code $scoringc
5453
$this->scoringcode = $scoringcode;
5554
}
5655
}
57-
58-
array_converter::configure(attempt_scored::class, function (converter_config $config) {
59-
$config->rename("scoringstate", "scoring_state")
60-
->rename("scoringcode", "scoring_code");
61-
});

classes/api/attempt_started.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717
namespace qtype_questionpy\api;
1818

19-
use qtype_questionpy\array_converter\array_converter;
20-
use qtype_questionpy\array_converter\converter_config;
21-
22-
defined('MOODLE_INTERNAL') || die;
19+
use qtype_questionpy\array_converter\attributes\array_key;
2320

2421
/**
2522
* Response from the server for a newly started attempt.
@@ -31,6 +28,7 @@
3128
*/
3229
class attempt_started extends attempt {
3330
/** @var string */
31+
#[array_key("attempt_state")]
3432
public string $attemptstate;
3533

3634
/**
@@ -45,7 +43,3 @@ public function __construct(int $variant, attempt_ui $ui, string $attemptstate)
4543
$this->attemptstate = $attemptstate;
4644
}
4745
}
48-
49-
array_converter::configure(attempt_started::class, function (converter_config $config) {
50-
$config->rename("attemptstate", "attempt_state");
51-
});

classes/api/attempt_ui.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616

1717
namespace qtype_questionpy\api;
1818

19-
use qtype_questionpy\array_converter\array_converter;
20-
use qtype_questionpy\array_converter\converter_config;
21-
22-
defined('MOODLE_INTERNAL') || die;
19+
use qtype_questionpy\array_converter\attributes\array_element_class;
20+
use qtype_questionpy\array_converter\attributes\array_key;
2321

2422
/**
2523
* Model defining an attempt's UI source and associated data, such as parameters for placeholders.
@@ -34,24 +32,30 @@ class attempt_ui {
3432
public string $formulation;
3533

3634
/** @var string|null */
35+
#[array_key("general_feedback")]
3736
public ?string $generalfeedback = null;
3837

3938
/** @var string|null */
39+
#[array_key("specific_feedback")]
4040
public ?string $specificfeedback = null;
4141

4242
/** @var string|null */
43+
#[array_key("right_answer")]
4344
public ?string $rightanswer = null;
4445

4546
/** @var array<string, string> string to string mapping of placeholder names to the values (to be replaced in the content) */
4647
public array $placeholders = [];
4748

4849
/** @var string[]|null */
50+
#[array_key("css_files")]
4951
public ?array $cssfiles = null;
5052

5153
/** @var array<string, attempt_file> specifics TBD */
54+
#[array_element_class(attempt_file::class)]
5255
public array $files = [];
5356

5457
/** @var string specifics TBD */
58+
#[array_key("cache_control")]
5559
public string $cachecontrol = "PRIVATE_CACHE";
5660

5761
/**
@@ -63,13 +67,3 @@ public function __construct(string $formulation) {
6367
$this->formulation = $formulation;
6468
}
6569
}
66-
67-
array_converter::configure(attempt_ui::class, function (converter_config $config) {
68-
$config
69-
->rename("generalfeedback", "general_feedback")
70-
->rename("specificfeedback", "specific_feedback")
71-
->rename("rightanswer", "right_answer")
72-
->rename("cssfiles", "css_files")
73-
->rename("cachecontrol", "cache_control")
74-
->array_elements("files", attempt_file::class);
75-
});

classes/api/question_edit_form_response.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@
1616

1717
namespace qtype_questionpy\api;
1818

19-
use qtype_questionpy\array_converter\array_converter;
20-
use qtype_questionpy\array_converter\converter_config;
19+
use qtype_questionpy\array_converter\attributes\array_key;
2120
use qtype_questionpy\form\qpy_form;
2221

23-
defined('MOODLE_INTERNAL') || die;
24-
2522
/**
2623
* Response from the server to a request for the question edit form.
2724
*
@@ -35,21 +32,17 @@ class question_edit_form_response {
3532
public qpy_form $definition;
3633

3734
/** @var array */
35+
#[array_key("form_data")]
3836
public array $formdata;
3937

4038
/**
4139
* Initialize a new question response.
4240
*
4341
* @param qpy_form $definition form definition
44-
* @param array $formdata current values of the form elements
42+
* @param array $formdata current values of the form elements
4543
*/
4644
public function __construct(qpy_form $definition, array $formdata) {
4745
$this->definition = $definition;
4846
$this->formdata = $formdata;
4947
}
5048
}
51-
52-
array_converter::configure(question_edit_form_response::class, function (converter_config $config) {
53-
$config
54-
->rename("formdata", "form_data");
55-
});

classes/api/question_response.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717
namespace qtype_questionpy\api;
1818

19-
use qtype_questionpy\array_converter\array_converter;
20-
use qtype_questionpy\array_converter\converter_config;
21-
22-
defined('MOODLE_INTERNAL') || die;
19+
use qtype_questionpy\array_converter\attributes\array_key;
2320

2421
/**
2522
* Response from the server for a created or updated question.
@@ -31,48 +28,44 @@
3128
*/
3229
class question_response {
3330
/** @var string */
31+
#[array_key("question_state")]
3432
public string $state;
3533

3634
/** @var string */
35+
#[array_key("scoring_method")]
3736
public string $scoringmethod;
3837

3938
/** @var float|int */
39+
#[array_key("score_min")]
4040
public float $scoremin = 0;
4141

4242
/** @var float|int */
43+
#[array_key("score_max")]
4344
public float $scoremax = 1;
4445

4546
/** @var float|null */
4647
public ?float $penalty = null;
4748

4849
/** @var float|null */
50+
#[array_key("random_guess_score")]
4951
public ?float $randomguessscore = null;
5052

5153
/** @var bool */
54+
#[array_key("render_every_view")]
5255
public bool $rendereveryview = false;
5356

5457
/** @var string|null */
58+
#[array_key("general_feedback")]
5559
public ?string $generalfeedback = null;
5660

5761
/**
5862
* Initialize a new question response.
5963
*
60-
* @param string $state new question state
64+
* @param string $state new question state
6165
* @param string $scoringmethod
6266
*/
6367
public function __construct(string $state, string $scoringmethod) {
6468
$this->state = $state;
6569
$this->scoringmethod = $scoringmethod;
6670
}
6771
}
68-
69-
array_converter::configure(question_response::class, function (converter_config $config) {
70-
$config
71-
->rename("state", "question_state")
72-
->rename("scoringmethod", "scoring_method")
73-
->rename("scoremin", "score_min")
74-
->rename("scoremax", "score_max")
75-
->rename("randomguessscore", "random_guess_score")
76-
->rename("rendereveryview", "render_every_view")
77-
->rename("generalfeedback", "general_feedback");
78-
});

classes/api/status.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717
namespace qtype_questionpy\api;
1818

19-
use qtype_questionpy\array_converter\array_converter;
20-
use qtype_questionpy\array_converter\converter_config;
21-
22-
defined('MOODLE_INTERNAL') || die;
19+
use qtype_questionpy\array_converter\attributes\array_key;
2320

2421
/**
2522
* Response from the server containing server status.
@@ -37,9 +34,11 @@ class status {
3734
public string $version;
3835

3936
/** @var bool */
37+
#[array_key("allow_lms_packages")]
4038
public bool $allowlmspackages = false;
4139

4240
/** @var string */
41+
#[array_key("max_package_size")]
4342
public string $maxpackagesize;
4443

4544
/** @var usage|null */
@@ -60,9 +59,3 @@ public function __construct(string $name, string $version, int $maxpackagesize)
6059
$this->maxpackagesize = display_size($maxpackagesize, 1, 'MB');
6160
}
6261
}
63-
64-
array_converter::configure(status::class, function (converter_config $config) {
65-
$config
66-
->rename("allowlmspackages", "allow_lms_packages")
67-
->rename("maxpackagesize", "max_package_size");
68-
});

classes/api/usage.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616

1717
namespace qtype_questionpy\api;
1818

19-
use qtype_questionpy\array_converter\array_converter;
20-
use qtype_questionpy\array_converter\converter_config;
21-
use qtype_questionpy\form\conditions\in;
22-
23-
defined('MOODLE_INTERNAL') || die;
19+
use qtype_questionpy\array_converter\attributes\array_key;
2420

2521
/**
2622
* Server usage.
@@ -32,9 +28,11 @@
3228
*/
3329
class usage {
3430
/** @var int */
31+
#[array_key("requests_in_process")]
3532
public int $requestsinprocess;
3633

3734
/** @var int */
35+
#[array_key("requests_in_queue")]
3836
public int $requestsinqueue;
3937

4038
/**
@@ -48,9 +46,3 @@ public function __construct(int $requestsinprocess, int $requestsinqueue) {
4846
$this->requestsinqueue = $requestsinqueue;
4947
}
5048
}
51-
52-
array_converter::configure(usage::class, function (converter_config $config) {
53-
$config
54-
->rename("requestsinprocess", "requests_in_process")
55-
->rename("requestsinqueue", "requests_in_queue");
56-
});

0 commit comments

Comments
 (0)