Completed
Push — master ( 3c1244...38faae )
by Timothy
03:43
created

AnimeClient::json_file_encode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4286
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
/**
3
 * Hummingbird Anime Client
4
 *
5
 * An API client for Hummingbird to manage anime and manga watch lists
6
 *
7
 * @package     HummingbirdAnimeClient
8
 * @author      Timothy J. Warren
9
 * @copyright   Copyright (c) 2015 - 2016
10
 * @link        https://github.com/timw4mail/HummingBirdAnimeClient
11
 * @license     MIT
12
 */
13
14
namespace Aviat\AnimeClient;
15
16
/**
17
 * Odds and Ends class
18
 */
19
class AnimeClient {
20
21
	use \Aviat\Ion\Di\ContainerAware;
22
23
	const SESSION_SEGMENT = 'Aviat\AnimeClient\Auth';
24
	const DEFAULT_CONTROLLER_NAMESPACE = 'Aviat\AnimeClient\Controller';
25
	const DEFAULT_CONTROLLER = 'Aviat\AnimeClient\Controller\Anime';
26
	const DEFAULT_CONTROLLER_METHOD = 'index';
27
	const NOT_FOUND_METHOD = 'not_found';
28
	const ERROR_MESSAGE_METHOD = 'error_page';
29
	const SRC_DIR = __DIR__ . '/../../';
30
31
	private static $form_pages = [
32
		'edit',
33
		'add',
34
		'update',
35
		'update_form',
36
		'login',
37
		'logout'
38
	];
39
40
	/**
41
	 * HTML selection helper function
42
	 *
43
	 * @param string $a - First item to compare
44
	 * @param string $b - Second item to compare
45
	 * @return string
46
	 */
47
	public static function is_selected($a, $b)
48
	{
49
		return ($a === $b) ? 'selected' : '';
50
	}
51
52
	/**
53
	 * Inverse of selected helper function
54
	 *
55
	 * @param string $a - First item to compare
56
	 * @param string $b - Second item to compare
57
	 * @return string
58
	 */
59
	public static function is_not_selected($a, $b)
60
	{
61
		return ($a !== $b) ? 'selected' : '';
62
	}
63
64
	/**
65
	 * Decode a json file into a php data structure
66
	 *
67
	 * @param  string $file
68
	 * @param  bool $as_array
69
	 * @return array|object
70
	 */
71
	public static function json_file_decode($file, $as_array=TRUE)
72
	{
73
	    return json_decode(
74
	        file_get_contents($file),
75
	        $as_array
76
	    );
77
	}
78
79
	/**
80
	 * Encode json data and save to the selected file
81
	 *
82
	 * @param string $file
83
	 * @param mixed $data
84
	 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
85
	 */
86
	public static function json_file_encode($file, $data)
87
	{
88
		return file_put_contents(
89
			$file,
90
			json_encode($data)
91
		);
92
	}
93
94
	/**
95
	 * Determine whether to show the sub-menu
96
	 *
97
	 * @return bool
98
	 */
99
	public function is_view_page()
100
	{
101
		$url = $this->container->get('request')
102
			->url->get();
103
		$page_segments = explode("/", $url);
104
105
		$intersect = array_intersect($page_segments, self::$form_pages);
106
107
		return empty($intersect);
108
	}
109
110
	/**
111
	 * Determine whether the page is a page with a form, and
112
	 * not suitable for redirection
113
	 *
114
	 * @return boolean
115
	 */
116
	public function is_form_page()
117
	{
118
		return ! $this->is_view_page();
119
	}
120
121
}
122
// End of anime_client.php