diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b0ec83b --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/vendor +vendor/ +vendor/* +composer.lock +composer.phar \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..3aff2ea --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "wondeltd/php-client", + "description": "PHP Client for Wonde", + "license": "MIT", + "keywords": ["wonde"], + "authors": [ + { + "name": "Philip Clifton", + "email": "pclifton@wonde.com" + } + ], + "require": { + "guzzlehttp/guzzle": "~5.0" + }, + "require-dev": { + "phpunit/phpunit": "4.0.*" + }, + "autoload": { + "psr-4": { + "Wonde\\": "src" + } + } +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..d7e1f24 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,17 @@ + + + + + ./tests/ + + + \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..0a966d3 --- /dev/null +++ b/readme.md @@ -0,0 +1,271 @@ +## Installation + +Requires PHP 5.6. + +Using Composer: + +```json +{ + "require": { + "wondeltd/php-client": "1.0.*" + } +} +``` + + +### Client + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); +``` + +### Schools + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +// Loop through the schools your account has access to +foreach ($client->schools->all() as $school) { + // Display school name + echo $school->name . PHP_EOL; +} +``` + +### Students + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +$school = $client->school('SCHOOL_ID_GOES_HERE'); + +// Get students +foreach ($school->students->all() as $student) { + echo $students->forename . ' ' . $students->surname . PHP_EOL; +} + +// Get single student +$student = $school->students->get('STUDENT_ID_GOES_HERE'); + +// Get students and include contact_details object +foreach ($school->students->all(['contact_details']) as $student) { + echo $students->forename . ' ' . $students->surname . PHP_EOL; +} + +// Get students and include contacts array +foreach ($school->students->all(['contacts']) as $student) { + echo $students->forename . ' ' . $students->surname . PHP_EOL; +} + +// Get students, include contact_details object, include extended_details object and filter by updated after date +foreach ($school->students->all(['contact_details', 'extended_details'], ['updated_after' => '2016-06-24 00:00:00']) as $student) { + echo $students->forename . ' ' . $students->surname . PHP_EOL; +} +``` + +### Achievements + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +$school = $client->school('SCHOOL_ID_GOES_HERE'); + +// Get achievements +foreach ($school->achievements->all() as $achievement) { + echo $achievement->comment . PHP_EOL; +} +``` + +### Attendance + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +$school = $client->school('SCHOOL_ID_GOES_HERE'); + +// Get attendance +foreach ($school->attendance->all() as $attendance) { + echo $attendance->comment . PHP_EOL; +} +``` + +### Attendance Codes + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +$school = $client->school('SCHOOL_ID_GOES_HERE'); + +// Get attendance codes +foreach ($client->attendanceCodes->all() as $attendanceCode) { + echo $attendanceCode->code . PHP_EOL; + } +``` + +### Behaviours + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +$school = $client->school('SCHOOL_ID_GOES_HERE'); + +// Get behaviours +foreach ($school->behaviours->all() as $behaviour) { + echo $behaviour->incident . PHP_EOL; +} +``` + +### Classes + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +$school = $client->school('SCHOOL_ID_GOES_HERE'); + +// Get classes +foreach ($school->classes->all() as $class) { + echo $class->name . PHP_EOL; +} +``` + +### Contacts + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +$school = $client->school('SCHOOL_ID_GOES_HERE'); + +// Get contacts +foreach ($school->contacts->all() as $contacts) { + echo $contacts->forename . ' ' . $contacts->surname . PHP_EOL; +} +``` + +### Employees + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +$school = $client->school('SCHOOL_ID_GOES_HERE'); + +// Get employees +foreach ($school->employees->all() as $employee) { + echo $employee->forename . ' ' . $employee->surname . PHP_EOL; +} +``` + +### Groups + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +$school = $client->school('SCHOOL_ID_GOES_HERE'); + +// Get groups +foreach ($school->groups->all() as $group) { + echo $group->name . PHP_EOL; +} +``` + +### Lessons + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +$school = $client->school('SCHOOL_ID_GOES_HERE'); + +// Get lessons +foreach ($school->lessons->all() as $lesson) { + echo $lesson->period_id . '-' . $lesson->class_id . PHP_EOL; +} +``` + +### Lesson Attendance + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +$school = $client->school('SCHOOL_ID_GOES_HERE'); + +// Get lesson attendance +foreach ($school->lessonAttendance->all() as $lessonAttendance) { + echo $lessonAttendance->comment . PHP_EOL; +} +``` + +### Medical Conditions + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +$school = $client->school('SCHOOL_ID_GOES_HERE'); + +// Get medical conditions +foreach ($school->medicalConditions->all() as $medicalCondition) { + echo $medicalCondition->description . PHP_EOL; +} +``` + +### Medical Events + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +$school = $client->school('SCHOOL_ID_GOES_HERE'); + +// Get medical events +foreach ($school->medicalEvents->all() as $medicalEvent) { + echo $medicalEvent->description . PHP_EOL; +} +``` + +### Periods + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +$school = $client->school('SCHOOL_ID_GOES_HERE'); + +// Get periods +foreach ($school->periods->all() as $period) { + echo $period->name . PHP_EOL; +} +``` + +### Photos + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +$school = $client->school('SCHOOL_ID_GOES_HERE'); + +// Get photos +foreach ($school->photos->all() as $photo) { + echo $photo->hash . PHP_EOL; +} +``` + +### Rooms + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +$school = $client->school('SCHOOL_ID_GOES_HERE'); + +// Get rooms +foreach ($school->rooms->all() as $room) { + echo $room->name . PHP_EOL; +} +``` + +### Subjects + +```php +$client = new \Wonde\Client('TOKEN_GOES_HERE'); + +$school = $client->school('SCHOOL_ID_GOES_HERE'); + +// Get subjects +foreach ($school->subjects->all() as $subject) { + echo $subject->name . PHP_EOL; +} +``` \ No newline at end of file diff --git a/src/Client.php b/src/Client.php new file mode 100644 index 0000000..1623f23 --- /dev/null +++ b/src/Client.php @@ -0,0 +1,53 @@ +token = $token; + $this->schools = new Schools($token); + $this->attendanceCodes = new AttendanceCodes($token); + } + + /** + * Return endpoints for single school + * + * @param $id + * @return Schools + */ + public function school($id) + { + return new Schools($this->token, $id); + } +} + diff --git a/src/Endpoints/Achievements.php b/src/Endpoints/Achievements.php new file mode 100644 index 0000000..9551cf6 --- /dev/null +++ b/src/Endpoints/Achievements.php @@ -0,0 +1,9 @@ +token = $token; + + if ($uri) { + $this->uri = $uri . $this->uri; + } + } + + /** + * Get the default guzzle client + * + * @return Client + */ + private function client() + { + return new Client([ + 'headers' => [ + 'Authorization' => 'Basic ' . base64_encode($this->token . ':'), + 'User-Agent' => 'wonde-php-client-' . \Wonde\Client::version + ] + ]); + } + + /** + * Make a get request + * + * @param $endpoint + * @return mixed|\Psr\Http\Message\ResponseInterface + */ + private function getRequest($endpoint) + { + return $this->getUrl(self::endpoint . $endpoint); + } + + /** + * Make a get request to url + * + * @param $url + * @return mixed|\Psr\Http\Message\ResponseInterface + */ + public function getUrl($url) + { + return $this->client()->request('GET', $url); + } + + /** + * Get all of resource + * + * @param array $includes + * @param array $parameters + * @return mixed|\Psr\Http\Message\ResponseInterface + */ + public function all($includes = [], $parameters = []) + { + if ( ! empty($includes)) { + $parameters['include'] = implode(',', $includes); + } + + $uri = ! empty($parameters) ? $this->uri . '?' . http_build_query($parameters) : $this->uri; + + $response = $this->getRequest($uri)->getBody()->getContents(); + $decoded = json_decode($response); + + return new ResultIterator($decoded, $this->token); + } + + /** + * Get single resource + * + * @param $id + * @return mixed + */ + public function get($id, $includes = [], $parameters = []) + { + if ( ! empty($includes)) { + $parameters['include'] = implode(',', $includes); + } + + $uri = ! empty($parameters) ? $this->uri . $id . '?' . http_build_query($parameters) : $this->uri . $id; + + $response = $this->getRequest($uri)->getBody()->getContents(); + $decoded = json_decode($response); + + return $decoded->data; + } +} \ No newline at end of file diff --git a/src/Endpoints/Classes.php b/src/Endpoints/Classes.php new file mode 100644 index 0000000..e5274d9 --- /dev/null +++ b/src/Endpoints/Classes.php @@ -0,0 +1,9 @@ +token = $token; + + if ($id) { + $this->uri = $this->uri . $id . '/'; + } + + $this->achievements = new Achievements($token, $this->uri); + $this->attendance = new Attendance($token, $this->uri); + $this->behaviours = new Behaviours($token, $this->uri); + $this->classes = new Classes($token, $this->uri); + $this->contacts = new Contacts($token, $this->uri); + $this->counts = new Counts($token, $this->uri); + $this->employees = new Employees($token, $this->uri); + $this->groups = new Groups($token, $this->uri); + $this->lessons = new Lessons($token, $this->uri); + $this->lessonAttendance = new LessonAttendance($token, $this->uri); + $this->medicalConditions = new MedicalConditions($token, $this->uri); + $this->medicalEvents = new MedicalEvents($token, $this->uri); + $this->periods = new Periods($token, $this->uri); + $this->photos = new Photos($token, $this->uri); + $this->rooms = new Rooms($token, $this->uri); + $this->students = new Students($token, $this->uri); + $this->subjects = new Subjects($token, $this->uri); + } +} \ No newline at end of file diff --git a/src/Endpoints/Students.php b/src/Endpoints/Students.php new file mode 100644 index 0000000..550f540 --- /dev/null +++ b/src/Endpoints/Students.php @@ -0,0 +1,9 @@ +meta = $givenArray->meta; + $this->array = $givenArray->data; + $this->token = $token; + } + + function rewind() + { + return reset($this->array); + } + + function current() + { + return current($this->array); + } + + function key() + { + return key($this->array); + } + + function next() + { + return next($this->array); + } + + function valid() + { + $valid = key($this->array) !== null; + + if ( ! $valid) { + + if ( ! empty($this->meta->pagination->next)) { + + $nextResponse = $this->getUrl($this->meta->pagination->next)->getBody()->getContents(); + $decoded = json_decode($nextResponse); + + $this->meta = $decoded->meta; + $this->array = $decoded->data; + + reset($this->array); + + return (bool) count($this->array); + } + + } else { + return $valid; + } + } +} diff --git a/tests/ClientTest.php b/tests/ClientTest.php new file mode 100644 index 0000000..ca3d7cb --- /dev/null +++ b/tests/ClientTest.php @@ -0,0 +1,10 @@ +assertTrue($class instanceof \Wonde\Client); + } +} \ No newline at end of file diff --git a/tests/SchoolsTest.php b/tests/SchoolsTest.php new file mode 100644 index 0000000..928baff --- /dev/null +++ b/tests/SchoolsTest.php @@ -0,0 +1,115 @@ +schools->all() as $resultSchool) { + + // Display school name + echo $resultSchool->name . PHP_EOL; + + // Setup school + $school = $client->school('A1329183376'); + + // Get attendance + foreach ($school->attendance->all() as $attendance) { + echo $attendance->comment . PHP_EOL; + } + + // Get attendance codes + foreach ($client->attendanceCodes->all() as $attendanceCode) { + echo $attendanceCode->code . PHP_EOL; + } + + // Get achievements + foreach ($school->achievements->all() as $achievement) { + echo $achievement->type . PHP_EOL; + } + + // Get behaviours + foreach ($school->behaviours->all() as $behaviour) { + echo $behaviour->incident . PHP_EOL; + } + + // Get classes + foreach ($school->classes->all() as $class) { + echo $class->name . PHP_EOL; + } + + // Get groups + foreach ($school->groups->all() as $group) { + echo $group->name . PHP_EOL; + } + + // Get lessons + foreach ($school->lessons->all() as $lesson) { + echo $lesson->period_id . '-' . $lesson->class_id . PHP_EOL; + } + + // Get lesson attendance + foreach ($school->lessonAttendance->all() as $lessonAttendance) { + echo $lessonAttendance->comment . PHP_EOL; + } + + // Get medical conditions + foreach ($school->medicalConditions->all() as $medicalCondition) { + echo $medicalCondition->description . PHP_EOL; + } + + // Get medical events + foreach ($school->medicalEvents->all() as $medicalEvent) { + echo $medicalEvent->description . PHP_EOL; + } + + // Get periods + foreach ($school->periods->all() as $period) { + echo $period->name . PHP_EOL; + } + + // Get periods + foreach ($school->photos->all() as $photo) { + echo $photo->hash . PHP_EOL; + } + + // Get rooms + foreach ($school->rooms->all() as $room) { + echo $room->name . PHP_EOL; + } + + // Get subjects + foreach ($school->subjects->all() as $subject) { + echo $subject->name . PHP_EOL; + } + + + // Example - Get all students for a school + foreach ($client->school($school->id)->students->all() as $students) { + echo $students->forename . ' ' . $students->surname . PHP_EOL; + } + + // Example - Get all employees for a school + foreach ($client->school($school->id)->employees->all() as $employee) { + echo $employee->forename . ' ' . $employee->surname . PHP_EOL; + } + + // Example - Get employees with their contact details object + foreach ($school->employees->all(['contact_details']) as $employee) { + echo $employee->forename . ' ' . $employee->surname . PHP_EOL; + } + + // Example - Get students updated after a date with included data sets (contact_details, extended_details) + foreach ( + $school->students->all([ + 'contact_details', + 'extended_details' + ], ['updated_after' => '2016-06-24 00:00:00']) as $students + ) { + echo $students->forename . ' ' . $students->surname . PHP_EOL; + } + } + } +} \ No newline at end of file