forked from AppStateESS/InternshipInventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAcademicMajorList.php
79 lines (65 loc) · 2.15 KB
/
AcademicMajorList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace Intern;
class AcademicMajorList {
private $majors;
/**
* Creates an AcademicMajorList object given an array of stdClass objects containing
* undergraduate and graduate majors, and potentially including duplicates (from BannerMajorsProvider).
*
* @see BannerMajorsProvider
*/
public function __construct()
{
$this->majors = array();
}
public function addMajorsArray(Array $majorsArray)
{
// Add each of the given majors to list, checking for duplicates
foreach($majorsArray as $major){
$this->addIfNotDuplicate($major);
}
}
public function addMajor(AcademicMajor $major)
{
$this->majors[] = $major;
}
public function getMajorsByLevel(string $level): array
{
$filteredMajors = array();
foreach($this->majors as $m){
if($m->getLevel() === $level){
$filteredMajors[] = $m;
}
}
$this->sortList($filteredMajors);
return $filteredMajors;
}
/**
* Adds the array represnting a major to the set of majors if it is not already in the list.
* Prevents duplciate major arrays from being added to the list.
*
* @param Array $major The array holding a single major
*/
public function addIfNotDuplicate(AcademicMajor $major)
{
// Look through each sub-array in the set of majors
foreach($this->majors as $m){
// If the sub-array we're looking at matches the single major we were given, then we've
// found a duplicate and we can stop looking any further
if($m->getLevel() === $major->getLevel()
&& $m->getDescription() === $major->getDescription()){
return;
}
}
// If we didn't find any duplicates (i.e. $major did not exist in $destArray), then add it
$this->majors[] = $major;
}
private function sortList(&$list)
{
usort($list, array('self', 'compareFunc'));
}
public static function compareFunc($a, $b)
{
return strcasecmp($a->getDescription(), $b->getDescription());
}
}