Skip to content

Commit

Permalink
Merged Pull Request '#29 code-style->main : Apply code style fixes'
Browse files Browse the repository at this point in the history
Apply code style fixes
  • Loading branch information
Automation51D authored Nov 30, 2023
2 parents 589f647 + e110a93 commit c46e938
Show file tree
Hide file tree
Showing 35 changed files with 1,409 additions and 1,405 deletions.
43 changes: 43 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

return (new PhpCsFixer\Config())
->setRules([
'@PER' => true,
'@PSR12' => true,
'@Symfony' => true,
'@PhpCsFixer' => true,
'no_useless_return' => false,
'php_unit_test_class_requires_covers' => false,
'php_unit_internal_class' => false,
'phpdoc_align' => false,
'phpdoc_no_empty_return' => false,
'phpdoc_separation' => false,
'yoda_style' => false,
'trailing_comma_in_multiline' => false,
'multiline_whitespace_before_semicolons' => [
'strategy' => 'no_multi_line'
],
'concat_space' => [
'spacing' => 'one'
],
'not_operator_with_space' => false,
'operator_linebreak' => [
'position' => 'end',
'only_booleans' => true
],
'blank_line_before_statement' => [
'statements' => [
'return',
'throw',
'try',
'declare'
]
]
])
->setFinder(PhpCsFixer\Finder::create()
->in([
__DIR__ . '/src',
__DIR__ . '/tests/classes',
__DIR__ . '/examples',
])
);
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
},
"require-dev": {
"kint-php/kint": "^3.3",
"phpunit/phpunit": "*"
"phpunit/phpunit": "*",
"friendsofphp/php-cs-fixer": "^3.39"
},
"autoload": {
"psr-4": {
Expand Down
193 changes: 92 additions & 101 deletions examples/CustomFlowElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */

/**
/*
* @example CustomFlowElement.php
*
* This example demonstrates the creation of a custom flow element. In this case
Expand All @@ -31,180 +31,173 @@
* which gets a user's geolocation and saves the latitude as a cookie.
* This latitude is also then passed in to the FlowData to calculate if
* a person is in the northern or southern hemispheres.
*
*/


use fiftyone\pipeline\core\PipelineBuilder;
use fiftyone\pipeline\core\BasicListEvidenceKeyFilter;
use fiftyone\pipeline\core\FlowElement;
use fiftyone\pipeline\core\ElementDataDictionary;
use fiftyone\pipeline\core\FlowElement;
use fiftyone\pipeline\core\PipelineBuilder;

// Function to get star sign from month and day
function getStarSign($month, $day)
{
if (($month == 1 && $day <= 20) || ($month == 12 && $day >= 22)) {
return "capricorn";
} elseif (($month == 1 && $day >= 21) || ($month == 2 && $day <= 18)) {
return "aquarius";
} elseif (($month == 2 && $day >= 19) || ($month == 3 && $day <= 20)) {
return "pisces";
} elseif (($month == 3 && $day >= 21) || ($month == 4 && $day <= 20)) {
return "aries";
} elseif (($month == 4 && $day >= 21) || ($month == 5 && $day <= 20)) {
return "taurus";
} elseif (($month == 5 && $day >= 21) || ($month == 6 && $day <= 20)) {
return "gemini";
} elseif (($month == 6 && $day >= 22) || ($month == 7 && $day <= 22)) {
return "cancer";
} elseif (($month == 7 && $day >= 23) || ($month == 8 && $day <= 23)) {
return "leo";
} elseif (($month == 8 && $day >= 24) || ($month == 9 && $day <= 23)) {
return "virgo";
} elseif (($month == 9 && $day >= 24) || ($month == 10 && $day <= 23)) {
return "libra";
} elseif (($month == 10 && $day >= 24) || ($month == 11 && $day <= 22)) {
return "scorpio";
} elseif (($month == 11 && $day >= 23) || ($month == 12 && $day <= 21)) {
return "sagittarius";
return 'capricorn';
}
if (($month == 1 && $day >= 21) || ($month == 2 && $day <= 18)) {
return 'aquarius';
}
if (($month == 2 && $day >= 19) || ($month == 3 && $day <= 20)) {
return 'pisces';
}
if (($month == 3 && $day >= 21) || ($month == 4 && $day <= 20)) {
return 'aries';
}
if (($month == 4 && $day >= 21) || ($month == 5 && $day <= 20)) {
return 'taurus';
}
if (($month == 5 && $day >= 21) || ($month == 6 && $day <= 20)) {
return 'gemini';
}
if (($month == 6 && $day >= 22) || ($month == 7 && $day <= 22)) {
return 'cancer';
}
if (($month == 7 && $day >= 23) || ($month == 8 && $day <= 23)) {
return 'leo';
}
if (($month == 8 && $day >= 24) || ($month == 9 && $day <= 23)) {
return 'virgo';
}
if (($month == 9 && $day >= 24) || ($month == 10 && $day <= 23)) {
return 'libra';
}
if (($month == 10 && $day >= 24) || ($month == 11 && $day <= 22)) {
return 'scorpio';
}
if (($month == 11 && $day >= 23) || ($month == 12 && $day <= 21)) {
return 'sagittarius';
}
};
}

//! [class]
//! [declaration]
class AstrologyFlowElement extends FlowElement
{
//! [declaration]

// datakey used to categorise data coming back from this
// FlowElement in a Pipeline
public $dataKey = "astrology";
public $dataKey = 'astrology';

public $properties = [
'starSign' => [
'type' => 'string',
'description' => "the user's starsign"
],
'hemisphere' => [
'type' => 'string',
'description' => "the user's hemisphere"
],
'getLatitude' => [
'type' => 'javascript',
'description' => "JavaScript used to get a user's latitude"
]
];

// The processInternal function is the core working of a FlowElement.
// It takes FlowData, reads evidence and returns data.
public function processInternal($FlowData)
public function processInternal($flowData)
{
$result = [];


// Get the date of birth from the query string (submitted through
// a form on the client side)
$dateOfBirth = $FlowData->evidence->get("query.dateOfBirth");
$dateOfBirth = $flowData->evidence->get('query.dateOfBirth');

if ($dateOfBirth) {
$dateOfBirth = explode("-", $dateOfBirth);
$dateOfBirth = explode('-', $dateOfBirth);

$month = $dateOfBirth[1];
$day = $dateOfBirth[2];


$result["starSign"] = getStarSign($month, $day);
$result['starSign'] = getStarSign($month, $day);
}

// Serve some JavaScript to the user that will be used to save
// a cookie with the user's latitude in it
$result["getLatitude"] = "navigator.geolocation.getCurrentPosition(function(position) {
document.cookie = \"latitude=\" + position.coords.latitude;
$result['getLatitude'] = 'navigator.geolocation.getCurrentPosition(function(position) {
document.cookie = "latitude=" + position.coords.latitude;
loadHemisphere();
});";
});';

