Permalink
Browse files

Document public functions.

  • Loading branch information...
mudge committed Feb 2, 2014
1 parent d9457c6 commit 6a3aa409c8b21d07cb2a32b785432187aa90b4fb
Showing with 69 additions and 24 deletions.
  1. +5 −4 README.md
  2. +3 −2 project.clj
  3. +24 −7 src/php_clj/core.clj
  4. +24 −6 src/php_clj/reader.clj
  5. +7 −3 test/php_clj/t_core.clj
  6. +6 −2 test/php_clj/t_reader.clj
@@ -4,8 +4,8 @@ A Clojure library to deserialize PHP as generated by
[`serialize`](http://php.net/manual/en/function.serialize.php) into Clojure
data structures and vice versa.
**Current version**: 0.4.0
**Clojure version support:** 1.4, 1.5
**Current version**: 0.4.1
**Supported Clojure versions:** 1.4, 1.5, 1.5.1
## Usage
@@ -76,7 +76,7 @@ following to your [Leiningen](https://github.com/technomancy/leiningen)
dependencies:
```clojure
[php-clj "0.4.0"]
[php-clj "0.4.1"]
```
## References
@@ -90,4 +90,5 @@ dependencies:
Copyright © 2014 Paul Mucur.
Distributed under the Eclipse Public License, the same as Clojure.
Distributed under the [Eclipse Public
License](http://www.eclipse.org/legal/epl-v10.html).
@@ -1,11 +1,12 @@
(defproject php-clj "0.4.0"
(defproject php-clj "0.4.1"
:description "Deserialize PHP into Clojure data structures and back again."
:url "https://github.com/mudge/php-clj"
:dependencies [[ordered "1.3.2"]]
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:profiles {:dev {:dependencies [[midje "1.5.1"]]
:plugins [[lein-midje "3.0.0"]]}
:plugins [[lein-midje "3.0.0"]
[lein-kibit "0.0.8"]]}
:1.4 {:dependencies [[org.clojure/clojure "1.4.0"]]}
:1.5.0 {:dependencies [[org.clojure/clojure "1.5.0"]]}
:1.5.1 {:dependencies [[org.clojure/clojure "1.5.1"]]}}
@@ -1,5 +1,10 @@
;; Copyright (c) 2014, Paul Mucur (http://mudge.name)
;; Released under the Eclipse Public License:
;; http://www.eclipse.org/legal/epl-v10.html
(ns php_clj.core
(:require [php_clj.reader :as r]
[clojure.string :as s]
[ordered.map :refer [ordered-map]]))
(declare reader->clj)
@@ -41,20 +46,20 @@
arr (do (expect-char reader \{)
(loop [acc (ordered-map) n n-keys]
(if (zero? n) acc
(recur (assoc acc (reader->clj reader) (reader->clj reader))
(dec n)))))]
(recur (assoc acc (reader->clj reader) (reader->clj reader))
(dec n)))))]
(expect-char reader \})
arr))
(defn- array? [coll]
(defn- one-dimensional-array? [coll]
(loop [i 0 indices (keys coll)]
(cond
(nil? (seq indices)) true
(= i (first indices)) (recur (inc i) (next indices))
:else false)))
(defn- parse-vector [array]
(if (array? array) (-> array vals vec)
(if (one-dimensional-array? array) (-> array vals vec)
array))
(defn- reader->clj [reader]
@@ -66,7 +71,15 @@
\N (parse-null reader)
\a (parse-vector (parse-array reader))))
(defn php->clj [php]
(defn php->clj
"Converts serialized PHP into equivalent Clojure data structures.
Note that one-dimensional PHP arrays (those with consecutive indices starting
at 0 such as array(1, 2, 3)) will be converted to vectors while all others
will be converted to ordered maps therefore preserving insertion order.
Example: (php->clj \"a:2:{i:0;i:1;i:1;i:2;}\")"
[php]
(let [reader (r/buffered-input-stream php)]
(reader->clj reader)))
@@ -92,10 +105,14 @@
(defn- encode-collection [clj]
(str "a:"
(count clj) ":{"
(apply str (map-indexed #(str (clj->php %) (clj->php %2)) clj))
(s/join (map-indexed #(str (clj->php %) (clj->php %2)) clj))
"}"))
(defn clj->php [clj]
(defn clj->php
"Converts Clojure data structures into serialized PHP.
Example: (clj->php [1 2 3])"
[clj]
(cond
(map? clj) (encode-map clj)
(coll? clj) (encode-collection clj)
@@ -1,19 +1,37 @@
;; Copyright (c) 2014, Paul Mucur (http://mudge.name)
;; Released under the Eclipse Public License:
;; http://www.eclipse.org/legal/epl-v10.html
(ns php_clj.reader
(:require [clojure.string :as s])
(:import [java.io ByteArrayInputStream BufferedInputStream]))
(defn buffered-input-stream [^String s]
(defn buffered-input-stream
"Returns a BufferedInputStream for a string."
[^String s]
(-> s .getBytes ByteArrayInputStream. BufferedInputStream.))
(defn read-char [^BufferedInputStream stream]
(defn read-char
"Reads a single character from a BufferedInputStream and returns it. Note that
this mutates the given stream."
[^BufferedInputStream stream]
(-> stream .read char))
(defn read-str [^BufferedInputStream stream n]
(defn read-str
"Reads n bytes from a BufferedInputStream and returns them as a string. Note
that this mutates the given stream."
[^BufferedInputStream stream n]
(let [selected-bytes (byte-array n)]
(.read stream selected-bytes)
(String. selected-bytes)))
(defn read-until [reader delimiter]
(defn read-until
"Reads from the given stream until a specified character is found and returns
the string up to that point (not including the delimiter). Note that this
mutates the given stream."
[reader delimiter]
(loop [acc []]
(let [c (read-char reader)]
(if (= delimiter c) (apply str acc)
(recur (conj acc c))))))
(if (= delimiter c)
(s/join acc)
(recur (conj acc c))))))
@@ -1,7 +1,11 @@
;; Copyright (c) 2014, Paul Mucur (http://mudge.name)
;; Released under the Eclipse Public License:
;; http://www.eclipse.org/legal/epl-v10.html
(ns php_clj.t-core
(:require [ordered.map :refer [ordered-map]])
(:use midje.sweet)
(:use [php_clj.core]))
(:require [ordered.map :refer [ordered-map]]
[midje.sweet :refer :all]
[php_clj.core :refer :all]))
(facts "about `php->clj`"
(fact "it converts integers"
@@ -1,6 +1,10 @@
;; Copyright (c) 2014, Paul Mucur (http://mudge.name)
;; Released under the Eclipse Public License:
;; http://www.eclipse.org/legal/epl-v10.html
(ns php_clj.t-reader
(:use midje.sweet)
(:require [php_clj.reader :as r]))
(:require [php_clj.reader :as r]
[midje.sweet :refer :all]))
(def input (r/buffered-input-stream "s:2:\"é\";"))

0 comments on commit 6a3aa40

Please sign in to comment.