SMS API
This commit is contained in:
parent
e36ccbfcf4
commit
9dccf0fffb
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,6 +1,8 @@
|
|||
/propel.yaml
|
||||
/etc/propel/
|
||||
/etc/security/users.json
|
||||
/**/**/Om/
|
||||
/**/**/Map/
|
||||
/vendor
|
||||
/var/*
|
||||
!/var/.gitkeep
|
||||
|
|
|
@ -5,8 +5,9 @@ namespace App;
|
|||
use Silex\Application as SilexApplication;
|
||||
use Silex\Provider\SecurityServiceProvider;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use App\Validator\SmsValidator;
|
||||
use App\Factory\SmsFactory;
|
||||
use Propel\Runtime\Propel;
|
||||
|
||||
/**
|
||||
* class Application.
|
||||
|
@ -25,6 +26,7 @@ class Application extends SilexApplication
|
|||
*/
|
||||
public function configure()
|
||||
{
|
||||
Propel::init($this->getRootDir().'etc/propel/config.php');
|
||||
$users = json_decode(file_get_contents($this->getRootDir().'/etc/security/users.json'), true);
|
||||
|
||||
$this->register(new SecurityServiceProvider(), [
|
||||
|
@ -37,7 +39,8 @@ class Application extends SilexApplication
|
|||
],
|
||||
]);
|
||||
|
||||
$this['validator.sms'] = new SmsValidator();
|
||||
$this['sms.validator'] = new SmsValidator();
|
||||
$this['sms.factory'] = new SmsFactory();
|
||||
|
||||
$this->error(function (\Exception $e, Request $request, $code) {
|
||||
return $this->json([
|
||||
|
|
23
src/App/Factory/Factory.php
Normal file
23
src/App/Factory/Factory.php
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace App\Factory;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
|
||||
/**
|
||||
* interface Factory.
|
||||
*
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
interface Factory
|
||||
{
|
||||
/*
|
||||
* Validates data.
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function createByRequest(Request $request);
|
||||
}
|
82
src/App/Factory/SmsFactory.php
Normal file
82
src/App/Factory/SmsFactory.php
Normal file
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
|
||||
namespace App\Factory;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use App\Model\Sms;
|
||||
use Propel\Runtime\Collection\ObjectCollection;
|
||||
use App\Model\SmsQuery;
|
||||
|
||||
/**
|
||||
* class SmsFactory.
|
||||
*
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class SmsFactory implements Factory
|
||||
{
|
||||
/**
|
||||
* Creates a SMS.
|
||||
*
|
||||
* @param mixed $sender
|
||||
* @param mixed $message
|
||||
* @param mixed $time
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function create($sender, $message, $time)
|
||||
{
|
||||
return (new Sms())
|
||||
->setSender($sender)
|
||||
->setMessage($message)
|
||||
->setTime((int) $time)
|
||||
->save() > 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Finds SMS.
|
||||
*
|
||||
* @return ObjectCollection
|
||||
*/
|
||||
public function getAll()
|
||||
{
|
||||
return SmsQuery::create()->find();
|
||||
}
|
||||
|
||||
/*
|
||||
* Get a SMS.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return Sms|null
|
||||
*/
|
||||
public function get($id)
|
||||
{
|
||||
return SmsQuery::create()
|
||||
->filterById($id)
|
||||
->findOne();
|
||||
}
|
||||
|
||||
/*
|
||||
* Removes a SMS.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function remove(Sms $object)
|
||||
{
|
||||
return $object->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a SMS with a Request content.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function createByRequest(Request $request)
|
||||
{
|
||||
$content = json_decode($request->getContent(), true);
|
||||
|
||||
return $this->create($content['sender'], $content['message'], $content['time']);
|
||||
}
|
||||
}
|
1476
src/App/Model/Base/Sms.php
Normal file
1476
src/App/Model/Base/Sms.php
Normal file
File diff suppressed because it is too large
Load diff
609
src/App/Model/Base/SmsQuery.php
Normal file
609
src/App/Model/Base/SmsQuery.php
Normal file
|
@ -0,0 +1,609 @@
|
|||
<?php
|
||||
|
||||
namespace App\Model\Base;
|
||||
|
||||
use \Exception;
|
||||
use \PDO;
|
||||
use App\Model\Sms as ChildSms;
|
||||
use App\Model\SmsQuery as ChildSmsQuery;
|
||||
use App\Model\Map\SmsTableMap;
|
||||
use Propel\Runtime\Propel;
|
||||
use Propel\Runtime\ActiveQuery\Criteria;
|
||||
use Propel\Runtime\ActiveQuery\ModelCriteria;
|
||||
use Propel\Runtime\Collection\ObjectCollection;
|
||||
use Propel\Runtime\Connection\ConnectionInterface;
|
||||
use Propel\Runtime\Exception\PropelException;
|
||||
|
||||
/**
|
||||
* Base class that represents a query for the 'sms' table.
|
||||
*
|
||||
*
|
||||
*
|
||||
* @method ChildSmsQuery orderById($order = Criteria::ASC) Order by the id column
|
||||
* @method ChildSmsQuery orderBySender($order = Criteria::ASC) Order by the sender column
|
||||
* @method ChildSmsQuery orderByMessage($order = Criteria::ASC) Order by the message column
|
||||
* @method ChildSmsQuery orderByTime($order = Criteria::ASC) Order by the time column
|
||||
* @method ChildSmsQuery orderByCreatedAt($order = Criteria::ASC) Order by the created_at column
|
||||
* @method ChildSmsQuery orderByUpdatedAt($order = Criteria::ASC) Order by the updated_at column
|
||||
*
|
||||
* @method ChildSmsQuery groupById() Group by the id column
|
||||
* @method ChildSmsQuery groupBySender() Group by the sender column
|
||||
* @method ChildSmsQuery groupByMessage() Group by the message column
|
||||
* @method ChildSmsQuery groupByTime() Group by the time column
|
||||
* @method ChildSmsQuery groupByCreatedAt() Group by the created_at column
|
||||
* @method ChildSmsQuery groupByUpdatedAt() Group by the updated_at column
|
||||
*
|
||||
* @method ChildSmsQuery leftJoin($relation) Adds a LEFT JOIN clause to the query
|
||||
* @method ChildSmsQuery rightJoin($relation) Adds a RIGHT JOIN clause to the query
|
||||
* @method ChildSmsQuery innerJoin($relation) Adds a INNER JOIN clause to the query
|
||||
*
|
||||
* @method ChildSmsQuery leftJoinWith($relation) Adds a LEFT JOIN clause and with to the query
|
||||
* @method ChildSmsQuery rightJoinWith($relation) Adds a RIGHT JOIN clause and with to the query
|
||||
* @method ChildSmsQuery innerJoinWith($relation) Adds a INNER JOIN clause and with to the query
|
||||
*
|
||||
* @method ChildSms findOne(ConnectionInterface $con = null) Return the first ChildSms matching the query
|
||||
* @method ChildSms findOneOrCreate(ConnectionInterface $con = null) Return the first ChildSms matching the query, or a new ChildSms object populated from the query conditions when no match is found
|
||||
*
|
||||
* @method ChildSms findOneById(int $id) Return the first ChildSms filtered by the id column
|
||||
* @method ChildSms findOneBySender(string $sender) Return the first ChildSms filtered by the sender column
|
||||
* @method ChildSms findOneByMessage(resource $message) Return the first ChildSms filtered by the message column
|
||||
* @method ChildSms findOneByTime(string $time) Return the first ChildSms filtered by the time column
|
||||
* @method ChildSms findOneByCreatedAt(string $created_at) Return the first ChildSms filtered by the created_at column
|
||||
* @method ChildSms findOneByUpdatedAt(string $updated_at) Return the first ChildSms filtered by the updated_at column *
|
||||
|
||||
* @method ChildSms requirePk($key, ConnectionInterface $con = null) Return the ChildSms by primary key and throws \Propel\Runtime\Exception\EntityNotFoundException when not found
|
||||
* @method ChildSms requireOne(ConnectionInterface $con = null) Return the first ChildSms matching the query and throws \Propel\Runtime\Exception\EntityNotFoundException when not found
|
||||
*
|
||||
* @method ChildSms requireOneById(int $id) Return the first ChildSms filtered by the id column and throws \Propel\Runtime\Exception\EntityNotFoundException when not found
|
||||
* @method ChildSms requireOneBySender(string $sender) Return the first ChildSms filtered by the sender column and throws \Propel\Runtime\Exception\EntityNotFoundException when not found
|
||||
* @method ChildSms requireOneByMessage(resource $message) Return the first ChildSms filtered by the message column and throws \Propel\Runtime\Exception\EntityNotFoundException when not found
|
||||
* @method ChildSms requireOneByTime(string $time) Return the first ChildSms filtered by the time column and throws \Propel\Runtime\Exception\EntityNotFoundException when not found
|
||||
* @method ChildSms requireOneByCreatedAt(string $created_at) Return the first ChildSms filtered by the created_at column and throws \Propel\Runtime\Exception\EntityNotFoundException when not found
|
||||
* @method ChildSms requireOneByUpdatedAt(string $updated_at) Return the first ChildSms filtered by the updated_at column and throws \Propel\Runtime\Exception\EntityNotFoundException when not found
|
||||
*
|
||||
* @method ChildSms[]|ObjectCollection find(ConnectionInterface $con = null) Return ChildSms objects based on current ModelCriteria
|
||||
* @method ChildSms[]|ObjectCollection findById(int $id) Return ChildSms objects filtered by the id column
|
||||
* @method ChildSms[]|ObjectCollection findBySender(string $sender) Return ChildSms objects filtered by the sender column
|
||||
* @method ChildSms[]|ObjectCollection findByMessage(resource $message) Return ChildSms objects filtered by the message column
|
||||
* @method ChildSms[]|ObjectCollection findByTime(string $time) Return ChildSms objects filtered by the time column
|
||||
* @method ChildSms[]|ObjectCollection findByCreatedAt(string $created_at) Return ChildSms objects filtered by the created_at column
|
||||
* @method ChildSms[]|ObjectCollection findByUpdatedAt(string $updated_at) Return ChildSms objects filtered by the updated_at column
|
||||
* @method ChildSms[]|\Propel\Runtime\Util\PropelModelPager paginate($page = 1, $maxPerPage = 10, ConnectionInterface $con = null) Issue a SELECT query based on the current ModelCriteria and uses a page and a maximum number of results per page to compute an offset and a limit
|
||||
*
|
||||
*/
|
||||
abstract class SmsQuery extends ModelCriteria
|
||||
{
|
||||
protected $entityNotFoundExceptionClass = '\\Propel\\Runtime\\Exception\\EntityNotFoundException';
|
||||
|
||||
/**
|
||||
* Initializes internal state of \App\Model\Base\SmsQuery object.
|
||||
*
|
||||
* @param string $dbName The database name
|
||||
* @param string $modelName The phpName of a model, e.g. 'Book'
|
||||
* @param string $modelAlias The alias for the model in this query, e.g. 'b'
|
||||
*/
|
||||
public function __construct($dbName = 'default', $modelName = '\\App\\Model\\Sms', $modelAlias = null)
|
||||
{
|
||||
parent::__construct($dbName, $modelName, $modelAlias);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new ChildSmsQuery object.
|
||||
*
|
||||
* @param string $modelAlias The alias of a model in the query
|
||||
* @param Criteria $criteria Optional Criteria to build the query from
|
||||
*
|
||||
* @return ChildSmsQuery
|
||||
*/
|
||||
public static function create($modelAlias = null, Criteria $criteria = null)
|
||||
{
|
||||
if ($criteria instanceof ChildSmsQuery) {
|
||||
return $criteria;
|
||||
}
|
||||
$query = new ChildSmsQuery();
|
||||
if (null !== $modelAlias) {
|
||||
$query->setModelAlias($modelAlias);
|
||||
}
|
||||
if ($criteria instanceof Criteria) {
|
||||
$query->mergeWith($criteria);
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find object by primary key.
|
||||
* Propel uses the instance pool to skip the database if the object exists.
|
||||
* Go fast if the query is untouched.
|
||||
*
|
||||
* <code>
|
||||
* $obj = $c->findPk(12, $con);
|
||||
* </code>
|
||||
*
|
||||
* @param mixed $key Primary key to use for the query
|
||||
* @param ConnectionInterface $con an optional connection object
|
||||
*
|
||||
* @return ChildSms|array|mixed the result, formatted by the current formatter
|
||||
*/
|
||||
public function findPk($key, ConnectionInterface $con = null)
|
||||
{
|
||||
if ($key === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($con === null) {
|
||||
$con = Propel::getServiceContainer()->getReadConnection(SmsTableMap::DATABASE_NAME);
|
||||
}
|
||||
|
||||
$this->basePreSelect($con);
|
||||
|
||||
if (
|
||||
$this->formatter || $this->modelAlias || $this->with || $this->select
|
||||
|| $this->selectColumns || $this->asColumns || $this->selectModifiers
|
||||
|| $this->map || $this->having || $this->joins
|
||||
) {
|
||||
return $this->findPkComplex($key, $con);
|
||||
}
|
||||
|
||||
if ((null !== ($obj = SmsTableMap::getInstanceFromPool(null === $key || is_scalar($key) || is_callable([$key, '__toString']) ? (string) $key : $key)))) {
|
||||
// the object is already in the instance pool
|
||||
return $obj;
|
||||
}
|
||||
|
||||
return $this->findPkSimple($key, $con);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find object by primary key using raw SQL to go fast.
|
||||
* Bypass doSelect() and the object formatter by using generated code.
|
||||
*
|
||||
* @param mixed $key Primary key to use for the query
|
||||
* @param ConnectionInterface $con A connection object
|
||||
*
|
||||
* @throws \Propel\Runtime\Exception\PropelException
|
||||
*
|
||||
* @return ChildSms A model object, or null if the key is not found
|
||||
*/
|
||||
protected function findPkSimple($key, ConnectionInterface $con)
|
||||
{
|
||||
$sql = 'SELECT id, sender, message, time, created_at, updated_at FROM sms WHERE id = :p0';
|
||||
try {
|
||||
$stmt = $con->prepare($sql);
|
||||
$stmt->bindValue(':p0', $key, PDO::PARAM_INT);
|
||||
$stmt->execute();
|
||||
} catch (Exception $e) {
|
||||
Propel::log($e->getMessage(), Propel::LOG_ERR);
|
||||
throw new PropelException(sprintf('Unable to execute SELECT statement [%s]', $sql), 0, $e);
|
||||
}
|
||||
$obj = null;
|
||||
if ($row = $stmt->fetch(\PDO::FETCH_NUM)) {
|
||||
/** @var ChildSms $obj */
|
||||
$obj = new ChildSms();
|
||||
$obj->hydrate($row);
|
||||
SmsTableMap::addInstanceToPool($obj, null === $key || is_scalar($key) || is_callable([$key, '__toString']) ? (string) $key : $key);
|
||||
}
|
||||
$stmt->closeCursor();
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find object by primary key.
|
||||
*
|
||||
* @param mixed $key Primary key to use for the query
|
||||
* @param ConnectionInterface $con A connection object
|
||||
*
|
||||
* @return ChildSms|array|mixed the result, formatted by the current formatter
|
||||
*/
|
||||
protected function findPkComplex($key, ConnectionInterface $con)
|
||||
{
|
||||
// As the query uses a PK condition, no limit(1) is necessary.
|
||||
$criteria = $this->isKeepQuery() ? clone $this : $this;
|
||||
$dataFetcher = $criteria
|
||||
->filterByPrimaryKey($key)
|
||||
->doSelect($con);
|
||||
|
||||
return $criteria->getFormatter()->init($criteria)->formatOne($dataFetcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find objects by primary key
|
||||
* <code>
|
||||
* $objs = $c->findPks(array(12, 56, 832), $con);
|
||||
* </code>
|
||||
* @param array $keys Primary keys to use for the query
|
||||
* @param ConnectionInterface $con an optional connection object
|
||||
*
|
||||
* @return ObjectCollection|array|mixed the list of results, formatted by the current formatter
|
||||
*/
|
||||
public function findPks($keys, ConnectionInterface $con = null)
|
||||
{
|
||||
if (null === $con) {
|
||||
$con = Propel::getServiceContainer()->getReadConnection($this->getDbName());
|
||||
}
|
||||
$this->basePreSelect($con);
|
||||
$criteria = $this->isKeepQuery() ? clone $this : $this;
|
||||
$dataFetcher = $criteria
|
||||
->filterByPrimaryKeys($keys)
|
||||
->doSelect($con);
|
||||
|
||||
return $criteria->getFormatter()->init($criteria)->format($dataFetcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the query by primary key
|
||||
*
|
||||
* @param mixed $key Primary key to use for the query
|
||||
*
|
||||
* @return $this|ChildSmsQuery The current query, for fluid interface
|
||||
*/
|
||||
public function filterByPrimaryKey($key)
|
||||
{
|
||||
|
||||
return $this->addUsingAlias(SmsTableMap::COL_ID, $key, Criteria::EQUAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the query by a list of primary keys
|
||||
*
|
||||
* @param array $keys The list of primary key to use for the query
|
||||
*
|
||||
* @return $this|ChildSmsQuery The current query, for fluid interface
|
||||
*/
|
||||
public function filterByPrimaryKeys($keys)
|
||||
{
|
||||
|
||||
return $this->addUsingAlias(SmsTableMap::COL_ID, $keys, Criteria::IN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the query on the id column
|
||||
*
|
||||
* Example usage:
|
||||
* <code>
|
||||
* $query->filterById(1234); // WHERE id = 1234
|
||||
* $query->filterById(array(12, 34)); // WHERE id IN (12, 34)
|
||||
* $query->filterById(array('min' => 12)); // WHERE id > 12
|
||||
* </code>
|
||||
*
|
||||
* @param mixed $id The value to use as filter.
|
||||
* Use scalar values for equality.
|
||||
* Use array values for in_array() equivalent.
|
||||
* Use associative array('min' => $minValue, 'max' => $maxValue) for intervals.
|
||||
* @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
|
||||
*
|
||||
* @return $this|ChildSmsQuery The current query, for fluid interface
|
||||
*/
|
||||
public function filterById($id = null, $comparison = null)
|
||||
{
|
||||
if (is_array($id)) {
|
||||
$useMinMax = false;
|
||||
if (isset($id['min'])) {
|
||||
$this->addUsingAlias(SmsTableMap::COL_ID, $id['min'], Criteria::GREATER_EQUAL);
|
||||
$useMinMax = true;
|
||||
}
|
||||
if (isset($id['max'])) {
|
||||
$this->addUsingAlias(SmsTableMap::COL_ID, $id['max'], Criteria::LESS_EQUAL);
|
||||
$useMinMax = true;
|
||||
}
|
||||
if ($useMinMax) {
|
||||
return $this;
|
||||
}
|
||||
if (null === $comparison) {
|
||||
$comparison = Criteria::IN;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->addUsingAlias(SmsTableMap::COL_ID, $id, $comparison);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the query on the sender column
|
||||
*
|
||||
* Example usage:
|
||||
* <code>
|
||||
* $query->filterBySender('fooValue'); // WHERE sender = 'fooValue'
|
||||
* $query->filterBySender('%fooValue%', Criteria::LIKE); // WHERE sender LIKE '%fooValue%'
|
||||
* </code>
|
||||
*
|
||||
* @param string $sender The value to use as filter.
|
||||
* @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
|
||||
*
|
||||
* @return $this|ChildSmsQuery The current query, for fluid interface
|
||||
*/
|
||||
public function filterBySender($sender = null, $comparison = null)
|
||||
{
|
||||
if (null === $comparison) {
|
||||
if (is_array($sender)) {
|
||||
$comparison = Criteria::IN;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->addUsingAlias(SmsTableMap::COL_SENDER, $sender, $comparison);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the query on the message column
|
||||
*
|
||||
* @param mixed $message The value to use as filter
|
||||
* @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
|
||||
*
|
||||
* @return $this|ChildSmsQuery The current query, for fluid interface
|
||||
*/
|
||||
public function filterByMessage($message = null, $comparison = null)
|
||||
{
|
||||
|
||||
return $this->addUsingAlias(SmsTableMap::COL_MESSAGE, $message, $comparison);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the query on the time column
|
||||
*
|
||||
* Example usage:
|
||||
* <code>
|
||||
* $query->filterByTime(1234); // WHERE time = 1234
|
||||
* $query->filterByTime(array(12, 34)); // WHERE time IN (12, 34)
|
||||
* $query->filterByTime(array('min' => 12)); // WHERE time > 12
|
||||
* </code>
|
||||
*
|
||||
* @param mixed $time The value to use as filter.
|
||||
* Use scalar values for equality.
|
||||
* Use array values for in_array() equivalent.
|
||||
* Use associative array('min' => $minValue, 'max' => $maxValue) for intervals.
|
||||
* @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
|
||||
*
|
||||
* @return $this|ChildSmsQuery The current query, for fluid interface
|
||||
*/
|
||||
public function filterByTime($time = null, $comparison = null)
|
||||
{
|
||||
if (is_array($time)) {
|
||||
$useMinMax = false;
|
||||
if (isset($time['min'])) {
|
||||
$this->addUsingAlias(SmsTableMap::COL_TIME, $time['min'], Criteria::GREATER_EQUAL);
|
||||
$useMinMax = true;
|
||||
}
|
||||
if (isset($time['max'])) {
|
||||
$this->addUsingAlias(SmsTableMap::COL_TIME, $time['max'], Criteria::LESS_EQUAL);
|
||||
$useMinMax = true;
|
||||
}
|
||||
if ($useMinMax) {
|
||||
return $this;
|
||||
}
|
||||
if (null === $comparison) {
|
||||
$comparison = Criteria::IN;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->addUsingAlias(SmsTableMap::COL_TIME, $time, $comparison);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the query on the created_at column
|
||||
*
|
||||
* Example usage:
|
||||
* <code>
|
||||
* $query->filterByCreatedAt('2011-03-14'); // WHERE created_at = '2011-03-14'
|
||||
* $query->filterByCreatedAt('now'); // WHERE created_at = '2011-03-14'
|
||||
* $query->filterByCreatedAt(array('max' => 'yesterday')); // WHERE created_at > '2011-03-13'
|
||||
* </code>
|
||||
*
|
||||
* @param mixed $createdAt The value to use as filter.
|
||||
* Values can be integers (unix timestamps), DateTime objects, or strings.
|
||||
* Empty strings are treated as NULL.
|
||||
* Use scalar values for equality.
|
||||
* Use array values for in_array() equivalent.
|
||||
* Use associative array('min' => $minValue, 'max' => $maxValue) for intervals.
|
||||
* @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
|
||||
*
|
||||
* @return $this|ChildSmsQuery The current query, for fluid interface
|
||||
*/
|
||||
public function filterByCreatedAt($createdAt = null, $comparison = null)
|
||||
{
|
||||
if (is_array($createdAt)) {
|
||||
$useMinMax = false;
|
||||
if (isset($createdAt['min'])) {
|
||||
$this->addUsingAlias(SmsTableMap::COL_CREATED_AT, $createdAt['min'], Criteria::GREATER_EQUAL);
|
||||
$useMinMax = true;
|
||||
}
|
||||
if (isset($createdAt['max'])) {
|
||||
$this->addUsingAlias(SmsTableMap::COL_CREATED_AT, $createdAt['max'], Criteria::LESS_EQUAL);
|
||||
$useMinMax = true;
|
||||
}
|
||||
if ($useMinMax) {
|
||||
return $this;
|
||||
}
|
||||
if (null === $comparison) {
|
||||
$comparison = Criteria::IN;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->addUsingAlias(SmsTableMap::COL_CREATED_AT, $createdAt, $comparison);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the query on the updated_at column
|
||||
*
|
||||
* Example usage:
|
||||
* <code>
|
||||
* $query->filterByUpdatedAt('2011-03-14'); // WHERE updated_at = '2011-03-14'
|
||||
* $query->filterByUpdatedAt('now'); // WHERE updated_at = '2011-03-14'
|
||||
* $query->filterByUpdatedAt(array('max' => 'yesterday')); // WHERE updated_at > '2011-03-13'
|
||||
* </code>
|
||||
*
|
||||
* @param mixed $updatedAt The value to use as filter.
|
||||
* Values can be integers (unix timestamps), DateTime objects, or strings.
|
||||
* Empty strings are treated as NULL.
|
||||
* Use scalar values for equality.
|
||||
* Use array values for in_array() equivalent.
|
||||
* Use associative array('min' => $minValue, 'max' => $maxValue) for intervals.
|
||||
* @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
|
||||
*
|
||||
* @return $this|ChildSmsQuery The current query, for fluid interface
|
||||
*/
|
||||
public function filterByUpdatedAt($updatedAt = null, $comparison = null)
|
||||
{
|
||||
if (is_array($updatedAt)) {
|
||||
$useMinMax = false;
|
||||
if (isset($updatedAt['min'])) {
|
||||
$this->addUsingAlias(SmsTableMap::COL_UPDATED_AT, $updatedAt['min'], Criteria::GREATER_EQUAL);
|
||||
$useMinMax = true;
|
||||
}
|
||||
if (isset($updatedAt['max'])) {
|
||||
$this->addUsingAlias(SmsTableMap::COL_UPDATED_AT, $updatedAt['max'], Criteria::LESS_EQUAL);
|
||||
$useMinMax = true;
|
||||
}
|
||||
if ($useMinMax) {
|
||||
return $this;
|
||||
}
|
||||
if (null === $comparison) {
|
||||
$comparison = Criteria::IN;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->addUsingAlias(SmsTableMap::COL_UPDATED_AT, $updatedAt, $comparison);
|
||||
}
|
||||
|
||||
/**
|
||||
* Exclude object from result
|
||||
*
|
||||
* @param ChildSms $sms Object to remove from the list of results
|
||||
*
|
||||
* @return $this|ChildSmsQuery The current query, for fluid interface
|
||||
*/
|
||||
public function prune($sms = null)
|
||||
{
|
||||
if ($sms) {
|
||||
$this->addUsingAlias(SmsTableMap::COL_ID, $sms->getId(), Criteria::NOT_EQUAL);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all rows from the sms table.
|
||||
*
|
||||
* @param ConnectionInterface $con the connection to use
|
||||
* @return int The number of affected rows (if supported by underlying database driver).
|
||||
*/
|
||||
public function doDeleteAll(ConnectionInterface $con = null)
|
||||
{
|
||||
if (null === $con) {
|
||||
$con = Propel::getServiceContainer()->getWriteConnection(SmsTableMap::DATABASE_NAME);
|
||||
}
|
||||
|
||||
// use transaction because $criteria could contain info
|
||||
// for more than one table or we could emulating ON DELETE CASCADE, etc.
|
||||
return $con->transaction(function () use ($con) {
|
||||
$affectedRows = 0; // initialize var to track total num of affected rows
|
||||
$affectedRows += parent::doDeleteAll($con);
|
||||
// Because this db requires some delete cascade/set null emulation, we have to
|
||||
// clear the cached instance *after* the emulation has happened (since
|
||||
// instances get re-added by the select statement contained therein).
|
||||
SmsTableMap::clearInstancePool();
|
||||
SmsTableMap::clearRelatedInstancePool();
|
||||
|
||||
return $affectedRows;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a DELETE on the database based on the current ModelCriteria
|
||||
*
|
||||
* @param ConnectionInterface $con the connection to use
|
||||
* @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows
|
||||
* if supported by native driver or if emulated using Propel.
|
||||
* @throws PropelException Any exceptions caught during processing will be
|
||||
* rethrown wrapped into a PropelException.
|
||||
*/
|
||||
public function delete(ConnectionInterface $con = null)
|
||||
{
|
||||
if (null === $con) {
|
||||
$con = Propel::getServiceContainer()->getWriteConnection(SmsTableMap::DATABASE_NAME);
|
||||
}
|
||||
|
||||
$criteria = $this;
|
||||
|
||||
// Set the correct dbName
|
||||
$criteria->setDbName(SmsTableMap::DATABASE_NAME);
|
||||
|
||||
// use transaction because $criteria could contain info
|
||||
// for more than one table or we could emulating ON DELETE CASCADE, etc.
|
||||
return $con->transaction(function () use ($con, $criteria) {
|
||||
$affectedRows = 0; // initialize var to track total num of affected rows
|
||||
|
||||
SmsTableMap::removeInstanceFromPool($criteria);
|
||||
|
||||
$affectedRows += ModelCriteria::delete($con);
|
||||
SmsTableMap::clearRelatedInstancePool();
|
||||
|
||||
return $affectedRows;
|
||||
});
|
||||
}
|
||||
|
||||
// timestampable behavior
|
||||
|
||||
/**
|
||||
* Filter by the latest updated
|
||||
*
|
||||
* @param int $nbDays Maximum age of the latest update in days
|
||||
*
|
||||
* @return $this|ChildSmsQuery The current query, for fluid interface
|
||||
*/
|
||||
public function recentlyUpdated($nbDays = 7)
|
||||
{
|
||||
return $this->addUsingAlias(SmsTableMap::COL_UPDATED_AT, time() - $nbDays * 24 * 60 * 60, Criteria::GREATER_EQUAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Order by update date desc
|
||||
*
|
||||
* @return $this|ChildSmsQuery The current query, for fluid interface
|
||||
*/
|
||||
public function lastUpdatedFirst()
|
||||
{
|
||||
return $this->addDescendingOrderByColumn(SmsTableMap::COL_UPDATED_AT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Order by update date asc
|
||||
*
|
||||
* @return $this|ChildSmsQuery The current query, for fluid interface
|
||||
*/
|
||||
public function firstUpdatedFirst()
|
||||
{
|
||||
return $this->addAscendingOrderByColumn(SmsTableMap::COL_UPDATED_AT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Order by create date desc
|
||||
*
|
||||
* @return $this|ChildSmsQuery The current query, for fluid interface
|
||||
*/
|
||||
public function lastCreatedFirst()
|
||||
{
|
||||
return $this->addDescendingOrderByColumn(SmsTableMap::COL_CREATED_AT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by the latest created
|
||||
*
|
||||
* @param int $nbDays Maximum age of in days
|
||||
*
|
||||
* @return $this|ChildSmsQuery The current query, for fluid interface
|
||||
*/
|
||||
public function recentlyCreated($nbDays = 7)
|
||||
{
|
||||
return $this->addUsingAlias(SmsTableMap::COL_CREATED_AT, time() - $nbDays * 24 * 60 * 60, Criteria::GREATER_EQUAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Order by create date asc
|
||||
*
|
||||
* @return $this|ChildSmsQuery The current query, for fluid interface
|
||||
*/
|
||||
public function firstCreatedFirst()
|
||||
{
|
||||
return $this->addAscendingOrderByColumn(SmsTableMap::COL_CREATED_AT);
|
||||
}
|
||||
|
||||
} // SmsQuery
|
20
src/App/Model/Sms.php
Normal file
20
src/App/Model/Sms.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use App\Model\Base\Sms as BaseSms;
|
||||
|
||||
class Sms extends BaseSms
|
||||
{
|
||||
/*
|
||||
* Transforms and return the blob as string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMessage()
|
||||
{
|
||||
$blob = parent::getMessage();
|
||||
|
||||
return $blob !== null ? stream_get_contents($blob, -1, 0) : null;
|
||||
}
|
||||
}
|
10
src/App/Model/SmsQuery.php
Normal file
10
src/App/Model/SmsQuery.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use App\Model\Base\SmsQuery as BaseSmsQuery;
|
||||
|
||||
class SmsQuery extends BaseSmsQuery
|
||||
{
|
||||
|
||||
}
|
|
@ -3,7 +3,7 @@
|
|||
<table name="sms" isCrossRef="true">
|
||||
<column name="id" type="INTEGER" primaryKey="true" required="true" autoIncrement="true"/>
|
||||
<column name="sender" type="VARCHAR" size="50" required="true" />
|
||||
<column name="message" type="LONGTEXT" required="true" />
|
||||
<column name="message" type="BLOB" required="true" />
|
||||
<column name="time" type="BIGINT" required="true" />
|
||||
|
||||
<behavior name="timestampable"/>
|
||||
|
|
|
@ -9,16 +9,41 @@ $app = new Application();
|
|||
$app->setRootDir(__DIR__.'/../')->configure();
|
||||
|
||||
$app
|
||||
// ->post('/api/sms/create', function (Request $request) use ($app) {
|
||||
->get('/api/sms/create', function (Request $request) use ($app) {
|
||||
if (!$app['validator.sms']->validateRequest($request)) {
|
||||
->post('/api/sms/create', function (Request $request) use ($app) {
|
||||
if (!$app['sms.validator']->validateRequest($request)) {
|
||||
return $app->abort(422, 'Invalid request.');
|
||||
}
|
||||
|
||||
$persisted = $app['sms.factory']->createByRequest($request);
|
||||
|
||||
if (!$persisted) {
|
||||
return $app->abort(500, 'An error occured.');
|
||||
} else {
|
||||
return $app->json([
|
||||
'status' => true,
|
||||
'code' => 201,
|
||||
], 201);
|
||||
}
|
||||
})
|
||||
->bind('api_sms_create');
|
||||
|
||||
$app
|
||||
->delete('/api/sms/delete/{id}', function (Request $request, $id) use ($app) {
|
||||
$object = $app['sms.factory']->get($id);
|
||||
|
||||
if (!$object) {
|
||||
return $app->json([
|
||||
'status' => false,
|
||||
'code' => 404,
|
||||
], 404);
|
||||
}
|
||||
|
||||
$app['sms.factory']->remove($object);
|
||||
|
||||
return $app->json([
|
||||
'status' => true,
|
||||
'code' => 204,
|
||||
], 204);
|
||||
})
|
||||
->bind('api_sms_delete')
|
||||
->assert('id', '\d+')
|
||||
|
@ -28,6 +53,20 @@ $app
|
|||
|
||||
$app
|
||||
->get('/api/sms/list', function (Request $request) use ($app) {
|
||||
$objects = $app['sms.factory']->getAll()->getData();
|
||||
$data = [];
|
||||
|
||||
foreach ($objects as $object) {
|
||||
$data[] = [
|
||||
'id' => $object->getId(),
|
||||
'sender' => $object->getSender(),
|
||||
'message' => $object->getMessage(),
|
||||
'received_at' => $object->getTime(),
|
||||
'notified_at' => $object->getCreatedAt()->getTimestamp(),
|
||||
];
|
||||
}
|
||||
|
||||
return $app->json($data);
|
||||
})
|
||||
->bind('api_sms_list');
|
||||
|
||||
|
|
Loading…
Reference in a new issue