1 <?php
2 /**
3 * Contains Notification class for Alphred, providing basic notification functionality
4 *
5 * PHP version 5
6 *
7 * @package Alphred
8 * @copyright Shawn Patrick Rice 2014
9 * @license http://opensource.org/licenses/MIT MIT
10 * @version 1.0.0
11 * @author Shawn Patrick Rice <rice@shawnrice.org>
12 * @link http://www.github.com/shawnrice/alphred
13 * @link http://shawnrice.github.io/alphred
14 * @since File available since Release 1.0.0
15 *
16 */
17
18 namespace Alphred;
19
20 /**
21 * Creates simple notifications on OS X 10.8+
22 */
23 class Notification {
24
25 /**
26 * The available, built-in sounds that you can use
27 * @var array
28 */
29 private static $sounds = [
30 'Basso',
31 'Bottle',
32 'Funk',
33 'Hero',
34 'Ping',
35 'Purr',
36 'Submarine',
37 'Blow',
38 'Frog',
39 'Glass',
40 'Morse',
41 'Pop',
42 'Sosumi',
43 'Tink'
44 ];
45
46 /**
47 * Sends a system notification
48 *
49 * The notification will always have the script editor icon on it.
50 * Use CocoaDialog for better notifications.
51 *
52 * @param string|array $options [description]
53 * @return boolean [description]
54 */
55 public function notify( $options ) {
56 if ( is_string( $options ) ) {
57 exec( "osascript -e 'display notification \"{$options}\"'" );
58 return true;
59 }
60 if ( ! isset( $options['text'] ) ) {
61 // throw exception
62 return false;
63 }
64
65 $script = "osascript -e 'display notification \"{$options['text']}\"";
66 foreach ( $options as $field => $option ) :
67 switch ( $field ) :
68 case 'title' :
69 $script .= " with title \"{$option}\"";
70 break;
71 case 'subtitle' :
72 $script .= " subtitle \"{$option}\"";
73 break;
74 case 'sound' :
75 if ( in_array( $option, self::$sounds ) ) {
76 $script .= " sound name \"{$option}\"";
77 }
78 break;
79 default:
80 break;
81 endswitch;
82 endforeach;
83 $script .= "'";
84 exec( $script );
85 }
86 }