Functional Helpers
In which I define a load of helpful little functions.
Some of these are stolen from elsewhere, credit given where due.
| author | Barnaby Walters http://waterpigs.co.uk |
|---|
atomDate(string | string $date)
| todo | Allow $date to be a DateTime object |
|---|
stringstringA string representing the date to process
autolinkHashTags(string $text, string $baseUrl) : string
Given a string and a baseurl, finds all hashtags matching
#[\-_a-zA-Z0-9]+ and wraps them in an a element with rel=tag set
and a href of baseurl + '/' + tagname without the #.
| todo | Improve the regex to limit false matches |
|---|
string
string
stringThe original text with all hashtags auto-linkedcreateXmlRpcFaultResponse(string | int $fault, bool $asString) : \DOMDocument
Creates an XML-RPC Fault response with either an int or a string as the fault.
stringint
bool
createXmlRpcSuccessResponse(string $retVal, bool $asString) : \DOMDocument
Given the value to return (can only be a string at the mo), creates a response.
stringThe string to return
boolWhether or not to return the respons as a string or DOMDocument
findUrls(string $text, bool $tidy) : array
stringThe string to find URLs in
boolWhether or not to tidy the URLs with cassis web_address_to_uri(, true)
arrayAn array containing all the URLs found in $textgetAuthTags(array $tags)
Given an array of the tags associated with an object, find any which are in the auth namespace, parse them and return them.
Authorization tags are all positive and additive. That is, it is impossible for one tag to contradict another.
auth:(:any)=(:any), where
the wildcards must be:
example.com, waterpigs.co.uk,
bill.someservice.orgprivate
auth:private=* tags, it is a private
post and MUST ONLY be exposed to users who are authenticated as
one of the private-* domainsauth:private-* tags MAY be specified, resulting in
ANY of the specified users being able to view the contenteditable
auth:editable=user.com states that user.com is allowed to edit
this object. An edit UI should be made available to them.auth:editable CANNOT add
or remove auth:editable tags UNLESS they have
role == super-admin
Users with role == 'super-admin' MUST be able to perform ANY
action on ANY object. These users SHOULD be limited to server
administrators. AuthTag implementations should be aware of this but MUST
NOT implement it directly, it must be left to the outer authorization
code as the super-admin role may have different names on different systems.All of these examples are comma-separated tagstrings.
auth:private=example.com states that the object is private but user
example.com can view itauth:private=example.com, auth:private=someguy.org states that the
object is private but both example.com someguy.org can view itauth:private=someguy.org, auth:private=somegirl.com,
auth:editable=somegirl.com states that the object is private but both
someguy.org and somegirl.com can view it. somegirl.com can also edit it,
but cannot change authtag permissionsgetMachineTags(array $tags, string $namespace, boolean $trunc) : array
Given an array of opaque string tags, return an array of all the machine tags of format namespace:predicate=value, optionally filtering by namespace.
If the tags are filtered by namespace and $trunc is set, the namespace and following colon are removed from the results.
The return value is always an associative array of namespace:predicate => value.
array
string
boolean
arraygetPlainTags(array $tags) : array
Returns an array of all the non-machine tags in the passed array.
array
arrayThe input with all machine tags removedhumanise(string $string) : string
Makes a slugified/ASCIIfied string nice again
| todo | make this happen for CamelCase too? |
|---|---|
| todo | add more intelligent dash/underscore selection |
string
stringparseXmlRpcMethodCall(string $request) : array
Given an XML-RPC request body, returns an array like this:
[
'methodName': 'example.ex',
'params': ['string1', 'string2]
]
NOTE: Only currently accepts string parameters.
| todo | Make this handle param types other than `string` |
|---|
string
arrayrelativeTimeElement(\DateTime $dateTime, \DateTime $comparison, array $attrs) : string
sameHostname(string $a, string $b) : bool
Checks whether or not two given URLs have the same hostname
string
string
boolWhether or not a and b share a hostnamestringToHexColour(string $word) : string
Inspired by the Dopplr colours and the work of Brian Suda and Sandeep Shetty. This differs a little from the md5 technique as it should create similar colours for similar sounding words -- only time will tell if this is any use and/or completely ineffective.
| todo | Write tests |
|---|
stringThe string to calculate the colour for
stringa css hex colour of the form XXXXXXtimeElement(\DateTime $datetime) : string
Generates a
| todo | Add support for more precise times |
|---|---|
| todo | Add support for string dates using strtotime() |
stringA toAscii(string $str, array $replace, string $delimiter) : string
The ultimate safe URL generator, courtesy of http://cubiq.org/the-perfect-php-clean-url-generator
Given a string, makes it uber-readable and URI safe
stringThe string to process
arrayAn array of characters to replace with whitespace
stringThe character to use to separate words, defaulting to '-'
stringThe cleaned stringtruest()
This is a dirty shortcut.
uriMbStrlen(string $string, int $urilen) : int
stringThe string to process
intThe length to treat all URIs in $string as
intThe length $string would be if all URIs were $urilen long