@@ -2,10 +2,10 @@ |
||
| 2 | 2 | namespace BOTK\Core; |
| 3 | 3 | |
| 4 | 4 | use \InvalidArgumentException; |
| 5 | -use Respect\Rest\Router, |
|
| 6 | - Respect\Rest\Request; |
|
| 7 | -use BOTK\Core\Models\HttpProblem, |
|
| 8 | - BOTK\Core\Representations\Error; |
|
| 5 | +use Respect\Rest\Router; |
|
| 6 | +use Respect\Rest\Request; |
|
| 7 | +use BOTK\Core\Models\HttpProblem; |
|
| 8 | +use BOTK\Core\Representations\Error; |
|
| 9 | 9 | |
| 10 | 10 | |
| 11 | 11 | |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | protected $endPointPath=null; //lazy initialization |
| 54 | 54 | protected $endPointName=null; |
| 55 | 55 | |
| 56 | - // Hooks to build resusable end-poins |
|
| 56 | + // Hooks to build resusable end-poins |
|
| 57 | 57 | protected function setRoutes() {} |
| 58 | 58 | |
| 59 | 59 | |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | // |
| 95 | 95 | // $endPointPath: /service |
| 96 | 96 | $endPointPath = dirname($scriptName); |
| 97 | - } elseif (!empty($_SERVER['PATH_INFO']) && $pos = strpos($requestURI,$_SERVER['PATH_INFO'])) { |
|
| 97 | + } elseif (!empty($_SERVER['PATH_INFO']) && $pos = strpos($requestURI,$_SERVER['PATH_INFO'])) { |
|
| 98 | 98 | // Complex rewriting roule. Try extract application path using PATH_INFO variable |
| 99 | 99 | // $_SERVER['REQUEST_URI']: /service/v1/hello/to/world?query_string#fragment |
| 100 | 100 | // $_SERVER['SCRIPT_NAME']: /serviceV1/index.php |
@@ -186,7 +186,7 @@ discard block |
||
| 186 | 186 | //Warning this trigger is called only in php >5.4. Otherwhise just empty content is printed |
| 187 | 187 | //(but original status is preserved) |
| 188 | 188 | if (empty($result) && ($errorCode=Http::getHttpResponseCode()) >= 400) { |
| 189 | - $result = ErrorManager::getInstance()->serializeHttpProblem(new HttpProblem($errorCode)); |
|
| 189 | + $result = ErrorManager::getInstance()->serializeHttpProblem(new HttpProblem($errorCode)); |
|
| 190 | 190 | } |
| 191 | 191 | return $result; |
| 192 | 192 | } |
@@ -49,9 +49,9 @@ discard block |
||
| 49 | 49 | |
| 50 | 50 | class EndPoint extends Router |
| 51 | 51 | { |
| 52 | - protected $endPointCatalog=array(); |
|
| 53 | - protected $endPointPath=null; //lazy initialization |
|
| 54 | - protected $endPointName=null; |
|
| 52 | + protected $endPointCatalog = array(); |
|
| 53 | + protected $endPointPath = null; //lazy initialization |
|
| 54 | + protected $endPointName = null; |
|
| 55 | 55 | |
| 56 | 56 | // Hooks to build resusable end-poins |
| 57 | 57 | protected function setRoutes() {} |
@@ -94,17 +94,17 @@ discard block |
||
| 94 | 94 | // |
| 95 | 95 | // $endPointPath: /service |
| 96 | 96 | $endPointPath = dirname($scriptName); |
| 97 | - } elseif (!empty($_SERVER['PATH_INFO']) && $pos = strpos($requestURI,$_SERVER['PATH_INFO'])) { |
|
| 97 | + } elseif (!empty($_SERVER['PATH_INFO']) && $pos = strpos($requestURI, $_SERVER['PATH_INFO'])) { |
|
| 98 | 98 | // Complex rewriting roule. Try extract application path using PATH_INFO variable |
| 99 | 99 | // $_SERVER['REQUEST_URI']: /service/v1/hello/to/world?query_string#fragment |
| 100 | 100 | // $_SERVER['SCRIPT_NAME']: /serviceV1/index.php |
| 101 | 101 | // $_SERVER['PATH_INFO']: /hello/to/world |
| 102 | 102 | // |
| 103 | 103 | // $endPointPath: /service/v1 |
| 104 | - $endPointPath = substr($requestURI,0,$pos); |
|
| 104 | + $endPointPath = substr($requestURI, 0, $pos); |
|
| 105 | 105 | } else { |
| 106 | - throw new HttpErrorException( HttpProblem::factory( |
|
| 107 | - 404,'End-point not found',"Unable autodetecting application path: URI=$requestURI.")); |
|
| 106 | + throw new HttpErrorException(HttpProblem::factory( |
|
| 107 | + 404, 'End-point not found', "Unable autodetecting application path: URI=$requestURI.")); |
|
| 108 | 108 | } |
| 109 | 109 | $this->endPointPath = $endPointPath; |
| 110 | 110 | } |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | if (is_null($this->endPointName)) { |
| 122 | 122 | $endPointPath = $this->getEndPointPath(); |
| 123 | 123 | $pathinfo = substr($_SERVER['REQUEST_URI'], strlen($endPointPath)); |
| 124 | - $haystack = $pathinfo . '/'; |
|
| 124 | + $haystack = $pathinfo.'/'; |
|
| 125 | 125 | $this->endPointName = preg_match('@/([a-z0-9]+)[/?#]@', $haystack, $matches) |
| 126 | 126 | ? $matches[1] |
| 127 | 127 | : ''; |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | |
| 134 | 134 | public function __construct($virtualhost = null) |
| 135 | 135 | { |
| 136 | - if (is_null($virtualhost)) $virtualhost=$this->getEndPointPath(); |
|
| 136 | + if (is_null($virtualhost)) $virtualhost = $this->getEndPointPath(); |
|
| 137 | 137 | |
| 138 | 138 | // Anti-Pattens |
| 139 | 139 | // allow ?_output=MIMETYPE on the URI to override HTTP accept header requests. |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | /** |
| 157 | 157 | * Redefine run taking into account mountend end points |
| 158 | 158 | */ |
| 159 | - public function run(Request $request=null) |
|
| 159 | + public function run(Request $request = null) |
|
| 160 | 160 | { |
| 161 | 161 | $endPointName = $this->getEndPointName(); |
| 162 | 162 | $applicationPath = $this->getEndPointPath(); |
@@ -165,27 +165,27 @@ discard block |
||
| 165 | 165 | ? $this->endPointCatalog[$endPointName] |
| 166 | 166 | : ''; |
| 167 | 167 | |
| 168 | - $virtualhost = empty($endPointName)?$applicationPath:($applicationPath.'/'.$endPointName); |
|
| 168 | + $virtualhost = empty($endPointName) ? $applicationPath : ($applicationPath.'/'.$endPointName); |
|
| 169 | 169 | |
| 170 | - if ( !$routerClass ) { |
|
| 170 | + if (!$routerClass) { |
|
| 171 | 171 | $result = parent::run($request); //fall back to local application routing |
| 172 | 172 | } else { |
| 173 | 173 | // now we test that $routerclass is a valid end_point |
| 174 | 174 | $myClass = get_class(); |
| 175 | - if ($routerClass ==$myClass || is_subclass_of($routerClass, $myClass)){ |
|
| 175 | + if ($routerClass == $myClass || is_subclass_of($routerClass, $myClass)) { |
|
| 176 | 176 | // Create new end-point |
| 177 | 177 | $endpoint = new $routerClass($virtualhost); |
| 178 | 178 | $result = $endpoint->run(); |
| 179 | 179 | } else { |
| 180 | 180 | throw new HttpErrorException(HttpProblem::factory( |
| 181 | - 500, 'Invalid endpoint', $routerClass.' end point class is not a subClass of '. $myClass)); |
|
| 181 | + 500, 'Invalid endpoint', $routerClass.' end point class is not a subClass of '.$myClass)); |
|
| 182 | 182 | } |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | //now prepare an error report for humans when something went wrong |
| 186 | 186 | //Warning this trigger is called only in php >5.4. Otherwhise just empty content is printed |
| 187 | 187 | //(but original status is preserved) |
| 188 | - if (empty($result) && ($errorCode=Http::getHttpResponseCode()) >= 400) { |
|
| 188 | + if (empty($result) && ($errorCode = Http::getHttpResponseCode()) >= 400) { |
|
| 189 | 189 | $result = ErrorManager::getInstance()->serializeHttpProblem(new HttpProblem($errorCode)); |
| 190 | 190 | } |
| 191 | 191 | return $result; |
@@ -202,12 +202,12 @@ discard block |
||
| 202 | 202 | * See allo cachin at controller level (i.e. caching at resource level) |
| 203 | 203 | *************************************************************************/ |
| 204 | 204 | |
| 205 | - public function representationCachingProcessor($method=Caching::NO) |
|
| 205 | + public function representationCachingProcessor($method = Caching::NO) |
|
| 206 | 206 | { |
| 207 | 207 | $cachingClass = '\\BOTK\\Core\\Caching'; |
| 208 | 208 | |
| 209 | - if ( !method_exists($cachingClass, $method)) $method =Caching::NO; |
|
| 210 | - return array( $cachingClass, $method); |
|
| 209 | + if (!method_exists($cachingClass, $method)) $method = Caching::NO; |
|
| 210 | + return array($cachingClass, $method); |
|
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | } |
@@ -133,7 +133,9 @@ discard block |
||
| 133 | 133 | |
| 134 | 134 | public function __construct($virtualhost = null) |
| 135 | 135 | { |
| 136 | - if (is_null($virtualhost)) $virtualhost=$this->getEndPointPath(); |
|
| 136 | + if (is_null($virtualhost)) { |
|
| 137 | + $virtualhost=$this->getEndPointPath(); |
|
| 138 | + } |
|
| 137 | 139 | |
| 138 | 140 | // Anti-Pattens |
| 139 | 141 | // allow ?_output=MIMETYPE on the URI to override HTTP accept header requests. |
@@ -206,7 +208,9 @@ discard block |
||
| 206 | 208 | { |
| 207 | 209 | $cachingClass = '\\BOTK\\Core\\Caching'; |
| 208 | 210 | |
| 209 | - if ( !method_exists($cachingClass, $method)) $method =Caching::NO; |
|
| 211 | + if ( !method_exists($cachingClass, $method)) { |
|
| 212 | + $method =Caching::NO; |
|
| 213 | + } |
|
| 210 | 214 | return array( $cachingClass, $method); |
| 211 | 215 | } |
| 212 | 216 | |
@@ -65,6 +65,10 @@ |
||
| 65 | 65 | /************************************************************************* |
| 66 | 66 | * Parsing of response Header |
| 67 | 67 | *************************************************************************/ |
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @param string $name |
|
| 71 | + */ |
|
| 68 | 72 | public static function getResponseHeader($name) |
| 69 | 73 | { |
| 70 | 74 | $sentHeaders = headers_list(); |
@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | class Http |
| 8 | - { |
|
| 8 | + { |
|
| 9 | 9 | public static $STATUS_CODES = array(100 => "Continue", 101 => "Switching Protocols", 102 => "Processing", |
| 10 | 10 | 200 => "OK", 201 => "Created", 202 => "Accepted", 203 => "Non-Authoritative Information", |
| 11 | 11 | 204 => "No Content", 205 => "Reset Content", 206 => "Partial Content", 207 => "Multi-Status", |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | $GLOBALS['http_response_code'] = $statusCode; // no other way to simulate http_response_code() |
| 44 | 44 | header($protocol . ' ' . $statusCode . ' ' . $text); |
| 45 | 45 | } else { |
| 46 | - http_response_code($statusCode); |
|
| 46 | + http_response_code($statusCode); |
|
| 47 | 47 | } |
| 48 | 48 | } |
| 49 | 49 | |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | ? $GLOBALS['http_response_code'] |
| 57 | 57 | : 200; |
| 58 | 58 | } else { |
| 59 | - $statusCode=http_response_code(); |
|
| 59 | + $statusCode=http_response_code(); |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | return $statusCode; |
@@ -109,8 +109,8 @@ discard block |
||
| 109 | 109 | $meta = stream_get_meta_data($data); |
| 110 | 110 | for ($j = 0; isset($meta['wrapper_data'][$j]); $j++){ |
| 111 | 111 | if (strstr(strtolower($meta['wrapper_data'][$j]), 'etag')){ |
| 112 | - $etag = str_replace('"', '', trim(substr($meta['wrapper_data'][$j], 6))); |
|
| 113 | - break; |
|
| 112 | + $etag = str_replace('"', '', trim(substr($meta['wrapper_data'][$j], 6))); |
|
| 113 | + break; |
|
| 114 | 114 | } |
| 115 | 115 | } |
| 116 | 116 | } elseif (is_object($data) && method_exists($data, 'getEtag')){ |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | { |
| 139 | 139 | // Skypp if already sent header... |
| 140 | 140 | if ( $lastModifiedOnHeaderVal=self::getResponseHeader('Last-Modified')) { |
| 141 | - return $lastModifiedOn = new \DateTime(trim($lastModifiedOnHeaderVal)); |
|
| 141 | + return $lastModifiedOn = new \DateTime(trim($lastModifiedOnHeaderVal)); |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | //if $data is a resource try to get creation date from resource metadata |
@@ -154,9 +154,9 @@ discard block |
||
| 154 | 154 | |
| 155 | 155 | for ($j = 0; isset($meta['wrapper_data'][$j]); $j++){ |
| 156 | 156 | if (strstr(strtolower($meta['wrapper_data'][$j]), 'last-modified')){ |
| 157 | - $modtime = substr($meta['wrapper_data'][$j], 15); |
|
| 158 | - $lastModifiedOn = new \DateTime($modtime); |
|
| 159 | - break; |
|
| 157 | + $modtime = substr($meta['wrapper_data'][$j], 15); |
|
| 158 | + $lastModifiedOn = new \DateTime($modtime); |
|
| 159 | + break; |
|
| 160 | 160 | } |
| 161 | 161 | } |
| 162 | 162 | } |
@@ -169,4 +169,4 @@ discard block |
||
| 169 | 169 | |
| 170 | 170 | return $lastModifiedOn; |
| 171 | 171 | } |
| 172 | - } |
|
| 172 | + } |
|
@@ -36,12 +36,12 @@ discard block |
||
| 36 | 36 | public static function setHttpResponseCode($statusCode) |
| 37 | 37 | { |
| 38 | 38 | if (version_compare(phpversion(), '5.4', '<')) { |
| 39 | - $text = ($statusCode && isset( $httpStatusCodes[$statusCode])) |
|
| 39 | + $text = ($statusCode && isset($httpStatusCodes[$statusCode])) |
|
| 40 | 40 | ? Http::$STATUS_CODES[$statusCode] |
| 41 | 41 | : ''; |
| 42 | 42 | $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'); |
| 43 | 43 | $GLOBALS['http_response_code'] = $statusCode; // no other way to simulate http_response_code() |
| 44 | - header($protocol . ' ' . $statusCode . ' ' . $text); |
|
| 44 | + header($protocol.' '.$statusCode.' '.$text); |
|
| 45 | 45 | } else { |
| 46 | 46 | http_response_code($statusCode); |
| 47 | 47 | } |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | ? $GLOBALS['http_response_code'] |
| 57 | 57 | : 200; |
| 58 | 58 | } else { |
| 59 | - $statusCode=http_response_code(); |
|
| 59 | + $statusCode = http_response_code(); |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | return $statusCode; |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | { |
| 70 | 70 | $sentHeaders = headers_list(); |
| 71 | 71 | foreach ($sentHeaders as $sentHeader) { |
| 72 | - if (preg_match("/$name:(.+)/i", $sentHeader,$matches)){ |
|
| 72 | + if (preg_match("/$name:(.+)/i", $sentHeader, $matches)) { |
|
| 73 | 73 | return trim($matches[1]); |
| 74 | 74 | } |
| 75 | 75 | } |
@@ -78,18 +78,18 @@ discard block |
||
| 78 | 78 | |
| 79 | 79 | public static function getETagResponse() |
| 80 | 80 | { |
| 81 | - return preg_match('/"(.*)"/', self::getResponseHeader('ETag'),$matches)?trim($matches[1]):''; |
|
| 81 | + return preg_match('/"(.*)"/', self::getResponseHeader('ETag'), $matches) ? trim($matches[1]) : ''; |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | |
| 85 | 85 | public static function getResponseLinks() |
| 86 | 86 | { |
| 87 | 87 | $sentHeaders = headers_list(); |
| 88 | - $result=array(); |
|
| 88 | + $result = array(); |
|
| 89 | 89 | foreach ($sentHeaders as $sentHeader) { |
| 90 | - if (strpos($sentHeader, 'Link:')===0){ |
|
| 90 | + if (strpos($sentHeader, 'Link:') === 0) { |
|
| 91 | 91 | $l = new WebLink; |
| 92 | - $result[] =$l->httpParse($sentHeader); |
|
| 92 | + $result[] = $l->httpParse($sentHeader); |
|
| 93 | 93 | } |
| 94 | 94 | } |
| 95 | 95 | return $result; |
@@ -99,21 +99,21 @@ discard block |
||
| 99 | 99 | /** |
| 100 | 100 | * Override ETag with a new calculated value from $data |
| 101 | 101 | */ |
| 102 | - public static function setETagHeader($data,$etag=null) |
|
| 102 | + public static function setETagHeader($data, $etag = null) |
|
| 103 | 103 | { |
| 104 | 104 | // Recaluclate ETag overridin existent one Use Weak validation |
| 105 | 105 | if (is_null($etag)) { |
| 106 | - if (is_resource($data)){ |
|
| 106 | + if (is_resource($data)) { |
|
| 107 | 107 | $etag = uniqid(); |
| 108 | 108 | //try http wrapper to get a better value, |
| 109 | 109 | $meta = stream_get_meta_data($data); |
| 110 | - for ($j = 0; isset($meta['wrapper_data'][$j]); $j++){ |
|
| 111 | - if (strstr(strtolower($meta['wrapper_data'][$j]), 'etag')){ |
|
| 110 | + for ($j = 0; isset($meta['wrapper_data'][$j]); $j++) { |
|
| 111 | + if (strstr(strtolower($meta['wrapper_data'][$j]), 'etag')) { |
|
| 112 | 112 | $etag = str_replace('"', '', trim(substr($meta['wrapper_data'][$j], 6))); |
| 113 | 113 | break; |
| 114 | 114 | } |
| 115 | 115 | } |
| 116 | - } elseif (is_object($data) && method_exists($data, 'getEtag')){ |
|
| 116 | + } elseif (is_object($data) && method_exists($data, 'getEtag')) { |
|
| 117 | 117 | $etag = $data->getEtag(); |
| 118 | 118 | } else { |
| 119 | 119 | $etag = md5(serialize($data)); |
@@ -121,39 +121,39 @@ discard block |
||
| 121 | 121 | |
| 122 | 122 | } |
| 123 | 123 | // take into account ETags alread sent: |
| 124 | - $oldETag=self::getETagResponse(); |
|
| 125 | - if($oldETag){ |
|
| 126 | - $etag= "\"$oldETag/$etag\""; |
|
| 124 | + $oldETag = self::getETagResponse(); |
|
| 125 | + if ($oldETag) { |
|
| 126 | + $etag = "\"$oldETag/$etag\""; |
|
| 127 | 127 | } else { |
| 128 | - $etag= "\"$etag\""; |
|
| 128 | + $etag = "\"$etag\""; |
|
| 129 | 129 | } |
| 130 | - header ('ETag: '.$etag); // use Weak for semantically equivalent value |
|
| 130 | + header('ETag: '.$etag); // use Weak for semantically equivalent value |
|
| 131 | 131 | return $etag; |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | /** |
| 135 | 135 | * send Last-Modified header if not already set |
| 136 | 136 | */ |
| 137 | - public static function setLastModifiedHeader($data,\DateTime $defaultDate) |
|
| 137 | + public static function setLastModifiedHeader($data, \DateTime $defaultDate) |
|
| 138 | 138 | { |
| 139 | 139 | // Skypp if already sent header... |
| 140 | - if ( $lastModifiedOnHeaderVal=self::getResponseHeader('Last-Modified')) { |
|
| 141 | - return $lastModifiedOn = new \DateTime(trim($lastModifiedOnHeaderVal)); |
|
| 140 | + if ($lastModifiedOnHeaderVal = self::getResponseHeader('Last-Modified')) { |
|
| 141 | + return $lastModifiedOn = new \DateTime(trim($lastModifiedOnHeaderVal)); |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | //if $data is a resource try to get creation date from resource metadata |
| 145 | - if ( is_resource($data)){ |
|
| 145 | + if (is_resource($data)) { |
|
| 146 | 146 | // try fstat first |
| 147 | 147 | $fstat = @fstat($data); |
| 148 | - if (!empty($fstat)){ |
|
| 149 | - $lastModifiedOn = new \DateTime(date('r',$fstat['mtime'])); |
|
| 148 | + if (!empty($fstat)) { |
|
| 149 | + $lastModifiedOn = new \DateTime(date('r', $fstat['mtime'])); |
|
| 150 | 150 | } else { |
| 151 | 151 | //try http wrapper |
| 152 | 152 | $meta = stream_get_meta_data($data); |
| 153 | 153 | $lastModifiedOn = $defaultDate; |
| 154 | 154 | |
| 155 | - for ($j = 0; isset($meta['wrapper_data'][$j]); $j++){ |
|
| 156 | - if (strstr(strtolower($meta['wrapper_data'][$j]), 'last-modified')){ |
|
| 155 | + for ($j = 0; isset($meta['wrapper_data'][$j]); $j++) { |
|
| 156 | + if (strstr(strtolower($meta['wrapper_data'][$j]), 'last-modified')) { |
|
| 157 | 157 | $modtime = substr($meta['wrapper_data'][$j], 15); |
| 158 | 158 | $lastModifiedOn = new \DateTime($modtime); |
| 159 | 159 | break; |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | } elseif (is_object($data) && method_exists($data, 'getLastModifiedDate')) { |
| 164 | 164 | $lastModifiedOn = $data->getLastModifiedDate(); |
| 165 | 165 | } else { |
| 166 | - $lastModifiedOn = $defaultDate; |
|
| 166 | + $lastModifiedOn = $defaultDate; |
|
| 167 | 167 | } |
| 168 | 168 | header('Last-Modified: '.$lastModifiedOn->format(\DateTime::RFC2822)); |
| 169 | 169 | |
@@ -99,6 +99,7 @@ discard block |
||
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | 101 | * An helper to setup content type and alternate web link headers |
| 102 | + * @param string $contentType |
|
| 102 | 103 | */ |
| 103 | 104 | public static function setContentType($contentType, $policyClass = null) |
| 104 | 105 | { |
@@ -171,6 +172,7 @@ discard block |
||
| 171 | 172 | * if $useCustomFields use an xml representation for fields.. Not yet standarized in html5 |
| 172 | 173 | * |
| 173 | 174 | * This render use html5 semantic tagging. |
| 175 | + * @param boolean $dataIsHtmlFragment |
|
| 174 | 176 | */ |
| 175 | 177 | public static function htmlSerializer($data, |
| 176 | 178 | $meta = null, |
@@ -245,6 +247,7 @@ discard block |
||
| 245 | 247 | /** |
| 246 | 248 | * an super simple template engine! |
| 247 | 249 | * |
| 250 | + * @param string $template |
|
| 248 | 251 | */ |
| 249 | 252 | public static function templateSerializer($data, $template) { |
| 250 | 253 | if(ob_start()){ |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | */ |
| 24 | 24 | abstract class AbstractContentNegotiationPolicy |
| 25 | 25 | { |
| 26 | - // redefine this to change the list of supported response representation |
|
| 26 | + // redefine this to change the list of supported response representation |
|
| 27 | 27 | protected static $renderers = array(); |
| 28 | 28 | protected static $parsers = array(); |
| 29 | 29 | protected static $translators = array(); |
@@ -144,8 +144,8 @@ discard block |
||
| 144 | 144 | */ |
| 145 | 145 | public static function htmlWebLinks(array $links) { |
| 146 | 146 | $html = "<dl>\n"; |
| 147 | - $alternateLinks = array(); |
|
| 148 | - foreach($links as $link ){ |
|
| 147 | + $alternateLinks = array(); |
|
| 148 | + foreach($links as $link ){ |
|
| 149 | 149 | $linkName = $link->rel?$link->rel:'Link'; |
| 150 | 150 | $href=$link->href; |
| 151 | 151 | if(0===strcasecmp($linkName,'alternate')){ |
@@ -157,10 +157,10 @@ discard block |
||
| 157 | 157 | } else { |
| 158 | 158 | $html .= "<dt>$linkName:</dt><dd><a href='$href'>$href</a></dd>\n"; |
| 159 | 159 | } |
| 160 | - } |
|
| 161 | - if (count($alternateLinks)){ |
|
| 160 | + } |
|
| 161 | + if (count($alternateLinks)){ |
|
| 162 | 162 | $html .= "<dt>alternate:</dt><dd>".implode(', ',$alternateLinks)."</dd>\n"; |
| 163 | - } |
|
| 163 | + } |
|
| 164 | 164 | $html .= "</dl>\n"; |
| 165 | 165 | |
| 166 | 166 | return $html; |
@@ -205,7 +205,7 @@ discard block |
||
| 205 | 205 | // prepare head |
| 206 | 206 | $head = implode("\n",$metadata); |
| 207 | 207 | |
| 208 | - // Prepare data content |
|
| 208 | + // Prepare data content |
|
| 209 | 209 | $text = is_scalar($data) |
| 210 | 210 | ? ((string)$data) |
| 211 | 211 | : var_export($data, true); |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | protected static function getPolicies($index) |
| 36 | 36 | { |
| 37 | 37 | $result = array(); |
| 38 | - $thisClassName= get_called_class(); // late binging from PHP > 5.3 |
|
| 38 | + $thisClassName = get_called_class(); // late binging from PHP > 5.3 |
|
| 39 | 39 | foreach ($index as $key=>$function) { |
| 40 | 40 | if (!is_callable($function)) { |
| 41 | 41 | // use late binding classname scope scope |
@@ -74,11 +74,11 @@ discard block |
||
| 74 | 74 | */ |
| 75 | 75 | public static function render($data, array $renderers) |
| 76 | 76 | { |
| 77 | - $header = array_key_exists('HTTP_ACCEPT', $_SERVER)?$_SERVER['HTTP_ACCEPT']:false; |
|
| 77 | + $header = array_key_exists('HTTP_ACCEPT', $_SERVER) ? $_SERVER['HTTP_ACCEPT'] : false; |
|
| 78 | 78 | |
| 79 | - $best_match = $header?Mimeparse::bestMatch(array_keys($renderers), $header):reset($renderers); |
|
| 79 | + $best_match = $header ? Mimeparse::bestMatch(array_keys($renderers), $header) : reset($renderers); |
|
| 80 | 80 | |
| 81 | - $renderer = $best_match?$renderers[$best_match]:'json_encode'; |
|
| 81 | + $renderer = $best_match ? $renderers[$best_match] : 'json_encode'; |
|
| 82 | 82 | //fallback to json |
| 83 | 83 | return call_user_func($renderer, $data); |
| 84 | 84 | } |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | */ |
| 94 | 94 | public static function getResourceState($data) |
| 95 | 95 | { |
| 96 | - return is_object($data)?get_object_vars($data):$data; |
|
| 96 | + return is_object($data) ? get_object_vars($data) : $data; |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | |
@@ -103,14 +103,14 @@ discard block |
||
| 103 | 103 | public static function setContentType($contentType, $policyClass = null) |
| 104 | 104 | { |
| 105 | 105 | if (is_null($policyClass)) $policyClass = get_called_class(); |
| 106 | - header('Content-Type: ' . $contentType); |
|
| 107 | - $selfUri = isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:''; |
|
| 106 | + header('Content-Type: '.$contentType); |
|
| 107 | + $selfUri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; |
|
| 108 | 108 | if ($selfUri && is_subclass_of($policyClass, __CLASS__)) { |
| 109 | 109 | $altenatives = array_keys($policyClass::$renderers); |
| 110 | - header(WebLink::factory($selfUri)->rel('self')->httpSerializer(),false); |
|
| 110 | + header(WebLink::factory($selfUri)->rel('self')->httpSerializer(), false); |
|
| 111 | 111 | foreach ($altenatives as $type) { |
| 112 | 112 | if ($contentType != $type) { |
| 113 | - header(WebLink::factory($selfUri)->rel('alternate')->type($type)->httpSerializer(),false); |
|
| 113 | + header(WebLink::factory($selfUri)->rel('alternate')->type($type)->httpSerializer(), false); |
|
| 114 | 114 | } |
| 115 | 115 | } |
| 116 | 116 | } |
@@ -129,8 +129,8 @@ discard block |
||
| 129 | 129 | */ |
| 130 | 130 | public static function htmlWebLinkSerializer(\BOTK\Core\WebLink $link) { |
| 131 | 131 | $tag = '<link href="'.$link->href.'"'; |
| 132 | - foreach (array('rel', 'type','hreflang') as $attribute) { |
|
| 133 | - if( $value = $link->$attribute) $tag .= " $attribute='$value'"; |
|
| 132 | + foreach (array('rel', 'type', 'hreflang') as $attribute) { |
|
| 133 | + if ($value = $link->$attribute) $tag .= " $attribute='$value'"; |
|
| 134 | 134 | } |
| 135 | 135 | $tag .= ' />'; |
| 136 | 136 | |
@@ -145,21 +145,21 @@ discard block |
||
| 145 | 145 | public static function htmlWebLinks(array $links) { |
| 146 | 146 | $html = "<dl>\n"; |
| 147 | 147 | $alternateLinks = array(); |
| 148 | - foreach($links as $link ){ |
|
| 149 | - $linkName = $link->rel?$link->rel:'Link'; |
|
| 150 | - $href=$link->href; |
|
| 151 | - if(0===strcasecmp($linkName,'alternate')){ |
|
| 152 | - $type = $link->type?$link->type:'unspecfified type'; |
|
| 148 | + foreach ($links as $link) { |
|
| 149 | + $linkName = $link->rel ? $link->rel : 'Link'; |
|
| 150 | + $href = $link->href; |
|
| 151 | + if (0 === strcasecmp($linkName, 'alternate')) { |
|
| 152 | + $type = $link->type ? $link->type : 'unspecfified type'; |
|
| 153 | 153 | // manage antipatter to make alternate format ccallable from browser |
| 154 | - $glue=(false===strpos($href, '?'))?'?':'&'; |
|
| 155 | - $forceOutput=$glue.'_output='.urlencode($type); |
|
| 154 | + $glue = (false === strpos($href, '?')) ? '?' : '&'; |
|
| 155 | + $forceOutput = $glue.'_output='.urlencode($type); |
|
| 156 | 156 | $alternateLinks[] = "<a href='$href{$forceOutput}'>$type</a> "; |
| 157 | 157 | } else { |
| 158 | 158 | $html .= "<dt>$linkName:</dt><dd><a href='$href'>$href</a></dd>\n"; |
| 159 | 159 | } |
| 160 | 160 | } |
| 161 | - if (count($alternateLinks)){ |
|
| 162 | - $html .= "<dt>alternate:</dt><dd>".implode(', ',$alternateLinks)."</dd>\n"; |
|
| 161 | + if (count($alternateLinks)) { |
|
| 162 | + $html .= "<dt>alternate:</dt><dd>".implode(', ', $alternateLinks)."</dd>\n"; |
|
| 163 | 163 | } |
| 164 | 164 | $html .= "</dl>\n"; |
| 165 | 165 | |
@@ -177,20 +177,20 @@ discard block |
||
| 177 | 177 | $title = null, |
| 178 | 178 | $header = null, |
| 179 | 179 | $footer = null, |
| 180 | - $dataIsHtmlFragment=null) { |
|
| 180 | + $dataIsHtmlFragment = null) { |
|
| 181 | 181 | //set defaults |
| 182 | - if (is_null($meta)) {$meta=array();} |
|
| 183 | - if (is_null($title)) {$title= is_object($data)?get_class($data):gettype($data);} |
|
| 184 | - if (is_null($header)) {$header="<h1>$title</h1>";} |
|
| 185 | - if (is_null($footer)) {$footer='';} |
|
| 186 | - if (is_null($dataIsHtmlFragment)) {$dataIsHtmlFragment=false;} |
|
| 182 | + if (is_null($meta)) {$meta = array(); } |
|
| 183 | + if (is_null($title)) {$title = is_object($data) ? get_class($data) : gettype($data); } |
|
| 184 | + if (is_null($header)) {$header = "<h1>$title</h1>"; } |
|
| 185 | + if (is_null($footer)) {$footer = ''; } |
|
| 186 | + if (is_null($dataIsHtmlFragment)) {$dataIsHtmlFragment = false; } |
|
| 187 | 187 | |
| 188 | 188 | // initilalize metadata |
| 189 | 189 | |
| 190 | 190 | $metadata = array(); |
| 191 | - if (is_string($meta) && ($meta=trim($meta))){ |
|
| 191 | + if (is_string($meta) && ($meta = trim($meta))) { |
|
| 192 | 192 | $metadata[] = "<link rel='stylesheet' type='text/css' href='$meta'/>"; |
| 193 | - } elseif( is_array($metadata)){ |
|
| 193 | + } elseif (is_array($metadata)) { |
|
| 194 | 194 | $metadata = $meta; |
| 195 | 195 | } |
| 196 | 196 | |
@@ -200,20 +200,20 @@ discard block |
||
| 200 | 200 | foreach ($links as $link) { |
| 201 | 201 | $metadata[] = self::htmlWebLinkSerializer($link); |
| 202 | 202 | } |
| 203 | - $navLinks = empty($links)?'':static::htmlWebLinks($links); |
|
| 203 | + $navLinks = empty($links) ? '' : static::htmlWebLinks($links); |
|
| 204 | 204 | |
| 205 | 205 | // prepare head |
| 206 | - $head = implode("\n",$metadata); |
|
| 206 | + $head = implode("\n", $metadata); |
|
| 207 | 207 | |
| 208 | 208 | // Prepare data content |
| 209 | 209 | $text = is_scalar($data) |
| 210 | - ? ((string)$data) |
|
| 210 | + ? ((string) $data) |
|
| 211 | 211 | : var_export($data, true); |
| 212 | - $htmlDataRepresentation = $dataIsHtmlFragment?$text:htmlspecialchars($text); |
|
| 212 | + $htmlDataRepresentation = $dataIsHtmlFragment ? $text : htmlspecialchars($text); |
|
| 213 | 213 | |
| 214 | 214 | return is_null($meta) // if meta is null render just data as html fragment |
| 215 | - ? $htmlDataRepresentation |
|
| 216 | - : ( "<!DOCTYPE html> |
|
| 215 | + ? $htmlDataRepresentation |
|
| 216 | + : ("<!DOCTYPE html> |
|
| 217 | 217 | <html> |
| 218 | 218 | <head> |
| 219 | 219 | <meta charset='utf-8'> |
@@ -247,7 +247,7 @@ discard block |
||
| 247 | 247 | * |
| 248 | 248 | */ |
| 249 | 249 | public static function templateSerializer($data, $template) { |
| 250 | - if(ob_start()){ |
|
| 250 | + if (ob_start()) { |
|
| 251 | 251 | @require $template; |
| 252 | 252 | $result = ob_get_contents(); |
| 253 | 253 | ob_end_clean(); |
@@ -42,7 +42,9 @@ discard block |
||
| 42 | 42 | $function = array($thisClassName, $function); |
| 43 | 43 | } |
| 44 | 44 | // try again to see if now is callable... |
| 45 | - if (is_callable($function)) $result[$key] = $function; |
|
| 45 | + if (is_callable($function)) { |
|
| 46 | + $result[$key] = $function; |
|
| 47 | + } |
|
| 46 | 48 | } |
| 47 | 49 | |
| 48 | 50 | return $result; |
@@ -102,7 +104,9 @@ discard block |
||
| 102 | 104 | */ |
| 103 | 105 | public static function setContentType($contentType, $policyClass = null) |
| 104 | 106 | { |
| 105 | - if (is_null($policyClass)) $policyClass = get_called_class(); |
|
| 107 | + if (is_null($policyClass)) { |
|
| 108 | + $policyClass = get_called_class(); |
|
| 109 | + } |
|
| 106 | 110 | header('Content-Type: ' . $contentType); |
| 107 | 111 | $selfUri = isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:''; |
| 108 | 112 | if ($selfUri && is_subclass_of($policyClass, __CLASS__)) { |
@@ -130,7 +134,9 @@ discard block |
||
| 130 | 134 | public static function htmlWebLinkSerializer(\BOTK\Core\WebLink $link) { |
| 131 | 135 | $tag = '<link href="'.$link->href.'"'; |
| 132 | 136 | foreach (array('rel', 'type','hreflang') as $attribute) { |
| 133 | - if( $value = $link->$attribute) $tag .= " $attribute='$value'"; |
|
| 137 | + if( $value = $link->$attribute) { |
|
| 138 | + $tag .= " $attribute='$value'"; |
|
| 139 | + } |
|
| 134 | 140 | } |
| 135 | 141 | $tag .= ' />'; |
| 136 | 142 | |
@@ -41,7 +41,6 @@ discard block |
||
| 41 | 41 | /** |
| 42 | 42 | * Call Constructor. |
| 43 | 43 | * |
| 44 | - * @param string $file |
|
| 45 | 44 | */ |
| 46 | 45 | public static function factory($template='',$openDelimiter = '{', $closeDelimiter = '}') |
| 47 | 46 | { |
@@ -52,8 +51,8 @@ discard block |
||
| 52 | 51 | /** |
| 53 | 52 | * Sets the template string. |
| 54 | 53 | * |
| 55 | - * @param string $file |
|
| 56 | 54 | * @throws InvalidArgumentException |
| 55 | + * @param string $string |
|
| 57 | 56 | */ |
| 58 | 57 | public function setTemplate($string) |
| 59 | 58 | { |
@@ -111,8 +110,7 @@ discard block |
||
| 111 | 110 | /** |
| 112 | 111 | * Add one template variables. |
| 113 | 112 | * |
| 114 | - * @param tag $values |
|
| 115 | - * @param value $merge |
|
| 113 | + * @param tag $value |
|
| 116 | 114 | */ |
| 117 | 115 | public function addVar($tag,$value) |
| 118 | 116 | { |
@@ -55,12 +55,12 @@ discard block |
||
| 55 | 55 | * @param string $file |
| 56 | 56 | * @throws InvalidArgumentException |
| 57 | 57 | */ |
| 58 | - public function setTemplate($string) |
|
| 59 | - { |
|
| 60 | - $this->template=$string; |
|
| 58 | + public function setTemplate($string) |
|
| 59 | + { |
|
| 60 | + $this->template=$string; |
|
| 61 | 61 | |
| 62 | - return $this; |
|
| 63 | - } |
|
| 62 | + return $this; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | 65 | |
| 66 | 66 | /** |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | |
| 84 | 84 | else { |
| 85 | 85 | throw new InvalidArgumentException( |
| 86 | - 'Template file could not be loaded.' |
|
| 86 | + 'Template file could not be loaded.' |
|
| 87 | 87 | ); |
| 88 | 88 | } |
| 89 | 89 | |
@@ -157,14 +157,14 @@ discard block |
||
| 157 | 157 | $error = error_get_last(); |
| 158 | 158 | |
| 159 | 159 | throw new RuntimeException( |
| 160 | - sprintf( |
|
| 160 | + sprintf( |
|
| 161 | 161 | 'Could not write to %s: %s', |
| 162 | 162 | $target, |
| 163 | 163 | substr( |
| 164 | - $error['message'], |
|
| 165 | - strpos($error['message'], ':') + 2 |
|
| 164 | + $error['message'], |
|
| 165 | + strpos($error['message'], ':') + 2 |
|
| 166 | + ) |
|
| 166 | 167 | ) |
| 167 | - ) |
|
| 168 | 168 | ); |
| 169 | 169 | } |
| 170 | 170 | } |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * |
| 32 | 32 | * @param string $template |
| 33 | 33 | */ |
| 34 | - public function __construct($template='',$openDelimiter = '{', $closeDelimiter = '}') |
|
| 34 | + public function __construct($template = '', $openDelimiter = '{', $closeDelimiter = '}') |
|
| 35 | 35 | { |
| 36 | 36 | $this->setTemplate($template); |
| 37 | 37 | $this->openDelimiter = $openDelimiter; |
@@ -43,9 +43,9 @@ discard block |
||
| 43 | 43 | * |
| 44 | 44 | * @param string $file |
| 45 | 45 | */ |
| 46 | - public static function factory($template='',$openDelimiter = '{', $closeDelimiter = '}') |
|
| 46 | + public static function factory($template = '', $openDelimiter = '{', $closeDelimiter = '}') |
|
| 47 | 47 | { |
| 48 | - return new static($template,$openDelimiter, $closeDelimiter); |
|
| 48 | + return new static($template, $openDelimiter, $closeDelimiter); |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | */ |
| 58 | 58 | public function setTemplate($string) |
| 59 | 59 | { |
| 60 | - $this->template=$string; |
|
| 60 | + $this->template = $string; |
|
| 61 | 61 | |
| 62 | 62 | return $this; |
| 63 | 63 | } |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | */ |
| 72 | 72 | public function setFromFile($file) |
| 73 | 73 | { |
| 74 | - $distFile = $file . '.dist'; |
|
| 74 | + $distFile = $file.'.dist'; |
|
| 75 | 75 | |
| 76 | 76 | if (file_exists($file)) { |
| 77 | 77 | $this->template = file_get_contents($file); |
@@ -114,9 +114,9 @@ discard block |
||
| 114 | 114 | * @param tag $values |
| 115 | 115 | * @param value $merge |
| 116 | 116 | */ |
| 117 | - public function addVar($tag,$value) |
|
| 117 | + public function addVar($tag, $value) |
|
| 118 | 118 | { |
| 119 | - return $this->setVars(array ( $tag => $value)); |
|
| 119 | + return $this->setVars(array($tag => $value)); |
|
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | /** |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | $keys = array(); |
| 130 | 130 | |
| 131 | 131 | foreach ($this->values as $key => $value) { |
| 132 | - $keys[] = $this->openDelimiter . $key . $this->closeDelimiter; |
|
| 132 | + $keys[] = $this->openDelimiter.$key.$this->closeDelimiter; |
|
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | return str_replace($keys, $this->values, $this->template); |
@@ -75,13 +75,9 @@ |
||
| 75 | 75 | |
| 76 | 76 | if (file_exists($file)) { |
| 77 | 77 | $this->template = file_get_contents($file); |
| 78 | - } |
|
| 79 | - |
|
| 80 | - else if (file_exists($distFile)) { |
|
| 78 | + } else if (file_exists($distFile)) { |
|
| 81 | 79 | $this->setTemplate(file_get_contents($distFile)); |
| 82 | - } |
|
| 83 | - |
|
| 84 | - else { |
|
| 80 | + } else { |
|
| 85 | 81 | throw new InvalidArgumentException( |
| 86 | 82 | 'Template file could not be loaded.' |
| 87 | 83 | ); |
@@ -107,10 +107,10 @@ |
||
| 107 | 107 | (string) $e,// developer description is string view for exception |
| 108 | 108 | $isHttpErrorCode?null:($phpErrorURI.'#'.$errorCode), // guess problem instance |
| 109 | 109 | $isHttpErrorCode?null:$phpErrorURI // guess problem type |
| 110 | - ); |
|
| 110 | + ); |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - // Set header and rendering payload |
|
| 113 | + // Set header and rendering payload |
|
| 114 | 114 | Http::setHttpResponseCode($problem->httpStatus); |
| 115 | 115 | return $this->serializeHttpProblem($problem); |
| 116 | 116 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | |
| 17 | 17 | public function setContentPolicy($contentPolicy) |
| 18 | 18 | { |
| 19 | - if (!is_subclass_of($contentPolicy,'\\BOTK\\Core\\Representations\\Error')) { |
|
| 19 | + if (!is_subclass_of($contentPolicy, '\\BOTK\\Core\\Representations\\Error')) { |
|
| 20 | 20 | // do not use erromanagement inside erro management :-) |
| 21 | 21 | die("$contentPolicy is not a valid Content Negotiation Policy"); |
| 22 | 22 | } |
@@ -40,9 +40,9 @@ discard block |
||
| 40 | 40 | /** |
| 41 | 41 | * Register or unregister an error handler |
| 42 | 42 | */ |
| 43 | - public function registerErrorHandler( $f = null) |
|
| 43 | + public function registerErrorHandler($f = null) |
|
| 44 | 44 | { |
| 45 | - if (is_null($this->errorHandler) && is_callable($f)){ |
|
| 45 | + if (is_null($this->errorHandler) && is_callable($f)) { |
|
| 46 | 46 | // Initialize |
| 47 | 47 | $this->errorHandler = $f; |
| 48 | 48 | $prioryty = $this->priority; |
@@ -51,11 +51,11 @@ discard block |
||
| 51 | 51 | // Reset error handler |
| 52 | 52 | restore_error_handler(); |
| 53 | 53 | $this->errorHandler = null; |
| 54 | - } elseif (is_null($this->errorHandler) && is_null($f)) { |
|
| 54 | + } elseif (is_null($this->errorHandler) && is_null($f)) { |
|
| 55 | 55 | // Install default |
| 56 | 56 | $priority = $this->priority; |
| 57 | 57 | set_error_handler( |
| 58 | - function ($errno, $errstr, $errfile, $errline) use ($priority) |
|
| 58 | + function($errno, $errstr, $errfile, $errline) use ($priority) |
|
| 59 | 59 | { |
| 60 | 60 | throw new ErrorException($errstr, $errno, $priority, $errfile, $errline); |
| 61 | 61 | } |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | |
| 75 | - public function registerViewer( array $viewer, $errorCode = 0) |
|
| 75 | + public function registerViewer(array $viewer, $errorCode = 0) |
|
| 76 | 76 | { |
| 77 | 77 | $this->viewers[$errorCode] = $viewer; |
| 78 | 78 | } |
@@ -86,27 +86,27 @@ discard block |
||
| 86 | 86 | public function serializeHttpProblem(HttpProblem $problem) |
| 87 | 87 | { |
| 88 | 88 | $CNPClassName = $this->contentPolicy; |
| 89 | - return $CNPClassName::render($problem,$CNPClassName::renderers()); |
|
| 89 | + return $CNPClassName::render($problem, $CNPClassName::renderers()); |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - public function render( Exception $e) |
|
| 92 | + public function render(Exception $e) |
|
| 93 | 93 | { |
| 94 | 94 | // build error model from Exception |
| 95 | - if ($e instanceof HttpErrorException){ |
|
| 95 | + if ($e instanceof HttpErrorException) { |
|
| 96 | 96 | $problem = $e->getHttpProblem(); |
| 97 | 97 | } else { |
| 98 | 98 | // guess http status code |
| 99 | 99 | $errorCode = $e->getCode(); |
| 100 | - $isHttpErrorCode = ($errorCode >=400 && array_key_exists($errorCode, Http::$STATUS_CODES)); |
|
| 101 | - $statusCode = $isHttpErrorCode?$errorCode:500; |
|
| 100 | + $isHttpErrorCode = ($errorCode >= 400 && array_key_exists($errorCode, Http::$STATUS_CODES)); |
|
| 101 | + $statusCode = $isHttpErrorCode ? $errorCode : 500; |
|
| 102 | 102 | |
| 103 | - $phpErrorURI='http://php.net/manual/errorfunc.constants'; |
|
| 103 | + $phpErrorURI = 'http://php.net/manual/errorfunc.constants'; |
|
| 104 | 104 | $problem = new HttpProblem( // Populate a new HttpProblem model |
| 105 | 105 | $statusCode, |
| 106 | 106 | null, // use default title |
| 107 | - (string) $e,// developer description is string view for exception |
|
| 108 | - $isHttpErrorCode?null:($phpErrorURI.'#'.$errorCode), // guess problem instance |
|
| 109 | - $isHttpErrorCode?null:$phpErrorURI // guess problem type |
|
| 107 | + (string) $e, // developer description is string view for exception |
|
| 108 | + $isHttpErrorCode ? null : ($phpErrorURI.'#'.$errorCode), // guess problem instance |
|
| 109 | + $isHttpErrorCode ? null : $phpErrorURI // guess problem type |
|
| 110 | 110 | ); |
| 111 | 111 | } |
| 112 | 112 | |
@@ -73,12 +73,12 @@ discard block |
||
| 73 | 73 | public static function htmlRenderer($data) { |
| 74 | 74 | static::setContentType('text/html'); |
| 75 | 75 | $isHtmlFragment=false; |
| 76 | - $metadata = array( |
|
| 77 | - 'title' => null, |
|
| 78 | - 'htmlMetadata' => static::$htmlMetadata, |
|
| 79 | - 'header' => null, |
|
| 80 | - 'footer' => null, |
|
| 81 | - ); |
|
| 76 | + $metadata = array( |
|
| 77 | + 'title' => null, |
|
| 78 | + 'htmlMetadata' => static::$htmlMetadata, |
|
| 79 | + 'header' => null, |
|
| 80 | + 'footer' => null, |
|
| 81 | + ); |
|
| 82 | 82 | if( is_object($data) ){ |
| 83 | 83 | if (method_exists($data,'__toHtml')) { |
| 84 | 84 | $resourceState = $data->__toHtml(); |
@@ -86,11 +86,11 @@ discard block |
||
| 86 | 86 | } else { |
| 87 | 87 | $resourceState = static::getResourceState($data); |
| 88 | 88 | } |
| 89 | - if (method_exists($data,'__metadata')){ |
|
| 90 | - $metadata= array_merge($metadata, $data->__metadata()); |
|
| 91 | - } else { |
|
| 92 | - $metadata['title'] = get_class($data); |
|
| 93 | - } |
|
| 89 | + if (method_exists($data,'__metadata')){ |
|
| 90 | + $metadata= array_merge($metadata, $data->__metadata()); |
|
| 91 | + } else { |
|
| 92 | + $metadata['title'] = get_class($data); |
|
| 93 | + } |
|
| 94 | 94 | } else { |
| 95 | 95 | $resourceState = $data; |
| 96 | 96 | } |
@@ -72,22 +72,22 @@ discard block |
||
| 72 | 72 | */ |
| 73 | 73 | public static function htmlRenderer($data) { |
| 74 | 74 | static::setContentType('text/html'); |
| 75 | - $isHtmlFragment=false; |
|
| 75 | + $isHtmlFragment = false; |
|
| 76 | 76 | $metadata = array( |
| 77 | 77 | 'title' => null, |
| 78 | 78 | 'htmlMetadata' => static::$htmlMetadata, |
| 79 | 79 | 'header' => null, |
| 80 | 80 | 'footer' => null, |
| 81 | 81 | ); |
| 82 | - if( is_object($data) ){ |
|
| 83 | - if (method_exists($data,'__toHtml')) { |
|
| 84 | - $resourceState = $data->__toHtml(); |
|
| 85 | - $isHtmlFragment=true; |
|
| 82 | + if (is_object($data)) { |
|
| 83 | + if (method_exists($data, '__toHtml')) { |
|
| 84 | + $resourceState = $data->__toHtml(); |
|
| 85 | + $isHtmlFragment = true; |
|
| 86 | 86 | } else { |
| 87 | 87 | $resourceState = static::getResourceState($data); |
| 88 | 88 | } |
| 89 | - if (method_exists($data,'__metadata')){ |
|
| 90 | - $metadata= array_merge($metadata, $data->__metadata()); |
|
| 89 | + if (method_exists($data, '__metadata')) { |
|
| 90 | + $metadata = array_merge($metadata, $data->__metadata()); |
|
| 91 | 91 | } else { |
| 92 | 92 | $metadata['title'] = get_class($data); |
| 93 | 93 | } |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | $resourceState = $data; |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - return static::htmlSerializer( $resourceState, $metadata['htmlMetadata'], $metadata['title'], $metadata['header'], $metadata['footer'], $isHtmlFragment); |
|
| 98 | + return static::htmlSerializer($resourceState, $metadata['htmlMetadata'], $metadata['title'], $metadata['header'], $metadata['footer'], $isHtmlFragment); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | */ |
| 106 | 106 | public static function plaintextRenderer($data) { |
| 107 | 107 | static::setContentType('text/plain'); |
| 108 | - return (is_object($data) && method_exists($data,'__toString')) |
|
| 108 | + return (is_object($data) && method_exists($data, '__toString')) |
|
| 109 | 109 | ? (string) $data |
| 110 | 110 | : var_export(static::getResourceState($data), true); |
| 111 | 111 | } |
@@ -149,11 +149,11 @@ discard block |
||
| 149 | 149 | * Serialize data structure using Xmlon class. |
| 150 | 150 | * |
| 151 | 151 | */ |
| 152 | - public static function xmlSerializer($data, array $processingInstructions=array(),$rootElement=null) { |
|
| 152 | + public static function xmlSerializer($data, array $processingInstructions = array(), $rootElement = null) { |
|
| 153 | 153 | if (is_null($rootElement)) $rootElement = 'data'; |
| 154 | 154 | $encoder = new \Paranoiq\Xmlon\XmlonEncoder; // for xml serializing |
| 155 | 155 | $encoder->addXmlHeader = false; |
| 156 | - return implode("\n",$processingInstructions)."\n".$encoder->encode($data,$rootElement); |
|
| 156 | + return implode("\n", $processingInstructions)."\n".$encoder->encode($data, $rootElement); |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | } |
@@ -150,7 +150,9 @@ |
||
| 150 | 150 | * |
| 151 | 151 | */ |
| 152 | 152 | public static function xmlSerializer($data, array $processingInstructions=array(),$rootElement=null) { |
| 153 | - if (is_null($rootElement)) $rootElement = 'data'; |
|
| 153 | + if (is_null($rootElement)) { |
|
| 154 | + $rootElement = 'data'; |
|
| 155 | + } |
|
| 154 | 156 | $encoder = new \Paranoiq\Xmlon\XmlonEncoder; // for xml serializing |
| 155 | 157 | $encoder->addXmlHeader = false; |
| 156 | 158 | return implode("\n",$processingInstructions)."\n".$encoder->encode($data,$rootElement); |
@@ -50,13 +50,13 @@ discard block |
||
| 50 | 50 | static::setContentType('application/api-problem+xml'); |
| 51 | 51 | |
| 52 | 52 | $result = "<?xml version='1.0' encoding='UTF-8'?>\n"; |
| 53 | - $result.= "<problem>\n"; |
|
| 54 | - if($error->problemInstance) $result.= "\t<describedBy>".htmlspecialchars($error->problemInstance)."</describedBy>\n"; |
|
| 55 | - if($error->title) $result.= "\t<title>".htmlspecialchars($error->title)."</title>\n"; |
|
| 56 | - if($error->detail) $result.= "\t<detail>".htmlspecialchars($error->detail)."</detail>\n"; |
|
| 57 | - if($error->problemType) $result.= "\t<supportId>".htmlspecialchars($error->problemType)."</supportId>\n"; |
|
| 58 | - if($error->httpStatus) $result.= "\t<httpStatus>$error->httpStatus</httpStatus>\n"; |
|
| 59 | - $result.= "</problem>\n"; |
|
| 53 | + $result .= "<problem>\n"; |
|
| 54 | + if ($error->problemInstance) $result .= "\t<describedBy>".htmlspecialchars($error->problemInstance)."</describedBy>\n"; |
|
| 55 | + if ($error->title) $result .= "\t<title>".htmlspecialchars($error->title)."</title>\n"; |
|
| 56 | + if ($error->detail) $result .= "\t<detail>".htmlspecialchars($error->detail)."</detail>\n"; |
|
| 57 | + if ($error->problemType) $result .= "\t<supportId>".htmlspecialchars($error->problemType)."</supportId>\n"; |
|
| 58 | + if ($error->httpStatus) $result .= "\t<httpStatus>$error->httpStatus</httpStatus>\n"; |
|
| 59 | + $result .= "</problem>\n"; |
|
| 60 | 60 | |
| 61 | 61 | return $result; |
| 62 | 62 | } |
@@ -69,20 +69,20 @@ discard block |
||
| 69 | 69 | static::setContentType('Content-Type: text/html'); |
| 70 | 70 | // Reuse standard html serializer default management |
| 71 | 71 | $metadata = static::$htmlMetadata; |
| 72 | - if (is_string($metadata)){ |
|
| 72 | + if (is_string($metadata)) { |
|
| 73 | 73 | $metadata = array("<link rel='stylesheet' type='text/css' href='$metadata'/>"); |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - $head = implode("\n",$metadata); |
|
| 77 | - $title =htmlspecialchars("$error->httpStatus $error->title"); |
|
| 78 | - if($error->problemInstance) { |
|
| 76 | + $head = implode("\n", $metadata); |
|
| 77 | + $title = htmlspecialchars("$error->httpStatus $error->title"); |
|
| 78 | + if ($error->problemInstance) { |
|
| 79 | 79 | $pi = htmlspecialchars($error->problemInstance); |
| 80 | 80 | $problemInstance = "<a href='$pi'>$pi</a>"; |
| 81 | - } else { |
|
| 81 | + } else { |
|
| 82 | 82 | $problemInstance = 'unknown'; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - if($error->problemType) { |
|
| 85 | + if ($error->problemType) { |
|
| 86 | 86 | $pt = htmlspecialchars($error->problemType); |
| 87 | 87 | $problemType = "<a href='$pt'>$pt</a>"; |
| 88 | 88 | } else { |
@@ -51,11 +51,21 @@ |
||
| 51 | 51 | |
| 52 | 52 | $result = "<?xml version='1.0' encoding='UTF-8'?>\n"; |
| 53 | 53 | $result.= "<problem>\n"; |
| 54 | - if($error->problemInstance) $result.= "\t<describedBy>".htmlspecialchars($error->problemInstance)."</describedBy>\n"; |
|
| 55 | - if($error->title) $result.= "\t<title>".htmlspecialchars($error->title)."</title>\n"; |
|
| 56 | - if($error->detail) $result.= "\t<detail>".htmlspecialchars($error->detail)."</detail>\n"; |
|
| 57 | - if($error->problemType) $result.= "\t<supportId>".htmlspecialchars($error->problemType)."</supportId>\n"; |
|
| 58 | - if($error->httpStatus) $result.= "\t<httpStatus>$error->httpStatus</httpStatus>\n"; |
|
| 54 | + if($error->problemInstance) { |
|
| 55 | + $result.= "\t<describedBy>".htmlspecialchars($error->problemInstance)."</describedBy>\n"; |
|
| 56 | + } |
|
| 57 | + if($error->title) { |
|
| 58 | + $result.= "\t<title>".htmlspecialchars($error->title)."</title>\n"; |
|
| 59 | + } |
|
| 60 | + if($error->detail) { |
|
| 61 | + $result.= "\t<detail>".htmlspecialchars($error->detail)."</detail>\n"; |
|
| 62 | + } |
|
| 63 | + if($error->problemType) { |
|
| 64 | + $result.= "\t<supportId>".htmlspecialchars($error->problemType)."</supportId>\n"; |
|
| 65 | + } |
|
| 66 | + if($error->httpStatus) { |
|
| 67 | + $result.= "\t<httpStatus>$error->httpStatus</httpStatus>\n"; |
|
| 68 | + } |
|
| 59 | 69 | $result.= "</problem>\n"; |
| 60 | 70 | |
| 61 | 71 | return $result; |
@@ -20,17 +20,17 @@ discard block |
||
| 20 | 20 | public $problemInstance; |
| 21 | 21 | public $httpStatus; |
| 22 | 22 | |
| 23 | - public function __construct($httpStatus=null, $title=null, $detail=null, $problemInstance=null, $problemType= null) |
|
| 23 | + public function __construct($httpStatus = null, $title = null, $detail = null, $problemInstance = null, $problemType = null) |
|
| 24 | 24 | { |
| 25 | 25 | // sanitize values |
| 26 | - if ($httpStatus<400 || $httpStatus> 599) $httpStatus=500; |
|
| 26 | + if ($httpStatus < 400 || $httpStatus > 599) $httpStatus = 500; |
|
| 27 | 27 | |
| 28 | 28 | // set defaults |
| 29 | - if ( is_null($problemType)) $problemType = 'http://dbpedia.org/resource/Category:HTTP_status_codes'; |
|
| 30 | - if ( is_null($httpStatus)) $httpStatus = 500; |
|
| 31 | - if ( is_null($title)) $title = isset(Http::$STATUS_CODES[$httpStatus])?Http::$STATUS_CODES[$httpStatus]:'Unknown server error'; |
|
| 32 | - if ( is_null($detail)) $detail = ''; |
|
| 33 | - if ( is_null($problemInstance)) $problemInstance = 'http://dbpedia.org/resource/HTTP_'.$httpStatus; |
|
| 29 | + if (is_null($problemType)) $problemType = 'http://dbpedia.org/resource/Category:HTTP_status_codes'; |
|
| 30 | + if (is_null($httpStatus)) $httpStatus = 500; |
|
| 31 | + if (is_null($title)) $title = isset(Http::$STATUS_CODES[$httpStatus]) ? Http::$STATUS_CODES[$httpStatus] : 'Unknown server error'; |
|
| 32 | + if (is_null($detail)) $detail = ''; |
|
| 33 | + if (is_null($problemInstance)) $problemInstance = 'http://dbpedia.org/resource/HTTP_'.$httpStatus; |
|
| 34 | 34 | |
| 35 | 35 | $this->httpStatus = $httpStatus; |
| 36 | 36 | $this->title = $title; |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | $this->problemType = $problemType; |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - public static function factory($httpStatus=null, $title=null, $detail=null, $problemInstance=null, $problemType= null) |
|
| 42 | + public static function factory($httpStatus = null, $title = null, $detail = null, $problemInstance = null, $problemType = null) |
|
| 43 | 43 | { |
| 44 | 44 | return new static($httpStatus, $title, $detail, $problemInstance, $problemType); |
| 45 | 45 | } |
@@ -23,14 +23,26 @@ |
||
| 23 | 23 | public function __construct($httpStatus=null, $title=null, $detail=null, $problemInstance=null, $problemType= null) |
| 24 | 24 | { |
| 25 | 25 | // sanitize values |
| 26 | - if ($httpStatus<400 || $httpStatus> 599) $httpStatus=500; |
|
| 26 | + if ($httpStatus<400 || $httpStatus> 599) { |
|
| 27 | + $httpStatus=500; |
|
| 28 | + } |
|
| 27 | 29 | |
| 28 | 30 | // set defaults |
| 29 | - if ( is_null($problemType)) $problemType = 'http://dbpedia.org/resource/Category:HTTP_status_codes'; |
|
| 30 | - if ( is_null($httpStatus)) $httpStatus = 500; |
|
| 31 | - if ( is_null($title)) $title = isset(Http::$STATUS_CODES[$httpStatus])?Http::$STATUS_CODES[$httpStatus]:'Unknown server error'; |
|
| 32 | - if ( is_null($detail)) $detail = ''; |
|
| 33 | - if ( is_null($problemInstance)) $problemInstance = 'http://dbpedia.org/resource/HTTP_'.$httpStatus; |
|
| 31 | + if ( is_null($problemType)) { |
|
| 32 | + $problemType = 'http://dbpedia.org/resource/Category:HTTP_status_codes'; |
|
| 33 | + } |
|
| 34 | + if ( is_null($httpStatus)) { |
|
| 35 | + $httpStatus = 500; |
|
| 36 | + } |
|
| 37 | + if ( is_null($title)) { |
|
| 38 | + $title = isset(Http::$STATUS_CODES[$httpStatus])?Http::$STATUS_CODES[$httpStatus]:'Unknown server error'; |
|
| 39 | + } |
|
| 40 | + if ( is_null($detail)) { |
|
| 41 | + $detail = ''; |
|
| 42 | + } |
|
| 43 | + if ( is_null($problemInstance)) { |
|
| 44 | + $problemInstance = 'http://dbpedia.org/resource/HTTP_'.$httpStatus; |
|
| 45 | + } |
|
| 34 | 46 | |
| 35 | 47 | $this->httpStatus = $httpStatus; |
| 36 | 48 | $this->title = $title; |
@@ -24,16 +24,16 @@ discard block |
||
| 24 | 24 | |
| 25 | 25 | public function options() |
| 26 | 26 | { |
| 27 | - $supportedOptions[]='OPTIONS'; |
|
| 28 | - if( method_exists($this, 'get')) { $supportedOptions[]='GET'; $supportedOptions[]='HEAD' ;} |
|
| 29 | - if( method_exists($this, 'post')) $supportedOptions[]='POST'; |
|
| 30 | - if( method_exists($this, 'put')) $supportedOptions[]='PUT'; |
|
| 31 | - if( method_exists($this, 'delete')) $supportedOptions[]='DELETE'; |
|
| 32 | - if( method_exists($this, 'patch')) $supportedOptions[]='PATCH'; |
|
| 33 | - if( method_exists($this, 'trace')) $supportedOptions[]='TRACE'; |
|
| 34 | - if( method_exists($this, 'connect')) $supportedOptions[]='CONNECT'; |
|
| 27 | + $supportedOptions[] = 'OPTIONS'; |
|
| 28 | + if (method_exists($this, 'get')) { $supportedOptions[] = 'GET'; $supportedOptions[] = 'HEAD'; } |
|
| 29 | + if (method_exists($this, 'post')) $supportedOptions[] = 'POST'; |
|
| 30 | + if (method_exists($this, 'put')) $supportedOptions[] = 'PUT'; |
|
| 31 | + if (method_exists($this, 'delete')) $supportedOptions[] = 'DELETE'; |
|
| 32 | + if (method_exists($this, 'patch')) $supportedOptions[] = 'PATCH'; |
|
| 33 | + if (method_exists($this, 'trace')) $supportedOptions[] = 'TRACE'; |
|
| 34 | + if (method_exists($this, 'connect')) $supportedOptions[] = 'CONNECT'; |
|
| 35 | 35 | |
| 36 | - header ( 'Allow: '. implode(',', $supportedOptions)); |
|
| 36 | + header('Allow: '.implode(',', $supportedOptions)); |
|
| 37 | 37 | |
| 38 | 38 | return''; |
| 39 | 39 | } |
@@ -43,28 +43,28 @@ discard block |
||
| 43 | 43 | * @param mixed $links can be a weblink, an array of or a single or null to bypas hypermedia management |
| 44 | 44 | * @param mixed cacheFor can be an integer that represents the max-age of cache or null to bypass caching |
| 45 | 45 | */ |
| 46 | - public function stateTransfer($resource=null, $links=array()) |
|
| 46 | + public function stateTransfer($resource = null, $links = array()) |
|
| 47 | 47 | { |
| 48 | 48 | // send links header: manage both a single link both an array |
| 49 | - if( is_array($links)){ |
|
| 50 | - foreach ($links as $link){ |
|
| 51 | - header($link->httpSerializer(),false); |
|
| 49 | + if (is_array($links)) { |
|
| 50 | + foreach ($links as $link) { |
|
| 51 | + header($link->httpSerializer(), false); |
|
| 52 | 52 | } |
| 53 | - } elseif( $links){ |
|
| 53 | + } elseif ($links) { |
|
| 54 | 54 | header($links->httpSerializer()); |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | return $resource; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - public function setState($resource=null, $restorers=array()) |
|
| 60 | + public function setState($resource = null, $restorers = array()) |
|
| 61 | 61 | { |
| 62 | 62 | // TBD |
| 63 | 63 | return $resource; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | |
| 67 | - public function resourceCachingProcessor($resouce, $sec=0) |
|
| 67 | + public function resourceCachingProcessor($resouce, $sec = 0) |
|
| 68 | 68 | { |
| 69 | 69 | return Caching::processor($resouce, $sec); |
| 70 | 70 | } |
@@ -26,12 +26,24 @@ |
||
| 26 | 26 | { |
| 27 | 27 | $supportedOptions[]='OPTIONS'; |
| 28 | 28 | if( method_exists($this, 'get')) { $supportedOptions[]='GET'; $supportedOptions[]='HEAD' ;} |
| 29 | - if( method_exists($this, 'post')) $supportedOptions[]='POST'; |
|
| 30 | - if( method_exists($this, 'put')) $supportedOptions[]='PUT'; |
|
| 31 | - if( method_exists($this, 'delete')) $supportedOptions[]='DELETE'; |
|
| 32 | - if( method_exists($this, 'patch')) $supportedOptions[]='PATCH'; |
|
| 33 | - if( method_exists($this, 'trace')) $supportedOptions[]='TRACE'; |
|
| 34 | - if( method_exists($this, 'connect')) $supportedOptions[]='CONNECT'; |
|
| 29 | + if( method_exists($this, 'post')) { |
|
| 30 | + $supportedOptions[]='POST'; |
|
| 31 | + } |
|
| 32 | + if( method_exists($this, 'put')) { |
|
| 33 | + $supportedOptions[]='PUT'; |
|
| 34 | + } |
|
| 35 | + if( method_exists($this, 'delete')) { |
|
| 36 | + $supportedOptions[]='DELETE'; |
|
| 37 | + } |
|
| 38 | + if( method_exists($this, 'patch')) { |
|
| 39 | + $supportedOptions[]='PATCH'; |
|
| 40 | + } |
|
| 41 | + if( method_exists($this, 'trace')) { |
|
| 42 | + $supportedOptions[]='TRACE'; |
|
| 43 | + } |
|
| 44 | + if( method_exists($this, 'connect')) { |
|
| 45 | + $supportedOptions[]='CONNECT'; |
|
| 46 | + } |
|
| 35 | 47 | |
| 36 | 48 | header ( 'Allow: '. implode(',', $supportedOptions)); |
| 37 | 49 | |