Skip to content

Instantly share code, notes, and snippets.

View mannion007's full-sized avatar

James Mannion mannion007

View GitHub Profile
@mannion007
mannion007 / iterator_aggregate.php
Last active September 23, 2016 20:42
Example use of IteratorAggregate interface to make a private array within the class iterable. Featuring gaming consoles because why not.
<?php
/**
* A class which contains a private list of gaming consoles.
* Can be looped as the class implements IteratorAggregate
*
* Class ConsoleCollection
*/
class ConsoleCollection implements \IteratorAggregate
{
<?php
/**
* @author James Mannion <mannion007@gmail.com>
* @link https://www.jamse.net
*/
/**
* Interface CarInterface
*/
interface CarInterface
{
@mannion007
mannion007 / example.php
Last active October 8, 2016 16:05
Surprising behaviour of try/catch/finally block with return statements
<?php
function exceptionThrower()
{
try {
throw new \Exception('Bang!');
} catch(\Exception $e) {
echo 'An exception happened but I caught it!' . PHP_EOL;
return 5;
} finally {
<?php
/** ...Make the request, decode the response */
/** Check scan reference is present before we can use it */
if (!isset($decodedResponse->scanReference)) {
throw new \Exception("Invalid response - Scan Reference is missing");
}
if (!is_string($decodedResponse->scanReference)) {
@mannion007
mannion007 / gist:edf733d5fe2a9310613df515648606a6
Last active November 12, 2019 17:20
Validating against a schema at runtime
<?php
/** ...Make the request, decode the response */
$validator->validate($decodedResponse, json_decode('details.json'));
if (!empty($validator->getErrors())) {
throw new InvalidResponseException(
sprintf('Invalid Net Verify response for [%s]: [%s]', $type, implode(',', $errorMessages))
);
@mannion007
mannion007 / main.go
Created February 26, 2020 23:23
SQS receive and delete
package main
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
)
@mannion007
mannion007 / race.go
Created February 27, 2020 21:38
Go Race condition
package main
import (
"fmt"
"runtime"
"sync"
)
func main() {
var counter int
@mannion007
mannion007 / main.go
Created February 27, 2020 23:15
Select statement for channel
package main
import (
"fmt"
)
func main() {
q := make(chan bool)
c := gen(q)
receive(c, q)
@mannion007
mannion007 / main.go
Created February 28, 2020 10:18
Fan in
package main
import (
"fmt"
"time"
"sync"
)
func main() {
@mannion007
mannion007 / main.go
Created February 29, 2020 17:16
Channel select
package main
import (
"fmt"
"time"
)
func main() {
slowChan := make(chan string)