Overview

Namespaces

  • Magyarjeti
    • Loripsum
      • Http
  • PHP

Classes

  • Client
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace Magyarjeti\Loripsum;
  4: 
  5: use Magyarjeti\Loripsum\Http\AdapterInterface;
  6: 
  7: /**
  8:  * Client for loripsum.net.
  9:  */
 10: class Client
 11: {
 12:     /**
 13:      * @const string API endpoint.
 14:      */
 15:     const API_URL = 'http://loripsum.net/api/';
 16: 
 17:     /**
 18:      * @var array Text generation capabilities.
 19:      */
 20:     protected $capabilities = [
 21:         'short',
 22:         'medium',
 23:         'long',
 24:         'verylong',
 25:         'decorate',
 26:         'link',
 27:         'ul',
 28:         'ol',
 29:         'dl',
 30:         'bq',
 31:         'code',
 32:         'headers',
 33:         'allcaps',
 34:         'prude'
 35:     ];
 36: 
 37:     /**
 38:      * @var array
 39:      */
 40:     protected $params = [];
 41: 
 42:     /**
 43:      * @var integer Number of paragraphs to generate.
 44:      */
 45:     protected $paragraphs;
 46: 
 47:     /**
 48:      * @var AdapterInterface HTTP adapter instance.
 49:      */
 50:     protected $conn;
 51: 
 52:     /**
 53:      * Create new loripsum client.
 54:      *
 55:      * @param AdapterInterface $conn
 56:      */
 57:     public function __construct(AdapterInterface $conn)
 58:     {
 59:         $this->conn = $conn;
 60:     }
 61: 
 62:     /**
 63:      * Ask for HTML text.
 64:      *
 65:      * @param integer $paragraphs Number of paragraphs.
 66:      * @return Client
 67:      */
 68:     public function html($paragraphs = null)
 69:     {
 70:         $this->paragraphs = $paragraphs;
 71: 
 72:         unset($this->params['plaintext']);
 73: 
 74:         return $this->generate();
 75:     }
 76: 
 77:     /**
 78:      * Ask for plain text.
 79:      *
 80:      * @param integer $paragraphs Number of paragraphs.
 81:      * @return Client
 82:      */
 83:     public function text($paragraphs = null)
 84:     {
 85:         $this->paragraphs = $paragraphs;
 86: 
 87:         $this->params['plaintext'] = true;
 88: 
 89:         return $this->generate();
 90:     }
 91: 
 92:     /**
 93:      * Set text generation parameters.
 94:      *
 95:      * @param string $name
 96:      * @param array  $params
 97:      * @return Client
 98:      * @throws \RuntimeException
 99:      */
100:     public function __call($name, array $params)
101:     {
102:         if (!in_array($name, $this->capabilities)) {
103:             throw new \RuntimeException("Unknown parameter: $name");
104:         }
105: 
106:         $this->params[$name] = true;
107: 
108:         return $this;
109:     }
110: 
111:     /**
112:      * Generate the text.
113:      *
114:      * @return string
115:      */
116:     protected function generate()
117:     {
118:         $params = array_keys($this->params);
119: 
120:         if ($this->paragraphs) {
121:             $params[] = $this->paragraphs;
122:         }
123: 
124:         $url = self::API_URL . implode('/', $params);
125: 
126:         return $this->conn->request($url);
127:     }
128: }
129: 
API documentation generated by ApiGen