Skip to content

Commit

Permalink
add initial codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed Jun 4, 2014
1 parent 8ed5f3b commit 705dc6a
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/Filterable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Sulphur;

/**
* @author Benedict Etzel <[email protected]>
*/
class Filterable {

private $fields;

public function __construct($fields) {
$this->fields = $fields;
}

public function is($field, $value) {
if(isset($this->fields[$field])) {
return $this->fields[$field] == $value;
}
return false;
}

public function isNot($field, $value) {
return !$this->is($field, $value);
}

public function contains($field, $needle, $ignore_case = false) {
if(isset($this->fields[$field])) {
if($ignore_case) {
return stripos($this->fields[$field], $needle) !== false;
}
return strpos($this->fields[$field], $needle) !== false;
}
return false;
}

public function doesNotContain($field, $needle, $ignore_case = false) {
return !$this->contains($field, $value);
}

public function matches($field, $expression) {
if(isset($this->fields[$field])) {
return preg_match($expression, $this->fields[$field]);
}
return false;
}

public function doesNotMatch($field, $expression) {
return !$this->matches($field, $expression);
}

public function exists($field) {
return isset($this->fields[$field]);
}

public function doesNotExist($field) {
return !$this->exists($field);
}

public function __get($name) {
if(isset($this->fields[$name])) {
return $this->fields[$name];
}
}

}
62 changes: 62 additions & 0 deletions src/FilterableList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Sulphur;

/**
* @author Benedict Etzel <[email protected]>
*/
class FilterableList implements \IteratorAggregate, \ArrayAccess, \Countable {

private $elements;
private $field;

public function __construct($elements, $field) {
$this->elements = $elements;
$this->field = $field;
}

public function __call($name, $arguments) {
array_unshift($arguments, $this->field);
$result = array();
foreach($this->elements as $element) {
if(call_user_func_array(array($element, $name), $arguments)) {
$result[] = $element;
}
}
return new FilterableList($result, $this->field);
}

public function where($field) {
return new FilterableList($this->elements, $field);
//$this->field = $field;
}

public function getIterator() {
return new \ArrayIterator($this->elements);
}

public function count() {
return count($this->elements);
}

public function offsetSet($offset, $value) {
if(is_null($offset)) {
$this->elements[] = $value;
} else {
$this->elements[$offset] = $value;
}
}

public function offsetExists($offset) {
return isset($this->elements[$offset]);
}

public function offsetUnset($offset) {
unset($this->elements[$offset]);
}

public function offsetGet($offset) {
return isset($this->elements[$offset]) ? $this->elements[$offset] : null;
}

}
57 changes: 57 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Sulphur;

/**
* @author Benedict Etzel <[email protected]>
*/
class Response {

private $references = array();

public function __construct($response) {

$response = str_replace(array("\r\n", "\r"), "\n", $response);
$response = preg_replace("/\n{2,}/", "\n", $response);

$lines = explode("\n", $response);

$reference = null;
$heading = true;
foreach($lines as $line) {
if(!$line) {
continue;
}
$matches = array();
if(preg_match('/^\[(.*)\]$/', $line, $matches)) {
switch($matches[1]) {
case 'Reference':
$heading = false;
if($reference) {
$this->addReference($reference);
}
$reference = array();
break;
}
} else {
if(preg_match('/^(.*)=(.*)$/', $line, $matches)) {
if(!$heading) {
$reference[$matches[1]] = $matches[2];
}
}
}
}
if($reference) {
$this->addReference($reference);
}
}

private function addReference($fields) {
$this->references[] = new Filterable($fields);
}

public function where($field) {
return new FilterableList($this->references, $field);
}

}
36 changes: 36 additions & 0 deletions src/ResponseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Sulphur;

/**
* @author Benedict Etzel <[email protected]>
*/
class ResponseFactory {

/**
* Creates a response corresponding to the references from url.
* @param string $url
* @return \Sulphur\Response
*/
public static function fromUrl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, __NAMESPACE__.'/1.0');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$string = curl_exec($ch);
curl_close($ch);

return self::fromString($string);
}

/**
* Creates a response corresponding to the references passed via string.
* @param string $string
* @return \Sulphur\Response
*/
public static function fromString($string) {
return new Response($string);
}

}

0 comments on commit 705dc6a

Please sign in to comment.