1+ <?php
2+
3+ /*!
4+ * Data Class
5+ *
6+ * Copyright (c) 2014 Dave Olsen, http://dmolsen.com
7+ * Licensed under the MIT license
8+ *
9+ * Acts as the overall data store for Pattern Lab. Takes in data found in JSON and YAML files.
10+ *
11+ */
12+
13+ namespace PatternLab ;
14+
15+ use \PatternLab \Config ;
16+ use \Symfony \Component \Yaml \Yaml ;
17+
18+ class Data {
19+
20+ public static $ store = array ();
21+ protected static $ reservedKeys = array ("listItems " ,"cacheBuster " ,"link " ,"patternSpecific " ,"patternFooterData " );
22+
23+ /**
24+ * Grab a copy of the $store
25+ *
26+ * @return {Array} a copy of the store
27+ */
28+ public static function copy () {
29+ return self ::$ store ;
30+ }
31+
32+ /**
33+ * Gather data from source/_data/_data.json, source/_data/_listitems.json, and pattern-specific json files
34+ *
35+ * Reserved attributes:
36+ * - Data::$store["listItems"] : listItems from listitems.json, duplicated into separate arrays for Data::$store["listItems"]["one"], Data::$store["listItems"]["two"]... etc.
37+ * - Data::$store["link"] : the links to each pattern
38+ * - Data::$store["cacheBuster"] : the cache buster value to be appended to URLs
39+ * - Data::$store["patternSpecific"] : holds attributes from the pattern-specific data files
40+ *
41+ * @return {Array} populates Data::$store
42+ */
43+ public static function gather ($ options = array ()) {
44+
45+ // default vars
46+ $ found = false ;
47+ $ dataJSON = array ();
48+ $ dataYAML = array ();
49+ $ listItemsJSON = array ();
50+ $ listItemsYAML = array ();
51+
52+ // gather the data from the main source data.json
53+ if (file_exists (Config::$ options ["sourceDir " ]."/_data/_data.json " )) {
54+ $ file = file_get_contents (Config::$ options ["sourceDir " ]."/_data/_data.json " );
55+ $ dataJSON = json_decode ($ file ,true );
56+ if ($ jsonErrorMessage = JSON ::hasError ()) {
57+ JSON ::lastErrorMsg ("_data/_data.json " ,$ jsonErrorMessage ,$ data );
58+ }
59+ $ found = true ;
60+ }
61+
62+ // gather the data from the main source data.yaml
63+ if (file_exists (Config::$ options ["sourceDir " ]."/_data/_data.yaml " )) {
64+ $ file = file_get_contents (Config::$ options ["sourceDir " ]."/_data/_data.yaml " );
65+ $ dataYAML = Yaml::parse ($ file );
66+ $ found = true ;
67+ }
68+
69+ if (!$ found ) {
70+ print "Missing a required file, source/_data/_data.json. Aborting. \n" ;
71+ exit ;
72+ }
73+
74+ self ::$ store = array_replace_recursive ($ dataJSON ,$ dataYAML );
75+
76+ if (is_array (self ::$ store )) {
77+ foreach (self ::$ reservedKeys as $ reservedKey ) {
78+ if (array_key_exists ($ reservedKey ,self ::$ store )) {
79+ print "\"" .$ reservedKey ."\" is a reserved key in Pattern Lab. The data using that key in _data.json will be overwritten. Please choose a new key. \n" ;
80+ }
81+ }
82+ }
83+
84+ $ listItemsJSON = self ::getListItems ("data/_listitems.json " );
85+ $ listItemsYAML = self ::getListItems ("data/_listitems.yaml " ,"yaml " );
86+
87+ self ::$ store ["listItems " ] = array_replace_recursive ($ listItemsJSON ,$ listItemsYAML );
88+ self ::$ store ["cacheBuster " ] = Config::$ options ["cacheBuster " ];
89+ self ::$ store ["link " ] = array ();
90+ self ::$ store ["patternSpecific " ] = array ();
91+
92+ }
93+
94+ /**
95+ * Generate the listItems array
96+ * @param {String} the filename for the pattern to be parsed
97+ * @param {String} the extension so that a flag switch can be used to parse data
98+ *
99+ * @return {Array} the final set of list items
100+ */
101+ protected static function getListItems ($ filepath ,$ ext = "json " ) {
102+
103+ $ listItems = array ();
104+ $ listItemsData = array ();
105+
106+ // add list item data, makes 'listItems' a reserved word
107+ if (file_exists (Config::$ options ["sourceDir " ]."/ " .$ filepath )) {
108+
109+ $ file = file_get_contents (Config::$ options ["sourceDir " ]."/ " .$ filepath );
110+
111+ if ($ ext == "json " ) {
112+ $ listItemsData = json_decode ($ file , true );
113+ if ($ jsonErrorMessage = JSON ::hasError ()) {
114+ JSON ::lastErrorMsg ($ filepath ,$ jsonErrorMessage ,$ listItems );
115+ }
116+ } else {
117+ $ listItemsData = Yaml::parse ($ file );
118+ }
119+
120+
121+ $ numbers = array ("one " ,"two " ,"three " ,"four " ,"five " ,"six " ,"seven " ,"eight " ,"nine " ,"ten " ,"eleven " ,"twelve " );
122+
123+ $ i = 0 ;
124+ $ k = 1 ;
125+ $ c = count ($ listItemsData )+1 ;
126+
127+ while ($ k < $ c ) {
128+
129+ shuffle ($ listItemsData );
130+ $ itemsArray = array ();
131+
132+ while ($ i < $ k ) {
133+ $ itemsArray [] = $ listItemsData [$ i ];
134+ $ i ++;
135+ }
136+
137+ $ listItems [$ numbers [$ k -1 ]] = $ itemsArray ;
138+
139+ $ i = 0 ;
140+ $ k ++;
141+
142+ }
143+
144+ }
145+
146+ return $ listItems ;
147+
148+ }
149+
150+ /**
151+ * Get the final data array specifically for a pattern
152+ * @param {String} the filename for the pattern to be parsed
153+ * @param {Array} any extra data that should be added to the pattern specific data that's being returned
154+ *
155+ * @return {Array} the final set of list items
156+ */
157+ public static function getPatternSpecificData ($ patternPartial ,$ extraData = array ()) {
158+
159+ // if there is pattern-specific data make sure to override the default in $this->d
160+ $ d = self ::copy ();
161+
162+ if (isset ($ d ["patternSpecific " ]) && array_key_exists ($ patternPartial ,$ d ["patternSpecific " ])) {
163+
164+ if (!empty ($ d ["patternSpecific " ][$ patternPartial ]["data " ])) {
165+ $ d = array_replace_recursive ($ d , $ d ["patternSpecific " ][$ patternPartial ]["data " ]);
166+ }
167+
168+ if (!empty ($ d ["patternSpecific " ][$ patternPartial ]["listItems " ])) {
169+
170+ $ numbers = array ("one " ,"two " ,"three " ,"four " ,"five " ,"six " ,"seven " ,"eight " ,"nine " ,"ten " ,"eleven " ,"twelve " );
171+
172+ $ k = 0 ;
173+ $ c = count ($ d ["patternSpecific " ][$ patternPartial ]["listItems " ]);
174+
175+ while ($ k < $ c ) {
176+ $ section = $ numbers [$ k ];
177+ $ d ["listItems " ][$ section ] = array_replace_recursive ( $ d ["listItems " ][$ section ], $ d ["patternSpecific " ][$ patternPartial ]["listItems " ][$ section ]);
178+ $ k ++;
179+ }
180+
181+ }
182+
183+ }
184+
185+ if (!empty ($ extraData )) {
186+ $ d = array_replace_recursive ($ d , $ extraData );
187+ }
188+
189+ unset($ d ["patternSpecific " ]);
190+
191+ return $ d ;
192+
193+ }
194+
195+ /**
196+ * Print out the data var. For debugging purposes
197+ *
198+ * @return {String} the formatted version of the d object
199+ */
200+ public static function printData () {
201+ print_r (self ::$ store );
202+ }
203+
204+ }
0 commit comments