/** * Constructs the PostgreSQL adapter and sets the default port to 5432. * * @param array $config Configuration options for this class. Available options * defined by this class: * - `'host'` : _string_ The IP or machine name where PostgreSQL is running, * followed by a colon, followed by a port number or socket. * Defaults to `'localhost:5432'`. * - `'schema'` : _string_ The name of the database schema to use. Defaults to 'public' * - `'timezone'`: _stirng_ The database timezone. Defaults to `null`. */ public function __construct($config = []) { $defaults = ['host' => 'localhost:5432', 'schema' => 'public', 'timezone' => null, 'classes' => ['dialect' => 'Lead\\Sql\\Dialect\\PostgreSql'], 'handlers' => ['cast' => ['boolean' => function ($value, $options = []) { return $value === 't'; }], 'datasource' => ['boolean' => function ($value, $options = []) { return $value ? 'true' : 'false'; }, 'array' => function ($data) { $data = (array) $data; $result = []; foreach ($data as $value) { if (is_array($value)) { $result[] = $this->_handlers['datasource']['array']($value); } else { $value = str_replace('"', '\\"', $value); if (!is_numeric($value)) { $value = '"' . $value . '"'; } $result[] = $value; } } return '{' . join(",", $result) . '}'; }]]]; $config = Set::merge($defaults, $config); parent::__construct($config + $defaults); $this->formatter('datasource', 'array', $this->_handlers['datasource']['array']); }
/** * Adds config values to the public properties when a new object is created. * * @param array $config Configuration options. Possible values are: * - `'version'` _string_ : (defaults `'1.1'`). * - `'scheme'` _string_ : (defaults `'http'`). * - `'type'` _string_ : (defaults `null`). * - `'headers'` _mixed_ : (defaults `[]`). */ public function __construct($config = []) { $defaults = ['version' => '1.1', 'type' => null, 'encoding' => null, 'format' => null, 'data' => null, 'headers' => [], 'classes' => ['auth' => 'Lead\\Net\\Http\\Auth', 'media' => 'Lead\\Net\\Http\\Media', 'stream' => 'Lead\\Storage\\Stream\\Stream', 'headers' => 'Lead\\Net\\Http\\Headers']]; $config = Set::merge($defaults, $config); $this->_classes = $config['classes']; $this->version($config['version']); if (is_object($config['headers'])) { $this->headers = $config['headers']; } else { $headers = $this->_classes['headers']; $this->headers = new $headers(['data' => $config['headers']]); } if ($config['type']) { $this->type($config['type']); } elseif (isset($this->headers['Content-Type'])) { $this->type($this->headers['Content-Type']->value()); } if ($config['encoding']) { $this->encoding($config['encoding']); } $this->format($config['format']); parent::__construct($config); if ($config['data'] !== null) { $this->set($config['data']); } }
/** * Constructor * * @param array $config The config array */ public function __construct($config = []) { $defaults = ['classes' => ['select' => 'Lead\\Sql\\Statement\\Sqlite\\Select', 'insert' => 'Lead\\Sql\\Statement\\Sqlite\\Insert', 'update' => 'Lead\\Sql\\Statement\\Sqlite\\Update', 'delete' => 'Lead\\Sql\\Statement\\Sqlite\\Delete', 'create table' => 'Lead\\Sql\\Statement\\CreateTable', 'drop table' => 'Lead\\Sql\\Statement\\DropTable'], 'operators' => [':union' => ['builder' => 'set'], ':union all' => ['builder' => 'set'], ':except' => ['builder' => 'set']]]; $config = Set::merge($defaults, $config); parent::__construct($config); $this->type('id', ['use' => 'integer']); $this->type('serial', ['use' => 'integer', 'serial' => true]); $this->type('string', ['use' => 'varchar', 'length' => 255]); $this->type('text', ['use' => 'text']); $this->type('integer', ['use' => 'integer']); $this->type('boolean', ['use' => 'boolean']); $this->type('float', ['use' => 'real']); $this->type('decimal', ['use' => 'decimal', 'precision' => 2]); $this->type('date', ['use' => 'date']); $this->type('time', ['use' => 'time']); $this->type('datetime', ['use' => 'timestamp']); $this->type('binary', ['use' => 'blob']); $this->map('boolean', 'boolean'); $this->map('blob', 'binary'); $this->map('date', 'date'); $this->map('integer', 'integer'); $this->map('decimal', 'decimal', ['precision' => 2]); $this->map('real', 'float'); $this->map('text', 'text'); $this->map('time', 'time'); $this->map('timestamp', 'datetime'); $this->map('varchar', 'string'); }
/** * Configures the schema class. * * @param array $config Possible options are: * - `'conventions'` _array_: Allow to override the default convention rules for generating * primary or foreign key as well as for table/collection names * from an entity class name. */ public function config($config = []) { $defaults = ['conventions' => ['source' => function ($class) { $basename = substr(strrchr($class, '\\'), 1); return Inflector::underscore($basename); }, 'key' => function () { return 'id'; }, 'reference' => function ($class) { $pos = strrpos($class, '\\'); $basename = substr($class, $pos !== false ? $pos + 1 : 0); return Inflector::underscore(Inflector::singularize($basename)) . '_id'; }, 'references' => function ($class) { $pos = strrpos($class, '\\'); $basename = substr($class, $pos !== false ? $pos + 1 : 0); return Inflector::underscore(Inflector::singularize($basename)) . '_ids'; }, 'field' => function ($class) { $pos = strrpos($class, '\\'); $basename = substr($class, $pos !== false ? $pos + 1 : 0); return Inflector::underscore(Inflector::singularize($basename)); }, 'multiple' => function ($name) { return Inflector::pluralize($name); }, 'single' => function ($name) { return Inflector::singularize($name); }]]; $config = Set::merge($defaults, $config); $this->_conventions = $config['conventions']; }
/** * Constructor. * * @param array $config Available configuration options are: * - `'body'` _mixed_ : The body string, resource or `storage\stream\Stream` instance * of the message (defaults `''`). * */ public function __construct($config = []) { $defaults = ['body' => '', 'chunkSize' => 256, 'classes' => ['scheme' => 'Lead\\Net\\Scheme', 'stream' => 'Lead\\Storage\\Stream\\Stream']]; $config = Set::merge($defaults, $config); $this->_classes = $config['classes']; $this->chunkSize($config['chunkSize']); $this->body($config['body']); }
/** * Constructor. * * @param array $config Possible options are: * - `'classes'` _array_ : The class dependencies. * - `'connection'` _object_ : The connection instance. */ public function __construct($config = []) { $defaults = ['classes' => ['schema' => 'chaos\\database\\Schema', 'model' => 'chaos\\Model'], 'connection' => null, 'fixtures' => []]; $config = Set::merge($defaults, $config); $this->_classes = $config['classes']; $this->_connection = $config['connection']; $this->_fixtures = $config['fixtures']; }
/** * The constructor * * @param array $config The config array. */ public function __construct($config = []) { $defaults = ['data' => [], 'cookies' => null, 'classes' => ['header' => 'Lead\\Net\\Http\\Header']]; $config = Set::merge($defaults, $config); $this->_classes = $config['classes']; $this->cookies = $config['cookies']; $this->push($config['data']); }
/** * Constructs the Sqlite adapter and sets the default port to 3306. * * @param array $config Configuration options for this class. Available options: * - `'database'` _string_ : database path. Defaults to `':memory:'`. */ public function __construct($config = []) { $defaults = ['database' => ':memory:', 'classes' => ['dialect' => 'Lead\\Sql\\Dialect\\Sqlite'], 'handlers' => []]; $config = Set::merge($defaults, $config); parent::__construct($config); $this->formatter('datasource', 'boolean', function ($value, $options) { return $value ? '1' : '0'; }); }
/** * Constructor * * @param $config The config. Possible values are: * - `'classes'` _array_ : The class dependencies. */ public function __construct($config = []) { $defaults = ['data' => [], 'classes' => ['header' => 'Lead\\Net\\Http\\Header', 'cookie' => 'Lead\\Net\\Http\\Cookie\\Cookie']]; $config = Set::merge($defaults, $config); $this->_classes = $config['classes']; foreach ($config['data'] as $key => $value) { $this[$key] = $value; } }
function defineGlobals($config = []) { $defaults = ['SERVER' => ['REQUEST_URI' => '/base/path/webroot/index.php/app?get=value', 'SCRIPT_NAME' => '/base/path/webroot/index.php'], 'GET' => ['get' => 'value'], 'POST' => ['post' => 'value'], 'FILES' => ['file' => ['name' => 'file.jpg', 'type' => 'image/jpeg', 'tmp_name' => '/private/var/tmp/phpows38J', 'error' => 0, 'size' => 418]]]; $config = Set::merge($defaults, $config); $_SERVER = $config['SERVER']; $_GET = $config['GET']; $_POST = $config['POST']; $_FILES = $config['FILES']; return $config; }
/** * Constructor * * @param $config The config. Possible values are: * - `'defaults'` _array_ : Default config options to pass to newly created cookie instances. * - `'classes'` _array_ : The class dependencies. */ public function __construct($config = []) { $defaults = ['data' => [], 'secure' => false, 'domain' => null, 'path' => '/', 'classes' => ['header' => 'Lead\\Net\\Http\\Header', 'cookie' => 'Lead\\Net\\Http\\Cookie\\SetCookie']]; $config = Set::merge($defaults, $config); $this->_classes = $config['classes']; $this->_scope = $config; foreach ($config['data'] as $key => $value) { $this[$key] = $value; } }
/** * Constructor. * * @param array $config Possible options are: * - `'connection'` _object_ : The connection instance. */ public function __construct($config = []) { $defaults = ['connection' => null, 'model' => $this->_model, 'meta' => [], 'alters' => $this->_alters]; $config = Set::merge($defaults, $config); $this->_connection = $config['connection']; $this->_meta = $config['meta']; $this->_alters = $config['alters']; $this->_model = $config['model']; $model = $this->_model; $model::connection($this->connection()); }
/** * Constructor * * @param array $config The config array. Possible values are: * - `'handlers'` _array_ : Some custom handlers. */ public function __construct($config = []) { $defaults = ['meta' => [], 'handlers' => [], 'classes' => $this->_classes, 'error' => function ($name, $options, $meta = []) { return Text::insert($options['message'] ?: $this->message($name), $options); }]; $config = Set::merge($defaults, $config); $this->_classes = $config['classes']; $this->set($config['handlers']); $this->error($config['error']); $this->meta($config['meta']); }
/** * Adds config values to the public properties when a new object is created. * * @param array $config Configuration options: * - `'status'` _mixed_ : The response status (default: `[]`). * - `'cookies'` _array_ : set-cookie definition (default: `[]`). */ public function __construct($config = []) { $defaults = ['status' => 200, 'location' => null, 'format' => 'html', 'cookies' => [], 'classes' => ['cookies' => 'Lead\\Net\\Http\\Cookie\\SetCookies']]; $config = Set::merge($defaults, $config); parent::__construct($config); $this->status($config['status']); if ($config['location']) { $this->headers['Location'] = $config['location']; } $cookies = $this->_classes['cookies']; $this->headers->cookies = new $cookies(['data' => $config['cookies']]); }
/** * Constructor * * @param array $config The config array. */ public function __construct($config = []) { $defaults = ['classes' => ['select' => 'Lead\\Sql\\Statement\\Select', 'insert' => 'Lead\\Sql\\Statement\\Insert', 'update' => 'Lead\\Sql\\Statement\\Update', 'delete' => 'Lead\\Sql\\Statement\\Delete', 'truncate' => 'Lead\\Sql\\Statement\\Truncate', 'create table' => 'Lead\\Sql\\Statement\\CreateTable', 'drop table' => 'Lead\\Sql\\Statement\\DropTable'], 'quoter' => null, 'caster' => null, 'types' => [], 'operators' => $this->_defaultOperators(), 'builders' => $this->_defaultBuilders(), 'formatters' => $this->_defaultFormatters(), 'dateFormat' => 'Y-m-d H:i:s']; $config = Set::merge($defaults, $config); $this->_classes = $config['classes']; $this->_quoter = $config['quoter']; $this->_caster = $config['caster']; $this->_dateFormat = $config['dateFormat']; $this->_types = $config['types']; $this->_builders = $config['builders']; $this->_formatters = $config['formatters']; $this->_operators = $config['operators']; }
/** * Constructor * * @param array $config The config array */ public function __construct($config = []) { $defaults = ['classes' => ['select' => 'Lead\\Sql\\Statement\\MySql\\Select', 'insert' => 'Lead\\Sql\\Statement\\MySql\\Insert', 'update' => 'Lead\\Sql\\Statement\\MySql\\Update', 'delete' => 'Lead\\Sql\\Statement\\MySql\\Delete', 'create table' => 'Lead\\Sql\\Statement\\CreateTable', 'drop table' => 'Lead\\Sql\\Statement\\DropTable'], 'operators' => ['#' => ['format' => '%s ^ %s'], ':union' => ['builder' => 'set'], ':union all' => ['builder' => 'set'], ':minus' => ['builder' => 'set'], ':except' => ['name' => 'MINUS', 'builder' => 'set']]]; $config = Set::merge($defaults, $config); parent::__construct($config); $this->type('id', ['use' => 'int']); $this->type('serial', ['use' => 'int', 'serial' => true]); $this->type('string', ['use' => 'varchar', 'length' => 255]); $this->type('text', ['use' => 'text']); $this->type('integer', ['use' => 'int']); $this->type('boolean', ['use' => 'boolean']); $this->type('float', ['use' => 'float']); $this->type('decimal', ['use' => 'decimal', 'precision' => 2]); $this->type('date', ['use' => 'date']); $this->type('time', ['use' => 'time']); $this->type('datetime', ['use' => 'datetime']); $this->type('binary', ['use' => 'blob']); $this->map('bigint', 'integer'); $this->map('bit', 'string'); $this->map('blob', 'string'); $this->map('char', 'string'); $this->map('date', 'date'); $this->map('datetime', 'datetime'); $this->map('decimal', 'decimal'); $this->map('double', 'float'); $this->map('float', 'float'); $this->map('geometry', 'string'); $this->map('geometrycollection', 'string'); $this->map('int', 'integer'); $this->map('linestring', 'string'); $this->map('longblob', 'string'); $this->map('longtext', 'string'); $this->map('mediumblob', 'string'); $this->map('mediumint', 'integer'); $this->map('mediumtext', 'string'); $this->map('multilinestring', 'string'); $this->map('multipolygon', 'string'); $this->map('multipoint', 'string'); $this->map('point', 'string'); $this->map('polygon', 'string'); $this->map('smallint', 'integer'); $this->map('text', 'string'); $this->map('time', 'string'); $this->map('timestamp', 'datetime'); $this->map('tinyblob', 'string'); $this->map('tinyint', 'boolean', ['length' => 1]); $this->map('tinyint', 'integer'); $this->map('tinytext', 'string'); $this->map('varchar', 'string'); $this->map('year', 'string'); }
/** * Expands a collection of entities by adding their related data. * * @param mixed $collection The collection to expand. * @return array The collection of related entities. */ public function embed(&$collection, $options = []) { $options = Set::merge(['fetchOptions' => ['collector' => $this->_collector($collection)]], $options); $name = $this->name(); $through = $this->through(); $using = $this->using(); $from = $this->from(); $relThrough = $from::definition()->relation($through); $middle = $relThrough->embed($collection, $options); $pivot = $relThrough->to(); $relUsing = $pivot::definition()->relation($using); $related = $relUsing->embed($middle, $options); $this->_cleanup($collection); $fromKey = $this->keys('from'); $indexes = $this->_index($related, $this->keys('to')); foreach ($collection as $index => $entity) { if (is_object($entity)) { foreach ($entity->{$through} as $key => $item) { if (isset($item->{$using})) { $value = $item->{$using}; if ($entity instanceof Model) { $entity->__get($name); // Too Many Magic Kill The Magic. } else { $entity->{$name}[] = $value; } } } } else { foreach ($entity[$through] as $key => $item) { if ($indexes->has($item[$fromKey])) { $collection[$index][$name][] = $related[$indexes->get($item[$fromKey])]; $collection[$index][$through][$key][$using] = $related[$indexes->get($item[$fromKey])]; } } } } return $related; }
/** * Expands a collection of entities by adding their related data. * * @param mixed $collection The collection to expand. * @param array $options The embedging options. * @return array The collection of related entities. */ public function embed(&$collection, $options = []) { $indexes = $this->_index($collection, $this->keys('from')); $related = $this->_find($indexes->keys(), Set::merge(['fetchOptions' => ['collector' => $this->_collector($collection)]], $options)); $name = $this->name(); $this->_cleanup($collection); foreach ($related as $index => $entity) { $values = is_object($entity) ? $entity->{$this->keys('to')} : $entity[$this->keys('to')]; $values = is_array($values) || $values instanceof Traversable ? $values : [$values]; foreach ($values as $value) { if ($indexes->has($value)) { if (is_object($collection[$indexes->get($value)])) { $source = $collection[$indexes->get($value)]; $source->{$name}[] = $entity; } else { $collection[$indexes->get($value)][$name][] = $entity; } } } } return $related; }
/** * Constructor. * * @param $config array Configuration options. Allowed options: * - `'classes'` : _array_ Some classes dependencies. * - `'meta '` : _array_ Some meta data. * - `'client'` : _object_ The PDO instance (optionnal). * - `'dialect'` : _object_ A SQL dialect adapter * - `'dns'` : _string_ The full dsn connection url. Defaults to `null`. * - `'host'` : _string_ Name/address of server to connect to. Defaults to 'localhost'. * - `'database'` : _string_ Name of the database to use. Defaults to `null`. * - `'username'` : _string_ Username to use when connecting to server. Defaults to 'root'. * - `'password'` : _string_ Password to use when connecting to server. Defaults to `''`. * - `'encoding'` : _string_ The database character encoding. * - `'connect'` : _boolean_ Autoconnect on construct if `true`. Defaults to `true`. * - `'persistent'`: _boolean_ If true a persistent connection will be attempted, provided the * adapter supports it. Defaults to `true`. * - `'options'` : _array_ Some PDO connection options to set. */ public function __construct($config = []) { parent::__construct($config); $defaults = ['classes' => ['cursor' => 'Chaos\\Database\\Cursor', 'schema' => 'Chaos\\Database\\Schema', 'dialect' => 'Lead\\Sql\\Dialect'], 'meta' => ['key' => 'id', 'locked' => true], 'client' => null, 'dialect' => null, 'dsn' => null, 'host' => 'localhost', 'username' => 'root', 'password' => '', 'database' => null, 'encoding' => null, 'connect' => true, 'persistent' => true, 'options' => []]; $config = Set::merge($defaults, $config); $this->_config = $config; $this->_classes = $this->_config['classes'] + $this->_classes; $this->_client = $this->_config['client']; unset($this->_config['client']); $this->_dialect = $config['dialect']; unset($this->_config['dialect']); if ($this->_dialect === null) { $dialect = $this->_classes['dialect']; $this->_dialect = new $dialect(['quoter' => function ($string) { return $this->quote($string); }, 'caster' => function ($value, $states = []) { $type = isset($states['type']) ? $states['type'] : gettype($value); if (is_array($type)) { $type = call_user_func($type, $states['name']); } return $this->convert('datasource', $type, $value); }]); } if ($this->_config['connect']) { $this->connect(); } $handlers = $this->_handlers; $this->formatter('datasource', 'id', $handlers['datasource']['string']); $this->formatter('datasource', 'serial', $handlers['datasource']['string']); $this->formatter('datasource', 'integer', $handlers['datasource']['string']); $this->formatter('datasource', 'float', $handlers['datasource']['string']); $this->formatter('datasource', 'decimal', $handlers['datasource']['decimal']); $this->formatter('datasource', 'date', $handlers['datasource']['date']); $this->formatter('datasource', 'datetime', $handlers['datasource']['datetime']); $this->formatter('datasource', 'boolean', $handlers['datasource']['boolean']); $this->formatter('datasource', 'null', $handlers['datasource']['null']); $this->formatter('datasource', 'string', $handlers['datasource']['quote']); $this->formatter('datasource', '_default_', $handlers['datasource']['quote']); }
/** * Expands a collection of entities by adding their related data. * * @param mixed $collection The collection to expand. * @param array $options The embedging options. * @return array The collection of related entities. */ public function embed(&$collection, $options = []) { $indexes = $this->_index($collection, $this->keys('from')); $related = $this->_find($indexes->keys(), Set::merge(['fetchOptions' => ['collector' => $this->_collector($collection)]], $options)); $name = $this->name(); $this->_cleanup($collection); foreach ($related as $index => $entity) { if (is_object($entity)) { $value = $entity->{$this->keys('to')}; if ($indexes->has($value)) { $source = $collection[$indexes->get($value)]; $source->{$name} = $entity; } } else { $value = $entity[$this->keys('to')]; if ($indexes->has($value)) { $collection[$indexes->get($value)][$name] = $entity; } } } return $related; }
/** * Constrcutor. * * @param $config array Configuration options. Allowed options: * - `'handlers'` : _array_ Casting handlers. */ public function __construct($config = []) { $defaults = ['handlers' => []]; $config += $defaults; $this->_handlers = Set::merge($this->_handlers(), $config['handlers']); $handlers = $this->_handlers; $this->formatter('cast', 'id', $handlers['cast']['integer']); $this->formatter('cast', 'serial', $handlers['cast']['integer']); $this->formatter('cast', 'integer', $handlers['cast']['integer']); $this->formatter('cast', 'float', $handlers['cast']['float']); $this->formatter('cast', 'decimal', $handlers['cast']['decimal']); $this->formatter('cast', 'date', $handlers['cast']['date']); $this->formatter('cast', 'datetime', $handlers['cast']['datetime']); $this->formatter('cast', 'boolean', $handlers['cast']['boolean']); $this->formatter('cast', 'null', $handlers['cast']['null']); $this->formatter('cast', 'string', $handlers['cast']['string']); $this->formatter('datasource', 'object', $handlers['datasource']['object']); $this->formatter('datasource', 'date', $handlers['datasource']['date']); $this->formatter('datasource', 'datetime', $handlers['datasource']['datetime']); $this->formatter('datasource', 'boolean', $handlers['datasource']['boolean']); $this->formatter('datasource', 'null', $handlers['datasource']['null']); $this->formatter('datasource', '_default_', $handlers['datasource']['string']); }
/** * Constructor * * @param array $config The available configuration options are the following. Further * options are inherited from the parent classes. * - `'ignorePath'` _string_ : Part of the base path to ignore. * - `'basePath'` _string_ : The base path. * - `'path'` _string_ : The url path. * - `'body'` _resource_: Body stream of message (defaults: php://input). * - `'data'` _array_ : Form data (defaults: `[]`). * - `'params'` _array_ : Custom routing params (defaults: `[]`). * - `'env'` _array_ : Environment variables (defaults: `[]`). * - `'locale'` _string_ : An optional locale string (defaults: `''`). * - `'classes'` _array_ : Classes dependencies. * - `'detectors'` _array_ : Request detector definition. */ public function __construct($config = []) { $defaults = ['body' => '', 'data' => [], 'params' => [], 'env' => [], 'locale' => null, 'classes' => ['environment' => 'Lead\\Env\\Env', 'auth' => 'Lead\\Net\\Http\\Auth'], 'detectors' => ['mobile' => ['http:user-agent' => ['iPhone', 'MIDP', 'AvantGo', 'BlackBerry', 'J2ME', 'Opera Mini', 'DoCoMo', 'NetFront', 'Nokia', 'PalmOS', 'PalmSource', 'portalmmm', 'Plucker', 'ReqwirelessWeb', 'iPod', 'SonyEricsson', 'Symbian', 'UP\\.Browser', 'Windows CE', 'Xiino', 'Android']], 'ajax' => ['http:x-requested-with' => 'XMLHttpRequest'], 'flash' => ['http:user-agent' => 'Shockwave Flash'], 'ssl' => ['http:ssl' => true], 'get' => ['http:method' => 'GET'], 'post' => ['http:method' => 'POST'], 'put' => ['http:method' => 'PUT'], 'patch' => ['http:method' => 'PATCH'], 'delete' => ['http:method' => 'DELETE'], 'head' => ['http:method' => 'HEAD'], 'options' => ['http:method' => 'OPTIONS']]]; $config = Set::merge($defaults, $config); if (!is_object($config['env'])) { $environment = $config['classes']['environment']; $config['env'] = new $environment($config['env']); } $config += $this->_defaults($config); $this->env = $config['env']; $this->_data = $config['data']; $this->_params = $config['params']; unset($config['data']); parent::__construct($config); $this->_detectors = $config['detectors']; $this->ignorePath($config['ignorePath']); $this->basePath($config['basePath']); $this->locale($config['locale']); if (isset($this->_data['_method'])) { $this->env['REQUEST_METHOD'] = strtoupper($this->_data['_method']); $this->method($this->env['REQUEST_METHOD']); unset($this->_data['_method']); } }
/** * Constructs the MySQL adapter and sets the default port to 3306. * * @param array $config Configuration options for this class. Available options * defined by this class: * - `'host'`: _string_ The IP or machine name where MySQL is running, * followed by a colon, followed by a port number or socket. * Defaults to `'localhost:3306'`. */ public function __construct($config = []) { $defaults = ['host' => 'localhost:3306', 'classes' => ['dialect' => 'Lead\\Sql\\Dialect\\MySql'], 'handlers' => []]; $config = Set::merge($defaults, $config); parent::__construct($config); }
$result = Set::merge(['foo'], []); expect($result)->toBe(['foo']); }); it("merges arrays recursively", function () { $a = ['users' => ['joe' => ['id' => 1, 'password' => 'may the force']], 'gun']; $b = ['users' => ['bob' => ['id' => 2, 'password' => 'be with you']], 'ice-cream']; $result = Set::merge($a, $b); expect($result)->toBe(['users' => ['joe' => ['id' => 1, 'password' => 'may the force'], 'bob' => ['id' => 2, 'password' => 'be with you']], 'gun', 'ice-cream']); }); it("throws an exception with not enough parameters", function () { $closure = function () { Set::merge(); }; expect($closure)->toThrow(new BadFunctionCallException("Not enough parameters")); $closure = function () { Set::merge([]); }; expect($closure)->toThrow(new BadFunctionCallException("Not enough parameters")); }); }); describe("::slice()", function () { it("slices two arrays", function () { $data = ['key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3']; list($kept, $removed) = Set::slice($data, ['key3']); expect($removed)->toBe(['key3' => 'val3']); expect($kept)->toBe(['key1' => 'val1', 'key2' => 'val2']); }); }); describe("::flatten()", function () { $this->expanded = [['Post' => ['id' => '1', 'author_id' => '1', 'title' => 'First Post'], 'Author' => ['id' => '1', 'user' => 'nate', 'password' => 'foo']], ['Post' => ['id' => '2', 'author_id' => '5', 'title' => 'Second Post'], 'Author' => ['id' => '5', 'user' => 'jeff', 'password' => null]]]; $this->flattened = ['0.Post.id' => '1', '0.Post.author_id' => '1', '0.Post.title' => 'First Post', '0.Author.id' => '1', '0.Author.user' => 'nate', '0.Author.password' => 'foo', '1.Post.id' => '2', '1.Post.author_id' => '5', '1.Post.title' => 'Second Post', '1.Author.id' => '5', '1.Author.user' => 'jeff', '1.Author.password' => null];
/** * Configures the meta for use. * * @param array $config Possible options are: * - `'connection'` _object_ : The connection instance (defaults to `null`). * - `'source'` _string_ : The source name (defaults to `null`). * - `'model'` _string_ : The fully namespaced model class name (defaults to `null`). * - `'locked'` _boolean_: set the ability to dynamically add/remove fields (defaults to `false`). * - `'key'` _string_ : The primary key value (defaults to `id`). * - `'columns' _array_ : array of field definition where keys are field names and values are arrays * with the following keys. All properties are optionnal except the `'type'`: * - `'type'` _string_ : the type of the field. * - `'default'` _mixed_ : the default value (default to '`null`'). * - `'null'` _boolean_: allow null value (default to `'null'`). * - `'length'` _integer_: the length of the data (default to `'null'`). * - `'precision'` _integer_: the precision (for decimals) (default to `'null'`). * - `'use'` _string_ : the database type to override the associated type for * this type (default to `'null'`). * - `'serial'` _string_ : autoincremented field (default to `'null'`). * - `'primary'` _boolead_: primary key (default to `'null'`). * - `'unique'` _boolead_: unique key (default to `'null'`). * - `'reference'` _string_ : foreign key (default to `'null'`). * - `'meta'` _array_ : array of meta definitions for the schema. The definitions are related to * the datasource. For the MySQL adapter the following options are available: * - `'charset'` _string_: the charset value to use for the table. * - `'collate'` _string_: the collate value to use for the table. * - `'engine'` _stirng_: the engine value to use for the table. * - `'tablespace'` _string_: the tablespace value to use for the table. * - `'handlers'` _array_ : casting handlers. * - `'conventions'` _object_ : The naming conventions instance. * - `'classes'` _array_ : The class dependencies. */ public function __construct($config = []) { $defaults = ['connection' => null, 'source' => null, 'model' => Document::class, 'locked' => true, 'columns' => [], 'meta' => [], 'handlers' => [], 'conventions' => null, 'classes' => $this->_classes]; $config = Set::merge($defaults, $config); $this->_classes = $config['classes']; $this->_connection = $config['connection']; $this->_locked = $config['locked']; $this->_meta = $config['meta']; $this->_handlers = Set::merge($config['handlers'], $this->_handlers()); $this->_conventions = $config['conventions'] ?: new Conventions(); $config += ['key' => $this->_conventions->apply('key')]; $this->_columns = $config['columns']; $this->_source = $config['source']; $this->_model = $config['model']; $this->_key = $config['key']; foreach ($config['columns'] as $key => $value) { $this->_columns[$key] = $this->_initColumn($value); } if ($this->_connection) { $this->_formatters = $this->_connection->formatters(); } $handlers = $this->_handlers; $this->formatter('array', 'id', $handlers['array']['integer']); $this->formatter('array', 'serial', $handlers['array']['integer']); $this->formatter('array', 'integer', $handlers['array']['integer']); $this->formatter('array', 'float', $handlers['array']['float']); $this->formatter('array', 'decimal', $handlers['array']['string']); $this->formatter('array', 'date', $handlers['array']['date']); $this->formatter('array', 'datetime', $handlers['array']['datetime']); $this->formatter('array', 'boolean', $handlers['array']['boolean']); $this->formatter('array', 'null', $handlers['array']['null']); $this->formatter('array', '_default_', $handlers['array']['string']); $this->formatter('cast', 'integer', $handlers['cast']['integer']); $this->formatter('cast', 'float', $handlers['cast']['float']); $this->formatter('cast', 'decimal', $handlers['cast']['decimal']); $this->formatter('cast', 'date', $handlers['cast']['datetime']); $this->formatter('cast', 'datetime', $handlers['cast']['datetime']); $this->formatter('cast', 'boolean', $handlers['cast']['boolean']); $this->formatter('cast', 'null', $handlers['cast']['null']); $this->formatter('cast', 'string', $handlers['cast']['string']); if ($this->_connection) { $this->_formatters = Set::merge($this->_formatters, $this->_connection->formatters()); } }
/** * Creates a new record object with default values. * * @param array $config Possible options are: * - `'collector'` _object_ : A collector instance. * - `'uuid'` _object_ : The object UUID. * - `'schema'` _object_ : The schema instance. * - `'basePath'` _string_ : A dotted field names path (for embedded entities). * - `'defaults'` _boolean_ Populates or not the fields default values. * - `'data'` _array_ : The entity's data. * */ public function __construct($config = []) { $defaults = ['collector' => null, 'uuid' => null, 'schema' => null, 'basePath' => null, 'defaults' => true, 'data' => []]; $config += $defaults; $this->_parents = new Map(); $this->collector($config['collector']); $this->basePath($config['basePath']); $this->schema($config['schema']); if ($config['defaults'] && !$config['basePath']) { $config['data'] = Set::merge($this->schema()->defaults(), $config['data']); } $this->set($config['data']); $this->_persisted = $this->_data; $this->uuid($config['uuid']); }
/** * Adds config values to the public properties when a new object is created. * * @param array $config Configuration options: * - `'version'` _string_ : '1.1' * - `'method'` _string_ : 'GET' * - `'scheme'` _string_ : 'http' * - `'host'` _string_ : 'localhost' * - `'port'` _integer_: null * - `'username'` _string_ : null * - `'password'` _string_ : null * - `'path'` _string_ : null * - `'query'` _array_ : [] * - `'headers'` _array_ : [] * - `'type'` _string_ : null * - `'auth'` _mixed_ : null * - `'body'` _mixed_ : null */ public function __construct($config = []) { $defaults = ['scheme' => 'http', 'host' => 'localhost', 'port' => null, 'username' => null, 'password' => null, 'method' => 'GET', 'path' => '', 'query' => [], 'fragment' => '', 'auth' => null, 'cookies' => [], 'mode' => 'origin', 'classes' => ['cookies' => 'Lead\\Net\\Http\\Cookie\\Cookies']]; $config = Set::merge($defaults, $config); parent::__construct($config); if (!isset($this->headers['User-Agent'])) { $this->headers->prepend('User-Agent', 'Mozilla/5.0'); } if (!isset($this->headers['Connection'])) { $this->headers->prepend('Connection', 'Close'); } $this->mode($config['mode']); $this->scheme($config['scheme']); $this->port($config['port']); $this->host($config['host']); $this->username($config['username']); $this->password($config['password']); $this->method($config['method']); $this->path($config['path']); $this->query($config['query']); $this->fragment($config['fragment']); $this->auth($config['auth']); $cookies = $this->_classes['cookies']; $this->headers->cookies = new $cookies(['data' => $config['cookies']]); }
/** * Finds a record by its primary key. * * @param array $options Options for the query. * -`'conditions'` : The conditions array. * - other options depend on the ones supported by the query instance. * * @return object An instance of `Query`. */ public static function find($options = []) { $options = Set::merge(static::query(), $options); $schema = static::definition(); return $schema->query(['query' => $options] + ['finders' => static::finders()]); }
/** * Sets the relations to retrieve. * * @param array $embed The relations to load with the query. * @return object Returns `$this`. */ public function embed($embed = null, $conditions = []) { if (!$embed) { return $this->_embed; } if (!is_array($embed)) { $embed = [$embed => $conditions]; } $embed = Set::normalize($embed); $this->_embed = Set::merge($this->_embed, $embed); return $this; }
/** * Constructor * * @param array $config The config array */ public function __construct($config = []) { $defaults = ['classes' => ['select' => 'Lead\\Sql\\Statement\\PostgreSql\\Select', 'insert' => 'Lead\\Sql\\Statement\\PostgreSql\\Insert', 'update' => 'Lead\\Sql\\Statement\\PostgreSql\\Update', 'delete' => 'Lead\\Sql\\Statement\\PostgreSql\\Delete', 'create table' => 'Lead\\Sql\\Statement\\CreateTable', 'drop table' => 'Lead\\Sql\\Statement\\DropTable'], 'operators' => [':regexp' => ['format' => '%s ~ %s'], ':regexpi' => ['format' => '%s ~* %s'], ':not regexp' => ['format' => '%s !~ %s'], ':not regexpi' => ['format' => '%s !~* %s'], ':square root' => ['format' => '|/ %s'], ':cube root' => ['format' => '||/ %s'], ':fact' => ['format' => '!! %s'], '|/' => ['format' => '|/ %s'], '||/' => ['format' => '||/ %s'], '!!' => ['format' => '!! %s'], ':concat' => ['format' => '%s || %s'], ':pow' => ['format' => '%s ^ %s'], '@' => ['format' => '@ %s'], ':union' => ['builder' => 'set'], ':union all' => ['builder' => 'set'], ':except' => ['builder' => 'set'], ':except all' => ['builder' => 'set'], ':intersect' => ['builder' => 'set'], ':intersect all' => ['builder' => 'set']]]; $config = Set::merge($defaults, $config); parent::__construct($config); $this->type('id', ['use' => 'integer']); $this->type('serial', ['use' => 'serial', 'serial' => true]); $this->type('string', ['use' => 'varchar', 'length' => 255]); $this->type('text', ['use' => 'text']); $this->type('integer', ['use' => 'integer']); $this->type('boolean', ['use' => 'boolean']); $this->type('float', ['use' => 'real']); $this->type('decimal', ['use' => 'numeric', 'precision' => 2]); $this->type('date', ['use' => 'date']); $this->type('time', ['use' => 'time']); $this->type('datetime', ['use' => 'timestamp']); $this->type('binary', ['use' => 'bytea']); $this->map('bit', 'string'); $this->map('bool', 'boolean'); $this->map('boolean', 'boolean'); $this->map('box', 'string'); $this->map('bytea', 'binary'); $this->map('char', 'string'); $this->map('character', 'string'); $this->map('character varying', 'string'); $this->map('cidr', 'string'); $this->map('circle', 'string'); $this->map('date', 'date'); $this->map('decimal', 'string'); $this->map('float4', 'float'); $this->map('float8', 'float'); $this->map('inet', 'string'); $this->map('int2', 'integer'); $this->map('int4', 'integer'); $this->map('int8', 'integer'); $this->map('integer', 'integer'); $this->map('json', 'string'); $this->map('lseg', 'string'); $this->map('line', 'string'); $this->map('macaddr', 'string'); $this->map('numeric', 'decimal'); $this->map('path', 'string'); $this->map('polygon', 'string'); $this->map('real', 'float'); $this->map('serial', 'serial'); $this->map('string', 'string'); $this->map('text', 'string'); $this->map('time', 'time'); $this->map('time with time zone', 'time'); $this->map('time without time zone', 'time'); $this->map('timestamp', 'datetime'); $this->map('timestamp with time zone', 'datetime'); $this->map('timestamp without time zone', 'datetime'); $this->map('timestamptz', 'datetime'); $this->map('tsquery', 'string'); $this->map('tsvector', 'string'); $this->map('txid_snapshot', 'string'); $this->map('uuid', 'string'); $this->map('varbit', 'string'); $this->map('varchar', 'string'); $this->map('xml', 'string'); }