// Get the latitude from the above cookie
$latitude = $FlowData->evidence->get("cookie.latitude");
$latitude = $flowData->evidence->get('cookie.latitude');

// Calculate the hemisphere
if ($latitude) {
$result["hemisphere"] = $latitude > 0 ? "Northern" : "Southern";
$result['hemisphere'] = $latitude > 0 ? 'Northern' : 'Southern';
}


$data = new ElementDataDictionary($this, $result);

$FlowData->setElementData($data);
$flowData->setElementData($data);
}

public $properties = array(
"starSign" => array(
"type" => "string",
"description" => "the user's starsign"
),
"hemisphere" => array(
"type" => "string",
"description" => "the user's hemisphere"
),
"getLatitude" => array(
"type" => "javascript",
"description" => "JavaScript used to get a user's latitude"
)
);

public function getEvidenceKeyFilter()
{

// A filter (in this case a basic list) stating which evidence
// the FlowElement is interested in
return new BasicListEvidenceKeyFilter(["cookie.latitude", "query.dateOfBirth"]);
return new BasicListEvidenceKeyFilter(['cookie.latitude', 'query.dateOfBirth']);
}
}

//! [class]
//! [usage]

// Add some callback settings for the page to make a request with extra evidence from the client side, in this case the same url with an extra query string.

$javascriptBuilderSettings = array(
"host" => "localhost:3000",
"protocol" => "http",
"endpoint" => "/?json"
);
// Add some callback settings for the page to make a request with extra evidence from the client side
// in this case the same url with an extra query string.
$javascriptBuilderSettings = [
'host' => 'localhost:3000',
'protocol' => 'http',
'endpoint' => '/?json'
];

// Make the Pipeline and add the element we want to it
$Pipeline = (new PipelineBuilder(['javascriptBuilderSettings' => $javascriptBuilderSettings]))
->add(new AstrologyFlowElement())
->build();

$Pipeline = (new PipelineBuilder(["javascriptBuilderSettings"=>$javascriptBuilderSettings]))->add(new AstrologyFlowElement())->build();

$FlowData = $Pipeline->createFlowData();
$flowData = $Pipeline->createFlowData();

// Add any information from the request (headers, cookies and additional
// client side provided information)

$FlowData->evidence->setFromWebRequest();
$flowData->evidence->setFromWebRequest();

// Process the FlowData

$FlowData->process();
$flowData->process();

// The client side JavaScript calls back to this page

if (isset($_GET["json"])) {
header("Content-Type: application/json; charset=UTF-8");
echo json_encode($FlowData->jsonbundler->json);
if (isset($_GET['json'])) {
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($flowData->jsonbundler->json);

return;
}

// Generate the HTML for the form that gets a user's starsign
$output = '';

$output = "";

$output .= "<h1>Starsign</h1>";

$output .= '<h1>Starsign</h1>';
$output .= "<form><label for='dateOfBirth'>Date of birth</label><input type='date' name='dateOfBirth' id='dateOfBirth'><input type='submit'></form>";

// Add the results if they're available


if ($FlowData->astrology->starSign) {
$output .= "<p>Your starsign is " . $FlowData->astrology->starSign . "</p>";
if ($flowData->astrology->starSign) {
$output .= '<p>Your starsign is ' . $flowData->astrology->starSign . '</p>';
}

$output .= "<div id='hemispheretext'>";

if ($FlowData->astrology->hemisphere) {
$output .= "<p>Look at the " . $FlowData->astrology->hemisphere . " hemisphere stars tonight!</p>";
if ($flowData->astrology->hemisphere) {
$output .= '<p>Look at the ' . $flowData->astrology->hemisphere . ' hemisphere stars tonight!</p>';
}

$output .= "</div>";

$output .= "<script>";
$output .= '</div>';
$output .= '<script>';

// This function will fire when the JSON data object is updated
// with information from the server.
Expand All @@ -214,7 +207,7 @@ public function getEvidenceKeyFilter()
// 3. The web server responds with new JSON data that contains the hemisphere based on the location.
// 4. The JavaScript integrates the new JSON data and fires the onChange callback below.

$output .= $FlowData->javascriptbuilder->javascript;
$output .= $flowData->javascriptbuilder->javascript;

$output .= 'loadHemisphere = function() {
fod.complete(function (data) {
Expand All @@ -234,9 +227,7 @@ public function getEvidenceKeyFilter()
}
})};';

$output .= "</script>";
$output .= '</script>';

// Return the full output to the page

echo $output;
//! [usage]
Loading

0 comments on commit c46e938

Please sign in to comment.