MongoCode.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. */
  15. class MongoCode implements \Alcaeus\MongoDbAdapter\TypeInterface
  16. {
  17. /**
  18. * @var string
  19. */
  20. private $code;
  21. /**
  22. * @var array
  23. */
  24. private $scope;
  25. /**
  26. * @link http://php.net/manual/en/mongocode.construct.php
  27. * @param string $code A string of code
  28. * @param array $scope The scope to use for the code
  29. */
  30. public function __construct($code, array $scope = [])
  31. {
  32. if ($code instanceof \MongoDB\BSON\Javascript) {
  33. // @todo Use properties from object once they are accessible
  34. $code = '';
  35. }
  36. $this->code = $code;
  37. $this->scope = $scope;
  38. }
  39. /**
  40. * Returns this code as a string
  41. * @return string
  42. */
  43. public function __toString()
  44. {
  45. return $this->code;
  46. }
  47. /**
  48. * Converts this MongoCode to the new BSON JavaScript type
  49. *
  50. * @return \MongoDB\BSON\Javascript
  51. * @internal This method is not part of the ext-mongo API
  52. */
  53. public function toBSONType()
  54. {
  55. return new \MongoDB\BSON\Javascript($this->code, $this->scope);
  56. }
  57. }