Skip to content

Commit c25d0c3

Browse files
Add composer package (#82)
* Initial commit. Spec.php, composer.json, types.php, LICENSE added README.md: Added composer package instructions .gitignore: Added standard ignore statements for most composer packages Build.php: Added writePHPObject() function to create the types.php file package-lock.json: Did NOT bump version * Requested changes to README.md
1 parent 5cf1ebe commit c25d0c3

9 files changed

Lines changed: 1337 additions & 516 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
node_modules
22
/lib
3-
/coverage
3+
/coverage
4+
/vendor/
5+
composer.phar
6+
/.idea

Build.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function __construct()
6969
$this->writeReadme($table);
7070
}
7171
$this->writeTypeScript();
72+
$this->writePHPObject();
7273
}
7374

7475
/**
@@ -182,6 +183,33 @@ private function writeTypeScript(): void
182183
$json = file_get_contents('./data/types.json');
183184
file_put_contents('./src/types.ts', 'export const types = ' . $json . ';');
184185
}
186+
187+
private function writePHPObject(): void
188+
{
189+
// Fetch the JSON file
190+
$jsonData = file_get_contents('./data/types.json');
191+
if ($jsonData === false) {
192+
die("Error fetching JSON file.");
193+
}
194+
195+
// Decode JSON data
196+
$dataObject = json_decode($jsonData);
197+
if ($dataObject === null) {
198+
die("Error decoding JSON data.");
199+
}
200+
201+
// Prepare the PHP content
202+
$phpContent = "<?php\n\n" . 'return ' . var_export($dataObject, true) . ";\n";
203+
204+
// Write to types.php in the src directory
205+
$fileWritten = file_put_contents(__DIR__ . '/src/types.php', $phpContent);
206+
207+
if ($fileWritten === false) {
208+
die("Error writing to types.php.");
209+
}
210+
211+
echo "types.php successfully written.\n";
212+
}
185213
}
186214

187215
new Build();

LICENSE

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Michael Dowling <mtdowling@gmail.com>
4+
Copyright (c) 2015 Márk Sági-Kazár <mark.sagikazar@gmail.com>
5+
Copyright (c) 2015 Graham Campbell <hello@gjcampbell.co.uk>
6+
Copyright (c) 2016 Tobias Schultze <webmaster@tubo-world.de>
7+
Copyright (c) 2016 George Mponos <gmponos@gmail.com>
8+
Copyright (c) 2018 Tobias Nyholm <tobias.nyholm@gmail.com>
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a copy
11+
of this software and associated documentation files (the "Software"), to deal
12+
in the Software without restriction, including without limitation the rights
13+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
copies of the Software, and to permit persons to whom the Software is
15+
furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included in
18+
all copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
THE SOFTWARE.

README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,93 @@ Some possible next steps for this format include:
276276
- contact information for following up on issues with feed or meeting info
277277
- language split out into its own fields
278278
- indication of which language was used for geocoding
279+
280+
## Use the Spec in your code
281+
282+
### PHP
283+
***
284+
### Code4Recovery Spec Composer Package
285+
286+
This package contains a class that makes the most up-to-date meeting types available to your application. Updates are released anytime new meeting types are added.
287+
288+
### Installation
289+
```shell
290+
composer require code4recovery/spec
291+
```
292+
293+
### Get all available languages
294+
Returns an array of all available languages for types can be translated into. The array is keyed by language code and has the expanded language name as the value.
295+
```php
296+
$spec::getLanguages();
297+
```
298+
Example returned value
299+
```php
300+
[
301+
'en' => 'English',
302+
'es' => 'Español',
303+
'fr' => 'Français',
304+
'ja' => '日本語',
305+
'sv' => 'Svenska',
306+
];
307+
```
308+
309+
### Get all types
310+
Returns an object containing all current meeeting types in every language.
311+
```php
312+
$spec::getAllTypes();
313+
```
314+
Example returned value (truncated)
315+
```php
316+
{
317+
"11": {
318+
"en": "11th Step Meditation",
319+
"es": "Meditación del Paso 11",
320+
"fr": "Méditation sur la 11e Étape",
321+
"ja": "ステップ11 黙想",
322+
"sv": "11th Stegs Meditation"
323+
},
324+
"12x12": {
325+
"en": "12 Steps & 12 Traditions",
326+
"es": "12 Pasos y 12 Tradiciones",
327+
"fr": "12 Étapes et 12 Traditions",
328+
"ja": "12のステップと12の伝統",
329+
"sv": "12 Steg & 12 Traditioner"
330+
},
331+
...
332+
};
333+
```
334+
335+
### Get types by language
336+
Returns an array of types translated into a specified language. Pass the desired language key as a string ('en', 'es', 'fr', etc.)
337+
338+
```php
339+
$spec::getTypesByLanguage('en');
340+
```
341+
Example returned value
342+
```php
343+
[
344+
11 => "11th Step Meditation"
345+
"12x12" => "12 Steps & 12 Traditions"
346+
"A" => "Secular"
347+
"ABSI" => "As Bill Sees It"
348+
...
349+
];
350+
```
351+
352+
### Typescript/Javascript
353+
***
354+
355+
### Installation
356+
```shell
357+
npm i @code4recovery/spec
358+
```
359+
360+
### Usage
361+
```shell
362+
import { getTypesForLanguage } from '@code4recovery/spec';
363+
364+
const types = getTypesForLanguage('en');
365+
```
366+
367+
## License
368+
Code4Recovery Spec is made available under the MIT License (MIT). Please see [License File](LICENSE) for more information.

composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "code4recovery/spec",
3+
"description": "The goal of the Meeting Guide API is help sync information about AA meetings. It was developed for the Meeting Guide app, but it is non-proprietary and other systems are encouraged to make use of it.",
4+
"require": {
5+
"php": ">=7.4",
6+
"ext-json": "*"
7+
},
8+
"license": "MIT",
9+
"autoload": {
10+
"psr-4": {
11+
"Code4Recovery\\": "src"
12+
}
13+
},
14+
"authors": [
15+
{
16+
"name": "Code for Recovery"
17+
}
18+
]
19+
}

src/Spec.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
class Spec
4+
{
5+
private static array $languages = [
6+
'en' => 'English',
7+
'es' => 'Español',
8+
'fr' => 'Français',
9+
'ja' => '日本語',
10+
'sv' => 'Svenska',
11+
];
12+
13+
/**
14+
* Get all languages.
15+
*
16+
* Retrieves and returns all languages with language code and expanded language translation that is stored
17+
* in the type repository.
18+
*
19+
* @return string[]
20+
*/
21+
public static function getLanguages(): array
22+
{
23+
return self::$languages;
24+
}
25+
26+
/**
27+
* Get all types.
28+
*
29+
* Retrieves and returns all types stored in the type repository.
30+
*
31+
* @return object The array of all types.
32+
*/
33+
public static function getAllTypes (): object
34+
{
35+
$types = include 'types.php';
36+
return $types;
37+
}
38+
39+
/**
40+
* Returns an array of types translated into the specified language.
41+
*
42+
* @param string $language The language code to retrieve the translations
43+
* for.
44+
*
45+
* @return array An array of type translations in the specified language.
46+
*/
47+
public static function getTypesByLanguage(string $language): array
48+
{
49+
$typesByLanguage = [];
50+
$types = self::getAllTypes();
51+
foreach ($types as $typeKey => $typeTranslations) {
52+
if (isset($typeTranslations->$language)) {
53+
$typesByLanguage[$typeKey] = $typeTranslations->$language;
54+
}
55+
}
56+
return $typesByLanguage;
57+
}
58+
}

src/languages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const languages = ["en", "es", "fr", "ja", "sv"] as const;
1+
export const languages = ["en","es","fr","ja","sv"] as const;

0 commit comments

Comments
 (0)