-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |